| .. | .. |
|---|
| 4 | 4 |
|
|---|
| 5 | 5 | import javax.inject.Inject;
|
|---|
| 6 | 6 | import javax.inject.Named;
|
|---|
| 7 | +import javax.ws.rs.core.MediaType;
|
|---|
| 8 | +import javax.ws.rs.core.Response;
|
|---|
| 9 | +import javax.ws.rs.ext.ExceptionMapper;
|
|---|
| 10 | +import javax.ws.rs.ext.Provider;
|
|---|
| 7 | 11 |
|
|---|
| 8 | 12 | import net.curisit.securis.ioc.RequestsModule;
|
|---|
| 9 | 13 | import net.curisit.securis.ioc.SecurisModule;
|
|---|
| 10 | 14 |
|
|---|
| 11 | 15 | import org.eclipse.jetty.server.Server;
|
|---|
| 16 | +import org.eclipse.jetty.servlet.ErrorPageErrorHandler;
|
|---|
| 12 | 17 | import org.eclipse.jetty.servlet.FilterHolder;
|
|---|
| 13 | 18 | import org.eclipse.jetty.servlet.ServletContextHandler;
|
|---|
| 14 | 19 | import org.eclipse.jetty.servlet.ServletHolder;
|
|---|
| 15 | 20 | import org.jboss.resteasy.plugins.guice.GuiceResteasyBootstrapServletContextListener;
|
|---|
| 16 | 21 | import org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher;
|
|---|
| 22 | +import org.jboss.resteasy.util.HttpResponseCodes;
|
|---|
| 17 | 23 | import org.slf4j.Logger;
|
|---|
| 18 | 24 | import org.slf4j.LoggerFactory;
|
|---|
| 19 | 25 |
|
|---|
| .. | .. |
|---|
| 53 | 59 | context.setContextPath("/");
|
|---|
| 54 | 60 | context.addEventListener(injector.getInstance(GuiceResteasyBootstrapServletContextListener.class));
|
|---|
| 55 | 61 |
|
|---|
| 56 | | - ServletHolder sh = new ServletHolder(HttpServletDispatcher.class);
|
|---|
| 57 | 62 | context.setInitParameter("resteasy.role.based.security", "true");
|
|---|
| 63 | + context.setInitParameter("resteasy.providers", DefaultExceptionHandler.class.getName());
|
|---|
| 58 | 64 | context.addFilter(new FilterHolder(injector.getInstance(AuthFilter.class)), "/*", null);
|
|---|
| 59 | | - // context.addServlet(DefaultServlet.class, "/*");
|
|---|
| 60 | | - context.addServlet(sh, "/*");
|
|---|
| 61 | 65 |
|
|---|
| 66 | + ServletHolder sh = new ServletHolder(HttpServletDispatcher.class);
|
|---|
| 67 | + // context.addServlet(DefaultServlet.class, "/*");
|
|---|
| 68 | + context.addServlet(sh, "/");
|
|---|
| 69 | + ErrorPageErrorHandler errorHandler = new ErrorPageErrorHandler();
|
|---|
| 70 | + context.setErrorHandler(errorHandler);
|
|---|
| 71 | + errorHandler.addErrorPage(HttpResponseCodes.SC_FORBIDDEN, "/login");
|
|---|
| 72 | + errorHandler.addErrorPage(HttpResponseCodes.SC_NOT_FOUND, "/");
|
|---|
| 73 | + errorHandler.addErrorPage(javax.ws.rs.NotFoundException.class, "/");
|
|---|
| 74 | + errorHandler.addErrorPage(javax.ws.rs.ForbiddenException.class, "/");
|
|---|
| 75 | + errorHandler.addErrorPage(javax.ws.rs.ForbiddenException.class.getCanonicalName(), "/");
|
|---|
| 76 | + errorHandler.addErrorPage(ErrorPageErrorHandler.GLOBAL_ERROR_PAGE, "/");
|
|---|
| 77 | +
|
|---|
| 78 | + log.info("Error Handlers: " + context.getErrorHandler());
|
|---|
| 62 | 79 | server.setHandler(context);
|
|---|
| 80 | +
|
|---|
| 63 | 81 | server.start();
|
|---|
| 64 | 82 | server.join();
|
|---|
| 65 | 83 | // rc.packages("net.curisit.securis.services", "org.codehaus.jackson.jaxrs");
|
|---|
| .. | .. |
|---|
| 68 | 86 |
|
|---|
| 69 | 87 | }
|
|---|
| 70 | 88 |
|
|---|
| 89 | + /**
|
|---|
| 90 | + * User: Nuwan.N.Bandara
|
|---|
| 91 | + */
|
|---|
| 92 | + @Provider
|
|---|
| 93 | + public static class DefaultExceptionHandler implements ExceptionMapper<Exception> {
|
|---|
| 94 | +
|
|---|
| 95 | + public DefaultExceptionHandler() {
|
|---|
| 96 | + log.info("Creating DefaultExceptionHandler ");
|
|---|
| 97 | + }
|
|---|
| 98 | +
|
|---|
| 99 | + @Override
|
|---|
| 100 | + public Response toResponse(Exception e) {
|
|---|
| 101 | + // For simplicity I am preparing error xml by hand.
|
|---|
| 102 | + // Ideally we should create an ErrorResponse class to hold the error info.
|
|---|
| 103 | + StringBuilder response = new StringBuilder("<response>");
|
|---|
| 104 | + response.append("<status>ERROR</status>");
|
|---|
| 105 | + response.append("<message>" + e.getMessage() + "</message>");
|
|---|
| 106 | + response.append("</response>");
|
|---|
| 107 | + return Response.serverError().entity(response.toString()).type(MediaType.APPLICATION_XML).build();
|
|---|
| 108 | + }
|
|---|
| 109 | + }
|
|---|
| 110 | +
|
|---|
| 71 | 111 | }
|
|---|
| 112 | +
|
|---|
| 113 | +/*
|
|---|
| 114 | + * Constraint constraint = new Constraint(); constraint.setName(Constraint.__FORM_AUTH);; constraint.setRoles(new String[]{"user","admin","moderator"}); constraint.setAuthenticate(true);
|
|---|
| 115 | + *
|
|---|
| 116 | + * ConstraintMapping constraintMapping = new ConstraintMapping(); constraintMapping.setConstraint(constraint); constraintMapping.setPathSpec("/*");
|
|---|
| 117 | + *
|
|---|
| 118 | + * ConstraintSecurityHandler securityHandler = new ConstraintSecurityHandler(); securityHandler.addConstraintMapping(constraintMapping); HashLoginService loginService = new HashLoginService(); loginService.putUser("username", new
|
|---|
| 119 | + * Password("password"), new String[] {"user"}); securityHandler.setLoginService(loginService);
|
|---|
| 120 | + *
|
|---|
| 121 | + * FormAuthenticator authenticator = new FormAuthenticator("/login", "/login", false); securityHandler.setAuthenticator(authenticator);
|
|---|
| 122 | + *
|
|---|
| 123 | + * context.setSecurityHandler(securityHandler);
|
|---|
| 124 | + */ |
|---|