1 | package eu.clarin.cmdi.virtualcollectionregistry.gui; |
---|
2 | |
---|
3 | import java.io.BufferedReader; |
---|
4 | import java.io.FileInputStream; |
---|
5 | import java.io.IOException; |
---|
6 | import java.io.InputStreamReader; |
---|
7 | import java.util.HashSet; |
---|
8 | import java.util.Set; |
---|
9 | |
---|
10 | import org.apache.wicket.Page; |
---|
11 | import org.apache.wicket.authentication.AuthenticatedWebApplication; |
---|
12 | import org.apache.wicket.authentication.AuthenticatedWebSession; |
---|
13 | import org.apache.wicket.authorization.strategies.role.Roles; |
---|
14 | import org.apache.wicket.markup.html.WebPage; |
---|
15 | import org.apache.wicket.session.pagemap.LeastRecentlyAccessedEvictionStrategy; |
---|
16 | |
---|
17 | import eu.clarin.cmdi.virtualcollectionregistry.gui.pages.AdminPage; |
---|
18 | import eu.clarin.cmdi.virtualcollectionregistry.gui.pages.BrowsePrivateCollectionsPage; |
---|
19 | import eu.clarin.cmdi.virtualcollectionregistry.gui.pages.CreateVirtualCollectionPage; |
---|
20 | import eu.clarin.cmdi.virtualcollectionregistry.gui.pages.BrowsePublicCollectionsPage; |
---|
21 | import eu.clarin.cmdi.virtualcollectionregistry.gui.pages.LoginPage; |
---|
22 | |
---|
23 | public class Application extends AuthenticatedWebApplication { |
---|
24 | private static final String CONFIG_PARAM_ADMINDB = "admindb"; |
---|
25 | private Set<String> adminUsers = |
---|
26 | new HashSet<String>(); |
---|
27 | |
---|
28 | @Override |
---|
29 | protected void init() { |
---|
30 | super.init(); |
---|
31 | |
---|
32 | String s = getServletContext().getInitParameter(CONFIG_PARAM_ADMINDB); |
---|
33 | if (s != null) { |
---|
34 | try { |
---|
35 | loadAdminDatabase(s); |
---|
36 | } catch (IOException e ) { |
---|
37 | // FIXME: handle error |
---|
38 | } |
---|
39 | } |
---|
40 | if (adminUsers.isEmpty()) { |
---|
41 | // FIXME: better logging |
---|
42 | System.err.println("WARNING: no admin users have been defined"); |
---|
43 | } |
---|
44 | getMarkupSettings().setDefaultMarkupEncoding("utf-8"); |
---|
45 | getRequestCycleSettings().setResponseRequestEncoding("utf-8"); |
---|
46 | getSessionSettings().setMaxPageMaps(3); |
---|
47 | getSessionSettings().setPageMapEvictionStrategy( |
---|
48 | new LeastRecentlyAccessedEvictionStrategy(3)); |
---|
49 | if (getConfigurationType() != DEPLOYMENT) { |
---|
50 | getMarkupSettings().setStripWicketTags(true); |
---|
51 | getMarkupSettings().setStripComments(true); |
---|
52 | } |
---|
53 | mountBookmarkablePage("/public", |
---|
54 | BrowsePublicCollectionsPage.class); |
---|
55 | mountBookmarkablePage("/private", |
---|
56 | BrowsePrivateCollectionsPage.class); |
---|
57 | mountBookmarkablePage("/create", CreateVirtualCollectionPage.class); |
---|
58 | mountBookmarkablePage("/admin", AdminPage.class); |
---|
59 | } |
---|
60 | |
---|
61 | @Override |
---|
62 | public Class<? extends Page> getHomePage() { |
---|
63 | return BrowsePublicCollectionsPage.class; |
---|
64 | } |
---|
65 | |
---|
66 | @Override |
---|
67 | protected Class<? extends WebPage> getSignInPageClass() { |
---|
68 | return LoginPage.class; |
---|
69 | } |
---|
70 | |
---|
71 | @Override |
---|
72 | protected Class<? extends AuthenticatedWebSession> getWebSessionClass() { |
---|
73 | return ApplicationSession.class; |
---|
74 | } |
---|
75 | |
---|
76 | public boolean hasAnyRole(String[] roles) { |
---|
77 | if (roles != null) { |
---|
78 | final Roles sessionRoles = AuthenticatedWebSession.get().getRoles(); |
---|
79 | if (sessionRoles != null) { |
---|
80 | for (String role : roles) { |
---|
81 | if (sessionRoles.hasRole(role)) { |
---|
82 | return true; |
---|
83 | } |
---|
84 | } |
---|
85 | } |
---|
86 | } |
---|
87 | return false; |
---|
88 | } |
---|
89 | |
---|
90 | boolean isAdmin(String user) { |
---|
91 | return adminUsers.contains(user); |
---|
92 | } |
---|
93 | |
---|
94 | private void loadAdminDatabase(String filename) throws IOException { |
---|
95 | adminUsers.clear(); |
---|
96 | BufferedReader reader = new BufferedReader(new InputStreamReader( |
---|
97 | new FileInputStream(filename))); |
---|
98 | String line; |
---|
99 | while ((line = reader.readLine()) != null) { |
---|
100 | line = line.trim(); |
---|
101 | if (line.isEmpty() || line.startsWith("#")) { |
---|
102 | continue; |
---|
103 | } |
---|
104 | adminUsers.add(line); |
---|
105 | } // while |
---|
106 | reader.close(); |
---|
107 | } |
---|
108 | |
---|
109 | } // class Application |
---|