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