1 | /** |
---|
2 | * MailArchiver is an application that provides services for storing and managing e-mail messages through a Web Services SOAP interface. |
---|
3 | * Copyright (C) 2012 Marcio Andre Scholl Levien and Fernando Alberto Reuter Wendt and Jose Ronaldo Nogueira Fonseca Junior |
---|
4 | * |
---|
5 | * This program is free software: you can redistribute it and/or modify |
---|
6 | * it under the terms of the GNU Affero General Public License as |
---|
7 | * published by the Free Software Foundation, either version 3 of the |
---|
8 | * License, or (at your option) any later version. |
---|
9 | * |
---|
10 | * This program is distributed in the hope that it will be useful, |
---|
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
13 | * GNU Affero General Public License for more details. |
---|
14 | * |
---|
15 | * You should have received a copy of the GNU Affero General Public License |
---|
16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
---|
17 | */ |
---|
18 | |
---|
19 | /******************************************************************************\ |
---|
20 | * |
---|
21 | * This product was developed by |
---|
22 | * |
---|
23 | * SERVIÃO FEDERAL DE PROCESSAMENTO DE DADOS (SERPRO), |
---|
24 | * |
---|
25 | * a government company established under Brazilian law (5.615/70), |
---|
26 | * at Department of Development of Porto Alegre. |
---|
27 | * |
---|
28 | \******************************************************************************/ |
---|
29 | |
---|
30 | package serpro.mailarchiver.config; |
---|
31 | |
---|
32 | import java.io.IOException; |
---|
33 | import java.io.Writer; |
---|
34 | import java.lang.reflect.InvocationHandler; |
---|
35 | import java.lang.reflect.Method; |
---|
36 | import java.lang.reflect.Proxy; |
---|
37 | import java.net.URISyntaxException; |
---|
38 | import java.net.URL; |
---|
39 | import java.net.UnknownHostException; |
---|
40 | import java.sql.SQLException; |
---|
41 | import java.util.*; |
---|
42 | |
---|
43 | import javax.servlet.DispatcherType; |
---|
44 | import javax.servlet.ServletException; |
---|
45 | import javax.servlet.http.HttpServletRequest; |
---|
46 | import javax.servlet.http.HttpServletResponse; |
---|
47 | |
---|
48 | import org.aspectj.lang.Aspects; |
---|
49 | import org.eclipse.jetty.http.MimeTypes; |
---|
50 | import org.eclipse.jetty.rewrite.handler.RewriteHandler; |
---|
51 | import org.eclipse.jetty.server.Handler; |
---|
52 | import org.eclipse.jetty.server.Request; |
---|
53 | import org.eclipse.jetty.server.handler.ErrorHandler; |
---|
54 | import org.eclipse.jetty.server.handler.HandlerList; |
---|
55 | import org.eclipse.jetty.server.handler.HandlerWrapper; |
---|
56 | import org.eclipse.jetty.server.nio.SelectChannelConnector; |
---|
57 | import org.eclipse.jetty.server.ssl.SslSelectChannelConnector; |
---|
58 | import org.eclipse.jetty.servlet.DefaultServlet; |
---|
59 | import org.eclipse.jetty.servlet.FilterHolder; |
---|
60 | import org.eclipse.jetty.servlet.ServletContextHandler; |
---|
61 | import org.eclipse.jetty.servlet.ServletHolder; |
---|
62 | import org.eclipse.jetty.servlets.CrossOriginFilter; |
---|
63 | import org.eclipse.jetty.util.resource.FileResource; |
---|
64 | import org.eclipse.jetty.util.ssl.SslContextFactory; |
---|
65 | import org.eclipse.jetty.util.thread.QueuedThreadPool; |
---|
66 | import org.eclipse.jetty.webapp.WebAppContext; |
---|
67 | import org.springframework.context.Lifecycle; |
---|
68 | import org.springframework.context.annotation.*; |
---|
69 | import org.springframework.core.io.ClassPathResource; |
---|
70 | import org.springframework.core.io.Resource; |
---|
71 | import org.springframework.orm.jdo.JdoTransactionManager; |
---|
72 | import org.springframework.orm.jdo.TransactionAwarePersistenceManagerFactoryProxy; |
---|
73 | |
---|
74 | import com.eventrouter.DispatchContext; |
---|
75 | import com.eventrouter.SubscriptionRegistry; |
---|
76 | |
---|
77 | import com.jolbox.bonecp.BoneCPDataSource; |
---|
78 | |
---|
79 | import serpro.mailarchiver.MailArchiver; |
---|
80 | import serpro.mailarchiver.util.*; |
---|
81 | import serpro.mailarchiver.util.jdo.DNJdoDialect; |
---|
82 | import serpro.mailarchiver.util.jdo.DNPersistenceManagerFactory; |
---|
83 | import serpro.mailarchiver.util.jdo.InstanceLifecycleListener; |
---|
84 | import serpro.mailarchiver.util.transaction.AnnotationTransactionAspect; |
---|
85 | |
---|
86 | @Configuration |
---|
87 | public class InfrastructureConfig { |
---|
88 | |
---|
89 | private static final Logger log = Logger.getLocalLogger(); |
---|
90 | |
---|
91 | //SSL data parameters |
---|
92 | //private static final String TRUST_STORE_PASSWORD = "OBF:1vny1zlo1x8e1vnw1vn61x8g1zlu1vn4"; |
---|
93 | //private static final String KEY_STORE_PASSWORD = "OBF:1vny1zlo1x8e1vnw1vn61x8g1zlu1vn4"; |
---|
94 | //private static final String KEY_MANAGER_PASSWORD = "OBF:1u2u1wml1z7s1z7a1wnl1u2g"; |
---|
95 | |
---|
96 | private static final String TRUST_STORE_PASSWORD = "serpromailarchiver"; |
---|
97 | private static final String KEY_STORE_PASSWORD = "serpromailarchiver"; |
---|
98 | private static final String KEY_MANAGER_PASSWORD = "serpromailarchiver"; |
---|
99 | |
---|
100 | public InfrastructureConfig() { |
---|
101 | log.debug("Instanciando configuration bean InfrastructureConfig"); |
---|
102 | } |
---|
103 | |
---|
104 | @Bean(destroyMethod = "destroy") |
---|
105 | public MailArchiver mailArchiver() throws UnknownHostException, URISyntaxException, IOException { |
---|
106 | log.debug("Instanciando bean mailArchiver"); |
---|
107 | MailArchiver mailArchiver = new MailArchiver(); |
---|
108 | log.debug("Bean mailArchiver configurado"); |
---|
109 | return mailArchiver; |
---|
110 | } |
---|
111 | |
---|
112 | @Bean |
---|
113 | public Resource webRoot() throws IOException { |
---|
114 | log.debug("Instanciando bean webRoot"); |
---|
115 | Resource webRoot = new ClassPathResource("/serpro/mailarchiver/config/webroot"); |
---|
116 | log.debug("Bean webRoot configurado"); |
---|
117 | return webRoot; |
---|
118 | } |
---|
119 | |
---|
120 | @Bean(initMethod = "start", destroyMethod = "stop") |
---|
121 | public org.h2.tools.Server h2TcpServer() throws SQLException { |
---|
122 | log.debug("Instanciando bean h2TcpServer"); |
---|
123 | |
---|
124 | List<String> options = new ArrayList<String>(); |
---|
125 | options.add("-ifExists"); |
---|
126 | options.add("-trace"); |
---|
127 | options.add("-baseDir"); |
---|
128 | options.add(userAppConfig().SERVER.getArchiveDir().toString()); |
---|
129 | options.add("-tcpPort"); |
---|
130 | options.add(String.valueOf(userAppConfig().META_ARCHIVE.getTcpPort())); |
---|
131 | if(userAppConfig().META_ARCHIVE.getTcpAllowOthers()) { |
---|
132 | options.add("-tcpAllowOthers"); |
---|
133 | } |
---|
134 | |
---|
135 | org.h2.tools.Server h2TcpServer = org.h2.tools.Server.createTcpServer(options.toArray(new String[options.size()])); |
---|
136 | |
---|
137 | log.debug("Bean h2TcpServer configurado"); |
---|
138 | return h2TcpServer; |
---|
139 | } |
---|
140 | |
---|
141 | @Bean(initMethod = "start", destroyMethod = "stop") |
---|
142 | public Lifecycle h2WebServer() throws SQLException { |
---|
143 | |
---|
144 | return new Lifecycle() { |
---|
145 | |
---|
146 | org.h2.tools.Server h2WebServer; |
---|
147 | { |
---|
148 | if(userAppConfig().META_ARCHIVE.getWebPort() > 0) { |
---|
149 | |
---|
150 | log.debug("Instanciando bean h2WebServer"); |
---|
151 | |
---|
152 | List<String> options = new ArrayList<String>(); |
---|
153 | options.add("-ifExists"); |
---|
154 | //options.add("-trace"); |
---|
155 | options.add("-baseDir"); |
---|
156 | options.add(userAppConfig().SERVER.getArchiveDir().toString()); |
---|
157 | options.add("-webPort"); |
---|
158 | options.add(String.valueOf(userAppConfig().META_ARCHIVE.getWebPort())); |
---|
159 | if(userAppConfig().META_ARCHIVE.getWebAllowOthers()) { |
---|
160 | options.add("-webAllowOthers"); |
---|
161 | } |
---|
162 | |
---|
163 | h2WebServer = org.h2.tools.Server.createWebServer(options.toArray(new String[options.size()])); |
---|
164 | |
---|
165 | log.debug("Bean h2WebServer configurado"); |
---|
166 | } |
---|
167 | } |
---|
168 | |
---|
169 | @Override |
---|
170 | public void start() { |
---|
171 | if(h2WebServer != null) { |
---|
172 | try { |
---|
173 | h2WebServer.start(); |
---|
174 | } |
---|
175 | catch(SQLException ex) { |
---|
176 | throw new RuntimeException(ex); |
---|
177 | } |
---|
178 | } |
---|
179 | } |
---|
180 | |
---|
181 | @Override |
---|
182 | public void stop() { |
---|
183 | if(isRunning()) { |
---|
184 | h2WebServer.stop(); |
---|
185 | } |
---|
186 | } |
---|
187 | |
---|
188 | @Override |
---|
189 | public boolean isRunning() { |
---|
190 | if(h2WebServer != null) { |
---|
191 | return h2WebServer.isRunning(false); |
---|
192 | } |
---|
193 | return false; |
---|
194 | } |
---|
195 | }; |
---|
196 | } |
---|
197 | |
---|
198 | @Bean(destroyMethod="close") |
---|
199 | @DependsOn({"h2TcpServer", "h2WebServer"}) |
---|
200 | public BoneCPDataSource h2ConnectionPool() { |
---|
201 | log.debug("Instanciando bean h2ConnectionPool"); |
---|
202 | BoneCPDataSource h2ConnectionPool = new BoneCPDataSource(); |
---|
203 | h2ConnectionPool.setDriverClass("org.h2.Driver"); |
---|
204 | h2ConnectionPool.setJdbcUrl( |
---|
205 | "jdbc:h2:metaarchive;" + |
---|
206 | "MVCC=TRUE;" + |
---|
207 | "TRACE_LEVEL_FILE=1;" + //0:OFF, 1:ERROR, 2:INFO, 3:DEBUG, 4:SLF4J |
---|
208 | "TRACE_LEVEL_SYSTEM_OUT=1;" + |
---|
209 | "CACHE_SIZE=131072;" + |
---|
210 | "DB_CLOSE_ON_EXIT=FALSE"); |
---|
211 | h2ConnectionPool.setUsername("mail"); |
---|
212 | h2ConnectionPool.setPassword("archiver"); |
---|
213 | h2ConnectionPool.setMinConnectionsPerPartition(10); |
---|
214 | h2ConnectionPool.setMaxConnectionsPerPartition(30); |
---|
215 | h2ConnectionPool.setPartitionCount(3); |
---|
216 | h2ConnectionPool.setAcquireIncrement(5); |
---|
217 | h2ConnectionPool.setStatementsCacheSize(100); |
---|
218 | h2ConnectionPool.setReleaseHelperThreads(3); |
---|
219 | log.debug("Bean h2ConnectionPool configurado"); |
---|
220 | return h2ConnectionPool; |
---|
221 | } |
---|
222 | |
---|
223 | private void CustomErrorPageHandler(String jettyContext, Writer writer) throws IOException{ |
---|
224 | try{ |
---|
225 | StringBuilder pageContent = new StringBuilder(""); |
---|
226 | pageContent.append("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n"); |
---|
227 | pageContent.append("<html xmlns=\"http://www.w3.org/1999/xhtml\">\n"); |
---|
228 | pageContent.append("<head>\n"); |
---|
229 | pageContent.append("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"/>\n"); |
---|
230 | pageContent.append("<link rel=\"shortcut icon\" type=\"image/vnd.microsoft.icon\" href=\"/admin/VAADIN/themes/console/favicon.ico\" />\n"); |
---|
231 | pageContent.append("<link rel=\"icon\" type=\"image/vnd.microsoft.icon\" href=\"/admin/VAADIN/themes/console/favicon.ico\" />\n"); |
---|
232 | pageContent.append("<title>MailArchiver</title>\n"); |
---|
233 | pageContent.append("</head>\n"); |
---|
234 | pageContent.append("<body scroll=\"auto\" class=\"v-generated-body\" style=\"background-color: #D0E2EC;\">\n"); |
---|
235 | pageContent.append("<div id=\"error-screen\" style=\"text-align:center; margin: 0 auto; width:630px; background-color: white; margin-top:210px; border: 1px solid silver; padding: 15px;\" >\n"); |
---|
236 | pageContent.append("<img src=\"/admin/VAADIN/themes/console/toolbar/img/mailarchiver_logo.png\" style=\"float:left; margin-top: 30px;\"/>\n"); |
---|
237 | pageContent.append("<h1 style=\"font-family: Sans-Serif, Arial;font-size: 32pt;font-weight: bold;\">MailArchiver</h1>\n"); |
---|
238 | pageContent.append("<h3 style=\"font-family: Sans-Serif, Arial;font-size: 22pt;font-weight: normal;\">Serviços de arquivamento local de mensagens</h3>\n"); |
---|
239 | pageContent.append("<p style=\"font-family: sans-serif, arial;font-size: 12pt; text-align: center;\">Acesse o <a href=\"https://127.0.0.1:4334/admin/\">console de administração</a></p>\n"); |
---|
240 | pageContent.append("<p></p><p></p>\n"); |
---|
241 | pageContent.append("<p style=\"font-family: sans-serif, arial;font-size: 9pt; text-align: center;\">Serviço Federal de Processamento de Dados - SERPRO</p>\n"); |
---|
242 | pageContent.append("<img src=\"/arcservutil/solucaoSerpro.gif\"/> \n"); |
---|
243 | pageContent.append("<img src=\"/arcservutil/softwareLivre.gif\"/>\n"); |
---|
244 | pageContent.append("</div>\n"); |
---|
245 | pageContent.append("</body>\n"); |
---|
246 | pageContent.append("</html>\n"); |
---|
247 | writer.write(pageContent.toString()); |
---|
248 | log.debug("ErrorHandler adicionado com sucesso ao contexto jetty \"" + jettyContext + "\""); |
---|
249 | } |
---|
250 | catch (IOException ex){ |
---|
251 | log.error("Erro ao adicionar ErrorHandler ao contexto jetty \"" + jettyContext + "\""); |
---|
252 | } |
---|
253 | } |
---|
254 | |
---|
255 | @Bean(destroyMethod = "stop") |
---|
256 | public org.eclipse.jetty.server.Server jetty() throws UnknownHostException, URISyntaxException, IOException { |
---|
257 | log.debug("Instanciando bean jetty"); |
---|
258 | org.eclipse.jetty.server.Server jetty = new org.eclipse.jetty.server.Server(); |
---|
259 | |
---|
260 | |
---|
261 | //thread pool |
---|
262 | QueuedThreadPool threadPool = new QueuedThreadPool(); |
---|
263 | threadPool.setMinThreads(10); |
---|
264 | threadPool.setMaxThreads(200); |
---|
265 | //threadPool.setDetailedDump(true); |
---|
266 | log.debug("threadPool configurado"); |
---|
267 | |
---|
268 | |
---|
269 | //connector |
---|
270 | SelectChannelConnector connector = new SelectChannelConnector(); |
---|
271 | connector.setPort(userAppConfig().SERVER.getPort()); |
---|
272 | connector.setAcceptors(2 * Runtime.getRuntime().availableProcessors()); |
---|
273 | connector.setStatsOn(false); |
---|
274 | connector.setLowResourcesConnections(20000); |
---|
275 | log.debug("connector configurado"); |
---|
276 | |
---|
277 | |
---|
278 | //ssl connector |
---|
279 | //String keyStorePath = new ClassPathResource("/serpro/mailarchiver/config").getURI().toString() + "/keystore"; |
---|
280 | String keyStorePath = new ClassPathResource("/serpro/mailarchiver/config").getURI().toString() + "/mailarchiver"; |
---|
281 | SslContextFactory sslContextFactory = new SslContextFactory(keyStorePath); |
---|
282 | sslContextFactory.setTrustStore(keyStorePath); |
---|
283 | sslContextFactory.setTrustStorePassword(TRUST_STORE_PASSWORD); |
---|
284 | sslContextFactory.setKeyStorePassword(KEY_STORE_PASSWORD); |
---|
285 | sslContextFactory.setKeyManagerPassword(KEY_MANAGER_PASSWORD); |
---|
286 | |
---|
287 | SslSelectChannelConnector sslConnector = new SslSelectChannelConnector(sslContextFactory); |
---|
288 | sslConnector.setPort(userAppConfig().SERVER.getConfidentialPort()); |
---|
289 | sslConnector.setAcceptors(2 * Runtime.getRuntime().availableProcessors()); |
---|
290 | sslConnector.setStatsOn(false); |
---|
291 | sslConnector.setLowResourcesConnections(20000); |
---|
292 | log.debug("ssl connector configurado com URL keystore = " + keyStorePath); |
---|
293 | |
---|
294 | |
---|
295 | //virtual hosts |
---|
296 | Set<String> hosts = new HashSet<String>(); |
---|
297 | log.debug("Virtual Hosts: iniciando mapeamento local de hosts..."); |
---|
298 | /*for(InetAddress internetAddress : environment().getInternetAddresses()) { |
---|
299 | hosts.add(internetAddress.getHostAddress()); |
---|
300 | //log.debug("Adicionando Host -> inetAddress.getHostAddress = : %s", internetAddress.getHostAddress().toString()); |
---|
301 | hosts.add(internetAddress.getHostName()); |
---|
302 | //log.debug("Adicionando Host -> inetAddress.getHostName = : %s", internetAddress.getHostName().toString()); |
---|
303 | hosts.add(internetAddress.getCanonicalHostName()); |
---|
304 | //log.debug("Adicionando Host -> inetAddress.getCanonicalHostName = : %s", internetAddress.getCanonicalHostName().toString()); |
---|
305 | }*/ |
---|
306 | //Limitar acesso somente ao local host |
---|
307 | hosts.add("0:0:0:0:0:0:0:1%1"); |
---|
308 | hosts.add("localhost"); |
---|
309 | hosts.add("127.0.0.1"); |
---|
310 | |
---|
311 | log.debug("Virtual Hosts: mapeamento local de hosts da estação encerrado."); |
---|
312 | String[] virtualHosts = hosts.toArray(new String[hosts.size()]); |
---|
313 | log.debug("Virtual Hosts: %s", hosts.toString()); |
---|
314 | |
---|
315 | |
---|
316 | //handlers |
---|
317 | Handler arcservHandler = null; |
---|
318 | { |
---|
319 | WebAppContext webAppContext = new WebAppContext(); |
---|
320 | webAppContext.setServer(jetty); |
---|
321 | webAppContext.setContextPath("/arcserv"); |
---|
322 | String war = webRoot().getURI().toString() + "/arcserv"; |
---|
323 | webAppContext.setWar(war); |
---|
324 | webAppContext.setExtractWAR(false); |
---|
325 | webAppContext.setVirtualHosts(virtualHosts); |
---|
326 | |
---|
327 | webAppContext.setErrorHandler(new ErrorHandler() { |
---|
328 | |
---|
329 | @Override |
---|
330 | protected void writeErrorPage(HttpServletRequest request, Writer writer, int code, String message, boolean showStacks) throws IOException { |
---|
331 | CustomErrorPageHandler("arcser", writer); |
---|
332 | } |
---|
333 | }); |
---|
334 | |
---|
335 | arcservHandler = webAppContext; |
---|
336 | |
---|
337 | log.debug("Contexto arcserv configurado. War: %s", war); |
---|
338 | } |
---|
339 | |
---|
340 | |
---|
341 | Handler adminHandler = null; |
---|
342 | { |
---|
343 | WebAppContext webAppContext = new WebAppContext(); |
---|
344 | webAppContext.setServer(jetty); |
---|
345 | webAppContext.setContextPath("/admin"); |
---|
346 | String war = webRoot().getURI().toString() + "/admin"; |
---|
347 | webAppContext.setWar(war); |
---|
348 | webAppContext.setExtractWAR(true); |
---|
349 | webAppContext.setVirtualHosts(virtualHosts); |
---|
350 | adminHandler = webAppContext; |
---|
351 | |
---|
352 | log.debug("Contexto admin configurado. War: %s", war); |
---|
353 | } |
---|
354 | |
---|
355 | Handler arcservutilHandler = null; |
---|
356 | { |
---|
357 | WebAppContext webAppContext = new WebAppContext(); |
---|
358 | webAppContext.setServer(jetty); |
---|
359 | webAppContext.setContextPath("/arcservutil"); |
---|
360 | |
---|
361 | String war = webRoot().getURI().toString() + "/arcservutil"; |
---|
362 | webAppContext.setWar(war); |
---|
363 | webAppContext.setExtractWAR(true); |
---|
364 | webAppContext.setVirtualHosts(virtualHosts); |
---|
365 | |
---|
366 | webAppContext.setErrorHandler(new ErrorHandler() { |
---|
367 | |
---|
368 | @Override |
---|
369 | protected void writeErrorPage(HttpServletRequest request, Writer writer, int code, String message, boolean showStacks) throws IOException { |
---|
370 | CustomErrorPageHandler("arcservutil", writer); |
---|
371 | } |
---|
372 | }); |
---|
373 | |
---|
374 | webAppContext.setInitParameter("org.eclipse.jetty.servlet.Default.dirAllowed", "false"); |
---|
375 | //webAppContext.setInitParameter("xpoweredBy", "false"); |
---|
376 | log.debug("Parâmetros de inicialização aplicados ao contexto arcservutil"); |
---|
377 | |
---|
378 | arcservutilHandler = webAppContext; |
---|
379 | |
---|
380 | log.debug("Contexto arcservutil configurado. War: %s", war); |
---|
381 | } |
---|
382 | |
---|
383 | Handler mailHandler = null; |
---|
384 | { |
---|
385 | CrossOriginFilter crossOriginFilter = new CrossOriginFilter(); |
---|
386 | |
---|
387 | FilterHolder filterHolder = new FilterHolder(); |
---|
388 | filterHolder.setFilter(crossOriginFilter); |
---|
389 | filterHolder.setInitParameter("allowedOrigins", "*"); |
---|
390 | filterHolder.setInitParameter("allowedMethods", "GET,POST,OPTIONS"); |
---|
391 | filterHolder.setInitParameter("allowedHeaders", "Content-Type,MessageType,SOAPAction,X-Requested-With"); |
---|
392 | filterHolder.setInitParameter("preflightMaxAge", "1800"); |
---|
393 | filterHolder.setInitParameter("allowCredentials", "true"); |
---|
394 | |
---|
395 | DefaultServlet defaultServlet = new DefaultServlet(); |
---|
396 | |
---|
397 | ServletHolder servletHolder = new ServletHolder(); |
---|
398 | servletHolder.setServlet(defaultServlet); |
---|
399 | //TODO: true somente para teste |
---|
400 | //servletHolder.setInitParameter("dirAllowed", "true"); |
---|
401 | servletHolder.setInitParameter("org.eclipse.jetty.servlet.Default.dirAllowed", "false"); |
---|
402 | |
---|
403 | MimeTypes mimeTypes = new MimeTypes(); |
---|
404 | mimeTypes.addMimeMapping("txt", MimeTypes.TEXT_PLAIN); |
---|
405 | mimeTypes.addMimeMapping("eml", MimeTypes.TEXT_PLAIN); |
---|
406 | mimeTypes.addMimeMapping("msg", MimeTypes.TEXT_PLAIN); |
---|
407 | mimeTypes.addMimeMapping("rfc822", MimeTypes.TEXT_PLAIN); |
---|
408 | |
---|
409 | URL url = userAppConfig().SERVER.getArchiveDir().resolve("mail").toUri().toURL(); |
---|
410 | |
---|
411 | RewriteHandler rewriteHandler = new RewriteHandler(); |
---|
412 | rewriteHandler.setServer(jetty); |
---|
413 | rewriteHandler.setRewritePathInfo(true); |
---|
414 | rewriteHandler.setRewriteRequestURI(true); |
---|
415 | MailAccessRule mailAccessRule = new MailAccessRule(); |
---|
416 | rewriteHandler.addRule(mailAccessRule); |
---|
417 | |
---|
418 | ServletContextHandler servletContextHandler = new ServletContextHandler(ServletContextHandler.SESSIONS); |
---|
419 | servletContextHandler.setServer(jetty); |
---|
420 | servletContextHandler.setContextPath("/mail"); |
---|
421 | servletContextHandler.setHandler(rewriteHandler); |
---|
422 | servletContextHandler.addServlet(servletHolder,"/*"); |
---|
423 | servletContextHandler.addFilter(filterHolder, "/*", EnumSet.of(DispatcherType.REQUEST)); |
---|
424 | servletContextHandler.setVirtualHosts(virtualHosts); |
---|
425 | servletContextHandler.setBaseResource(new FileResource(url)); |
---|
426 | servletContextHandler.setMimeTypes(mimeTypes); |
---|
427 | |
---|
428 | servletContextHandler.setErrorHandler(new ErrorHandler() { |
---|
429 | |
---|
430 | @Override |
---|
431 | protected void writeErrorPage(HttpServletRequest request, Writer writer, int code, String message, boolean showStacks) throws IOException { |
---|
432 | CustomErrorPageHandler("arcservutil", writer); |
---|
433 | } |
---|
434 | }); |
---|
435 | |
---|
436 | mailHandler = servletContextHandler; |
---|
437 | |
---|
438 | log.debug("Contexto mail configurado"); |
---|
439 | } |
---|
440 | |
---|
441 | Handler tempHandler = null; |
---|
442 | { |
---|
443 | CrossOriginFilter crossOriginFilter = new CrossOriginFilter(); |
---|
444 | |
---|
445 | FilterHolder filterHolder = new FilterHolder(); |
---|
446 | filterHolder.setFilter(crossOriginFilter); |
---|
447 | filterHolder.setInitParameter("allowedOrigins", "*"); |
---|
448 | filterHolder.setInitParameter("allowedMethods", "GET,POST,OPTIONS"); |
---|
449 | filterHolder.setInitParameter("allowedHeaders", "Content-Type,MessageType,SOAPAction,X-Requested-With"); |
---|
450 | filterHolder.setInitParameter("preflightMaxAge", "1800"); |
---|
451 | filterHolder.setInitParameter("allowCredentials", "true"); |
---|
452 | |
---|
453 | DefaultServlet defaultServlet = new DefaultServlet(); |
---|
454 | ServletHolder servletHolder = new ServletHolder(); |
---|
455 | servletHolder.setServlet(defaultServlet); |
---|
456 | //TODO: true somente para teste |
---|
457 | //servletHolder.setInitParameter("dirAllowed", "true"); |
---|
458 | |
---|
459 | MimeTypes mimeTypes = new MimeTypes(); |
---|
460 | mimeTypes.addMimeMapping("txt", MimeTypes.TEXT_PLAIN); |
---|
461 | mimeTypes.addMimeMapping("eml", MimeTypes.TEXT_PLAIN); |
---|
462 | mimeTypes.addMimeMapping("msg", MimeTypes.TEXT_PLAIN); |
---|
463 | mimeTypes.addMimeMapping("rfc822", MimeTypes.TEXT_PLAIN); |
---|
464 | |
---|
465 | URL url = userAppConfig().SERVER.getArchiveDir().resolve("temp").toUri().toURL(); |
---|
466 | |
---|
467 | RewriteHandler rewriteHandler = new RewriteHandler(); |
---|
468 | rewriteHandler.setServer(jetty); |
---|
469 | rewriteHandler.setRewritePathInfo(true); |
---|
470 | rewriteHandler.setRewriteRequestURI(true); |
---|
471 | TempPublishRule tempPublishRule = new TempPublishRule(); |
---|
472 | rewriteHandler.addRule(tempPublishRule); |
---|
473 | |
---|
474 | ServletContextHandler servletContextHandler = new ServletContextHandler(ServletContextHandler.SESSIONS); |
---|
475 | servletContextHandler.setServer(jetty); |
---|
476 | servletContextHandler.setContextPath("/temp"); |
---|
477 | servletContextHandler.setHandler(rewriteHandler); |
---|
478 | servletContextHandler.addServlet(servletHolder,"/*"); |
---|
479 | servletContextHandler.addFilter(filterHolder, "/*", EnumSet.of(DispatcherType.REQUEST)); |
---|
480 | servletContextHandler.setVirtualHosts(virtualHosts); |
---|
481 | servletContextHandler.setBaseResource(new FileResource(url)); |
---|
482 | servletContextHandler.setMimeTypes(mimeTypes); |
---|
483 | |
---|
484 | servletContextHandler.setInitParameter("org.eclipse.jetty.servlet.Default.dirAllowed", "false"); |
---|
485 | //webAppContext.setInitParameter("xpoweredBy", "false"); |
---|
486 | log.debug("Parâmetros de inicialização aplicados ao contexto temp"); |
---|
487 | |
---|
488 | // servletContextHandler.setErrorHandler(createErrorHandler()); |
---|
489 | |
---|
490 | servletContextHandler.setErrorHandler(new ErrorHandler() { |
---|
491 | |
---|
492 | @Override |
---|
493 | protected void writeErrorPage(HttpServletRequest request, Writer writer, int code, String message, boolean showStacks) throws IOException { |
---|
494 | CustomErrorPageHandler("arcservutil", writer); |
---|
495 | } |
---|
496 | }); |
---|
497 | |
---|
498 | HandlerWrapper handlerWrapper = new HandlerWrapper() { |
---|
499 | @Override |
---|
500 | public void handle( |
---|
501 | String target, |
---|
502 | Request baseRequest, |
---|
503 | HttpServletRequest request, |
---|
504 | final HttpServletResponse response) throws IOException, ServletException { |
---|
505 | |
---|
506 | HttpServletResponse responseProxy = (HttpServletResponse) Proxy.newProxyInstance( |
---|
507 | response.getClass().getClassLoader(), |
---|
508 | response.getClass().getInterfaces(), |
---|
509 | new InvocationHandler() { |
---|
510 | |
---|
511 | private boolean redirected = false; |
---|
512 | |
---|
513 | @Override |
---|
514 | public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { |
---|
515 | String methodName = method.getName(); |
---|
516 | if(methodName.equals("sendError") && redirected) { |
---|
517 | int statusCode = ((Integer)args[0]).intValue(); |
---|
518 | if(statusCode == HttpServletResponse.SC_NOT_FOUND) { |
---|
519 | return null; |
---|
520 | } |
---|
521 | } |
---|
522 | Object result = method.invoke(response, args); |
---|
523 | if(methodName.equals("sendRedirect")) { |
---|
524 | redirected = true; |
---|
525 | } |
---|
526 | return result; |
---|
527 | } |
---|
528 | }); |
---|
529 | |
---|
530 | super.handle(target, baseRequest, request, responseProxy); |
---|
531 | } |
---|
532 | }; |
---|
533 | handlerWrapper.setServer(jetty); |
---|
534 | handlerWrapper.setHandler(servletContextHandler); |
---|
535 | |
---|
536 | tempHandler = handlerWrapper; |
---|
537 | |
---|
538 | log.debug("Contexto temp configurado"); |
---|
539 | } |
---|
540 | |
---|
541 | HandlerList handlers = new HandlerList(); |
---|
542 | handlers.setHandlers(new Handler[] { |
---|
543 | arcservHandler, |
---|
544 | adminHandler, |
---|
545 | arcservutilHandler, |
---|
546 | mailHandler, |
---|
547 | tempHandler |
---|
548 | }); |
---|
549 | log.debug("handlerList configurado"); |
---|
550 | |
---|
551 | jetty.setThreadPool(threadPool); |
---|
552 | jetty.addConnector(connector); |
---|
553 | jetty.addConnector(sslConnector); |
---|
554 | |
---|
555 | jetty.setHandler(handlers); |
---|
556 | |
---|
557 | jetty.setStopAtShutdown(true); |
---|
558 | jetty.setSendServerVersion(true); |
---|
559 | jetty.setSendDateHeader(true); |
---|
560 | jetty.setGracefulShutdown(5000); |
---|
561 | |
---|
562 | // jetty.setDumpAfterStart(true); |
---|
563 | |
---|
564 | String dump = jetty.dump(); |
---|
565 | System.out.println(dump); |
---|
566 | log.debug("Jetty dump:%n%n%s", dump); |
---|
567 | |
---|
568 | log.debug("Bean jetty configurado"); |
---|
569 | |
---|
570 | return jetty; |
---|
571 | } |
---|
572 | |
---|
573 | @Bean(destroyMethod = "close") |
---|
574 | public DNPersistenceManagerFactory pmf() { |
---|
575 | log.debug("Instanciando bean pmf"); |
---|
576 | |
---|
577 | Map<String, Object> props = new HashMap<String, Object>(); |
---|
578 | |
---|
579 | props.put("datanucleus.storeManagerType", "rdbms"); |
---|
580 | props.put("datanucleus.manageRelationships", "false"); |
---|
581 | props.put("datanucleus.persistenceByReachabilityAtCommit", "false"); |
---|
582 | props.put("datanucleus.findObject.validateWhenCached", "false"); |
---|
583 | |
---|
584 | props.put("datanucleus.autoCreateSchema", "false"); |
---|
585 | props.put("datanucleus.autoCreateTables", "false"); |
---|
586 | props.put("datanucleus.autoCreateColumns", "false"); |
---|
587 | props.put("datanucleus.autoCreateConstraints", "false"); |
---|
588 | |
---|
589 | props.put("datanucleus.validateTables", "true"); |
---|
590 | props.put("datanucleus.validateColumns", "true"); |
---|
591 | props.put("datanucleus.validateConstraints", "true"); |
---|
592 | |
---|
593 | DNPersistenceManagerFactory pmf = new DNPersistenceManagerFactory(props); |
---|
594 | |
---|
595 | pmf.setConnectionFactory(h2ConnectionPool()); |
---|
596 | pmf.setNontransactionalRead(true); |
---|
597 | pmf.addInstanceLifecycleListener(lifecycleListener(), null); |
---|
598 | log.debug("Bean pmf configurado"); |
---|
599 | return pmf; |
---|
600 | } |
---|
601 | |
---|
602 | @Bean |
---|
603 | public InstanceLifecycleListener lifecycleListener() { |
---|
604 | log.debug("Instanciando bean lifecycleListener"); |
---|
605 | InstanceLifecycleListener lifecycleListener = new InstanceLifecycleListener(); |
---|
606 | log.debug("Bean lifecycleListener configurado"); |
---|
607 | return lifecycleListener; |
---|
608 | } |
---|
609 | |
---|
610 | @Bean |
---|
611 | public TransactionAwarePersistenceManagerFactoryProxy pmfProxy() { |
---|
612 | log.debug("Instanciando bean pmfProxy"); |
---|
613 | TransactionAwarePersistenceManagerFactoryProxy pmfProxy = new TransactionAwarePersistenceManagerFactoryProxy(); |
---|
614 | pmfProxy.setTargetPersistenceManagerFactory(pmf()); |
---|
615 | pmfProxy.setAllowCreate(false); |
---|
616 | log.debug("Bean pmfProxy configurado"); |
---|
617 | return pmfProxy; |
---|
618 | } |
---|
619 | |
---|
620 | @Bean |
---|
621 | @DependsOn({"transactionAspect"}) |
---|
622 | public JdoTransactionManager txManager() { |
---|
623 | log.debug("Instanciando bean txManager"); |
---|
624 | JdoTransactionManager txManager = new JdoTransactionManager(); |
---|
625 | txManager.setJdoDialect(jdoDialect()); |
---|
626 | txManager.setPersistenceManagerFactory(pmf()); |
---|
627 | Aspects.aspectOf(AnnotationTransactionAspect.class).setTransactionManager(txManager); |
---|
628 | log.debug("Bean txManager configurado"); |
---|
629 | return txManager; |
---|
630 | } |
---|
631 | |
---|
632 | @Bean |
---|
633 | public AnnotationTransactionAspect transactionAspect() { |
---|
634 | log.debug("Instanciando bean transactionAspect"); |
---|
635 | AnnotationTransactionAspect transactionAspect = new AnnotationTransactionAspect(); |
---|
636 | log.debug("Bean transactionAspect configurado"); |
---|
637 | return transactionAspect; |
---|
638 | } |
---|
639 | |
---|
640 | @Bean |
---|
641 | public DNJdoDialect jdoDialect() { |
---|
642 | log.debug("Instanciando bean jdoDialect"); |
---|
643 | DNJdoDialect jdoDialect = new DNJdoDialect(); |
---|
644 | log.debug("Bean jdoDialect configurado"); |
---|
645 | return jdoDialect; |
---|
646 | } |
---|
647 | |
---|
648 | @Bean |
---|
649 | @Scope(value = "session", proxyMode = ScopedProxyMode.TARGET_CLASS) |
---|
650 | public SubscriptionRegistry subscriptionRegistry() { |
---|
651 | log.debug("Instanciando bean subscriptionRegistry"); |
---|
652 | SubscriptionRegistry subscriptionRegistry = new SubscriptionRegistry(); |
---|
653 | log.debug("Bean subscriptionRegistry configurado"); |
---|
654 | return subscriptionRegistry; |
---|
655 | } |
---|
656 | |
---|
657 | @Bean |
---|
658 | @Scope(value = "session", proxyMode = ScopedProxyMode.TARGET_CLASS) |
---|
659 | public DispatchContext dispatchContext() { |
---|
660 | log.debug("Instanciando bean dispatchContext"); |
---|
661 | DispatchContext dispatchContext = DispatchContext.synchronousUnsafe(subscriptionRegistry()); |
---|
662 | log.debug("Bean dispatchContext configurado"); |
---|
663 | return dispatchContext; |
---|
664 | } |
---|
665 | |
---|
666 | @Bean |
---|
667 | public ArchiveServicesProxyFactory archiveServicesProxyFactory() { |
---|
668 | log.debug("Instanciando bean archiveServicesProxyFactory"); |
---|
669 | ArchiveServicesProxyFactory archiveServicesProxyFactory = new ArchiveServicesProxyFactory(); |
---|
670 | log.debug("Bean archiveServicesProxyFactory configurado"); |
---|
671 | return archiveServicesProxyFactory; |
---|
672 | } |
---|
673 | |
---|
674 | @Bean |
---|
675 | public SystemAuthenticator systemAuthenticator() { |
---|
676 | log.debug("Instanciando bean systemAuthenticator"); |
---|
677 | SystemAuthenticator systemAuthenticator = new SystemAuthenticator(); |
---|
678 | log.debug("Bean systemAuthenticator configurado"); |
---|
679 | return systemAuthenticator; |
---|
680 | } |
---|
681 | |
---|
682 | @Bean |
---|
683 | public TrayIconAdapter trayIcon() { |
---|
684 | log.debug("Instanciando bean trayIcon"); |
---|
685 | TrayIconAdapter trayIcon = new TrayIconAdapter(); |
---|
686 | log.debug("Bean trayIcon configurado"); |
---|
687 | return trayIcon; |
---|
688 | } |
---|
689 | |
---|
690 | @Bean |
---|
691 | public UserAppConfig userAppConfig() { |
---|
692 | log.debug("Adquirindo bean userAppConfig"); |
---|
693 | UserAppConfig userAppConfig = UserAppConfig.getInstance(); |
---|
694 | log.debug("Bean userAppConfig configurado"); |
---|
695 | return userAppConfig; |
---|
696 | } |
---|
697 | |
---|
698 | @Bean |
---|
699 | public Environment environment() { |
---|
700 | log.debug("Adquirindo bean environment"); |
---|
701 | Environment environment = Environment.getInstance(); |
---|
702 | log.debug("Bean environment configurado"); |
---|
703 | return environment; |
---|
704 | } |
---|
705 | } |
---|