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.RequestCycle; |
---|
13 | import org.apache.wicket.request.RequestParameters; |
---|
14 | import org.apache.wicket.Response; |
---|
15 | import org.apache.wicket.protocol.http.WebApplication; |
---|
16 | import org.apache.wicket.protocol.http.WebRequest; |
---|
17 | import org.apache.wicket.protocol.http.WebRequestCycle; |
---|
18 | import org.apache.wicket.protocol.http.WebResponse; |
---|
19 | |
---|
20 | /** |
---|
21 | * Virtual Language Observatory web application<br><br> |
---|
22 | * |
---|
23 | * <describe VLO> |
---|
24 | * |
---|
25 | * While the application is intended to run inside a web server container, |
---|
26 | * running the Start class enables you to run it without outside one. |
---|
27 | */ |
---|
28 | public class VloWebApplication extends WebApplication { |
---|
29 | |
---|
30 | /** |
---|
31 | * Customised client request cycle<br><br> |
---|
32 | * |
---|
33 | * <intercept resquest in order to update session parameter list> |
---|
34 | * |
---|
35 | * Add behaviour to the web request handling by retrieving persistent |
---|
36 | * parameters to the application from from client requests, and store |
---|
37 | * the in the application object. |
---|
38 | */ |
---|
39 | private class CustomCycle extends WebRequestCycle { |
---|
40 | |
---|
41 | // find out why this is necessary |
---|
42 | CustomCycle (WebApplication app, WebRequest req, Response res){ |
---|
43 | super (app, req, res); |
---|
44 | } |
---|
45 | |
---|
46 | /** |
---|
47 | * Add the behaviour to the beginning of the processing of a request |
---|
48 | */ |
---|
49 | @Override |
---|
50 | public void onBeginRequest() { |
---|
51 | // first, invoke the default behavior |
---|
52 | super.onBeginRequest(); |
---|
53 | // after that, get the parameters of the request itself |
---|
54 | RequestParameters reqParam = this.request.getRequestParameters(); |
---|
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 | String[] object = map.get("theme"); |
---|
59 | |
---|
60 | if (object == null) { |
---|
61 | // no theme choosen, keep the current one |
---|
62 | } else { |
---|
63 | // check if the users requests a different theme |
---|
64 | if (object[0].matches(((VloSession)getSession()).getCurrentTheme().name)) { |
---|
65 | // current theme requested, nothing to do |
---|
66 | } else { |
---|
67 | // different theme requested, compose it |
---|
68 | ((VloSession)getSession()).setCurrentTheme(new Theme (object[0])); |
---|
69 | // remember the theme as a vlo session page parameter |
---|
70 | ((VloSession)getSession()).vloSessionPageParameters.add("theme", object[0]); |
---|
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 | // install the custom request cycle |
---|
116 | WebRequest req = (WebRequest) RequestCycle.get().getRequest(); |
---|
117 | WebResponse res = (WebResponse) RequestCycle.get().getResponse(); |
---|
118 | |
---|
119 | CustomCycle cycle; |
---|
120 | cycle = new CustomCycle(this, req, res); |
---|
121 | |
---|
122 | // creata an object referring to the search results |
---|
123 | searchResults = new SearchResultsDao(); |
---|
124 | |
---|
125 | // hand over control to the application |
---|
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 | |
---|
148 | VloConfig.readPackagedConfig(); |
---|
149 | |
---|
150 | // let the {@literal init()} method know that there will be a context |
---|
151 | |
---|
152 | inContext = true; |
---|
153 | } |
---|
154 | |
---|
155 | /** |
---|
156 | * Web application constructor<br><br> |
---|
157 | * |
---|
158 | * Allows for the creation of an application instance that does not rely on |
---|
159 | * a web server context. When send the message 'false', this constructor |
---|
160 | * will create an object that will not look for an external configuration |
---|
161 | * file; it will exclusively rely on the packaged configuration. Typically, |
---|
162 | * the application's tests will send false to the application constructor. |
---|
163 | * <br><br> |
---|
164 | * |
---|
165 | * @param inContext If and only if this parameter equals true. later on, the |
---|
166 | * {@literal init} method will try to determine the web server's container |
---|
167 | * context so that, if it is defined in it, an external configuration can be |
---|
168 | * switched to. |
---|
169 | */ |
---|
170 | public VloWebApplication(Boolean inContext) { |
---|
171 | |
---|
172 | // remember that the application does not live in a web server context |
---|
173 | |
---|
174 | this.inContext = inContext; |
---|
175 | |
---|
176 | searchResults = new SearchResultsDao(); |
---|
177 | } |
---|
178 | |
---|
179 | /** |
---|
180 | * @see org.apache.wicket.Application#getHomePage() |
---|
181 | */ |
---|
182 | @Override |
---|
183 | public Class<FacetedSearchPage> getHomePage() { |
---|
184 | return FacetedSearchPage.class; |
---|
185 | } |
---|
186 | |
---|
187 | public SearchResultsDao getSearchResultsDao() { |
---|
188 | return searchResults; |
---|
189 | } |
---|
190 | } |
---|