Roberto Sánchez
2013-12-19 4362922ba00a22d9177df1731673b5fb4db03fb5
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package net.curisit.securis;
import java.net.URI;
import javax.inject.Inject;
import javax.inject.Named;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;
import net.curisit.securis.ioc.RequestsModule;
import net.curisit.securis.ioc.SecurisModule;
import org.eclipse.jetty.server.Server;
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.jboss.resteasy.plugins.guice.GuiceResteasyBootstrapServletContextListener;
import org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher;
import org.jboss.resteasy.util.HttpResponseCodes;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.Key;
import com.google.inject.name.Names;
public class MainApp {
   private static final Logger log = LoggerFactory.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...");
       injector = Guice.createInjector(new SecurisModule(), new RequestsModule());
       // createBiDirectionalGuiceBridge(ServiceLocatorFactory.getInstance().create("default"), new SecurisModule());
       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...");
       // ResourceConfig rc = new PackagesResourceConfig("net.curisit.securis.services", "org.codehaus.jackson.jaxrs");
       // IoCComponentProviderFactory ioc = new GuiceComponentProviderFactory(rc, injector);
       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);
       ServletHolder sh = new ServletHolder(HttpServletDispatcher.class);
       // context.addServlet(DefaultServlet.class, "/*");
       context.addServlet(sh, "/");
       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());
       server.setHandler(context);
       server.start();
       server.join();
       // rc.packages("net.curisit.securis.services", "org.codehaus.jackson.jaxrs");
       // new CLStaticHttpHandler(new URLClassLoader(new URL[] {new URL("file:///home/username/staticfiles.jar")})), "/www")
   }
   /**
    * User: Nuwan.N.Bandara
    */
   @Provider
   public static class DefaultExceptionHandler implements ExceptionMapper<Exception> {
       public DefaultExceptionHandler() {
           log.info("Creating DefaultExceptionHandler ");
       }
       @Override
       public Response toResponse(Exception e) {
           // For simplicity I am preparing error xml by hand.
           // Ideally we should create an ErrorResponse class to hold the error info.
           StringBuilder response = new StringBuilder("<response>");
           response.append("<status>ERROR</status>");
           response.append("<message>" + e.getMessage() + "</message>");
           response.append("</response>");
           return Response.serverError().entity(response.toString()).type(MediaType.APPLICATION_XML).build();
       }
   }
}
/*
 * Constraint constraint = new Constraint(); constraint.setName(Constraint.__FORM_AUTH);; constraint.setRoles(new String[]{"user","admin","moderator"}); constraint.setAuthenticate(true);
 * 
 * ConstraintMapping constraintMapping = new ConstraintMapping(); constraintMapping.setConstraint(constraint); constraintMapping.setPathSpec("/*");
 * 
 * ConstraintSecurityHandler securityHandler = new ConstraintSecurityHandler(); securityHandler.addConstraintMapping(constraintMapping); HashLoginService loginService = new HashLoginService(); loginService.putUser("username", new
 * Password("password"), new String[] {"user"}); securityHandler.setLoginService(loginService);
 * 
 * FormAuthenticator authenticator = new FormAuthenticator("/login", "/login", false); securityHandler.setAuthenticator(authenticator);
 * 
 * context.setSecurityHandler(securityHandler);
 */