1 | package eu.clarin.cmdi.vlo; |
---|
2 | |
---|
3 | import eu.clarin.cmdi.vlo.config.VloConfig; |
---|
4 | import eu.clarin.cmdi.vlo.config.VloContextConfig; |
---|
5 | import eu.clarin.cmdi.vlo.dao.SearchResultsDao; |
---|
6 | import eu.clarin.cmdi.vlo.pages.BasePage; |
---|
7 | import eu.clarin.cmdi.vlo.pages.BasePanel; |
---|
8 | import eu.clarin.cmdi.vlo.pages.FacetedSearchPage; |
---|
9 | import eu.clarin.cmdi.vlo.pages.ShowAllFacetValuesPage; |
---|
10 | import static eu.clarin.cmdi.vlo.pages.ShowAllFacetValuesPage.SELECTED_FACET_PARAM; |
---|
11 | import eu.clarin.cmdi.vlo.pages.ShowResultPage; |
---|
12 | import javax.servlet.ServletContext; |
---|
13 | import org.apache.wicket.Session; |
---|
14 | import org.apache.wicket.protocol.http.WebApplication; |
---|
15 | import org.apache.wicket.request.IRequestParameters; |
---|
16 | import org.apache.wicket.request.Request; |
---|
17 | import org.apache.wicket.request.Response; |
---|
18 | import org.apache.wicket.request.cycle.AbstractRequestCycleListener; |
---|
19 | import org.apache.wicket.request.cycle.RequestCycle; |
---|
20 | import org.apache.wicket.util.string.StringValue; |
---|
21 | |
---|
22 | /** |
---|
23 | * Virtual Language Observatory web application<br><br> |
---|
24 | * |
---|
25 | * <describe VLO> |
---|
26 | * |
---|
27 | * While the application is intended to run inside a web server container, |
---|
28 | * running the Start class enables you to run it without outside one. |
---|
29 | */ |
---|
30 | public class VloWebApplication extends WebApplication { |
---|
31 | |
---|
32 | /** |
---|
33 | * Customised client request cycle<br><br> |
---|
34 | * |
---|
35 | * <intercept resquest in order to update session parameter list> |
---|
36 | * |
---|
37 | * Add behaviour to the web request handling by retrieving persistent |
---|
38 | * parameters to the application from from client requests, and store the in |
---|
39 | * the application object. |
---|
40 | */ |
---|
41 | private class CustomRequestCycleListener extends AbstractRequestCycleListener { |
---|
42 | |
---|
43 | @Override |
---|
44 | public void onBeginRequest(RequestCycle cycle) { |
---|
45 | // first, invoke the default behavior |
---|
46 | super.onBeginRequest(cycle); |
---|
47 | // after that, get the parameters of the request itself |
---|
48 | IRequestParameters reqParam = cycle.getRequest().getRequestParameters(); |
---|
49 | |
---|
50 | // from these, get the parameters represented in the URL |
---|
51 | // Map <String, String[]> map = this.getWebRequest().getParameterMap(); |
---|
52 | // check if there is a theme parameter |
---|
53 | StringValue object = reqParam.getParameterValue("theme"); |
---|
54 | |
---|
55 | if (object.isEmpty()) { |
---|
56 | // no theme choosen, keep the current one |
---|
57 | } else { |
---|
58 | // check if the users requests a different theme |
---|
59 | if (object.toString().matches(((VloSession) Session.get()).getCurrentTheme().name)) { |
---|
60 | // current theme requested, nothing to do |
---|
61 | } else { |
---|
62 | // different theme requested, compose it |
---|
63 | ((VloSession) Session.get()).setCurrentTheme(new Theme(object.toString())); |
---|
64 | // remember the theme as a vlo session page parameter |
---|
65 | ((VloSession) Session.get()).vloSessionPageParameters.add("theme", object); |
---|
66 | } |
---|
67 | } |
---|
68 | } |
---|
69 | } |
---|
70 | |
---|
71 | /** |
---|
72 | * Flag indicating whether or not the application object lives in a web |
---|
73 | * server context. |
---|
74 | */ |
---|
75 | boolean inContext; |
---|
76 | |
---|
77 | /** |
---|
78 | * Method that will be invoked when the application starts. |
---|
79 | */ |
---|
80 | @Override |
---|
81 | public void init() { |
---|
82 | |
---|
83 | if (inContext) { |
---|
84 | |
---|
85 | /* |
---|
86 | * send messages to objects that need a static reference to this web |
---|
87 | * application object. While this, at a one point in time, was only |
---|
88 | * required in the case of the results page BookmarkablePageLink |
---|
89 | * method, uniform approach might be the most prefarable one. |
---|
90 | */ |
---|
91 | BasePage.setWebApp(this); |
---|
92 | BasePanel.setWebApp(this); |
---|
93 | |
---|
94 | // install theme -> compose theme |
---|
95 | // get the servlet's context |
---|
96 | ServletContext servletContext; |
---|
97 | servletContext = this.getServletContext(); |
---|
98 | |
---|
99 | /* |
---|
100 | * Send the application context to the configuration object to |
---|
101 | * enable it to read an external {@literal VloConfig.xml} |
---|
102 | * configuration file. |
---|
103 | */ |
---|
104 | VloContextConfig.switchToExternalConfig(servletContext); |
---|
105 | |
---|
106 | getRequestCycleListeners().add(new CustomRequestCycleListener()); |
---|
107 | } |
---|
108 | |
---|
109 | // creata an object referring to the search results |
---|
110 | searchResults = new SearchResultsDao(); |
---|
111 | |
---|
112 | // mount pages on URL's for bookmarkable links |
---|
113 | mountPages(); |
---|
114 | |
---|
115 | // hand over control to the application |
---|
116 | } |
---|
117 | |
---|
118 | private void mountPages() { |
---|
119 | // Record (query result) page. E.g. /vlo/record?docId=abc123 |
---|
120 | // (cannot encode docId in path because it contains a slash) |
---|
121 | mountPage("/record", ShowResultPage.class); |
---|
122 | |
---|
123 | // All facet values page. E.g. /vlo/values/genre?facetMinOccurs=1 |
---|
124 | // (min occurs not in path because it's a filter on the facet list) |
---|
125 | mountPage("/values/${" + SELECTED_FACET_PARAM + "}", ShowAllFacetValuesPage.class); |
---|
126 | } |
---|
127 | |
---|
128 | // remember the search results |
---|
129 | private SearchResultsDao searchResults; |
---|
130 | |
---|
131 | /** |
---|
132 | * Web application constructor<br><br> |
---|
133 | * |
---|
134 | * Create an application instance configured to be living inside a web |
---|
135 | * server container. |
---|
136 | */ |
---|
137 | public VloWebApplication() { |
---|
138 | |
---|
139 | /* |
---|
140 | * Read the application's packaged configuration |
---|
141 | * |
---|
142 | * Because on instantiation a web application cannot said to be living |
---|
143 | * in a web server context, parameters defined in the context can only |
---|
144 | * be added to the configuration later, in this case: when the {@literal |
---|
145 | * init()} method will be invoked. |
---|
146 | */ |
---|
147 | VloConfig.readPackagedConfig(); |
---|
148 | |
---|
149 | // let the {@literal init()} method know that there will be a context |
---|
150 | inContext = true; |
---|
151 | } |
---|
152 | |
---|
153 | /** |
---|
154 | * Web application constructor<br><br> |
---|
155 | * |
---|
156 | * Allows for the creation of an application instance that does not rely on |
---|
157 | * a web server context. When send the message 'false', this constructor |
---|
158 | * will create an object that will not look for an external configuration |
---|
159 | * file; it will exclusively rely on the packaged configuration. Typically, |
---|
160 | * the application's tests will send false to the application constructor. |
---|
161 | * <br><br> |
---|
162 | * |
---|
163 | * @param inContext If and only if this parameter equals true. later on, the |
---|
164 | * {@literal init} method will try to determine the web server's container |
---|
165 | * context so that, if it is defined in it, an external configuration can be |
---|
166 | * switched to. |
---|
167 | */ |
---|
168 | public VloWebApplication(Boolean inContext) { |
---|
169 | |
---|
170 | // remember that the application does not live in a web server context |
---|
171 | this.inContext = inContext; |
---|
172 | |
---|
173 | searchResults = new SearchResultsDao(); |
---|
174 | } |
---|
175 | |
---|
176 | /** |
---|
177 | * @see org.apache.wicket.Application#getHomePage() |
---|
178 | */ |
---|
179 | @Override |
---|
180 | public Class<FacetedSearchPage> getHomePage() { |
---|
181 | return FacetedSearchPage.class; |
---|
182 | } |
---|
183 | |
---|
184 | public SearchResultsDao getSearchResultsDao() { |
---|
185 | return searchResults; |
---|
186 | } |
---|
187 | |
---|
188 | @Override |
---|
189 | public VloSession newSession(Request request, Response response) { |
---|
190 | return new VloSession(request); |
---|
191 | } |
---|
192 | } |
---|