Photo application now working fully.
[photos] / src / main / java / org / wamblee / photos / wicket / BasePage.java
1 /*
2  * Copyright 2005-2010 the original author or authors.
3  * 
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  * 
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  * 
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.wamblee.photos.wicket;
17
18 import java.security.Principal;
19 import javax.inject.Inject;
20 import javax.servlet.http.HttpServletRequest;
21
22 import org.apache.wicket.RedirectToUrlException;
23 import org.apache.wicket.markup.html.CSSPackageResource;
24 import org.apache.wicket.markup.html.WebMarkupContainer;
25 import org.apache.wicket.markup.html.WebPage;
26 import org.apache.wicket.markup.html.basic.Label;
27 import org.apache.wicket.markup.html.link.Link;
28 import org.apache.wicket.markup.html.panel.FeedbackPanel;
29 import org.apache.wicket.model.IModel;
30 import org.wamblee.security.authentication.UserAdministration;
31 import org.wamblee.wicket.behavior.TitleAttributeTooltipBehavior;
32 import org.wamblee.wicket.css.ResetCssBehavior;
33 import org.wamblee.wicket.page.ExpireBehavior;
34 import org.wamblee.wicket.page.WebApplicationBasePage;
35
36 public class BasePage extends WebApplicationBasePage {
37
38     @Inject
39     private HttpServletRequest request;
40
41     @Inject
42     private transient UserAdministration userAdmin;
43
44     private boolean isExpired = false;
45
46     public BasePage() {
47         this(null);
48     }
49
50     public BasePage(IModel aModel) {
51         super(aModel);
52
53         Principal userPrincipal = request.getUserPrincipal();
54         if (userPrincipal == null) {
55             throw redirectToLoginPage();
56         }
57         String username = userPrincipal.getName();
58         if (isAdminPage() && !isAdministrator(username)) {
59             error("Unauthorized URL accessed");
60             throw redirectToLoginPage();
61         }
62
63         add(new ResetCssBehavior());
64         add(new TitleAttributeTooltipBehavior());
65         add(CSSPackageResource.getHeaderContribution(BasePage.class, "photos.css"));
66         disableCaching();
67
68         add(new FeedbackPanel("feedback"));
69         add(new Label("title", getTitle()));
70
71         addBehavior(new ExpireBehavior() {
72             @Override
73             protected boolean isExpired(WebPage aPage) {
74                 return isExpired;
75             }
76         });
77
78         add(new Link("logout") {
79             @Override
80             public void onClick() {
81                 getRequestCycle().getSession().invalidate();
82                 throw redirectToLoginPage();
83             }
84         });
85
86         WebMarkupContainer adminAccess = new WebMarkupContainer("adminAccess");
87         if (!isAdministrator(username)) {
88             adminAccess.setVisible(false);
89         }
90         add(adminAccess);
91     }
92
93     protected boolean isAdminPage() {
94         return false;
95     }
96
97     protected boolean isAdministrator(String aUsername) {
98         return userAdmin.isInGroup(aUsername, "administrators");
99     }
100
101     private RedirectToUrlException redirectToLoginPage() {
102         return new RedirectToUrlException("login.jsp");
103     }
104
105     public void setExpired(boolean aExpired) {
106         isExpired = aExpired;
107     }
108
109     private String getTitle() {
110         String name = getClass().getSimpleName();
111         name = name.replaceAll("([A-Z]+)([A-Z][a-z])", "$1 $2");
112         name = name.replaceAll("[A-Z]+", " $0");
113         name = name.replaceAll("^ ", "");
114         name = name.replaceAll("  ", " ");
115         return name;
116     }
117 }