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

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

minor refactoring for readability

File size: 7.2 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                final String themeName = object.toString();
60                // check if the users requests a different theme
61                if (themeName.matches(((VloSession) Session.get()).getCurrentTheme().name)) {
62                    // current theme requested, nothing to do
63                } else {
64                    // different theme requested, compose it
65                    VloSession.get().setCurrentTheme(new Theme(themeName));
66                    // remember the theme as a vlo session page parameter
67                    PageParameters params = new PageParameters();
68                    params.add("theme", object);
69                    VloSession.get().addVloSessionPageParameters(params);
70                }
71            }
72        }
73    }
74
75    /**
76     * Flag indicating whether or not the application object lives in a web
77     * server context.
78     */
79    boolean inContext;
80
81    /**
82     * Method that will be invoked when the application starts.
83     */
84    @Override
85    public void init() {
86
87        if (inContext) {
88
89            /*
90             * send messages to objects that need a static reference to this web
91             * application object. While this, at a one point in time, was only
92             * required in the case of the results page BookmarkablePageLink
93             * method, uniform approach might be the most prefarable one.
94             */
95            BasePage.setWebApp(this);
96            BasePanel.setWebApp(this);
97
98            // install theme -> compose theme
99            // get the servlet's context
100            ServletContext servletContext;
101            servletContext = this.getServletContext();
102
103            /*
104             * Send the application context to the configuration object to
105             * enable it to read an external {@literal VloConfig.xml}
106             * configuration file.
107             */
108            VloContextConfig.switchToExternalConfig(servletContext);
109
110            getRequestCycleListeners().add(new CustomRequestCycleListener());
111        }
112
113        // creata an object referring to the search results
114        searchResults = new SearchResultsDao();
115
116        // mount pages on URL's for bookmarkable links
117        mountPages();
118
119        // hand over control to the application
120    }
121
122    private void mountPages() {
123        // Record (query result) page. E.g. /vlo/record?docId=abc123
124        // (cannot encode docId in path because it contains a slash)
125        mountPage("/record", ShowResultPage.class);
126
127        // All facet values page. E.g. /vlo/values/genre?facetMinOccurs=1
128        // (min occurs not in path because it's a filter on the facet list)
129        mountPage("/values/${" + SELECTED_FACET_PARAM + "}", ShowAllFacetValuesPage.class);
130    }
131
132    // remember the search results
133    private SearchResultsDao searchResults;
134
135    /**
136     * Web application constructor<br><br>
137     *
138     * Create an application instance configured to be living inside a web
139     * server container.
140     */
141    public VloWebApplication() {
142
143        /*
144         * Read the application's packaged configuration
145         *
146         * Because on instantiation a web application cannot said to be living
147         * in a web server context, parameters defined in the context can only
148         * be added to the configuration later, in this case: when the {@literal
149         * init()} method will be invoked.
150         */
151        VloConfig.readPackagedConfig();
152
153        // let the {@literal init()} method know that there will be a context
154        inContext = true;
155    }
156
157    /**
158     * Web application constructor<br><br>
159     *
160     * Allows for the creation of an application instance that does not rely on
161     * a web server context. When send the message 'false', this constructor
162     * will create an object that will not look for an external configuration
163     * file; it will exclusively rely on the packaged configuration. Typically,
164     * the application's tests will send false to the application constructor.
165     * <br><br>
166     *
167     * @param inContext If and only if this parameter equals true. later on, the
168     * {@literal init} method will try to determine the web server's container
169     * context so that, if it is defined in it, an external configuration can be
170     * switched to.
171     */
172    public VloWebApplication(Boolean inContext) {
173
174        // remember that the application does not live in a web server context
175        this.inContext = inContext;
176
177        searchResults = new SearchResultsDao();
178    }
179
180    /**
181     * @see org.apache.wicket.Application#getHomePage()
182     */
183    @Override
184    public Class<FacetedSearchPage> getHomePage() {
185        return FacetedSearchPage.class;
186    }
187
188    public SearchResultsDao getSearchResultsDao() {
189        return searchResults;
190    }
191
192    @Override
193    public VloSession newSession(Request request, Response response) {
194        return new VloSession(request);
195    }
196}
Note: See TracBrowser for help on using the repository browser.