Roberto Sánchez
2014-09-18 52ce72b22ef8d92a1f35b4993bcddaaa66d67350
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package net.curisit.securis;
import java.net.URI;
import java.util.Properties;
import javax.inject.Inject;
import javax.inject.Named;
import net.curisit.securis.ioc.RequestsModule;
import net.curisit.securis.ioc.SecurisModule;
import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.ContextHandlerCollection;
import org.eclipse.jetty.server.handler.ResourceHandler;
import org.eclipse.jetty.servlet.ErrorPageErrorHandler;
import org.eclipse.jetty.servlet.FilterHolder;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.eclipse.jetty.util.resource.Resource;
import org.jboss.resteasy.plugins.guice.GuiceResteasyBootstrapServletContextListener;
import org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.Key;
import com.google.inject.name.Names;
import com.google.inject.persist.PersistFilter;
import com.google.inject.persist.jpa.JpaPersistModule;
public class MainApp {
   private static final Logger LOG = LogManager.getLogger(MainApp.class);
   private static Server server;
   private static Injector injector = null;
   @Inject
   @Named("base-uri")
   private URI uri;
   public static void main(String[] args) throws Exception {
       LOG.info("SeCuris init...");
       SecurisModule securisModule = new SecurisModule();
       JpaPersistModule jpaPersistModule = new JpaPersistModule("localdb");
       Properties props = new Properties();
       props.put("javax.persistence.jdbc.password", securisModule.getPassword());
       props.put("javax.persistence.jdbc.url", securisModule.getUrl(securisModule.getAppDir()));
       LOG.info("BD Url: {} {}", securisModule.getUrl(securisModule.getAppDir()), securisModule.getPassword());
       jpaPersistModule.properties(props);
       injector = Guice.createInjector(securisModule, new RequestsModule(), jpaPersistModule);
       // injector.getInstance(JpaInitializer.class);
       startServer(injector.getInstance(Key.get(URI.class, Names.named("base-uri"))));
       while (true) {
           Thread.currentThread().sleep(100);
       }
   }
   private static void startServer(URI uri) throws Exception {
       System.out.println("Starting jetty...");
       server = new Server(9997);
       ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
       context.setContextPath("/");
       context.addEventListener(injector.getInstance(GuiceResteasyBootstrapServletContextListener.class));
       context.setInitParameter("resteasy.role.based.security", "true");
       context.setInitParameter("resteasy.providers", DefaultExceptionHandler.class.getName());
       // context.addFilter(new FilterHolder(injector.getInstance(AuthFilter.class)), "/*", null);
       context.addFilter(new FilterHolder(injector.getInstance(PersistFilter.class)), "/*", null);
       ServletHolder sh = new ServletHolder(HttpServletDispatcher.class);
       sh.setName("resteasy");
       context.addServlet(sh, "/*");
       ResourceHandler staticResources = new ResourceHandler();
       staticResources.setBaseResource(Resource.newResource(MainApp.class.getResource("/static").toURI()));
       staticResources.setWelcomeFiles(new String[]
           { "/main.html" });
       context.setHandler(staticResources);
       ErrorPageErrorHandler errorHandler = new ErrorPageErrorHandler();
       context.setErrorHandler(errorHandler);
       // errorHandler.addErrorPage(HttpResponseCodes.SC_FORBIDDEN, "/login");
       // errorHandler.addErrorPage(HttpResponseCodes.SC_NOT_FOUND, "/");
       // errorHandler.addErrorPage(javax.ws.rs.NotFoundException.class, "/");
       // errorHandler.addErrorPage(javax.ws.rs.ForbiddenException.class, "/");
       // errorHandler.addErrorPage(javax.ws.rs.ForbiddenException.class.getCanonicalName(), "/");
       // errorHandler.addErrorPage(ErrorPageErrorHandler.GLOBAL_ERROR_PAGE, "/");
       LOG.info("Error Handlers: " + context.getErrorHandler());
       ContextHandlerCollection contexts = new ContextHandlerCollection();
       contexts.setHandlers(new Handler[]
           { staticResources, context });
       // server.setHandler(contexts);
       server.setHandler(context);
       server.start();
       server.join();
       LOG.info("Started server in: http://127.0.0.1:9997/");
   }
}