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 java.util.Map; |
---|
10 | import javax.servlet.ServletContext; |
---|
11 | import org.apache.wicket.Application; |
---|
12 | import org.apache.wicket.PageParameters; |
---|
13 | import org.apache.wicket.Request; |
---|
14 | import org.apache.wicket.RequestCycle; |
---|
15 | import org.apache.wicket.Response; |
---|
16 | import org.apache.wicket.protocol.http.WebApplication; |
---|
17 | import org.apache.wicket.protocol.http.WebRequest; |
---|
18 | import org.apache.wicket.protocol.http.WebRequestCycle; |
---|
19 | import org.apache.wicket.protocol.http.WebSession; |
---|
20 | import org.apache.wicket.request.RequestParameters; |
---|
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 | * Customized client request cycle<br><br> |
---|
34 | * |
---|
35 | * <intercept resquest in order to update session parameter list> |
---|
36 | * |
---|
37 | * Add behavior to the web request handling by retrieving persistent |
---|
38 | * parameters to the application from from client requests, and store |
---|
39 | * the in the application object. |
---|
40 | */ |
---|
41 | private class CustomCycle extends WebRequestCycle { |
---|
42 | |
---|
43 | // find out why this is necessary |
---|
44 | CustomCycle (WebApplication app, WebRequest req, Response res){ |
---|
45 | super (app, req, res); |
---|
46 | } |
---|
47 | |
---|
48 | /** |
---|
49 | * Add the behavior to the beginning of the processing of a request |
---|
50 | */ |
---|
51 | @Override |
---|
52 | public void onBeginRequest() { |
---|
53 | // first, invoke the default behavior |
---|
54 | super.onBeginRequest(); |
---|
55 | // after that, get the parameters of the request itself |
---|
56 | RequestParameters reqParam = this.request.getRequestParameters(); |
---|
57 | // from these, get the parameters represented in the URL |
---|
58 | Map <String, String[]> map = this.getWebRequest().getParameterMap(); |
---|
59 | // check if there is a theme parameter |
---|
60 | String[] object = map.get("theme"); |
---|
61 | |
---|
62 | if (object == null) { |
---|
63 | // no theme choosen, keep the current one |
---|
64 | } else { |
---|
65 | // check if the users requests a different theme |
---|
66 | if (object[0].matches(((ThemedSession)getSession()).getCurrentTheme().name)) { |
---|
67 | // current theme requested, nothing to do |
---|
68 | } else { |
---|
69 | // different theme requested, compose it |
---|
70 | ((ThemedSession)getSession()).setCurrentTheme(new Theme (object[0])); |
---|
71 | // remember the theme as a persistent parameter |
---|
72 | ((ThemedSession)getSession()).persistentParameters.put("theme", object[0]); |
---|
73 | } |
---|
74 | } |
---|
75 | } |
---|
76 | } |
---|
77 | |
---|
78 | /** |
---|
79 | * Put the the customized request cycle up for installation<br><br> |
---|
80 | * |
---|
81 | * Note that casting the request to a WebRequest is assumed to be safe. |
---|
82 | * |
---|
83 | * @param request the request to be passed on the the new handler |
---|
84 | * @param response the response to be passed on the the new handler |
---|
85 | * @return the new handler |
---|
86 | */ |
---|
87 | @Override |
---|
88 | public RequestCycle newRequestCycle(Request request, Response response){ |
---|
89 | |
---|
90 | /* Pass on the application object and parameters to new the request |
---|
91 | * cycle when creating it. |
---|
92 | */ |
---|
93 | CustomCycle cycle = new CustomCycle(this, (WebRequest)request, response); |
---|
94 | |
---|
95 | return cycle; |
---|
96 | } |
---|
97 | |
---|
98 | /** |
---|
99 | * Compose a map to be included in the HTML document, designating the |
---|
100 | * positions of the links to partner web sites |
---|
101 | * |
---|
102 | * @return |
---|
103 | */ |
---|
104 | private String getClarinDPartnerLinkMap(){ |
---|
105 | String map; |
---|
106 | |
---|
107 | map = "<map name=\"partnerLinks\">\n"; |
---|
108 | map = map + "</map>"; |
---|
109 | |
---|
110 | return map; |
---|
111 | } |
---|
112 | |
---|
113 | /** |
---|
114 | * Compose a map to be included in the HTML document, designating the |
---|
115 | * positions of the links to partner web sites |
---|
116 | * |
---|
117 | * @return |
---|
118 | */ |
---|
119 | private String getDefaultPartnerLinkMap(){ |
---|
120 | String map; |
---|
121 | |
---|
122 | map = "<map name=\"partnerLinks\">\n"; |
---|
123 | map = appendToPartnerLinkMap (map, |
---|
124 | "114.00,65,167.50,104", |
---|
125 | "http://www.clarin.eu", |
---|
126 | "clarin link"); |
---|
127 | map = appendToPartnerLinkMap (map, |
---|
128 | "177.00,65,214,104", |
---|
129 | "http://wals.info", |
---|
130 | "wals link"); |
---|
131 | map = appendToPartnerLinkMap (map, |
---|
132 | "229,65,279,104", |
---|
133 | "http://linguistlist.org", |
---|
134 | "linguistlist link"); |
---|
135 | map = appendToPartnerLinkMap (map, |
---|
136 | "290,65,320,104", |
---|
137 | "http://www.elra.info", |
---|
138 | "elra link"); |
---|
139 | map = appendToPartnerLinkMap (map, |
---|
140 | "328,65,370,104", |
---|
141 | "http://www.mpi.nl/dobes", |
---|
142 | "dobes link"); |
---|
143 | map = appendToPartnerLinkMap (map, |
---|
144 | "379,65,428,104", |
---|
145 | "http://www.dfki.de/web", |
---|
146 | "dfki link"); |
---|
147 | map = appendToPartnerLinkMap (map, |
---|
148 | "434,65,484,104", |
---|
149 | "http://www.delaman.org", |
---|
150 | "deleman link"); |
---|
151 | |
---|
152 | map = map + "</map>"; |
---|
153 | |
---|
154 | return map; |
---|
155 | } |
---|
156 | |
---|
157 | /** |
---|
158 | * Add a link location to the map indicating the partner links |
---|
159 | * |
---|
160 | * @param map |
---|
161 | * @return |
---|
162 | */ |
---|
163 | private String appendToPartnerLinkMap(String map, String coordinates, |
---|
164 | String URL, String alt) { |
---|
165 | |
---|
166 | if (map == null) { |
---|
167 | map = "<map name=\"partnerLinks\">\n"; |
---|
168 | } else if (map.equals("")) { |
---|
169 | map = "<map name=\"partnerLinks\">\n"; |
---|
170 | } |
---|
171 | |
---|
172 | map = map + "<AREA SHAPE=\"rect\" COORDS=\"" + coordinates + "\" HREF=\"" + |
---|
173 | URL + "\" alt=\"" + alt + "\"/>\n"; |
---|
174 | |
---|
175 | return map; |
---|
176 | } |
---|
177 | |
---|
178 | public class ThemedSession extends WebSession { |
---|
179 | |
---|
180 | /** |
---|
181 | * Remember the parameters that should persist in URLs to VLO pages |
---|
182 | * <br><br> |
---|
183 | */ |
---|
184 | public PageParameters persistentParameters = new PageParameters(); |
---|
185 | |
---|
186 | /** |
---|
187 | * |
---|
188 | * @return |
---|
189 | */ |
---|
190 | public PageParameters getPersistentParameters (){ |
---|
191 | return persistentParameters; |
---|
192 | } |
---|
193 | |
---|
194 | /** |
---|
195 | * Reflect the persistent parameters in the page parameter map<br><br> |
---|
196 | * |
---|
197 | * @param parameters a page parameter map |
---|
198 | * @return the page parameter map including the current persistent |
---|
199 | * parameters |
---|
200 | */ |
---|
201 | public PageParameters reflectPersistentParameters(PageParameters parameters) { |
---|
202 | |
---|
203 | parameters.putAll(persistentParameters); |
---|
204 | |
---|
205 | // parameters.add("theme", "defaultTheme"); |
---|
206 | return parameters; |
---|
207 | } |
---|
208 | |
---|
209 | /** |
---|
210 | * Theme currently applied in the VLO web application |
---|
211 | */ |
---|
212 | private Theme currentTheme = new Theme ("defaultTheme"); |
---|
213 | |
---|
214 | public ThemedSession(Application application, Request request) { |
---|
215 | super(application, request); |
---|
216 | } |
---|
217 | |
---|
218 | public Theme getCurrentTheme() { |
---|
219 | return currentTheme; |
---|
220 | } |
---|
221 | |
---|
222 | public void setCurrentTheme(Theme currentTheme) { |
---|
223 | this.currentTheme = currentTheme; |
---|
224 | } |
---|
225 | } |
---|
226 | |
---|
227 | |
---|
228 | @Override |
---|
229 | public ThemedSession newSession(Request request, Response response) { |
---|
230 | |
---|
231 | return new ThemedSession(this, request); |
---|
232 | } |
---|
233 | |
---|
234 | /** |
---|
235 | * A theme is composed from a page title, a CSS file, two image files, and a |
---|
236 | * partner link map relating coordinates in the right image to partner links |
---|
237 | */ |
---|
238 | public class Theme { |
---|
239 | |
---|
240 | public String name, pageTitle, topLeftImage, topRightImage, cssFile, |
---|
241 | partnerLinkMap; |
---|
242 | |
---|
243 | /** |
---|
244 | * Compose a theme<br><br> |
---|
245 | * |
---|
246 | * @param name the name of the theme to be composed |
---|
247 | */ |
---|
248 | public Theme(String themeName) { |
---|
249 | |
---|
250 | String prefix = VloConfig.getReverseProxyPrefix(); |
---|
251 | |
---|
252 | if (themeName.matches("CLARIN-D")) { |
---|
253 | // select the CLARIN-D theme's components |
---|
254 | |
---|
255 | pageTitle = "CLARIN-D Virtual Language Observatory - Resources"; |
---|
256 | topLeftImage = "Images/topleft-clarin-d.png"; |
---|
257 | topRightImage = "Images/topright-clarin-d.png"; |
---|
258 | if (prefix.length() == 0) { |
---|
259 | cssFile = "css/clarin-d.css"; |
---|
260 | } else { |
---|
261 | cssFile = prefix + "css/clarin-d.css"; |
---|
262 | } |
---|
263 | partnerLinkMap = getClarinDPartnerLinkMap(); |
---|
264 | name = "CLARIN-D"; |
---|
265 | } else { |
---|
266 | // select the default theme elements |
---|
267 | pageTitle = "CLARIN Virtual Language Observatory - Resources"; |
---|
268 | topLeftImage = "Images/topleftvlo.gif"; |
---|
269 | topRightImage = "Images/toprightvlo.gif"; |
---|
270 | |
---|
271 | if (prefix.length() == 0) { |
---|
272 | cssFile = "css/main.css"; |
---|
273 | } else { |
---|
274 | cssFile = prefix + "css/main.css"; |
---|
275 | } |
---|
276 | partnerLinkMap = getDefaultPartnerLinkMap(); |
---|
277 | name = "defaultTheme"; |
---|
278 | } |
---|
279 | // remember the theme as a persistent parameter |
---|
280 | // getPersistentParameters.put("theme", name); |
---|
281 | } |
---|
282 | } |
---|
283 | |
---|
284 | /** |
---|
285 | * Flag indicating whether or not the application object lives in a web |
---|
286 | * server context. |
---|
287 | */ |
---|
288 | boolean inContext; |
---|
289 | |
---|
290 | /** |
---|
291 | * Method that will be invoked when the application starts. |
---|
292 | */ |
---|
293 | @Override |
---|
294 | public void init() { |
---|
295 | |
---|
296 | if (inContext) { |
---|
297 | |
---|
298 | /* |
---|
299 | * send messages to objects that need a static reference to this web |
---|
300 | * application object. While this, at a one point in time, was only |
---|
301 | * required in the case of the results page BookmarkablePageLink |
---|
302 | * method, uniform approach might be the most prefarable one. |
---|
303 | */ |
---|
304 | BasePage.setWebApp(this); |
---|
305 | BasePanel.setWebApp(this); |
---|
306 | |
---|
307 | // install theme -> compose theme |
---|
308 | |
---|
309 | // get the servlet's context |
---|
310 | |
---|
311 | ServletContext servletContext; |
---|
312 | servletContext = this.getServletContext(); |
---|
313 | |
---|
314 | /* |
---|
315 | * Send the application context to the configuration object to |
---|
316 | * enable it to read an external {@literal VloConfig.xml} |
---|
317 | * configuration file. |
---|
318 | */ |
---|
319 | |
---|
320 | VloContextConfig.switchToExternalConfig(servletContext); |
---|
321 | } |
---|
322 | |
---|
323 | // creata an object referring to the search results |
---|
324 | searchResults = new SearchResultsDao(); |
---|
325 | |
---|
326 | // hand over control to the application |
---|
327 | } |
---|
328 | |
---|
329 | // remember the search results |
---|
330 | private SearchResultsDao searchResults; |
---|
331 | |
---|
332 | /** |
---|
333 | * Web application constructor<br><br> |
---|
334 | * |
---|
335 | * Create an application instance configured to be living inside a web |
---|
336 | * server container. |
---|
337 | */ |
---|
338 | public VloWebApplication() { |
---|
339 | |
---|
340 | /* |
---|
341 | * Read the application's packaged configuration |
---|
342 | * |
---|
343 | * Because on instantiation a web application cannot said to be living |
---|
344 | * in a web server context, parameters defined in the context can only |
---|
345 | * be added to the configuration later, in this case: when the {@literal |
---|
346 | * init()} method will be invoked. |
---|
347 | */ |
---|
348 | |
---|
349 | VloConfig.readPackagedConfig(); |
---|
350 | |
---|
351 | // let the {@literal init()} method know that there will be a context |
---|
352 | |
---|
353 | inContext = true; |
---|
354 | } |
---|
355 | |
---|
356 | /** |
---|
357 | * Web application constructor<br><br> |
---|
358 | * |
---|
359 | * Allows for the creation of an application instance that does not rely on |
---|
360 | * a web server context. When send the message 'false', this constructor |
---|
361 | * will create an object that will not look for an external configuration |
---|
362 | * file; it will exclusively rely on the packaged configuration. Typically, |
---|
363 | * the application's tests will send false to the application constructor. |
---|
364 | * <br><br> |
---|
365 | * |
---|
366 | * @param inContext If and only if this parameter equals true. later on, the |
---|
367 | * {@literal init} method will try to determine the web server's container |
---|
368 | * context so that, if it is defined in it, an external configuration can be |
---|
369 | * switched to. |
---|
370 | */ |
---|
371 | public VloWebApplication(Boolean inContext) { |
---|
372 | |
---|
373 | // remember that the application does not live in a web server context |
---|
374 | |
---|
375 | this.inContext = inContext; |
---|
376 | |
---|
377 | searchResults = new SearchResultsDao(); |
---|
378 | } |
---|
379 | |
---|
380 | /** |
---|
381 | * @see org.apache.wicket.Application#getHomePage() |
---|
382 | */ |
---|
383 | @Override |
---|
384 | public Class<FacetedSearchPage> getHomePage() { |
---|
385 | return FacetedSearchPage.class; |
---|
386 | } |
---|
387 | |
---|
388 | public SearchResultsDao getSearchResultsDao() { |
---|
389 | return searchResults; |
---|
390 | } |
---|
391 | } |
---|