source: vlo/branches/to-wicket-1.6/vlo_web_app/src/main/java/eu/clarin/cmdi/vlo/VloWebApplication.java @ 4240

Last change on this file since 4240 was 4240, checked in by twagoo, 10 years ago

Replaced calls getting session (either on component or statically) and casting to VloSession? with new convenience method VloSession?.get()

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