Roberto Sánchez
2013-12-26 6d04b0ae0f4eeb9f0963b1595d0f2e7469fa5f3f
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
125
126
127
128
129
130
131
132
133
134
135
136
137
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.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.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.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...");
       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);
       sh.setName("resteasy");
       context.addServlet(sh, "/*");
       ResourceHandler staticResources = new ResourceHandler();
       staticResources.setBaseResource(Resource.newResource(MainApp.class.getResource("/static").toURI()));
       staticResources.setWelcomeFiles(new String[]
           { "/login.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, "/");
       context.setWelcomeFiles(new String[]
           { "/index" });
       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();
   }
   /**
    * User: Nuwan.N.Bandara
    */
   @Provider
   public static class DefaultExceptionHandler implements ExceptionMapper<Exception> {
       public DefaultExceptionHandler() {
           log.info("Creating DefaultExceptionHandler ");
       }
       @Override
       public Response toResponse(Exception e) {
           // log.info("Creating DefaultExceptionHandler ");
           e.printStackTrace();
           // 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);
 */