1 | package eu.clarin.cmdi.vlo; |
---|
2 | |
---|
3 | import eu.clarin.cmdi.vlo.service.FacetDescriptionService; |
---|
4 | import eu.clarin.cmdi.vlo.config.VloConfig; |
---|
5 | import eu.clarin.cmdi.vlo.service.PermalinkService; |
---|
6 | import eu.clarin.cmdi.vlo.service.XmlTransformationService; |
---|
7 | import eu.clarin.cmdi.vlo.service.solr.SolrDocumentService; |
---|
8 | import eu.clarin.cmdi.vlo.wicket.pages.AboutPage; |
---|
9 | import eu.clarin.cmdi.vlo.wicket.pages.HelpPage; |
---|
10 | import eu.clarin.cmdi.vlo.wicket.pages.AllFacetValuesPage; |
---|
11 | import eu.clarin.cmdi.vlo.wicket.pages.FacetedSearchPage; |
---|
12 | import eu.clarin.cmdi.vlo.wicket.pages.RecordPage; |
---|
13 | import eu.clarin.cmdi.vlo.wicket.pages.SimpleSearchPage; |
---|
14 | import eu.clarin.cmdi.vlo.wicket.pages.VloBasePage; |
---|
15 | import eu.clarin.cmdi.vlo.wicket.provider.FieldValueConverterProvider; |
---|
16 | import java.io.IOException; |
---|
17 | import java.io.InputStream; |
---|
18 | import java.util.Properties; |
---|
19 | import javax.inject.Inject; |
---|
20 | import org.apache.wicket.Application; |
---|
21 | import org.apache.wicket.markup.html.WebPage; |
---|
22 | import org.apache.wicket.protocol.http.WebApplication; |
---|
23 | import org.apache.wicket.resource.loader.BundleStringResourceLoader; |
---|
24 | import org.apache.wicket.spring.injection.annot.SpringComponentInjector; |
---|
25 | import org.apache.wicket.util.lang.Bytes; |
---|
26 | import org.slf4j.Logger; |
---|
27 | import org.slf4j.LoggerFactory; |
---|
28 | import org.springframework.beans.BeansException; |
---|
29 | import org.springframework.context.ApplicationContext; |
---|
30 | import org.springframework.context.ApplicationContextAware; |
---|
31 | |
---|
32 | /** |
---|
33 | * Application object for your web application. If you want to run this |
---|
34 | * application without deploying, run the Start class. |
---|
35 | * |
---|
36 | * @see eu.clarin.cmdi.Start#main(String[]) |
---|
37 | */ |
---|
38 | public class VloWicketApplication extends WebApplication implements ApplicationContextAware { |
---|
39 | |
---|
40 | private final static Logger logger = LoggerFactory.getLogger(VloWicketApplication.class); |
---|
41 | |
---|
42 | @Inject |
---|
43 | private SolrDocumentService documentService; |
---|
44 | @Inject |
---|
45 | private XmlTransformationService cmdiTransformationService; |
---|
46 | @Inject |
---|
47 | private FieldValueConverterProvider fieldValueConverterProvider; |
---|
48 | @Inject |
---|
49 | private FacetDescriptionService facetDescriptionService; |
---|
50 | @Inject |
---|
51 | private PermalinkService permalinkService; |
---|
52 | @Inject |
---|
53 | private VloConfig vloConfig; |
---|
54 | |
---|
55 | private ApplicationContext applicationContext; |
---|
56 | private String appVersionQualifier; |
---|
57 | |
---|
58 | /** |
---|
59 | * @return the home page of this application |
---|
60 | * @see org.apache.wicket.Application#getHomePage() |
---|
61 | */ |
---|
62 | @Override |
---|
63 | public Class<? extends WebPage> getHomePage() { |
---|
64 | return SimpleSearchPage.class; |
---|
65 | } |
---|
66 | |
---|
67 | /** |
---|
68 | * @see org.apache.wicket.Application#init() |
---|
69 | */ |
---|
70 | @Override |
---|
71 | public void init() { |
---|
72 | super.init(); |
---|
73 | |
---|
74 | // register global resource bundles (from .properties files) |
---|
75 | registerResourceBundles(); |
---|
76 | |
---|
77 | // mount pages on URL paths |
---|
78 | mountPages(); |
---|
79 | |
---|
80 | // configure Wicket cache according to parameters set in VloConfig |
---|
81 | setupCache(); |
---|
82 | |
---|
83 | // determine version qualifier (e.g. 'beta'), which can be used to visually mark the base page |
---|
84 | appVersionQualifier = determineVersionQualifier(); |
---|
85 | logger.info("Version qualifier: {}", appVersionQualifier); |
---|
86 | } |
---|
87 | |
---|
88 | private void registerResourceBundles() { |
---|
89 | // this listener will inject any spring beans that need to be autowired |
---|
90 | getComponentInstantiationListeners().add(new SpringComponentInjector(this, applicationContext)); |
---|
91 | // register the resource of field names (used by eu.clarin.cmdi.vlo.wicket.componentsSolrFieldNameLabel) |
---|
92 | getResourceSettings().getStringResourceLoaders().add(new BundleStringResourceLoader("fieldNames")); |
---|
93 | // register the resource of resource type names and class properties |
---|
94 | getResourceSettings().getStringResourceLoaders().add(new BundleStringResourceLoader("resourceTypes")); |
---|
95 | // register the resource of application properties (version information filtered at build time) |
---|
96 | getResourceSettings().getStringResourceLoaders().add(new BundleStringResourceLoader("application")); |
---|
97 | // register JavaScript bundle (combines JavaScript source in a single resource to decrease number of client requests) |
---|
98 | getResourceBundles().addJavaScriptBundle(VloBasePage.class, "vlo-js", |
---|
99 | JavaScriptResources.getVloFrontJS(), |
---|
100 | JavaScriptResources.getVloHeaderJS(), |
---|
101 | JavaScriptResources.getSyntaxHelpJS(), |
---|
102 | JavaScriptResources.getVloFacetsJS(), |
---|
103 | JavaScriptResources.getJQueryUIJS(), |
---|
104 | JavaScriptResources.getJQueryWatermarkJS() |
---|
105 | ); |
---|
106 | } |
---|
107 | |
---|
108 | private void mountPages() { |
---|
109 | // Faceted search page (simple search is on root) |
---|
110 | mountPage("/search", FacetedSearchPage.class); |
---|
111 | // Record (query result) page. E.g. /vlo/record?docId=abc123 |
---|
112 | // (cannot encode docId in path because it contains a slash) |
---|
113 | mountPage("/record", RecordPage.class); |
---|
114 | // All facet values page (kept for compatibility with old bookmarks) |
---|
115 | // E.g. /vlo/values/genre?facetMinOccurs=1 (min occurs not in path |
---|
116 | // because it's a filter on the facet list) |
---|
117 | mountPage("/values/${" + AllFacetValuesPage.SELECTED_FACET_PARAM + "}", AllFacetValuesPage.class); |
---|
118 | // About page |
---|
119 | mountPage("/about", AboutPage.class); |
---|
120 | // Help page |
---|
121 | mountPage("/help", HelpPage.class); |
---|
122 | } |
---|
123 | |
---|
124 | private void setupCache() { |
---|
125 | // configure cache by applying the vlo configuration settings to it |
---|
126 | final int pagesInApplicationCache = vloConfig.getPagesInApplicationCache(); |
---|
127 | logger.info("Setting Wicket in-memory cache size to {}", pagesInApplicationCache); |
---|
128 | this.getStoreSettings().setInmemoryCacheSize(pagesInApplicationCache); |
---|
129 | |
---|
130 | final Bytes sessionCacheSize = Bytes.kilobytes((long) vloConfig.getSessionCacheSize()); |
---|
131 | logger.info("Setting Wicket max size per session to {}", sessionCacheSize); |
---|
132 | this.getStoreSettings().setMaxSizePerSession(sessionCacheSize); |
---|
133 | } |
---|
134 | |
---|
135 | /** |
---|
136 | * |
---|
137 | * @return a version qualifier, either 'snapshot', 'beta' or null |
---|
138 | */ |
---|
139 | private String determineVersionQualifier() { |
---|
140 | try (InputStream applicationPropertiesStream = getClass().getResourceAsStream("/application.properties")) { |
---|
141 | Properties applicationProperties = new Properties(); |
---|
142 | applicationProperties.load(applicationPropertiesStream); |
---|
143 | final String version = applicationProperties.getProperty("vlo.version"); |
---|
144 | if (version != null) { |
---|
145 | if (version.endsWith("-SNAPSHOT")) { |
---|
146 | return "snapshot"; |
---|
147 | } else if (version.contains("beta")) { |
---|
148 | return "beta"; |
---|
149 | } |
---|
150 | } |
---|
151 | } catch (IOException ex) { |
---|
152 | logger.error("Could not read application properties on init", ex); |
---|
153 | } |
---|
154 | return null; |
---|
155 | } |
---|
156 | |
---|
157 | /** |
---|
158 | * |
---|
159 | * @return the active VLO wicket application |
---|
160 | */ |
---|
161 | public static VloWicketApplication get() { |
---|
162 | return (VloWicketApplication) Application.get(); |
---|
163 | } |
---|
164 | |
---|
165 | /** |
---|
166 | * Method needed for dynamic injection of application context (as happens in |
---|
167 | * unit tests) |
---|
168 | * |
---|
169 | * @param applicationContext |
---|
170 | * @throws BeansException |
---|
171 | */ |
---|
172 | @Override |
---|
173 | public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { |
---|
174 | this.applicationContext = applicationContext; |
---|
175 | } |
---|
176 | |
---|
177 | /** |
---|
178 | * |
---|
179 | * @return a service that retrieves SolrDocuments from the attached index |
---|
180 | */ |
---|
181 | public SolrDocumentService getDocumentService() { |
---|
182 | return documentService; |
---|
183 | } |
---|
184 | |
---|
185 | /** |
---|
186 | * |
---|
187 | * @return a service that transforms CMDI documents to HTML representations |
---|
188 | */ |
---|
189 | public XmlTransformationService getCmdiTransformationService() { |
---|
190 | return cmdiTransformationService; |
---|
191 | } |
---|
192 | |
---|
193 | public FieldValueConverterProvider getFieldValueConverterProvider() { |
---|
194 | return fieldValueConverterProvider; |
---|
195 | } |
---|
196 | |
---|
197 | public FacetDescriptionService getFacetDescriptionService() { |
---|
198 | return facetDescriptionService; |
---|
199 | } |
---|
200 | |
---|
201 | public PermalinkService getPermalinkService() { |
---|
202 | return permalinkService; |
---|
203 | } |
---|
204 | |
---|
205 | public String getAppVersionQualifier() { |
---|
206 | return appVersionQualifier; |
---|
207 | } |
---|
208 | |
---|
209 | } |
---|