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 { 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.append("ERROR"); response.append("" + e.getMessage() + ""); response.append(""); 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); */