package net.curisit.securis.services; import java.io.IOException; import java.net.URI; import javax.annotation.security.RolesAllowed; import javax.servlet.http.HttpServletRequest; import javax.ws.rs.FormParam; import javax.ws.rs.GET; import javax.ws.rs.POST; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.core.Context; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; import javax.ws.rs.core.Response.Status; import javax.ws.rs.core.UriBuilder; import org.apache.commons.io.IOUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * Basic services for login a nd basic app wrkflow * * @author roberto */ @Path("/") public class BasicServices { // private LicenseHelper licenseHelper = InjectorFactory.getInjector().getInstance(LicenseHelper.class); private static final Logger log = LoggerFactory.getLogger(BasicServices.class); public BasicServices() { } /** * * @return the server version in format majorVersion.minorVersion */ @GET @Produces( { MediaType.TEXT_HTML }) public Response index(@Context HttpServletRequest request) { log.info("index session: " + request.getSession()); URI uri = UriBuilder.fromUri("/login").build(); return Response.seeOther(uri).build(); // return Response.ok().entity("License server").build(); } @GET @Path("/login") @Produces( { MediaType.TEXT_HTML }) public Response login(@Context HttpServletRequest request) { log.info("index login: " + request.getSession()); try { String index = IOUtils.toString(this.getClass().getResourceAsStream("/static/login.html")); return Response.ok().entity(index).build(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return Response.serverError().build(); } @POST @Path("/login") @Produces( { MediaType.TEXT_HTML }) public Response login(@FormParam("user") String user, @FormParam("password") String password, @Context HttpServletRequest request) { log.info("index session: " + request.getSession()); log.info("Request: " + request.getParameter("user")); log.info("is user in role: {} == {} ? ", "advance", request.isUserInRole("advance")); // log.info("user: {} == {} ? " + request.getParameter("user"), user); request.getSession().setAttribute("user", user); URI uri = UriBuilder.fromUri("/main").build(); return Response.seeOther(uri).build(); } /** * @return the version of the three entities that can be synchronized (Users, DataSet and Settings) */ @GET @Path("/main") @Produces( { MediaType.TEXT_HTML }) @RolesAllowed("advance") public Response main(@Context HttpServletRequest request) { try { log.info("Is user in role advance: {}", request.isUserInRole("advance")); String index = IOUtils.toString(this.getClass().getResourceAsStream("/static/main.html")); return Response.ok().entity(index).build(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return Response.status(Status.FORBIDDEN).build(); } @GET @Path("/logout") @Produces( { MediaType.TEXT_HTML }) public Response logout(@Context HttpServletRequest request) { request.getSession().setAttribute("user", null); URI uri = UriBuilder.fromUri("/login").build(); return Response.seeOther(uri).build(); } // // private ServiceResponse buildErrorResponse(ServiceResponse response, String msgErrorCode) { // response.setSuccess(false); // response.setErrorMessage(localManager.getString(msgErrorCode)); // response.setErrorMessageCode(msgErrorCode); // return response; // } // // private Date calculateCaducation() { // Integer licenseExpiration = systemParams.getParamAsInt(SystemParams.Keys.CONFIG_SERVER_LICENSE_EXPIRATION); // if (licenseExpiration == null) // licenseExpiration = DEFAULT_LICENSE_EXPIRATION; // return Utils.addDays(new Date(), licenseExpiration); // } // // private boolean validateLicense(String license) { // BasicApplication ba = basicApplicationDao.findByLicense(license); // return (ba != null); // } // // private boolean validateVersion(int minorVersion, int majorVersion) { // return (versionManager.getMajorVersion() == majorVersion); // } // // private BasicApplication findBasicApp(String license) { // BasicApplication ba = basicApplicationDao.findByLicense(license); // return ba; // } // // private License generateLicense() { // // TODO complete all field of the license // License license = new License(); // license.setCustomerCode(systemParams.getParam(SystemParams.Keys.CONFIG_COMMON_CUSTOMER_CODE)); // license.setCSCode(systemParams.getParam(SystemParams.Keys.CONFIG_COMMON_CS_CODE)); // license.setCRCLogo("00000000"); // license.setExpirationDate(calculateCaducation()); // license.setInstallCode(codeGenerator.generateInstalationNumber()); // return license; // } }