| .. | .. |
|---|
| 1 | 1 | package net.curisit.securis.services; |
|---|
| 2 | 2 | |
|---|
| 3 | | -import java.io.IOException; |
|---|
| 4 | 3 | import java.net.URI; |
|---|
| 4 | +import java.util.Date; |
|---|
| 5 | 5 | |
|---|
| 6 | | -import javax.annotation.security.RolesAllowed; |
|---|
| 7 | 6 | import javax.servlet.http.HttpServletRequest; |
|---|
| 8 | | -import javax.ws.rs.FormParam; |
|---|
| 7 | +import javax.servlet.http.HttpSession; |
|---|
| 9 | 8 | import javax.ws.rs.GET; |
|---|
| 10 | | -import javax.ws.rs.POST; |
|---|
| 11 | 9 | import javax.ws.rs.Path; |
|---|
| 12 | 10 | import javax.ws.rs.Produces; |
|---|
| 13 | 11 | import javax.ws.rs.core.Context; |
|---|
| 14 | 12 | import javax.ws.rs.core.MediaType; |
|---|
| 15 | 13 | import javax.ws.rs.core.Response; |
|---|
| 16 | | -import javax.ws.rs.core.Response.Status; |
|---|
| 17 | 14 | import javax.ws.rs.core.UriBuilder; |
|---|
| 18 | 15 | |
|---|
| 19 | | -import org.apache.commons.io.IOUtils; |
|---|
| 20 | 16 | import org.slf4j.Logger; |
|---|
| 21 | 17 | import org.slf4j.LoggerFactory; |
|---|
| 22 | 18 | |
|---|
| 23 | 19 | /** |
|---|
| 24 | | - * Basic services for login a nd basic app wrkflow |
|---|
| 20 | + * Basic services for login and basic app wrkflow |
|---|
| 25 | 21 | * |
|---|
| 26 | 22 | * @author roberto <roberto.sanchez@curisit.net> |
|---|
| 27 | 23 | */ |
|---|
| 28 | 24 | @Path("/") |
|---|
| 29 | 25 | public class BasicServices { |
|---|
| 30 | 26 | |
|---|
| 31 | | - // private LicenseHelper licenseHelper = InjectorFactory.getInjector().getInstance(LicenseHelper.class); |
|---|
| 32 | 27 | private static final Logger log = LoggerFactory.getLogger(BasicServices.class); |
|---|
| 33 | 28 | |
|---|
| 34 | 29 | public BasicServices() { |
|---|
| .. | .. |
|---|
| 39 | 34 | * @return the server version in format majorVersion.minorVersion |
|---|
| 40 | 35 | */ |
|---|
| 41 | 36 | @GET |
|---|
| 37 | + @Path("/index") |
|---|
| 42 | 38 | @Produces( |
|---|
| 43 | 39 | { MediaType.TEXT_HTML }) |
|---|
| 44 | 40 | public Response index(@Context HttpServletRequest request) { |
|---|
| 45 | 41 | log.info("index session: " + request.getSession()); |
|---|
| 46 | | - URI uri = UriBuilder.fromUri("/login").build(); |
|---|
| 42 | + HttpSession session = request.getSession(false); |
|---|
| 43 | + String page = session != null && session.getAttribute("user") != null ? "/main.html" : "/login.html"; |
|---|
| 44 | + URI uri = UriBuilder.fromUri(page).build(); |
|---|
| 47 | 45 | return Response.seeOther(uri).build(); |
|---|
| 48 | 46 | // return Response.ok().entity("License server").build(); |
|---|
| 49 | 47 | } |
|---|
| 50 | 48 | |
|---|
| 51 | 49 | @GET |
|---|
| 52 | | - @Path("/login") |
|---|
| 50 | + @Path("/info") |
|---|
| 53 | 51 | @Produces( |
|---|
| 54 | | - { MediaType.TEXT_HTML }) |
|---|
| 55 | | - public Response login(@Context HttpServletRequest request) { |
|---|
| 56 | | - log.info("index login: " + request.getSession()); |
|---|
| 57 | | - try { |
|---|
| 58 | | - String index = IOUtils.toString(this.getClass().getResourceAsStream("/static/login.html")); |
|---|
| 59 | | - return Response.ok().entity(index).build(); |
|---|
| 60 | | - } catch (IOException e) { |
|---|
| 61 | | - // TODO Auto-generated catch block |
|---|
| 62 | | - e.printStackTrace(); |
|---|
| 63 | | - } |
|---|
| 64 | | - return Response.serverError().build(); |
|---|
| 52 | + { MediaType.TEXT_PLAIN }) |
|---|
| 53 | + public Response info(@Context HttpServletRequest request) { |
|---|
| 54 | + return Response.ok().entity("License server running OK. Date: " + new Date()).build(); |
|---|
| 65 | 55 | } |
|---|
| 66 | | - |
|---|
| 67 | | - @POST |
|---|
| 68 | | - @Path("/login") |
|---|
| 69 | | - @Produces( |
|---|
| 70 | | - { MediaType.TEXT_HTML }) |
|---|
| 71 | | - public Response login(@FormParam("user") String user, @FormParam("password") String password, @Context HttpServletRequest request) { |
|---|
| 72 | | - log.info("index session: " + request.getSession()); |
|---|
| 73 | | - log.info("Request: " + request.getParameter("user")); |
|---|
| 74 | | - log.info("is user in role: {} == {} ? ", "advance", request.isUserInRole("advance")); |
|---|
| 75 | | - // log.info("user: {} == {} ? " + request.getParameter("user"), user); |
|---|
| 76 | | - request.getSession().setAttribute("user", user); |
|---|
| 77 | | - URI uri = UriBuilder.fromUri("/main").build(); |
|---|
| 78 | | - return Response.seeOther(uri).build(); |
|---|
| 79 | | - } |
|---|
| 80 | | - |
|---|
| 81 | | - /** |
|---|
| 82 | | - * @return the version of the three entities that can be synchronized (Users, DataSet and Settings) |
|---|
| 83 | | - */ |
|---|
| 84 | | - @GET |
|---|
| 85 | | - @Path("/main") |
|---|
| 86 | | - @Produces( |
|---|
| 87 | | - { MediaType.TEXT_HTML }) |
|---|
| 88 | | - @RolesAllowed("advance") |
|---|
| 89 | | - public Response main(@Context HttpServletRequest request) { |
|---|
| 90 | | - try { |
|---|
| 91 | | - log.info("Is user in role advance: {}", request.isUserInRole("advance")); |
|---|
| 92 | | - String index = IOUtils.toString(this.getClass().getResourceAsStream("/static/main.html")); |
|---|
| 93 | | - return Response.ok().entity(index).build(); |
|---|
| 94 | | - } catch (IOException e) { |
|---|
| 95 | | - // TODO Auto-generated catch block |
|---|
| 96 | | - e.printStackTrace(); |
|---|
| 97 | | - } |
|---|
| 98 | | - return Response.status(Status.FORBIDDEN).build(); |
|---|
| 99 | | - |
|---|
| 100 | | - } |
|---|
| 101 | | - |
|---|
| 102 | | - @GET |
|---|
| 103 | | - @Path("/logout") |
|---|
| 104 | | - @Produces( |
|---|
| 105 | | - { MediaType.TEXT_HTML }) |
|---|
| 106 | | - public Response logout(@Context HttpServletRequest request) { |
|---|
| 107 | | - request.getSession().setAttribute("user", null); |
|---|
| 108 | | - URI uri = UriBuilder.fromUri("/login").build(); |
|---|
| 109 | | - return Response.seeOther(uri).build(); |
|---|
| 110 | | - |
|---|
| 111 | | - } |
|---|
| 112 | | - |
|---|
| 113 | | - // |
|---|
| 114 | | - // private <T> ServiceResponse<T> buildErrorResponse(ServiceResponse<T> response, String msgErrorCode) { |
|---|
| 115 | | - // response.setSuccess(false); |
|---|
| 116 | | - // response.setErrorMessage(localManager.getString(msgErrorCode)); |
|---|
| 117 | | - // response.setErrorMessageCode(msgErrorCode); |
|---|
| 118 | | - // return response; |
|---|
| 119 | | - // } |
|---|
| 120 | | - // |
|---|
| 121 | | - // private Date calculateCaducation() { |
|---|
| 122 | | - // Integer licenseExpiration = systemParams.getParamAsInt(SystemParams.Keys.CONFIG_SERVER_LICENSE_EXPIRATION); |
|---|
| 123 | | - // if (licenseExpiration == null) |
|---|
| 124 | | - // licenseExpiration = DEFAULT_LICENSE_EXPIRATION; |
|---|
| 125 | | - // return Utils.addDays(new Date(), licenseExpiration); |
|---|
| 126 | | - // } |
|---|
| 127 | | - // |
|---|
| 128 | | - // private boolean validateLicense(String license) { |
|---|
| 129 | | - // BasicApplication ba = basicApplicationDao.findByLicense(license); |
|---|
| 130 | | - // return (ba != null); |
|---|
| 131 | | - // } |
|---|
| 132 | | - // |
|---|
| 133 | | - // private boolean validateVersion(int minorVersion, int majorVersion) { |
|---|
| 134 | | - // return (versionManager.getMajorVersion() == majorVersion); |
|---|
| 135 | | - // } |
|---|
| 136 | | - // |
|---|
| 137 | | - // private BasicApplication findBasicApp(String license) { |
|---|
| 138 | | - // BasicApplication ba = basicApplicationDao.findByLicense(license); |
|---|
| 139 | | - // return ba; |
|---|
| 140 | | - // } |
|---|
| 141 | | - // |
|---|
| 142 | | - // private License generateLicense() { |
|---|
| 143 | | - // // TODO complete all field of the license |
|---|
| 144 | | - // License license = new License(); |
|---|
| 145 | | - // license.setCustomerCode(systemParams.getParam(SystemParams.Keys.CONFIG_COMMON_CUSTOMER_CODE)); |
|---|
| 146 | | - // license.setCSCode(systemParams.getParam(SystemParams.Keys.CONFIG_COMMON_CS_CODE)); |
|---|
| 147 | | - // license.setCRCLogo("00000000"); |
|---|
| 148 | | - // license.setExpirationDate(calculateCaducation()); |
|---|
| 149 | | - // license.setInstallCode(codeGenerator.generateInstalationNumber()); |
|---|
| 150 | | - // return license; |
|---|
| 151 | | - // } |
|---|
| 152 | 56 | |
|---|
| 153 | 57 | } |
|---|