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

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

enabled custom request cycle. fixed an issue in html page

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