#396 fix - Fixed some SonarQube issues
| .. | .. |
|---|
| 23 | 23 | @WebFilter(urlPatterns = "/*") |
|---|
| 24 | 24 | public class AuthFilter implements Filter { |
|---|
| 25 | 25 | |
|---|
| 26 | | - private static final Logger log = LogManager.getLogger(AuthFilter.class); |
|---|
| 26 | + private static final Logger LOG = LogManager.getLogger(AuthFilter.class); |
|---|
| 27 | 27 | |
|---|
| 28 | 28 | @Override |
|---|
| 29 | 29 | public void init(FilterConfig fc) throws ServletException { |
|---|
| .. | .. |
|---|
| 40 | 40 | username = (String) req.getSession().getAttribute("user"); |
|---|
| 41 | 41 | String role = username.equals("advance") ? "advance" : "normal"; |
|---|
| 42 | 42 | // ResteasyProviderFactory.pushContext(User.class, new User(sr.getParameter("user"))); |
|---|
| 43 | | - log.info("Role for user: {} = {}", username, role); |
|---|
| 43 | + LOG.info("Role for user: {} = {}", username, role); |
|---|
| 44 | 44 | fc.doFilter(new UserRoleRequestWrapper(role, sr.getParameter("user"), req), sr1); |
|---|
| 45 | 45 | } else { |
|---|
| 46 | 46 | fc.doFilter(req, sr1); |
|---|
| .. | .. |
|---|
| 65 | 65 | |
|---|
| 66 | 66 | @Override |
|---|
| 67 | 67 | public boolean isUserInRole(String role) { |
|---|
| 68 | | - log.info("isUserRole METHOD: {}, {}", role, this.role); |
|---|
| 68 | + LOG.info("isUserRole METHOD: {}, {}", role, this.role); |
|---|
| 69 | 69 | if (this.role == null) { |
|---|
| 70 | 70 | return super.isUserInRole(role); |
|---|
| 71 | 71 | } |
|---|
| .. | .. |
|---|
| 17 | 17 | |
|---|
| 18 | 18 | @Provider |
|---|
| 19 | 19 | public class DefaultExceptionHandler implements ExceptionMapper<Exception> { |
|---|
| 20 | | - private static final Logger log = LogManager.getLogger(DefaultExceptionHandler.class); |
|---|
| 20 | + private static final Logger LOG = LogManager.getLogger(DefaultExceptionHandler.class); |
|---|
| 21 | 21 | |
|---|
| 22 | 22 | public static final String ERROR_MESSAGE_HEADER = "X-SECURIS-ERROR"; |
|---|
| 23 | 23 | |
|---|
| 24 | 24 | public DefaultExceptionHandler() { |
|---|
| 25 | | - log.info("Creating DefaultExceptionHandler "); |
|---|
| 25 | + LOG.info("Creating DefaultExceptionHandler "); |
|---|
| 26 | 26 | } |
|---|
| 27 | 27 | |
|---|
| 28 | 28 | @Context |
|---|
| .. | .. |
|---|
| 32 | 32 | |
|---|
| 33 | 33 | @Override |
|---|
| 34 | 34 | public Response toResponse(Exception e) { |
|---|
| 35 | | - // log.info("Creating DefaultExceptionHandler "); |
|---|
| 36 | | - // e.printStackTrace(); |
|---|
| 37 | 35 | if (e instanceof ForbiddenException) { |
|---|
| 38 | | - log.warn("Unauthorized access to {}, user: {}", request.getPathInfo(), bsc.getUserPrincipal()); |
|---|
| 36 | + LOG.warn("Unauthorized access to {}, user: {}", request.getPathInfo(), bsc.getUserPrincipal()); |
|---|
| 39 | 37 | return Response.status(Status.UNAUTHORIZED).header(ERROR_MESSAGE_HEADER, "Unathorized access to the application").type(MediaType.APPLICATION_JSON).build(); |
|---|
| 40 | 38 | } |
|---|
| 41 | 39 | |
|---|
| 42 | 40 | if (e instanceof SeCurisServiceException) { |
|---|
| 43 | | - log.warn("SeCurisServiceException accessing to {}, user: {}", request.getPathInfo(), bsc.getUserPrincipal()); |
|---|
| 41 | + LOG.warn("SeCurisServiceException accessing to {}, user: {}", request.getPathInfo(), bsc.getUserPrincipal()); |
|---|
| 44 | 42 | return Response.status(Status.fromStatusCode(((SeCurisServiceException) e).getStatus())).header(ERROR_MESSAGE_HEADER, e.getMessage()).type(MediaType.APPLICATION_JSON).build(); |
|---|
| 45 | 43 | } |
|---|
| 46 | 44 | |
|---|
| 47 | | - log.error("Unexpected error accesing to '{}' by user: {}", request.getPathInfo(), bsc.getUserPrincipal()); |
|---|
| 48 | | - log.error("Request sent from {}, with User-Agent: {}", request.getRemoteHost(), request.getHeader("User-Agent")); |
|---|
| 49 | | - log.error("Request url: " + request.getRequestURL(), e); |
|---|
| 45 | + LOG.error("Unexpected error accesing to '{}' by user: {}", request.getPathInfo(), bsc.getUserPrincipal()); |
|---|
| 46 | + LOG.error("Request sent from {}, with User-Agent: {}", request.getRemoteHost(), request.getHeader("User-Agent")); |
|---|
| 47 | + LOG.error("Request url: " + request.getRequestURL(), e); |
|---|
| 50 | 48 | return Response.serverError().header(ERROR_MESSAGE_HEADER, "Unexpected error: " + e.toString()).type(MediaType.APPLICATION_JSON).build(); |
|---|
| 51 | 49 | } |
|---|
| 52 | 50 | } |
|---|
| .. | .. |
|---|
| 32 | 32 |
|
|---|
| 33 | 33 | public class MainApp {
|
|---|
| 34 | 34 |
|
|---|
| 35 | | - static final Logger log = LogManager.getLogger(MainApp.class);
|
|---|
| 35 | + private static final Logger LOG = LogManager.getLogger(MainApp.class);
|
|---|
| 36 | 36 |
|
|---|
| 37 | 37 | private static Server server;
|
|---|
| 38 | 38 | private static Injector injector = null;
|
|---|
| .. | .. |
|---|
| 42 | 42 | private URI uri;
|
|---|
| 43 | 43 |
|
|---|
| 44 | 44 | public static void main(String[] args) throws Exception {
|
|---|
| 45 | | - log.info("SeCuris init...");
|
|---|
| 45 | + LOG.info("SeCuris init...");
|
|---|
| 46 | 46 |
|
|---|
| 47 | 47 | SecurisModule securisModule = new SecurisModule();
|
|---|
| 48 | 48 | JpaPersistModule jpaPersistModule = new JpaPersistModule("localdb");
|
|---|
| 49 | 49 | Properties props = new Properties();
|
|---|
| 50 | 50 | props.put("javax.persistence.jdbc.password", securisModule.getPassword());
|
|---|
| 51 | 51 | props.put("javax.persistence.jdbc.url", securisModule.getUrl(securisModule.getAppDir()));
|
|---|
| 52 | | - log.info("BD Url: {} {}", securisModule.getUrl(securisModule.getAppDir()), securisModule.getPassword());
|
|---|
| 52 | + LOG.info("BD Url: {} {}", securisModule.getUrl(securisModule.getAppDir()), securisModule.getPassword());
|
|---|
| 53 | 53 | jpaPersistModule.properties(props);
|
|---|
| 54 | 54 |
|
|---|
| 55 | 55 | injector = Guice.createInjector(securisModule, new RequestsModule(), jpaPersistModule);
|
|---|
| .. | .. |
|---|
| 91 | 91 | // errorHandler.addErrorPage(javax.ws.rs.ForbiddenException.class, "/");
|
|---|
| 92 | 92 | // errorHandler.addErrorPage(javax.ws.rs.ForbiddenException.class.getCanonicalName(), "/");
|
|---|
| 93 | 93 | // errorHandler.addErrorPage(ErrorPageErrorHandler.GLOBAL_ERROR_PAGE, "/");
|
|---|
| 94 | | - log.info("Error Handlers: " + context.getErrorHandler());
|
|---|
| 94 | + LOG.info("Error Handlers: " + context.getErrorHandler());
|
|---|
| 95 | 95 | ContextHandlerCollection contexts = new ContextHandlerCollection();
|
|---|
| 96 | 96 |
|
|---|
| 97 | 97 | contexts.setHandlers(new Handler[]
|
|---|
| .. | .. |
|---|
| 101 | 101 | server.setHandler(context);
|
|---|
| 102 | 102 | server.start();
|
|---|
| 103 | 103 | server.join();
|
|---|
| 104 | | - log.info("Started server in: http://127.0.0.1:9997/");
|
|---|
| 104 | + LOG.info("Started server in: http://127.0.0.1:9997/");
|
|---|
| 105 | 105 | }
|
|---|
| 106 | 106 |
|
|---|
| 107 | 107 | }
|
|---|
| .. | .. |
|---|
| 13 | 13 |
|
|---|
| 14 | 14 | public class RestServicesApplication extends Application {
|
|---|
| 15 | 15 |
|
|---|
| 16 | | - private static final Logger log = LogManager.getLogger(RestServicesApplication.class);
|
|---|
| 16 | + private static final Logger LOG = LogManager.getLogger(RestServicesApplication.class);
|
|---|
| 17 | 17 |
|
|---|
| 18 | 18 | @Override
|
|---|
| 19 | 19 | public Set<Class<?>> getClasses() {
|
|---|
| .. | .. |
|---|
| 21 | 21 | classes.add(LicenseServices.class);
|
|---|
| 22 | 22 | classes.add(BasicServices.class);
|
|---|
| 23 | 23 |
|
|---|
| 24 | | - log.info("Returnes classes for services: {}", classes);
|
|---|
| 24 | + LOG.info("Returnes classes for services: {}", classes);
|
|---|
| 25 | 25 | return classes;
|
|---|
| 26 | 26 | }
|
|---|
| 27 | 27 |
|
|---|
| .. | .. |
|---|
| 34 | 34 | { @NamedQuery(name = "list-license_types", query = "SELECT lt FROM LicenseType lt") }) |
|---|
| 35 | 35 | public class LicenseType implements Serializable { |
|---|
| 36 | 36 | |
|---|
| 37 | | - private static final Logger log = LogManager.getLogger(LicenseType.class); |
|---|
| 37 | + private static final Logger LOG = LogManager.getLogger(LicenseType.class); |
|---|
| 38 | 38 | private static final long serialVersionUID = 1L; |
|---|
| 39 | 39 | |
|---|
| 40 | 40 | @Id |
|---|
| .. | .. |
|---|
| 96 | 96 | |
|---|
| 97 | 97 | @JsonProperty("application_id") |
|---|
| 98 | 98 | public Integer getApplicationId() { |
|---|
| 99 | | - log.info("application " + application); |
|---|
| 99 | + LOG.info("application " + application); |
|---|
| 100 | 100 | return application == null ? null : application.getId(); |
|---|
| 101 | 101 | } |
|---|
| 102 | 102 | |
|---|
| 103 | 103 | @JsonProperty("application_id") |
|---|
| 104 | 104 | public void setApplicationId(Integer appId) { |
|---|
| 105 | | - log.info("setApplicationId(Integer appId) " + appId); |
|---|
| 105 | + LOG.info("setApplicationId(Integer appId) " + appId); |
|---|
| 106 | 106 | application = new Application(); |
|---|
| 107 | 107 | application.setId(appId); |
|---|
| 108 | 108 | } |
|---|
| .. | .. |
|---|
| 42 | 42 | public class Organization implements Serializable { |
|---|
| 43 | 43 | |
|---|
| 44 | 44 | @SuppressWarnings("unused") |
|---|
| 45 | | - private static final Logger log = LogManager.getLogger(Organization.class); |
|---|
| 45 | + private static final Logger LOG = LogManager.getLogger(Organization.class); |
|---|
| 46 | 46 | |
|---|
| 47 | 47 | private static final long serialVersionUID = 1L; |
|---|
| 48 | 48 | |
|---|
| .. | .. |
|---|
| 25 | 25 | private static final int DEFAULT_PORT = 9997; |
|---|
| 26 | 26 | private static final String PROPERTIES_FILE_NAME = "/server.properties"; |
|---|
| 27 | 27 | |
|---|
| 28 | | - private static final Logger log = LogManager.getLogger(SecurisModule.class); |
|---|
| 28 | + private static final Logger LOG = LogManager.getLogger(SecurisModule.class); |
|---|
| 29 | 29 | |
|---|
| 30 | 30 | @Override |
|---|
| 31 | 31 | protected void configure() { |
|---|
| .. | .. |
|---|
| 52 | 52 | try { |
|---|
| 53 | 53 | // String url = MessageFormat.format("http://{0}/", InetAddress.getLocalHost().getHostAddress()); |
|---|
| 54 | 54 | String url = MessageFormat.format("http://{0}/", "0.0.0.0"); |
|---|
| 55 | | - log.debug("Server url{}", url); |
|---|
| 55 | + LOG.debug("Server url{}", url); |
|---|
| 56 | 56 | return UriBuilder.fromUri(url).port(getPort()).build(); |
|---|
| 57 | 57 | } catch (IllegalArgumentException | UriBuilderException e) { |
|---|
| 58 | 58 | return UriBuilder.fromUri("http://localhost/").port(getPort()).build(); |
|---|
| .. | .. |
|---|
| 89 | 89 | if (!ftmp.exists()) { |
|---|
| 90 | 90 | if (!ftmp.mkdirs()) |
|---|
| 91 | 91 | return null; |
|---|
| 92 | | - log.debug("Created temporary directory for app in: {}", ftmp.getAbsolutePath()); |
|---|
| 92 | + LOG.debug("Created temporary directory for app in: {}", ftmp.getAbsolutePath()); |
|---|
| 93 | 93 | ftmp.deleteOnExit(); |
|---|
| 94 | 94 | } |
|---|
| 95 | 95 | return ftmp; |
|---|
| .. | .. |
|---|
| 108 | 108 | if (!fAppDir.exists()) { |
|---|
| 109 | 109 | if (!fAppDir.mkdirs()) |
|---|
| 110 | 110 | return null; |
|---|
| 111 | | - log.debug("Created app working directory app in: {}", fAppDir.getAbsolutePath()); |
|---|
| 111 | + LOG.debug("Created app working directory app in: {}", fAppDir.getAbsolutePath()); |
|---|
| 112 | 112 | } |
|---|
| 113 | 113 | return fAppDir; |
|---|
| 114 | 114 | } |
|---|
| .. | .. |
|---|
| 149 | 149 | dataSource.setURL(getUrl(appDir)); |
|---|
| 150 | 150 | dataSource.setUser("curis"); |
|---|
| 151 | 151 | dataSource.setPassword(getPassword()); |
|---|
| 152 | | - log.debug("JdbcDataSource: {}", dataSource); |
|---|
| 152 | + LOG.debug("JdbcDataSource: {}", dataSource); |
|---|
| 153 | 153 | return dataSource; |
|---|
| 154 | 154 | } |
|---|
| 155 | 155 | |
|---|
| .. | .. |
|---|
| 34 | 34 | // @PreMatching |
|---|
| 35 | 35 | @Priority(Priorities.AUTHENTICATION) |
|---|
| 36 | 36 | public class SecurityInterceptor implements javax.ws.rs.container.ContainerRequestFilter { |
|---|
| 37 | | - private static final Logger log = LogManager.getLogger(SecurityInterceptor.class); |
|---|
| 37 | + private static final Logger LOG = LogManager.getLogger(SecurityInterceptor.class); |
|---|
| 38 | 38 | |
|---|
| 39 | 39 | @Inject |
|---|
| 40 | 40 | private TokenHelper tokenHelper; |
|---|
| .. | .. |
|---|
| 59 | 59 | return; |
|---|
| 60 | 60 | String token = servletRequest.getHeader(TokenHelper.TOKEN_HEADER_PÀRAM); |
|---|
| 61 | 61 | if (token == null || !tokenHelper.isTokenValid(token)) { |
|---|
| 62 | | - log.info("Access denied to '{}', Token not valid.", servletRequest.getPathInfo()); |
|---|
| 62 | + LOG.info("Access denied to '{}', Token not valid.", servletRequest.getPathInfo()); |
|---|
| 63 | 63 | containerRequestContext.abortWith(Response.status(Status.UNAUTHORIZED).build()); |
|---|
| 64 | 64 | } else { |
|---|
| 65 | 65 | Securable sec = method.getAnnotation(Securable.class); |
|---|
| .. | .. |
|---|
| 69 | 69 | int userRoles = getUserRoles(username); |
|---|
| 70 | 70 | // if (sec.roles() != 0) { |
|---|
| 71 | 71 | // if ((sec.roles() & userRoles) == 0) { |
|---|
| 72 | | - // log.info("User {} has no necessary role to access url: {}", username, servletRequest.getPathInfo()); |
|---|
| 72 | + // LOG.info("User {} has no necessary role to access url: {}", username, servletRequest.getPathInfo()); |
|---|
| 73 | 73 | // containerRequestContext.abortWith(Response.status(Status.UNAUTHORIZED).build()); |
|---|
| 74 | 74 | // } |
|---|
| 75 | 75 | // } |
|---|
| .. | .. |
|---|
| 80 | 80 | containerRequestContext.setSecurityContext(scw); |
|---|
| 81 | 81 | // Next line provide injection in resource methods |
|---|
| 82 | 82 | ResteasyProviderFactory.pushContext(BasicSecurityContext.class, scw); |
|---|
| 83 | | - log.debug("Added custom SecurityContext for user {}, orgs: {}", username, orgs); |
|---|
| 83 | + LOG.debug("Added custom SecurityContext for user {}, orgs: {}", username, orgs); |
|---|
| 84 | 84 | } |
|---|
| 85 | 85 | } |
|---|
| 86 | 86 | |
|---|
| .. | .. |
|---|
| 23 | 23 | public class ApiResource { |
|---|
| 24 | 24 | |
|---|
| 25 | 25 | @SuppressWarnings("unused") |
|---|
| 26 | | - private static final Logger log = LogManager.getLogger(ApiResource.class); |
|---|
| 26 | + private static final Logger LOG = LogManager.getLogger(ApiResource.class); |
|---|
| 27 | 27 | |
|---|
| 28 | 28 | @Inject |
|---|
| 29 | 29 | TokenHelper tokenHelper; |
|---|
| .. | .. |
|---|
| 46 | 46 | @Inject |
|---|
| 47 | 47 | Provider<EntityManager> emProvider; |
|---|
| 48 | 48 | |
|---|
| 49 | | - private static final Logger log = LogManager.getLogger(ApplicationResource.class); |
|---|
| 49 | + private static final Logger LOG = LogManager.getLogger(ApplicationResource.class); |
|---|
| 50 | 50 | |
|---|
| 51 | 51 | public ApplicationResource() { |
|---|
| 52 | 52 | } |
|---|
| .. | .. |
|---|
| 60 | 60 | @Produces( |
|---|
| 61 | 61 | { MediaType.APPLICATION_JSON }) |
|---|
| 62 | 62 | public Response index() { |
|---|
| 63 | | - log.info("Getting applications list "); |
|---|
| 63 | + LOG.info("Getting applications list "); |
|---|
| 64 | 64 | |
|---|
| 65 | 65 | EntityManager em = emProvider.get(); |
|---|
| 66 | 66 | TypedQuery<Application> q = em.createNamedQuery("list-applications", Application.class); |
|---|
| .. | .. |
|---|
| 78 | 78 | @Produces( |
|---|
| 79 | 79 | { MediaType.APPLICATION_JSON }) |
|---|
| 80 | 80 | public Response get(@PathParam("appid") String appid, @HeaderParam(TokenHelper.TOKEN_HEADER_PÀRAM) String token) { |
|---|
| 81 | | - log.info("Getting application data for id: {}: ", appid); |
|---|
| 81 | + LOG.info("Getting application data for id: {}: ", appid); |
|---|
| 82 | 82 | if (appid == null || appid.equals("")) { |
|---|
| 83 | | - log.error("Application ID is mandatory"); |
|---|
| 83 | + LOG.error("Application ID is mandatory"); |
|---|
| 84 | 84 | return Response.status(Status.NOT_FOUND).build(); |
|---|
| 85 | 85 | } |
|---|
| 86 | 86 | |
|---|
| 87 | 87 | EntityManager em = emProvider.get(); |
|---|
| 88 | 88 | Application app = em.find(Application.class, Integer.parseInt(appid)); |
|---|
| 89 | 89 | if (app == null) { |
|---|
| 90 | | - log.error("Application with id {} not found in DB", appid); |
|---|
| 90 | + LOG.error("Application with id {} not found in DB", appid); |
|---|
| 91 | 91 | |
|---|
| 92 | 92 | return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "Application not found with ID: " + appid).build(); |
|---|
| 93 | 93 | } |
|---|
| .. | .. |
|---|
| 101 | 101 | { MediaType.APPLICATION_JSON }) |
|---|
| 102 | 102 | @Transactional |
|---|
| 103 | 103 | public Response create(Application app, @HeaderParam(TokenHelper.TOKEN_HEADER_PÀRAM) String token) { |
|---|
| 104 | | - log.info("Creating new application"); |
|---|
| 104 | + LOG.info("Creating new application"); |
|---|
| 105 | 105 | EntityManager em = emProvider.get(); |
|---|
| 106 | 106 | app.setCreationTimestamp(new Date()); |
|---|
| 107 | 107 | em.persist(app); |
|---|
| .. | .. |
|---|
| 117 | 117 | @Produces( |
|---|
| 118 | 118 | { MediaType.APPLICATION_JSON }) |
|---|
| 119 | 119 | public Response modify(Application app, @PathParam("appid") String appid, @HeaderParam(TokenHelper.TOKEN_HEADER_PÀRAM) String token) { |
|---|
| 120 | | - log.info("Modifying application with id: {}", appid); |
|---|
| 120 | + LOG.info("Modifying application with id: {}", appid); |
|---|
| 121 | 121 | EntityManager em = emProvider.get(); |
|---|
| 122 | 122 | Application currentapp = em.find(Application.class, Integer.parseInt(appid)); |
|---|
| 123 | 123 | if (currentapp == null) { |
|---|
| 124 | | - log.error("Application with id {} not found in DB", appid); |
|---|
| 124 | + LOG.error("Application with id {} not found in DB", appid); |
|---|
| 125 | 125 | return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "Application not found with ID: " + appid).build(); |
|---|
| 126 | 126 | } |
|---|
| 127 | 127 | currentapp.setName(app.getName()); |
|---|
| .. | .. |
|---|
| 137 | 137 | @Produces( |
|---|
| 138 | 138 | { MediaType.APPLICATION_JSON }) |
|---|
| 139 | 139 | public Response delete(@PathParam("appid") String appid, @Context HttpServletRequest request) { |
|---|
| 140 | | - log.info("Deleting app with id: {}", appid); |
|---|
| 140 | + LOG.info("Deleting app with id: {}", appid); |
|---|
| 141 | 141 | EntityManager em = emProvider.get(); |
|---|
| 142 | 142 | Application app = em.find(Application.class, Integer.parseInt(appid)); |
|---|
| 143 | 143 | if (app == null) { |
|---|
| 144 | | - log.error("Application with id {} can not be deleted, It was not found in DB", appid); |
|---|
| 144 | + LOG.error("Application with id {} can not be deleted, It was not found in DB", appid); |
|---|
| 145 | 145 | return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "Application not found with ID: " + appid).build(); |
|---|
| 146 | 146 | } |
|---|
| 147 | 147 | |
|---|
| .. | .. |
|---|
| 36 | 36 | @Singleton |
|---|
| 37 | 37 | public class BasicServices { |
|---|
| 38 | 38 | |
|---|
| 39 | | - private static final Logger log = LogManager.getLogger(BasicServices.class); |
|---|
| 39 | + private static final Logger LOG = LogManager.getLogger(BasicServices.class); |
|---|
| 40 | 40 | |
|---|
| 41 | 41 | @Inject |
|---|
| 42 | 42 | TokenHelper tokenHelper; |
|---|
| .. | .. |
|---|
| 58 | 58 | @Produces( |
|---|
| 59 | 59 | { MediaType.TEXT_HTML }) |
|---|
| 60 | 60 | public Response init(@PathParam("module") String module, @Context HttpServletRequest request) { |
|---|
| 61 | | - log.info("App index main.html"); |
|---|
| 61 | + LOG.info("App index main.html"); |
|---|
| 62 | 62 | String page = "/main.html"; |
|---|
| 63 | 63 | URI uri = UriBuilder.fromUri(page).build(); |
|---|
| 64 | 64 | return Response.seeOther(uri).build(); |
|---|
| .. | .. |
|---|
| 69 | 69 | @Produces( |
|---|
| 70 | 70 | { MediaType.APPLICATION_JSON }) |
|---|
| 71 | 71 | public Response login(@FormParam("username") String user, @FormParam("password") String password, @Context HttpServletRequest request) { |
|---|
| 72 | | - log.info("index session: " + request.getSession()); |
|---|
| 73 | | - log.info("user: {}, pass: {}", user, password); |
|---|
| 74 | | - log.info("is user in role: {} == {} ? ", "advance", request.isUserInRole("advance")); |
|---|
| 72 | + LOG.info("index session: " + request.getSession()); |
|---|
| 73 | + LOG.info("user: {}, pass: {}", user, password); |
|---|
| 74 | + LOG.info("is user in role: {} == {} ? ", "advance", request.isUserInRole("advance")); |
|---|
| 75 | 75 | |
|---|
| 76 | 76 | if ("no".equals(password)) |
|---|
| 77 | 77 | return Response.status(Status.UNAUTHORIZED).build(); |
|---|
| .. | .. |
|---|
| 101 | 101 | if (!valid) |
|---|
| 102 | 102 | return Response.status(Status.UNAUTHORIZED).build(); |
|---|
| 103 | 103 | |
|---|
| 104 | | - // log.info("Token : " + token); |
|---|
| 104 | + // LOG.info("Token : " + token); |
|---|
| 105 | 105 | String user = tokenHelper.extractUserFromToken(token); |
|---|
| 106 | | - // log.info("Token user: " + user); |
|---|
| 106 | + // LOG.info("Token user: " + user); |
|---|
| 107 | 107 | Date date = tokenHelper.extractDateCreationFromToken(token); |
|---|
| 108 | | - // log.info("Token date: " + date); |
|---|
| 108 | + // LOG.info("Token date: " + date); |
|---|
| 109 | 109 | |
|---|
| 110 | 110 | return Response.ok(Utils.createMap("valid", true, "user", user, "date", date)).build(); |
|---|
| 111 | 111 | } |
|---|
| .. | .. |
|---|
| 119 | 119 | if (token == null) |
|---|
| 120 | 120 | Response.status(Status.BAD_REQUEST).build(); |
|---|
| 121 | 121 | String user = tokenHelper.extractUserFromToken(token); |
|---|
| 122 | | - log.info("User {} has logged out", user); |
|---|
| 122 | + LOG.info("User {} has logged out", user); |
|---|
| 123 | 123 | return Response.ok().build(); |
|---|
| 124 | 124 | } |
|---|
| 125 | 125 | } |
|---|
| .. | .. |
|---|
| 47 | 47 | @Path("/license") |
|---|
| 48 | 48 | public class LicenseResource { |
|---|
| 49 | 49 | |
|---|
| 50 | | - private static final Logger log = LogManager.getLogger(LicenseResource.class); |
|---|
| 50 | + private static final Logger LOG = LogManager.getLogger(LicenseResource.class); |
|---|
| 51 | 51 | |
|---|
| 52 | 52 | @Inject |
|---|
| 53 | 53 | TokenHelper tokenHelper; |
|---|
| .. | .. |
|---|
| 68 | 68 | @Produces( |
|---|
| 69 | 69 | { MediaType.APPLICATION_JSON }) |
|---|
| 70 | 70 | public Response index(@QueryParam("packId") Integer packId, @Context BasicSecurityContext bsc) { |
|---|
| 71 | | - log.info("Getting licenses list "); |
|---|
| 71 | + LOG.info("Getting licenses list "); |
|---|
| 72 | 72 | |
|---|
| 73 | 73 | EntityManager em = emProvider.get(); |
|---|
| 74 | 74 | |
|---|
| .. | .. |
|---|
| 77 | 77 | if (pack == null) |
|---|
| 78 | 78 | return Response.ok().build(); |
|---|
| 79 | 79 | if (!bsc.getOrganizationsIds().contains(pack.getOrganization().getId())) { |
|---|
| 80 | | - log.error("Pack with id {} not accesible by user {}", pack, bsc.getUserPrincipal()); |
|---|
| 80 | + LOG.error("Pack with id {} not accesible by user {}", pack, bsc.getUserPrincipal()); |
|---|
| 81 | 81 | return Response.status(Status.UNAUTHORIZED).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "Unathorized access to pack licenses").build(); |
|---|
| 82 | 82 | } |
|---|
| 83 | 83 | } |
|---|
| .. | .. |
|---|
| 99 | 99 | @Produces( |
|---|
| 100 | 100 | { MediaType.APPLICATION_JSON }) |
|---|
| 101 | 101 | public Response get(@PathParam("licId") Integer licId, @Context BasicSecurityContext bsc) throws SeCurisServiceException { |
|---|
| 102 | | - log.info("Getting organization data for id: {}: ", licId); |
|---|
| 102 | + LOG.info("Getting organization data for id: {}: ", licId); |
|---|
| 103 | 103 | |
|---|
| 104 | 104 | EntityManager em = emProvider.get(); |
|---|
| 105 | 105 | License lic = getCurrentLicense(licId, bsc, em); |
|---|
| .. | .. |
|---|
| 122 | 122 | License lic = getCurrentLicense(licId, bsc, em); |
|---|
| 123 | 123 | |
|---|
| 124 | 124 | if (lic.getLicenseData() == null) { |
|---|
| 125 | | - log.error("License with id {} has not license file generated", licId, bsc.getUserPrincipal()); |
|---|
| 125 | + LOG.error("License with id {} has not license file generated", licId, bsc.getUserPrincipal()); |
|---|
| 126 | 126 | throw new SeCurisServiceException(Status.FORBIDDEN.getStatusCode(), "License has not contain data to generate license file"); |
|---|
| 127 | 127 | } |
|---|
| 128 | 128 | if (License.Status.isActionValid(License.Action.DOWNLOAD, lic.getStatus())) { |
|---|
| 129 | | - log.error("License with id {} is not active, so It can not downloaded", licId, bsc.getUserPrincipal()); |
|---|
| 129 | + LOG.error("License with id {} is not active, so It can not downloaded", licId, bsc.getUserPrincipal()); |
|---|
| 130 | 130 | throw new SeCurisServiceException(Status.FORBIDDEN.getStatusCode(), "License is not active, so It can not be downloaded"); |
|---|
| 131 | 131 | } |
|---|
| 132 | 132 | return Response.ok(lic.getLicenseData()).build(); |
|---|
| .. | .. |
|---|
| 146 | 146 | License lic = getCurrentLicense(licId, bsc, em); |
|---|
| 147 | 147 | |
|---|
| 148 | 148 | if (License.Status.isActionValid(License.Action.ACTIVATION, lic.getStatus())) { |
|---|
| 149 | | - log.error("License with id {} can not be activated from current license status", licId); |
|---|
| 149 | + LOG.error("License with id {} can not be activated from current license status", licId); |
|---|
| 150 | 150 | throw new SeCurisServiceException(Status.FORBIDDEN.getStatusCode(), "License with id " + licId + " can not be activated from the current license status"); |
|---|
| 151 | 151 | } |
|---|
| 152 | 152 | |
|---|
| .. | .. |
|---|
| 193 | 193 | License lic = getCurrentLicense(licId, bsc, em); |
|---|
| 194 | 194 | |
|---|
| 195 | 195 | if (License.Status.isActionValid(License.Action.CANCEL, lic.getStatus())) { |
|---|
| 196 | | - log.error("License with id {} can not be canceled from current license status", licId); |
|---|
| 196 | + LOG.error("License with id {} can not be canceled from current license status", licId); |
|---|
| 197 | 197 | throw new SeCurisServiceException(Status.FORBIDDEN.getStatusCode(), "License with id " + licId + " can not be canceled from the current license status"); |
|---|
| 198 | 198 | } |
|---|
| 199 | 199 | |
|---|
| .. | .. |
|---|
| 214 | 214 | { MediaType.APPLICATION_JSON }) |
|---|
| 215 | 215 | @Transactional |
|---|
| 216 | 216 | public Response create(License lic, @Context BasicSecurityContext bsc) throws SeCurisServiceException { |
|---|
| 217 | | - log.info("Creating new license from create()"); |
|---|
| 217 | + LOG.info("Creating new license from create()"); |
|---|
| 218 | 218 | EntityManager em = emProvider.get(); |
|---|
| 219 | 219 | Pack pack = null; |
|---|
| 220 | 220 | if (lic.getPackId() != null) { |
|---|
| 221 | 221 | pack = em.find(Pack.class, lic.getPackId()); |
|---|
| 222 | 222 | if (pack == null) { |
|---|
| 223 | | - log.error("License pack with id {} not found in DB", lic.getPackId()); |
|---|
| 223 | + LOG.error("License pack with id {} not found in DB", lic.getPackId()); |
|---|
| 224 | 224 | return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "License's pack not found with ID: " + lic.getPackId()).build(); |
|---|
| 225 | 225 | } else { |
|---|
| 226 | 226 | if (!bsc.isUserInRole(BasicSecurityContext.ROL_ADMIN)) { |
|---|
| 227 | 227 | if (!bsc.getOrganizationsIds().contains(pack.getOrganization().getId())) { |
|---|
| 228 | | - log.error("License for pack with id {} can not be created by user {}", pack.getId(), bsc.getUserPrincipal()); |
|---|
| 228 | + LOG.error("License for pack with id {} can not be created by user {}", pack.getId(), bsc.getUserPrincipal()); |
|---|
| 229 | 229 | return Response.status(Status.UNAUTHORIZED).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "Unathorized action on pack license").build(); |
|---|
| 230 | 230 | } |
|---|
| 231 | 231 | } |
|---|
| .. | .. |
|---|
| 273 | 273 | @Produces( |
|---|
| 274 | 274 | { MediaType.APPLICATION_JSON }) |
|---|
| 275 | 275 | public Response modify(License lic, @PathParam("licId") Integer licId, @Context BasicSecurityContext bsc) throws SeCurisServiceException { |
|---|
| 276 | | - log.info("Modifying organization with id: {}", licId); |
|---|
| 276 | + LOG.info("Modifying organization with id: {}", licId); |
|---|
| 277 | 277 | |
|---|
| 278 | 278 | EntityManager em = emProvider.get(); |
|---|
| 279 | 279 | |
|---|
| .. | .. |
|---|
| 296 | 296 | @Produces( |
|---|
| 297 | 297 | { MediaType.APPLICATION_JSON }) |
|---|
| 298 | 298 | public Response delete(@PathParam("licId") Integer licId, @Context BasicSecurityContext bsc) throws SeCurisServiceException { |
|---|
| 299 | | - log.info("Deleting license with id: {}", licId); |
|---|
| 299 | + LOG.info("Deleting license with id: {}", licId); |
|---|
| 300 | 300 | EntityManager em = emProvider.get(); |
|---|
| 301 | 301 | License lic = getCurrentLicense(licId, bsc, em); |
|---|
| 302 | 302 | |
|---|
| 303 | 303 | if (lic.getStatus() != License.Status.CANCELED || lic.getStatus() != License.Status.CREATED) { |
|---|
| 304 | | - log.error("License {} can not be deleted with status {}", lic.getCode(), lic.getStatus()); |
|---|
| 304 | + LOG.error("License {} can not be deleted with status {}", lic.getCode(), lic.getStatus()); |
|---|
| 305 | 305 | return Response.status(Status.FORBIDDEN).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "License can not be deleted in current status").build(); |
|---|
| 306 | 306 | } |
|---|
| 307 | 307 | |
|---|
| .. | .. |
|---|
| 311 | 311 | |
|---|
| 312 | 312 | private License getCurrentLicense(Integer licId, BasicSecurityContext bsc, EntityManager em) throws SeCurisServiceException { |
|---|
| 313 | 313 | if (licId == null || licId.equals("")) { |
|---|
| 314 | | - log.error("License ID is mandatory"); |
|---|
| 314 | + LOG.error("License ID is mandatory"); |
|---|
| 315 | 315 | throw new SeCurisServiceException(Status.NOT_FOUND.getStatusCode(), "Missing license ID"); |
|---|
| 316 | 316 | } |
|---|
| 317 | 317 | |
|---|
| 318 | 318 | License lic = em.find(License.class, licId); |
|---|
| 319 | 319 | if (lic == null) { |
|---|
| 320 | | - log.error("License with id {} not found in DB", licId); |
|---|
| 320 | + LOG.error("License with id {} not found in DB", licId); |
|---|
| 321 | 321 | throw new SeCurisServiceException(Status.NOT_FOUND.getStatusCode(), "License not found for ID: " + licId); |
|---|
| 322 | 322 | } |
|---|
| 323 | 323 | if (!bsc.isUserInRole(BasicSecurityContext.ROL_ADMIN)) { |
|---|
| 324 | 324 | if (!bsc.getOrganizationsIds().contains(lic.getPack().getOrganization().getId())) { |
|---|
| 325 | | - log.error("License with id {} is not accesible by user {}", licId, bsc.getUserPrincipal()); |
|---|
| 325 | + LOG.error("License with id {} is not accesible by user {}", licId, bsc.getUserPrincipal()); |
|---|
| 326 | 326 | throw new SeCurisServiceException(Status.UNAUTHORIZED.getStatusCode(), "Unathorized access to license data"); |
|---|
| 327 | 327 | } |
|---|
| 328 | 328 | } |
|---|
| .. | .. |
|---|
| 33 | 33 | public class LicenseServices { |
|---|
| 34 | 34 | |
|---|
| 35 | 35 | // private LicenseHelper licenseHelper = InjectorFactory.getInjector().getInstance(LicenseHelper.class); |
|---|
| 36 | | - private static final Logger log = LogManager.getLogger(LicenseServices.class); |
|---|
| 36 | + private static final Logger LOG = LogManager.getLogger(LicenseServices.class); |
|---|
| 37 | 37 | |
|---|
| 38 | 38 | private static final int DEFAULT_LICENSE_EXPIRATION = 365; |
|---|
| 39 | 39 | private static final String LICENSE_STRING = "CurisIntegrity Config Server v{0}.{1}"; |
|---|
| .. | .. |
|---|
| 58 | 58 | String index = IOUtils.toString(this.getClass().getResourceAsStream("/static/index.html")); |
|---|
| 59 | 59 | return Response.ok().entity(index).build(); |
|---|
| 60 | 60 | } catch (IOException e) { |
|---|
| 61 | | - // TODO Auto-generated catch block |
|---|
| 62 | | - e.printStackTrace(); |
|---|
| 61 | + LOG.error("Error getting index.html", e); |
|---|
| 63 | 62 | } |
|---|
| 64 | 63 | return Response.ok().entity(MessageFormat.format(LICENSE_STRING, 0, 1)).build(); |
|---|
| 65 | 64 | } |
|---|
| .. | .. |
|---|
| 69 | 68 | @Produces( |
|---|
| 70 | 69 | { MediaType.TEXT_PLAIN }) |
|---|
| 71 | 70 | public Response dummy(@Context HttpServletRequest request) { |
|---|
| 72 | | - log.info("Request: " + request.getPathInfo()); |
|---|
| 71 | + LOG.info("Request: " + request.getPathInfo()); |
|---|
| 73 | 72 | return Response.ok().entity((uri == null)).build(); |
|---|
| 74 | 73 | } |
|---|
| 75 | 74 | |
|---|
| .. | .. |
|---|
| 82 | 81 | @Produces( |
|---|
| 83 | 82 | { MediaType.APPLICATION_JSON }) |
|---|
| 84 | 83 | public Response testFile1(@MultipartForm FileUploadForm mfdi) { |
|---|
| 85 | | - log.info("FORM: texto: {}, file: {}", mfdi.getTexto(), new String(mfdi.getFile())); |
|---|
| 84 | + LOG.info("FORM: texto: {}, file: {}", mfdi.getTexto(), new String(mfdi.getFile())); |
|---|
| 86 | 85 | return Response.ok("OK").build(); |
|---|
| 87 | 86 | } |
|---|
| 88 | 87 | |
|---|
| .. | .. |
|---|
| 92 | 91 | { MediaType.APPLICATION_JSON }) |
|---|
| 93 | 92 | public ServiceResponse<ServerConfigVersions> testFile(@PathParam("license") String license, @DefaultValue("-1") @QueryParam("minorVersion") int minorVersion, @DefaultValue("-1") @QueryParam("majorVersion") int majorVersion) { |
|---|
| 94 | 93 | |
|---|
| 95 | | - log.info("Called 'current' service with license: {}", license); |
|---|
| 94 | + LOG.info("Called 'current' service with license: {}", license); |
|---|
| 96 | 95 | ServiceResponse<ServerConfigVersions> response = new ServiceResponse<ServerConfigVersions>(); |
|---|
| 97 | 96 | |
|---|
| 98 | 97 | return response; |
|---|
| .. | .. |
|---|
| 24 | 24 | |
|---|
| 25 | 25 | import net.curisit.integrity.commons.Utils; |
|---|
| 26 | 26 | import net.curisit.securis.DefaultExceptionHandler; |
|---|
| 27 | +import net.curisit.securis.SeCurisException; |
|---|
| 27 | 28 | import net.curisit.securis.db.Application; |
|---|
| 28 | 29 | import net.curisit.securis.db.LicenseType; |
|---|
| 29 | 30 | import net.curisit.securis.utils.TokenHelper; |
|---|
| .. | .. |
|---|
| 41 | 42 | @Path("/licensetype") |
|---|
| 42 | 43 | public class LicenseTypeResource { |
|---|
| 43 | 44 | |
|---|
| 44 | | - private static final Logger log = LogManager.getLogger(LicenseTypeResource.class); |
|---|
| 45 | + private static final Logger LOG = LogManager.getLogger(LicenseTypeResource.class); |
|---|
| 45 | 46 | |
|---|
| 46 | 47 | @Inject |
|---|
| 47 | 48 | TokenHelper tokenHelper; |
|---|
| .. | .. |
|---|
| 61 | 62 | @Produces( |
|---|
| 62 | 63 | { MediaType.APPLICATION_JSON }) |
|---|
| 63 | 64 | public Response index() { |
|---|
| 64 | | - log.info("Getting license types list "); |
|---|
| 65 | + LOG.info("Getting license types list "); |
|---|
| 65 | 66 | |
|---|
| 66 | 67 | EntityManager em = emProvider.get(); |
|---|
| 67 | 68 | TypedQuery<LicenseType> q = em.createNamedQuery("list-license_types", LicenseType.class); |
|---|
| .. | .. |
|---|
| 79 | 80 | @Produces( |
|---|
| 80 | 81 | { MediaType.APPLICATION_JSON }) |
|---|
| 81 | 82 | public Response get(@PathParam("ltid") String ltid, @HeaderParam(TokenHelper.TOKEN_HEADER_PÀRAM) String token) { |
|---|
| 82 | | - log.info("Getting license type data for id: {}: ", ltid); |
|---|
| 83 | + LOG.info("Getting license type data for id: {}: ", ltid); |
|---|
| 83 | 84 | if (ltid == null || ltid.equals("")) { |
|---|
| 84 | | - log.error("LicenseType ID is mandatory"); |
|---|
| 85 | + LOG.error("LicenseType ID is mandatory"); |
|---|
| 85 | 86 | return Response.status(Status.NOT_FOUND).build(); |
|---|
| 86 | 87 | } |
|---|
| 87 | 88 | |
|---|
| 88 | 89 | EntityManager em = emProvider.get(); |
|---|
| 89 | 90 | LicenseType lt = em.find(LicenseType.class, Integer.parseInt(ltid)); |
|---|
| 90 | 91 | if (lt == null) { |
|---|
| 91 | | - log.error("LicenseType with id {} not found in DB", ltid); |
|---|
| 92 | + LOG.error("LicenseType with id {} not found in DB", ltid); |
|---|
| 92 | 93 | return Response.status(Status.NOT_FOUND).build(); |
|---|
| 93 | 94 | } |
|---|
| 94 | 95 | return Response.ok(lt).build(); |
|---|
| .. | .. |
|---|
| 101 | 102 | { MediaType.APPLICATION_JSON }) |
|---|
| 102 | 103 | @Transactional |
|---|
| 103 | 104 | public Response create(LicenseType lt, @HeaderParam(TokenHelper.TOKEN_HEADER_PÀRAM) String token) { |
|---|
| 104 | | - log.info("Creating new license type"); |
|---|
| 105 | + LOG.info("Creating new license type"); |
|---|
| 105 | 106 | EntityManager em = emProvider.get(); |
|---|
| 106 | | - Application app = null; |
|---|
| 107 | | - if (lt.getApplicationId() != null) { |
|---|
| 108 | | - app = em.find(Application.class, lt.getApplicationId()); |
|---|
| 109 | | - if (app == null) { |
|---|
| 110 | | - log.error("LicenseType application with id {} not found in DB", lt.getApplicationId()); |
|---|
| 111 | | - return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "License type's app not found with ID: " + lt.getApplicationId()).build(); |
|---|
| 112 | | - } |
|---|
| 113 | | - } else { |
|---|
| 114 | | - log.error("Application is missing for current license type data"); |
|---|
| 107 | + |
|---|
| 108 | + try { |
|---|
| 109 | + setApplication(lt, lt.getApplicationId(), em); |
|---|
| 110 | + } catch (SeCurisException e) { |
|---|
| 111 | + return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, e.getMessage()).build(); |
|---|
| 112 | + } |
|---|
| 113 | + |
|---|
| 114 | + if (lt.getApplicationId() == null) { |
|---|
| 115 | + LOG.error("Application is missing for current license type data"); |
|---|
| 115 | 116 | return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "Application is missing for current license type data").build(); |
|---|
| 116 | 117 | } |
|---|
| 117 | 118 | |
|---|
| 118 | | - lt.setApplication(app); |
|---|
| 119 | 119 | lt.setCreationTimestamp(new Date()); |
|---|
| 120 | 120 | em.persist(lt); |
|---|
| 121 | 121 | |
|---|
| .. | .. |
|---|
| 130 | 130 | @Produces( |
|---|
| 131 | 131 | { MediaType.APPLICATION_JSON }) |
|---|
| 132 | 132 | public Response modify(LicenseType lt, @PathParam("ltid") String ltid, @HeaderParam(TokenHelper.TOKEN_HEADER_PÀRAM) String token) { |
|---|
| 133 | | - log.info("Modifying license type with id: {}", ltid); |
|---|
| 133 | + LOG.info("Modifying license type with id: {}", ltid); |
|---|
| 134 | 134 | EntityManager em = emProvider.get(); |
|---|
| 135 | 135 | LicenseType currentlt = em.find(LicenseType.class, Integer.parseInt(ltid)); |
|---|
| 136 | 136 | if (currentlt == null) { |
|---|
| 137 | | - log.error("LicenseType with id {} not found in DB", ltid); |
|---|
| 137 | + LOG.error("LicenseType with id {} not found in DB", ltid); |
|---|
| 138 | 138 | return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "License type not found with ID: " + ltid).build(); |
|---|
| 139 | 139 | } |
|---|
| 140 | | - Application app = null; |
|---|
| 141 | | - if (lt.getApplicationId() != null) { |
|---|
| 142 | | - app = em.find(Application.class, lt.getApplicationId()); |
|---|
| 143 | | - if (app == null) { |
|---|
| 144 | | - log.error("LicenseType application with id {} not found in DB", lt.getApplicationId()); |
|---|
| 145 | | - return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "License type's app not found with ID: " + lt.getApplicationId()).build(); |
|---|
| 146 | | - } |
|---|
| 140 | + |
|---|
| 141 | + try { |
|---|
| 142 | + setApplication(currentlt, lt.getApplicationId(), em); |
|---|
| 143 | + } catch (SeCurisException e) { |
|---|
| 144 | + return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, e.getMessage()).build(); |
|---|
| 147 | 145 | } |
|---|
| 146 | + |
|---|
| 148 | 147 | currentlt.setCode(lt.getCode()); |
|---|
| 149 | 148 | currentlt.setName(lt.getName()); |
|---|
| 150 | 149 | currentlt.setDescription(lt.getDescription()); |
|---|
| 151 | | - currentlt.setApplication(app); |
|---|
| 152 | 150 | em.persist(currentlt); |
|---|
| 153 | 151 | |
|---|
| 154 | 152 | return Response.ok(currentlt).build(); |
|---|
| 153 | + } |
|---|
| 154 | + |
|---|
| 155 | + private void setApplication(LicenseType licType, Integer applicationId, EntityManager em) throws SeCurisException { |
|---|
| 156 | + Application app = null; |
|---|
| 157 | + if (applicationId != null) { |
|---|
| 158 | + app = em.find(Application.class, applicationId); |
|---|
| 159 | + if (app == null) { |
|---|
| 160 | + LOG.error("LicenseType application with id {} not found in DB", applicationId); |
|---|
| 161 | + |
|---|
| 162 | + throw new SecurityException("License type's app not found with ID: " + applicationId); |
|---|
| 163 | + } |
|---|
| 164 | + } |
|---|
| 165 | + licType.setApplication(app); |
|---|
| 155 | 166 | } |
|---|
| 156 | 167 | |
|---|
| 157 | 168 | @DELETE |
|---|
| .. | .. |
|---|
| 160 | 171 | @Produces( |
|---|
| 161 | 172 | { MediaType.APPLICATION_JSON }) |
|---|
| 162 | 173 | public Response delete(@PathParam("ltid") String ltid, @Context HttpServletRequest request) { |
|---|
| 163 | | - log.info("Deleting app with id: {}", ltid); |
|---|
| 174 | + LOG.info("Deleting app with id: {}", ltid); |
|---|
| 164 | 175 | EntityManager em = emProvider.get(); |
|---|
| 165 | 176 | LicenseType app = em.find(LicenseType.class, Integer.parseInt(ltid)); |
|---|
| 166 | 177 | if (app == null) { |
|---|
| 167 | | - log.error("LicenseType with id {} can not be deleted, It was not found in DB", ltid); |
|---|
| 178 | + LOG.error("LicenseType with id {} can not be deleted, It was not found in DB", ltid); |
|---|
| 168 | 179 | return Response.status(Status.NOT_FOUND).build(); |
|---|
| 169 | 180 | } |
|---|
| 170 | 181 | |
|---|
| .. | .. |
|---|
| 26 | 26 | |
|---|
| 27 | 27 | import net.curisit.integrity.commons.Utils; |
|---|
| 28 | 28 | import net.curisit.securis.DefaultExceptionHandler; |
|---|
| 29 | +import net.curisit.securis.SeCurisException; |
|---|
| 29 | 30 | import net.curisit.securis.db.Organization; |
|---|
| 30 | 31 | import net.curisit.securis.db.User; |
|---|
| 31 | 32 | import net.curisit.securis.security.BasicSecurityContext; |
|---|
| .. | .. |
|---|
| 46 | 47 | @Path("/organization") |
|---|
| 47 | 48 | public class OrganizationResource { |
|---|
| 48 | 49 | |
|---|
| 49 | | - private static final Logger log = LogManager.getLogger(OrganizationResource.class); |
|---|
| 50 | + private static final Logger LOG = LogManager.getLogger(OrganizationResource.class); |
|---|
| 50 | 51 | |
|---|
| 51 | 52 | @Inject |
|---|
| 52 | 53 | private Provider<EntityManager> emProvider; |
|---|
| .. | .. |
|---|
| 65 | 66 | @Securable |
|---|
| 66 | 67 | // @RolesAllowed(SecurityContextWrapper.ROL_ADVANCE) |
|---|
| 67 | 68 | public Response index(@Context BasicSecurityContext bsc) { |
|---|
| 68 | | - log.info("Getting organizations list "); |
|---|
| 69 | + LOG.info("Getting organizations list "); |
|---|
| 69 | 70 | |
|---|
| 70 | | - // log.info("User orgs: {}", request.getAttribute("oser_orgs")); |
|---|
| 71 | + // LOG.info("User orgs: {}", request.getAttribute("oser_orgs")); |
|---|
| 71 | 72 | BasicSecurityContext bsc2 = ResteasyProviderFactory.getContextData(BasicSecurityContext.class); |
|---|
| 72 | | - log.info("bsc: {}", bsc); |
|---|
| 73 | | - log.info("bsc2: {}", bsc2); |
|---|
| 74 | | - // log.info("securityContext: {}", scw); |
|---|
| 75 | | - log.info("securityContext ROL_ADMIN?: {}", bsc.isUserInRole(BasicSecurityContext.ROL_ADMIN)); |
|---|
| 73 | + LOG.info("bsc: {}", bsc); |
|---|
| 74 | + LOG.info("bsc2: {}", bsc2); |
|---|
| 75 | + // LOG.info("securityContext: {}", scw); |
|---|
| 76 | + LOG.info("securityContext ROL_ADMIN?: {}", bsc.isUserInRole(BasicSecurityContext.ROL_ADMIN)); |
|---|
| 76 | 77 | EntityManager em = emProvider.get(); |
|---|
| 77 | 78 | TypedQuery<Organization> q; |
|---|
| 78 | 79 | if (bsc.isUserInRole(BasicSecurityContext.ROL_ADMIN)) { |
|---|
| 79 | | - log.info("GEtting all orgs for user: " + bsc.getUserPrincipal()); |
|---|
| 80 | + LOG.info("GEtting all orgs for user: " + bsc.getUserPrincipal()); |
|---|
| 80 | 81 | q = em.createNamedQuery("list-organizations", Organization.class); |
|---|
| 81 | 82 | } else { |
|---|
| 82 | 83 | q = em.createNamedQuery("list-organizations", Organization.class); |
|---|
| 83 | 84 | // if (securityContext.getOrganizationsIds() == null) |
|---|
| 84 | 85 | // Response.ok().build(); |
|---|
| 85 | | - // log.info("Getting only {} orgs for user: {}", securityContext.getOrganizationsIds(), securityContext.getUserPrincipal()); |
|---|
| 86 | + // LOG.info("Getting only {} orgs for user: {}", securityContext.getOrganizationsIds(), securityContext.getUserPrincipal()); |
|---|
| 86 | 87 | // q = em.createNamedQuery("list-organizations-by-ids", Organization.class); |
|---|
| 87 | 88 | // q.setParameter("list_ids", securityContext.getOrganizationsIds()); |
|---|
| 88 | 89 | } |
|---|
| .. | .. |
|---|
| 102 | 103 | { MediaType.APPLICATION_JSON }) |
|---|
| 103 | 104 | @Securable |
|---|
| 104 | 105 | public Response get(@PathParam("orgid") String orgid, @HeaderParam(TokenHelper.TOKEN_HEADER_PÀRAM) String token) { |
|---|
| 105 | | - log.info("Getting organization data for id: {}: ", orgid); |
|---|
| 106 | + LOG.info("Getting organization data for id: {}: ", orgid); |
|---|
| 106 | 107 | if (orgid == null || orgid.equals("")) { |
|---|
| 107 | | - log.error("Organization ID is mandatory"); |
|---|
| 108 | + LOG.error("Organization ID is mandatory"); |
|---|
| 108 | 109 | return Response.status(Status.NOT_FOUND).build(); |
|---|
| 109 | 110 | } |
|---|
| 110 | 111 | // if (!securityContext.isOrgAccesible(Integer.parseInt(orgid))) { |
|---|
| 111 | | - // log.error("Organization with id {} not accessible for user: {}", orgid, securityContext.getUserPrincipal()); |
|---|
| 112 | + // LOG.error("Organization with id {} not accessible for user: {}", orgid, securityContext.getUserPrincipal()); |
|---|
| 112 | 113 | // return Response.status(Status.UNAUTHORIZED).build(); |
|---|
| 113 | 114 | // } |
|---|
| 114 | 115 | |
|---|
| 115 | 116 | EntityManager em = emProvider.get(); |
|---|
| 116 | 117 | Organization org = em.find(Organization.class, Integer.parseInt(orgid)); |
|---|
| 117 | 118 | if (org == null) { |
|---|
| 118 | | - log.error("Organization with id {} not found in DB", orgid); |
|---|
| 119 | + LOG.error("Organization with id {} not found in DB", orgid); |
|---|
| 119 | 120 | return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "Organization not found, id: " + orgid).build(); |
|---|
| 120 | 121 | } |
|---|
| 121 | 122 | return Response.ok(org).build(); |
|---|
| .. | .. |
|---|
| 139 | 140 | @Securable |
|---|
| 140 | 141 | @RolesAllowed(BasicSecurityContext.ROL_ADMIN) |
|---|
| 141 | 142 | public Response create(Organization org, @HeaderParam(TokenHelper.TOKEN_HEADER_PÀRAM) String token) { |
|---|
| 142 | | - log.info("Creating new organization"); |
|---|
| 143 | + LOG.info("Creating new organization"); |
|---|
| 143 | 144 | EntityManager em = emProvider.get(); |
|---|
| 144 | | - Organization parentOrg = null; |
|---|
| 145 | | - if (org.getParentOrgId() != null) { |
|---|
| 146 | | - parentOrg = em.find(Organization.class, org.getParentOrgId()); |
|---|
| 147 | | - if (parentOrg == null) { |
|---|
| 148 | | - log.error("Organization parent with id {} not found in DB", org.getParentOrgId()); |
|---|
| 149 | | - return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "Organization's parent not found with ID: " + org.getParentOrgId()).build(); |
|---|
| 150 | | - } |
|---|
| 145 | + |
|---|
| 146 | + try { |
|---|
| 147 | + this.setParentOrg(org, org.getParentOrgId(), em); |
|---|
| 148 | + } catch (SeCurisException e) { |
|---|
| 149 | + return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, e.getMessage()).build(); |
|---|
| 151 | 150 | } |
|---|
| 151 | + |
|---|
| 152 | 152 | List<User> users = null; |
|---|
| 153 | 153 | List<String> usersIds = org.getUsersIds(); |
|---|
| 154 | 154 | if (usersIds != null && usersIds.size() > 0) { |
|---|
| .. | .. |
|---|
| 156 | 156 | for (String username : usersIds) { |
|---|
| 157 | 157 | User user = em.find(User.class, username); |
|---|
| 158 | 158 | if (user == null) { |
|---|
| 159 | | - log.error("Organization user with id {} not found in DB", username); |
|---|
| 159 | + LOG.error("Organization user with id {} not found in DB", username); |
|---|
| 160 | 160 | return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "Organization's user not found with ID: " + username).build(); |
|---|
| 161 | 161 | } |
|---|
| 162 | 162 | users.add(user); |
|---|
| .. | .. |
|---|
| 164 | 164 | } |
|---|
| 165 | 165 | |
|---|
| 166 | 166 | org.setUsers(users); |
|---|
| 167 | | - org.setParentOrganization(parentOrg); |
|---|
| 168 | 167 | org.setCreationTimestamp(new Date()); |
|---|
| 169 | 168 | em.persist(org); |
|---|
| 170 | 169 | |
|---|
| 171 | 170 | return Response.ok(org).build(); |
|---|
| 171 | + } |
|---|
| 172 | + |
|---|
| 173 | + private void setParentOrg(Organization org, Integer parentOrgId, EntityManager em) throws SeCurisException { |
|---|
| 174 | + Organization parentOrg = null; |
|---|
| 175 | + if (parentOrgId != null) { |
|---|
| 176 | + parentOrg = em.find(Organization.class, parentOrgId); |
|---|
| 177 | + if (parentOrg == null) { |
|---|
| 178 | + LOG.error("Organization parent with id {} not found in DB", org.getParentOrgId()); |
|---|
| 179 | + throw new SecurityException("Organization's parent not found with ID: " + org.getParentOrgId()); |
|---|
| 180 | + } |
|---|
| 181 | + } |
|---|
| 182 | + |
|---|
| 183 | + org.setParentOrganization(parentOrg); |
|---|
| 184 | + } |
|---|
| 185 | + |
|---|
| 186 | + private void setOrgUsers(Organization org, List<String> usersIds, EntityManager em) throws SeCurisException { |
|---|
| 187 | + List<User> users = null; |
|---|
| 188 | + if (usersIds != null && usersIds.size() > 0) { |
|---|
| 189 | + users = new ArrayList<>(); |
|---|
| 190 | + for (String username : usersIds) { |
|---|
| 191 | + User user = em.find(User.class, username); |
|---|
| 192 | + if (user == null) { |
|---|
| 193 | + LOG.error("Organization user with id '{}' not found in DB", username); |
|---|
| 194 | + throw new SecurityException("Organization's user not found with ID: " + username); |
|---|
| 195 | + } |
|---|
| 196 | + users.add(user); |
|---|
| 197 | + } |
|---|
| 198 | + } |
|---|
| 199 | + |
|---|
| 200 | + org.setUsers(users); |
|---|
| 172 | 201 | } |
|---|
| 173 | 202 | |
|---|
| 174 | 203 | @PUT |
|---|
| .. | .. |
|---|
| 181 | 210 | @Securable |
|---|
| 182 | 211 | @RolesAllowed(BasicSecurityContext.ROL_ADMIN) |
|---|
| 183 | 212 | public Response modify(Organization org, @PathParam("orgid") String orgid, @HeaderParam(TokenHelper.TOKEN_HEADER_PÀRAM) String token) { |
|---|
| 184 | | - log.info("Modifying organization with id: {}", orgid); |
|---|
| 213 | + LOG.info("Modifying organization with id: {}", orgid); |
|---|
| 185 | 214 | EntityManager em = emProvider.get(); |
|---|
| 186 | 215 | Organization currentOrg = em.find(Organization.class, Integer.parseInt(orgid)); |
|---|
| 187 | 216 | if (currentOrg == null) { |
|---|
| 188 | | - log.error("Organization with id {} not found in DB", orgid); |
|---|
| 217 | + LOG.error("Organization with id {} not found in DB", orgid); |
|---|
| 189 | 218 | return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "Organization not found with ID: " + orgid).build(); |
|---|
| 190 | 219 | } |
|---|
| 191 | | - Organization parentOrg = null; |
|---|
| 192 | | - if (org.getParentOrgId() != null) { |
|---|
| 193 | | - parentOrg = em.find(Organization.class, org.getParentOrgId()); |
|---|
| 194 | | - if (parentOrg == null) { |
|---|
| 195 | | - log.error("Organization parent with id {} not found in DB", org.getParentOrgId()); |
|---|
| 196 | | - return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "Organization's parent not found with ID: " + org.getParentOrgId()).build(); |
|---|
| 197 | | - } |
|---|
| 198 | | - if (isCyclicalRelationship(currentOrg.getId(), parentOrg)) { |
|---|
| 199 | | - log.error("Organization parent generate a cyclical relationship, parent id {}, current id: {}", org.getParentOrgId(), currentOrg.getId()); |
|---|
| 200 | | - return Response.status(Status.FORBIDDEN).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "Cyclical relationships are not allowed, please change the parent organization, current Parent: " + parentOrg.getName()).build(); |
|---|
| 220 | + try { |
|---|
| 221 | + this.setParentOrg(currentOrg, org.getParentOrgId(), em); |
|---|
| 222 | + } catch (SeCurisException e) { |
|---|
| 223 | + return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, e.getMessage()).build(); |
|---|
| 224 | + } |
|---|
| 225 | + if (org.getParentOrganization() != null) { |
|---|
| 226 | + if (isCyclicalRelationship(currentOrg.getId(), org.getParentOrganization())) { |
|---|
| 227 | + LOG.error("Organization parent generate a cyclical relationship, parent id {}, current id: {}", org.getParentOrgId(), currentOrg.getId()); |
|---|
| 228 | + return Response.status(Status.FORBIDDEN).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "Cyclical relationships are not allowed, please change the parent organization, current Parent: " + org.getParentOrganization().getName()).build(); |
|---|
| 201 | 229 | } |
|---|
| 202 | 230 | } |
|---|
| 203 | | - |
|---|
| 204 | | - List<User> users = null; |
|---|
| 205 | | - List<String> usersIds = org.getUsersIds(); |
|---|
| 206 | | - if (usersIds != null && usersIds.size() > 0) { |
|---|
| 207 | | - users = new ArrayList<>(); |
|---|
| 208 | | - for (String username : usersIds) { |
|---|
| 209 | | - User user = em.find(User.class, username); |
|---|
| 210 | | - if (user == null) { |
|---|
| 211 | | - log.error("Organization user with id '{}' not found in DB", username); |
|---|
| 212 | | - return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "Organization's user not found with ID: " + username).build(); |
|---|
| 213 | | - } |
|---|
| 214 | | - users.add(user); |
|---|
| 215 | | - } |
|---|
| 231 | + |
|---|
| 232 | + try { |
|---|
| 233 | + setOrgUsers(currentOrg, org.getUsersIds(), em); |
|---|
| 234 | + } catch (SeCurisException e) { |
|---|
| 235 | + return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, e.getMessage()).build(); |
|---|
| 216 | 236 | } |
|---|
| 217 | 237 | |
|---|
| 218 | | - currentOrg.setUsers(users); |
|---|
| 219 | | - currentOrg.setParentOrganization(parentOrg); |
|---|
| 220 | 238 | currentOrg.setCode(org.getCode()); |
|---|
| 221 | 239 | currentOrg.setName(org.getName()); |
|---|
| 222 | 240 | currentOrg.setDescription(org.getDescription()); |
|---|
| .. | .. |
|---|
| 233 | 251 | @Securable |
|---|
| 234 | 252 | @RolesAllowed(BasicSecurityContext.ROL_ADMIN) |
|---|
| 235 | 253 | public Response delete(@PathParam("orgid") String orgid, @Context HttpServletRequest request) { |
|---|
| 236 | | - log.info("Deleting organization with id: {}", orgid); |
|---|
| 254 | + LOG.info("Deleting organization with id: {}", orgid); |
|---|
| 237 | 255 | EntityManager em = emProvider.get(); |
|---|
| 238 | 256 | Organization org = em.find(Organization.class, Integer.parseInt(orgid)); |
|---|
| 239 | 257 | if (org == null) { |
|---|
| 240 | | - log.error("Organization with id {} can not be deleted, It was not found in DB", orgid); |
|---|
| 258 | + LOG.error("Organization with id {} can not be deleted, It was not found in DB", orgid); |
|---|
| 241 | 259 | return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "Organization was not found, ID: " + orgid).build(); |
|---|
| 242 | 260 | } |
|---|
| 243 | 261 | if (org.getChildOrganizations() != null && org.getChildOrganizations().size() > 0) { |
|---|
| 244 | | - log.error("Organization has children and can not be deleted, ID: " + orgid); |
|---|
| 262 | + LOG.error("Organization has children and can not be deleted, ID: " + orgid); |
|---|
| 245 | 263 | return Response.status(Status.FORBIDDEN).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "Organization has children and can not be deleted, ID: " + orgid).build(); |
|---|
| 246 | 264 | } |
|---|
| 247 | 265 | |
|---|
| .. | .. |
|---|
| 5 | 5 | import java.util.List; |
|---|
| 6 | 6 | |
|---|
| 7 | 7 | import javax.annotation.security.RolesAllowed; |
|---|
| 8 | +import javax.crypto.SealedObject; |
|---|
| 8 | 9 | import javax.inject.Inject; |
|---|
| 9 | 10 | import javax.inject.Provider; |
|---|
| 10 | 11 | import javax.persistence.EntityManager; |
|---|
| .. | .. |
|---|
| 24 | 25 | |
|---|
| 25 | 26 | import net.curisit.integrity.commons.Utils; |
|---|
| 26 | 27 | import net.curisit.securis.DefaultExceptionHandler; |
|---|
| 28 | +import net.curisit.securis.SeCurisException; |
|---|
| 27 | 29 | import net.curisit.securis.db.LicenseType; |
|---|
| 28 | 30 | import net.curisit.securis.db.Organization; |
|---|
| 29 | 31 | import net.curisit.securis.db.Pack; |
|---|
| .. | .. |
|---|
| 45 | 47 | @Path("/pack") |
|---|
| 46 | 48 | public class PackResource { |
|---|
| 47 | 49 | |
|---|
| 48 | | - private static final Logger log = LogManager.getLogger(PackResource.class); |
|---|
| 50 | + private static final Logger LOG = LogManager.getLogger(PackResource.class); |
|---|
| 49 | 51 | |
|---|
| 50 | 52 | @Inject |
|---|
| 51 | 53 | TokenHelper tokenHelper; |
|---|
| .. | .. |
|---|
| 66 | 68 | @Produces( |
|---|
| 67 | 69 | { MediaType.APPLICATION_JSON }) |
|---|
| 68 | 70 | public Response index(@Context BasicSecurityContext bsc) { |
|---|
| 69 | | - log.info("Getting packs list "); |
|---|
| 71 | + LOG.info("Getting packs list "); |
|---|
| 70 | 72 | |
|---|
| 71 | 73 | EntityManager em = emProvider.get(); |
|---|
| 72 | 74 | // TypedQuery<Pack> q = em.createNamedQuery("list-packs-by-orgs", Pack.class); |
|---|
| 73 | 75 | |
|---|
| 74 | 76 | TypedQuery<Pack> q; |
|---|
| 75 | 77 | if (bsc.isUserInRole(BasicSecurityContext.ROL_ADMIN)) { |
|---|
| 76 | | - log.info("Getting all packs for user: " + bsc.getUserPrincipal()); |
|---|
| 78 | + LOG.info("Getting all packs for user: " + bsc.getUserPrincipal()); |
|---|
| 77 | 79 | q = em.createNamedQuery("list-packs", Pack.class); |
|---|
| 78 | 80 | } else { |
|---|
| 79 | 81 | q = em.createNamedQuery("list-packs-by-orgs", Pack.class); |
|---|
| .. | .. |
|---|
| 88 | 90 | } |
|---|
| 89 | 91 | |
|---|
| 90 | 92 | private Response generateErrorUnathorizedAccess(Pack pack, Principal user) { |
|---|
| 91 | | - log.error("Pack with id {} not accesible by user {}", pack, user); |
|---|
| 93 | + LOG.error("Pack with id {} not accesible by user {}", pack, user); |
|---|
| 92 | 94 | return Response.status(Status.UNAUTHORIZED).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "Unathorized access to pack").build(); |
|---|
| 93 | 95 | } |
|---|
| 94 | 96 | |
|---|
| .. | .. |
|---|
| 102 | 104 | @Produces( |
|---|
| 103 | 105 | { MediaType.APPLICATION_JSON }) |
|---|
| 104 | 106 | public Response get(@PathParam("packId") Integer packId, @Context BasicSecurityContext bsc) { |
|---|
| 105 | | - log.info("Getting pack data for id: {}: ", packId); |
|---|
| 107 | + LOG.info("Getting pack data for id: {}: ", packId); |
|---|
| 106 | 108 | if (packId == null || packId.equals("")) { |
|---|
| 107 | | - log.error("Pack ID is mandatory"); |
|---|
| 109 | + LOG.error("Pack ID is mandatory"); |
|---|
| 108 | 110 | return Response.status(Status.NOT_FOUND).build(); |
|---|
| 109 | 111 | } |
|---|
| 110 | 112 | |
|---|
| 111 | 113 | EntityManager em = emProvider.get(); |
|---|
| 112 | 114 | Pack pack = em.find(Pack.class, packId); |
|---|
| 113 | 115 | if (pack == null) { |
|---|
| 114 | | - log.error("Pack with id {} not found in DB", packId); |
|---|
| 116 | + LOG.error("Pack with id {} not found in DB", packId); |
|---|
| 115 | 117 | return Response.status(Status.NOT_FOUND).build(); |
|---|
| 116 | 118 | } |
|---|
| 117 | 119 | if (bsc.isUserInRole(BasicSecurityContext.ROL_ADVANCE)) { |
|---|
| .. | .. |
|---|
| 131 | 133 | { MediaType.APPLICATION_JSON }) |
|---|
| 132 | 134 | @Transactional |
|---|
| 133 | 135 | public Response create(Pack pack, @Context BasicSecurityContext bsc) { |
|---|
| 134 | | - log.info("Creating new pack"); |
|---|
| 136 | + LOG.info("Creating new pack"); |
|---|
| 135 | 137 | EntityManager em = emProvider.get(); |
|---|
| 136 | 138 | |
|---|
| 137 | | - Organization org = null; |
|---|
| 138 | | - if (pack.getOrgId() != null) { |
|---|
| 139 | | - org = em.find(Organization.class, pack.getOrgId()); |
|---|
| 140 | | - if (org == null) { |
|---|
| 141 | | - log.error("Organization pack with id {} not found in DB", pack.getOrgId()); |
|---|
| 142 | | - return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "Pack organization not found with ID: " + pack.getOrgId()).build(); |
|---|
| 143 | | - } |
|---|
| 139 | + try { |
|---|
| 140 | + setPackOrganization(pack, pack.getOrgId(), em); |
|---|
| 141 | + } catch (SeCurisException e) { |
|---|
| 142 | + return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, e.getMessage()).build(); |
|---|
| 144 | 143 | } |
|---|
| 145 | | - LicenseType lt = null; |
|---|
| 146 | | - if (pack.getLicTypeId() != null) { |
|---|
| 147 | | - lt = em.find(LicenseType.class, pack.getLicTypeId()); |
|---|
| 148 | | - if (lt == null) { |
|---|
| 149 | | - log.error("Pack license type with id {} not found in DB", pack.getLicTypeId()); |
|---|
| 150 | | - return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "Pack license type not found with ID: " + pack.getLicTypeId()).build(); |
|---|
| 151 | | - } |
|---|
| 144 | + |
|---|
| 145 | + try { |
|---|
| 146 | + setPackLicenseType(pack, pack.getLicTypeId(), em); |
|---|
| 147 | + } catch (SeCurisException e) { |
|---|
| 148 | + return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, e.getMessage()).build(); |
|---|
| 152 | 149 | } |
|---|
| 153 | 150 | |
|---|
| 154 | 151 | User user = em.find(User.class, bsc.getUserPrincipal().getName()); |
|---|
| 155 | 152 | |
|---|
| 156 | 153 | pack.setCreatedBy(user); |
|---|
| 157 | | - pack.setLicenseType(lt); |
|---|
| 158 | | - pack.setOrganization(org); |
|---|
| 159 | 154 | pack.setCreationTimestamp(new Date()); |
|---|
| 160 | 155 | em.persist(pack); |
|---|
| 161 | 156 | |
|---|
| 162 | 157 | return Response.ok(pack).build(); |
|---|
| 158 | + } |
|---|
| 159 | + |
|---|
| 160 | + private void setPackLicenseType(Pack pack, Integer licTypeId, EntityManager em) throws SeCurisException { |
|---|
| 161 | + LicenseType lt = null; |
|---|
| 162 | + if (licTypeId != null) { |
|---|
| 163 | + lt = em.find(LicenseType.class, pack.getLicTypeId()); |
|---|
| 164 | + if (lt == null) { |
|---|
| 165 | + LOG.error("Pack license type with id {} not found in DB", licTypeId); |
|---|
| 166 | + throw new SeCurisException("Pack license type not found with ID: " + licTypeId); |
|---|
| 167 | +// return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "Pack license type not found with ID: " + pack.getLicTypeId()).build(); |
|---|
| 168 | + } |
|---|
| 169 | + } |
|---|
| 170 | + pack.setLicenseType(lt); |
|---|
| 163 | 171 | } |
|---|
| 164 | 172 | |
|---|
| 165 | 173 | @PUT |
|---|
| .. | .. |
|---|
| 172 | 180 | @Produces( |
|---|
| 173 | 181 | { MediaType.APPLICATION_JSON }) |
|---|
| 174 | 182 | public Response modify(Pack pack, @PathParam("packId") Integer packId) { |
|---|
| 175 | | - log.info("Modifying pack with id: {}", packId); |
|---|
| 183 | + LOG.info("Modifying pack with id: {}", packId); |
|---|
| 176 | 184 | EntityManager em = emProvider.get(); |
|---|
| 177 | 185 | Pack currentPack = em.find(Pack.class, packId); |
|---|
| 178 | 186 | |
|---|
| 179 | | - Organization org = null; |
|---|
| 180 | | - if (pack.getOrgId() != null) { |
|---|
| 181 | | - org = em.find(Organization.class, pack.getOrgId()); |
|---|
| 182 | | - if (org == null) { |
|---|
| 183 | | - log.error("Organization pack with id {} not found in DB", pack.getOrgId()); |
|---|
| 184 | | - return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "Pack organization not found with ID: " + pack.getOrgId()).build(); |
|---|
| 185 | | - } |
|---|
| 187 | + try { |
|---|
| 188 | + setPackOrganization(currentPack, pack.getOrgId(), em); |
|---|
| 189 | + } catch (SeCurisException e) { |
|---|
| 190 | + return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, e.getMessage()).build(); |
|---|
| 186 | 191 | } |
|---|
| 187 | | - LicenseType lt = null; |
|---|
| 188 | | - if (pack.getLicTypeId() != null) { |
|---|
| 189 | | - lt = em.find(LicenseType.class, pack.getLicTypeId()); |
|---|
| 190 | | - if (lt == null) { |
|---|
| 191 | | - log.error("Pack license type with id {} not found in DB", pack.getLicTypeId()); |
|---|
| 192 | | - return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "Pack license type not found with ID: " + pack.getLicTypeId()).build(); |
|---|
| 193 | | - } |
|---|
| 192 | + |
|---|
| 193 | + try { |
|---|
| 194 | + setPackLicenseType(currentPack, pack.getLicTypeId(), em); |
|---|
| 195 | + } catch (SeCurisException e) { |
|---|
| 196 | + return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, e.getMessage()).build(); |
|---|
| 194 | 197 | } |
|---|
| 198 | + |
|---|
| 195 | 199 | currentPack.setLicensePreactivation(pack.isLicensePreactivation()); |
|---|
| 196 | | - currentPack.setLicenseType(lt); |
|---|
| 197 | | - currentPack.setOrganization(org); |
|---|
| 198 | 200 | currentPack.setCode(pack.getCode()); |
|---|
| 199 | 201 | currentPack.setComments(pack.getComments()); |
|---|
| 200 | 202 | currentPack.setNumLicenses(pack.getNumLicenses()); |
|---|
| .. | .. |
|---|
| 202 | 204 | em.persist(currentPack); |
|---|
| 203 | 205 | |
|---|
| 204 | 206 | return Response.ok(pack).build(); |
|---|
| 207 | + } |
|---|
| 208 | + |
|---|
| 209 | + private void setPackOrganization(Pack currentPack, Integer orgId, EntityManager em) throws SeCurisException { |
|---|
| 210 | + Organization org = null; |
|---|
| 211 | + if (orgId != null) { |
|---|
| 212 | + org = em.find(Organization.class, orgId); |
|---|
| 213 | + if (org == null) { |
|---|
| 214 | + LOG.error("Organization pack with id {} not found in DB", orgId); |
|---|
| 215 | + throw new SeCurisException("Pack organization not found with ID: " + orgId); |
|---|
| 216 | + } |
|---|
| 217 | + } |
|---|
| 218 | + currentPack.setOrganization(org); |
|---|
| 205 | 219 | } |
|---|
| 206 | 220 | |
|---|
| 207 | 221 | @DELETE |
|---|
| .. | .. |
|---|
| 212 | 226 | @Produces( |
|---|
| 213 | 227 | { MediaType.APPLICATION_JSON }) |
|---|
| 214 | 228 | public Response delete(@PathParam("packId") String packId) { |
|---|
| 215 | | - log.info("Deleting pack with id: {}", packId); |
|---|
| 229 | + LOG.info("Deleting pack with id: {}", packId); |
|---|
| 216 | 230 | EntityManager em = emProvider.get(); |
|---|
| 217 | 231 | Pack org = em.find(Pack.class, Integer.parseInt(packId)); |
|---|
| 218 | 232 | if (org == null) { |
|---|
| 219 | | - log.error("Pack with id {} can not be deleted, It was not found in DB", packId); |
|---|
| 233 | + LOG.error("Pack with id {} can not be deleted, It was not found in DB", packId); |
|---|
| 220 | 234 | return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "Pack was not found, ID: " + packId).build(); |
|---|
| 221 | 235 | } |
|---|
| 222 | 236 | |
|---|
| .. | .. |
|---|
| 28 | 28 | |
|---|
| 29 | 29 | import net.curisit.integrity.commons.Utils; |
|---|
| 30 | 30 | import net.curisit.securis.DefaultExceptionHandler; |
|---|
| 31 | +import net.curisit.securis.SeCurisException; |
|---|
| 31 | 32 | import net.curisit.securis.db.Organization; |
|---|
| 32 | 33 | import net.curisit.securis.db.User; |
|---|
| 33 | 34 | import net.curisit.securis.utils.TokenHelper; |
|---|
| .. | .. |
|---|
| 52 | 53 | Provider<EntityManager> emProvider; |
|---|
| 53 | 54 | |
|---|
| 54 | 55 | // private LicenseHelper licenseHelper = InjectorFactory.getInjector().getInstance(LicenseHelper.class); |
|---|
| 55 | | - private static final Logger log = LogManager.getLogger(UserResource.class); |
|---|
| 56 | + private static final Logger LOG = LogManager.getLogger(UserResource.class); |
|---|
| 56 | 57 | |
|---|
| 57 | 58 | public UserResource() { |
|---|
| 58 | 59 | } |
|---|
| .. | .. |
|---|
| 66 | 67 | @Produces( |
|---|
| 67 | 68 | { MediaType.APPLICATION_JSON }) |
|---|
| 68 | 69 | public Response index() { |
|---|
| 69 | | - log.info("Getting users list "); |
|---|
| 70 | + LOG.info("Getting users list "); |
|---|
| 70 | 71 | |
|---|
| 71 | 72 | EntityManager em = emProvider.get(); |
|---|
| 72 | 73 | TypedQuery<User> q = em.createNamedQuery("list-users", User.class); |
|---|
| .. | .. |
|---|
| 85 | 86 | @Produces( |
|---|
| 86 | 87 | { MediaType.APPLICATION_JSON }) |
|---|
| 87 | 88 | public Response get(@PathParam("uid") String uid, @HeaderParam(TokenHelper.TOKEN_HEADER_PÀRAM) String token) { |
|---|
| 88 | | - log.info("Getting user data for id: {}: ", uid); |
|---|
| 89 | + LOG.info("Getting user data for id: {}: ", uid); |
|---|
| 89 | 90 | if (uid == null || uid.equals("")) { |
|---|
| 90 | | - log.error("User ID is mandatory"); |
|---|
| 91 | + LOG.error("User ID is mandatory"); |
|---|
| 91 | 92 | return Response.status(Status.NOT_FOUND).build(); |
|---|
| 92 | 93 | } |
|---|
| 93 | 94 | |
|---|
| 94 | 95 | EntityManager em = emProvider.get(); |
|---|
| 95 | 96 | User lt = em.find(User.class, uid); |
|---|
| 96 | 97 | if (lt == null) { |
|---|
| 97 | | - log.error("User with id {} not found in DB", uid); |
|---|
| 98 | + LOG.error("User with id {} not found in DB", uid); |
|---|
| 98 | 99 | return Response.status(Status.NOT_FOUND).build(); |
|---|
| 99 | 100 | } |
|---|
| 100 | 101 | return Response.ok(lt).build(); |
|---|
| .. | .. |
|---|
| 107 | 108 | { MediaType.APPLICATION_JSON }) |
|---|
| 108 | 109 | @Transactional |
|---|
| 109 | 110 | public Response create(User user, @HeaderParam(TokenHelper.TOKEN_HEADER_PÀRAM) String token) { |
|---|
| 110 | | - log.info("Creating new user"); |
|---|
| 111 | + LOG.info("Creating new user"); |
|---|
| 111 | 112 | EntityManager em = emProvider.get(); |
|---|
| 112 | 113 | User currentUser = em.find(User.class, user.getUsername()); |
|---|
| 113 | 114 | if (currentUser != null) { |
|---|
| 114 | | - log.info("User with id {} was found in DB, we'll try to modify it", user.getUsername()); |
|---|
| 115 | + LOG.info("User with id {} was found in DB, we'll try to modify it", user.getUsername()); |
|---|
| 115 | 116 | return modify(user, user.getUsername(), token); |
|---|
| 116 | 117 | } |
|---|
| 117 | | - |
|---|
| 118 | | - Set<Organization> orgs = null; |
|---|
| 119 | | - Set<Integer> orgsIds = user.getOrgsIds(); |
|---|
| 120 | | - if (orgsIds != null && orgsIds.size() > 0) { |
|---|
| 121 | | - orgs = new HashSet<>(); |
|---|
| 122 | | - for (Integer orgId : orgsIds) { |
|---|
| 123 | | - Organization o = em.find(Organization.class, orgId); |
|---|
| 124 | | - if (o == null) { |
|---|
| 125 | | - log.error("User organization with id {} not found in DB", orgId); |
|---|
| 126 | | - return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "User's organization not found with ID: " + orgId).build(); |
|---|
| 127 | | - } |
|---|
| 128 | | - orgs.add(o); |
|---|
| 129 | | - } |
|---|
| 118 | + |
|---|
| 119 | + try { |
|---|
| 120 | + this.setUserOrg(user, user.getOrgsIds(), em); |
|---|
| 121 | + } catch (SeCurisException e) { |
|---|
| 122 | + return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, e.getMessage()).build(); |
|---|
| 130 | 123 | } |
|---|
| 131 | | - |
|---|
| 132 | | - user.setOrganizations(orgs); |
|---|
| 133 | 124 | user.setModificationTimestamp(new Date()); |
|---|
| 134 | 125 | user.setLastLogin(null); |
|---|
| 135 | 126 | user.setCreationTimestamp(new Date()); |
|---|
| 136 | 127 | em.persist(user); |
|---|
| 137 | 128 | |
|---|
| 138 | 129 | return Response.ok(user).build(); |
|---|
| 130 | + } |
|---|
| 131 | + |
|---|
| 132 | + private void setUserOrg(User user, Set<Integer> orgsIds, EntityManager em) throws SeCurisException { |
|---|
| 133 | + Set<Organization> orgs = null; |
|---|
| 134 | + if (orgsIds != null && orgsIds.size() > 0) { |
|---|
| 135 | + orgs = new HashSet<>(); |
|---|
| 136 | + for (Integer orgId : orgsIds) { |
|---|
| 137 | + Organization o = em.find(Organization.class, orgId); |
|---|
| 138 | + if (o == null) { |
|---|
| 139 | + LOG.error("User organization with id {} not found in DB", orgId); |
|---|
| 140 | + throw new SeCurisException("User's organization not found with ID: " + orgId); |
|---|
| 141 | + } |
|---|
| 142 | + orgs.add(o); |
|---|
| 143 | + } |
|---|
| 144 | + } |
|---|
| 145 | + |
|---|
| 146 | + user.setOrganizations(orgs); |
|---|
| 147 | + |
|---|
| 139 | 148 | } |
|---|
| 140 | 149 | |
|---|
| 141 | 150 | @PUT |
|---|
| .. | .. |
|---|
| 146 | 155 | @Produces( |
|---|
| 147 | 156 | { MediaType.APPLICATION_JSON }) |
|---|
| 148 | 157 | public Response modify(User user, @PathParam("uid") String uid, @HeaderParam(TokenHelper.TOKEN_HEADER_PÀRAM) String token) { |
|---|
| 149 | | - log.info("Modifying user with id: {}", uid); |
|---|
| 158 | + LOG.info("Modifying user with id: {}", uid); |
|---|
| 150 | 159 | EntityManager em = emProvider.get(); |
|---|
| 151 | 160 | User currentUser = em.find(User.class, uid); |
|---|
| 152 | 161 | if (currentUser == null) { |
|---|
| 153 | | - log.info("User with id {} not found in DB, we'll try to create it", uid); |
|---|
| 162 | + LOG.info("User with id {} not found in DB, we'll try to create it", uid); |
|---|
| 154 | 163 | return create(user, token); |
|---|
| 155 | 164 | } |
|---|
| 156 | 165 | |
|---|
| 157 | | - Set<Organization> orgs = null; |
|---|
| 158 | | - Set<Integer> orgsIds = user.getOrgsIds(); |
|---|
| 159 | | - if (orgsIds != null && orgsIds.size() > 0) { |
|---|
| 160 | | - orgs = new HashSet<>(); |
|---|
| 161 | | - for (Integer orgId : orgsIds) { |
|---|
| 162 | | - Organization o = em.find(Organization.class, orgId); |
|---|
| 163 | | - if (o == null) { |
|---|
| 164 | | - log.error("User organization with id {} not found in DB", orgId); |
|---|
| 165 | | - return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "User's user not found with ID: " + orgId).build(); |
|---|
| 166 | | - } |
|---|
| 167 | | - orgs.add(o); |
|---|
| 168 | | - } |
|---|
| 166 | + try { |
|---|
| 167 | + this.setUserOrg(currentUser, user.getOrgsIds(), em); |
|---|
| 168 | + } catch (SeCurisException e) { |
|---|
| 169 | + return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, e.getMessage()).build(); |
|---|
| 169 | 170 | } |
|---|
| 170 | | - |
|---|
| 171 | | - currentUser.setOrganizations(orgs); |
|---|
| 172 | 171 | currentUser.setFirstName(user.getFirstName()); |
|---|
| 173 | 172 | currentUser.setLastName(user.getLastName()); |
|---|
| 174 | 173 | currentUser.setRoles(user.getRoles()); |
|---|
| .. | .. |
|---|
| 188 | 187 | @Produces( |
|---|
| 189 | 188 | { MediaType.APPLICATION_JSON }) |
|---|
| 190 | 189 | public Response delete(@PathParam("uid") String uid, @Context HttpServletRequest request) { |
|---|
| 191 | | - log.info("Deleting app with id: {}", uid); |
|---|
| 190 | + LOG.info("Deleting app with id: {}", uid); |
|---|
| 192 | 191 | EntityManager em = emProvider.get(); |
|---|
| 193 | 192 | User app = em.find(User.class, uid); |
|---|
| 194 | 193 | if (app == null) { |
|---|
| 195 | | - log.error("User with id {} can not be deleted, It was not found in DB", uid); |
|---|
| 194 | + LOG.error("User with id {} can not be deleted, It was not found in DB", uid); |
|---|
| 196 | 195 | return Response.status(Status.NOT_FOUND).build(); |
|---|
| 197 | 196 | } |
|---|
| 198 | 197 | |
|---|
| .. | .. |
|---|
| 205 | 204 | @Produces( |
|---|
| 206 | 205 | { MediaType.APPLICATION_JSON }) |
|---|
| 207 | 206 | public Response login(@FormParam("username") String user, @FormParam("password") String password, @Context HttpServletRequest request) { |
|---|
| 208 | | - log.info("index session: " + request.getSession()); |
|---|
| 209 | | - log.info("user: {}, pass: {}", user, password); |
|---|
| 210 | | - log.info("is user in role: {} == {} ? ", "advance", request.isUserInRole("advance")); |
|---|
| 207 | + LOG.info("index session: " + request.getSession()); |
|---|
| 208 | + LOG.info("user: {}, pass: {}", user, password); |
|---|
| 209 | + LOG.info("is user in role: {} == {} ? ", "advance", request.isUserInRole("advance")); |
|---|
| 211 | 210 | |
|---|
| 212 | 211 | if ("no".equals(password)) |
|---|
| 213 | 212 | return Response.status(Status.UNAUTHORIZED).build(); |
|---|
| .. | .. |
|---|
| 233 | 232 | if (token == null) |
|---|
| 234 | 233 | return Response.status(Status.FORBIDDEN).build(); |
|---|
| 235 | 234 | |
|---|
| 236 | | - log.info("Token : " + token); |
|---|
| 235 | + LOG.info("Token : " + token); |
|---|
| 237 | 236 | String user = tokenHelper.extractUserFromToken(token); |
|---|
| 238 | | - log.info("Token user: " + user); |
|---|
| 237 | + LOG.info("Token user: " + user); |
|---|
| 239 | 238 | Date date = tokenHelper.extractDateCreationFromToken(token); |
|---|
| 240 | | - log.info("Token date: " + date); |
|---|
| 239 | + LOG.info("Token date: " + date); |
|---|
| 241 | 240 | boolean valid = tokenHelper.isTokenValid(token); |
|---|
| 242 | 241 | |
|---|
| 243 | | - log.info("Is Token valid: " + valid); |
|---|
| 242 | + LOG.info("Is Token valid: " + valid); |
|---|
| 244 | 243 | |
|---|
| 245 | 244 | return Response.ok(Utils.createMap("valid", true, "user", user, "date", date, "token", token)).build(); |
|---|
| 246 | 245 | } |
|---|
| .. | .. |
|---|
| 20 | 20 | @Singleton |
|---|
| 21 | 21 | public class CacheTTL { |
|---|
| 22 | 22 | |
|---|
| 23 | | - private static final Logger log = LogManager.getLogger(CacheTTL.class); |
|---|
| 23 | + private static final Logger LOG = LogManager.getLogger(CacheTTL.class); |
|---|
| 24 | 24 | |
|---|
| 25 | 25 | /** |
|---|
| 26 | 26 | * Period before token expires, set in seconds. |
|---|
| .. | .. |
|---|
| 42 | 42 | // We check for expired object every 60 seconds |
|---|
| 43 | 43 | Thread.sleep(60 * 1000); |
|---|
| 44 | 44 | } catch (InterruptedException e) { |
|---|
| 45 | | - log.error("Exiting from Cache Thread"); |
|---|
| 45 | + LOG.error("Exiting from Cache Thread"); |
|---|
| 46 | 46 | data.clear(); |
|---|
| 47 | 47 | return; |
|---|
| 48 | 48 | } |
|---|
| 49 | | - // log.info("Cheking expired objects " + new Date()); |
|---|
| 49 | + // LOG.info("Cheking expired objects " + new Date()); |
|---|
| 50 | 50 | Date now = new Date(); |
|---|
| 51 | 51 | List<String> keysToRemove = new ArrayList<>(); |
|---|
| 52 | 52 | for (String key : CacheTTL.this.data.keySet()) { |
|---|
| .. | .. |
|---|
| 20 | 20 | @Singleton |
|---|
| 21 | 21 | public class TokenHelper { |
|---|
| 22 | 22 | |
|---|
| 23 | | - private static final Logger log = LogManager.getLogger(TokenHelper.class); |
|---|
| 23 | + private static final Logger LOG = LogManager.getLogger(TokenHelper.class); |
|---|
| 24 | 24 | |
|---|
| 25 | 25 | /** |
|---|
| 26 | 26 | * Period before token expires, set in hours. |
|---|
| .. | .. |
|---|
| 52 | 52 | sb.append(Utils.toIsoFormat(date)); |
|---|
| 53 | 53 | return Base64.encodeBytes(sb.toString().getBytes("utf-8")); |
|---|
| 54 | 54 | } catch (NoSuchAlgorithmException e) { |
|---|
| 55 | | - log.error("Error generating SHA-256 hash", e); |
|---|
| 55 | + LOG.error("Error generating SHA-256 hash", e); |
|---|
| 56 | 56 | } catch (UnsupportedEncodingException e) { |
|---|
| 57 | | - log.error("Error generating SHA-256 hash", e); |
|---|
| 57 | + LOG.error("Error generating SHA-256 hash", e); |
|---|
| 58 | 58 | } |
|---|
| 59 | 59 | return null; |
|---|
| 60 | 60 | |
|---|
| .. | .. |
|---|
| 92 | 92 | String newSecret = generateSecret(user, date); |
|---|
| 93 | 93 | return newSecret.equals(secret); |
|---|
| 94 | 94 | } catch (IOException e) { |
|---|
| 95 | | - log.error("Error decoding Base64 token", e); |
|---|
| 95 | + LOG.error("Error decoding Base64 token", e); |
|---|
| 96 | 96 | } catch (NoSuchAlgorithmException e) { |
|---|
| 97 | | - log.error("Error generation secret to compare with", e); |
|---|
| 97 | + LOG.error("Error generation secret to compare with", e); |
|---|
| 98 | 98 | } |
|---|
| 99 | 99 | return false; |
|---|
| 100 | 100 | } |
|---|
| .. | .. |
|---|
| 110 | 110 | String user = parts[1]; |
|---|
| 111 | 111 | return user; |
|---|
| 112 | 112 | } catch (IOException e) { |
|---|
| 113 | | - log.error("Error decoding Base64 token", e); |
|---|
| 113 | + LOG.error("Error decoding Base64 token", e); |
|---|
| 114 | 114 | } |
|---|
| 115 | 115 | return null; |
|---|
| 116 | 116 | } |
|---|
| .. | .. |
|---|
| 124 | 124 | Date date = Utils.toDateFromIso(parts[2]); |
|---|
| 125 | 125 | return date; |
|---|
| 126 | 126 | } catch (IOException e) { |
|---|
| 127 | | - log.error("Error decoding Base64 token", e); |
|---|
| 127 | + LOG.error("Error decoding Base64 token", e); |
|---|
| 128 | 128 | } |
|---|
| 129 | 129 | return null; |
|---|
| 130 | 130 | } |
|---|
| .. | .. |
|---|
| 33 | 33 | */ |
|---|
| 34 | 34 | public class LicenseGenerator { |
|---|
| 35 | 35 | |
|---|
| 36 | | - private static final Logger log = LogManager.getLogger(LicenseGenerator.class); |
|---|
| 36 | + private static final Logger LOG = LogManager.getLogger(LicenseGenerator.class); |
|---|
| 37 | 37 | |
|---|
| 38 | 38 | private static LicenseGenerator singleton = new LicenseGenerator(); |
|---|
| 39 | 39 | |
|---|
| .. | .. |
|---|
| 58 | 58 | * @throws SeCurisException |
|---|
| 59 | 59 | */ |
|---|
| 60 | 60 | public LicenseBean generateLicense(RequestBean req, Map<String, Object> metadata, Date expirationDate, String licenseType, String licenseCode) throws SeCurisException { |
|---|
| 61 | | - log.info(MessageFormat.format("Generating license: MAC: {0}, Customer code: {1}, AppCode: {2}", req.getMacAddresses(), req.getCustomerCode(), req.getAppCode())); |
|---|
| 61 | + LOG.info(MessageFormat.format("Generating license: MAC: {0}, Customer code: {1}, AppCode: {2}", req.getMacAddresses(), req.getCustomerCode(), req.getAppCode())); |
|---|
| 62 | 62 | LicenseBean license = new LicenseBean(req); |
|---|
| 63 | 63 | license.setLicenseType(licenseType); |
|---|
| 64 | 64 | license.setLicenseCode(licenseCode); |
|---|
| .. | .. |
|---|
| 83 | 83 | json = JsonUtils.toJSON(signedLic, true).getBytes("utf-8"); |
|---|
| 84 | 84 | Files.write(Paths.get(file.toURI()), json, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING); |
|---|
| 85 | 85 | } catch (UnsupportedEncodingException e) { |
|---|
| 86 | | - log.error("Error creating json doc from license: " + license, e); |
|---|
| 86 | + LOG.error("Error creating json doc from license: " + license, e); |
|---|
| 87 | 87 | throw new SeCurisException("Error creating json doc from license: " + license, e); |
|---|
| 88 | 88 | } catch (IOException e) { |
|---|
| 89 | | - log.error("Error creating license file: " + file, e); |
|---|
| 89 | + LOG.error("Error creating license file: " + file, e); |
|---|
| 90 | 90 | throw new SeCurisException("Error creating json doc from license: " + license, e); |
|---|
| 91 | 91 | } |
|---|
| 92 | 92 | |
|---|
| 93 | | - log.info("License saved in {}", file); |
|---|
| 93 | + LOG.info("License saved in {}", file); |
|---|
| 94 | 94 | |
|---|
| 95 | 95 | } |
|---|
| 96 | 96 | |
|---|
| .. | .. |
|---|
| 119 | 119 | licBean.setSignature(Base64.encodeBase64String(signatureData)); |
|---|
| 120 | 120 | return licBean.getSignature(); |
|---|
| 121 | 121 | } catch (NoSuchAlgorithmException e) { |
|---|
| 122 | | - log.error("Error signing license for " + licBean, e); |
|---|
| 122 | + LOG.error("Error signing license for " + licBean, e); |
|---|
| 123 | 123 | } catch (InvalidKeyException e) { |
|---|
| 124 | | - log.error("Error signing license for " + licBean, e); |
|---|
| 124 | + LOG.error("Error signing license for " + licBean, e); |
|---|
| 125 | 125 | } catch (InvalidKeySpecException e) { |
|---|
| 126 | | - log.error("Error signing license for " + licBean, e); |
|---|
| 126 | + LOG.error("Error signing license for " + licBean, e); |
|---|
| 127 | 127 | } catch (IOException e) { |
|---|
| 128 | | - log.error("Error signing license for " + licBean, e); |
|---|
| 128 | + LOG.error("Error signing license for " + licBean, e); |
|---|
| 129 | 129 | } catch (SignatureException e) { |
|---|
| 130 | | - log.error("Error signing license for " + licBean, e); |
|---|
| 130 | + LOG.error("Error signing license for " + licBean, e); |
|---|
| 131 | 131 | } |
|---|
| 132 | 132 | throw new SeCurisException("License could not be generated"); |
|---|
| 133 | 133 | } |
|---|
| .. | .. |
|---|
| 136 | 136 | |
|---|
| 137 | 137 | RequestBean req = ReqGenerator.getInstance().loadRequest(new File("/Users/cproberto/Documents/wsCurisIT/SeCurisClient/license.req")); |
|---|
| 138 | 138 | Map<String, Object> metadata = new TreeMap<>(); |
|---|
| 139 | | - metadata.put("maxUsers", 20); |
|---|
| 140 | | - metadata.put("maxSessionUsers", 2); |
|---|
| 139 | +// CurisData parameters: |
|---|
| 140 | +// metadata.put("maxUsers", 5); |
|---|
| 141 | +// metadata.put("maxSessionUsers", 100); |
|---|
| 142 | + // curisIntegrity |
|---|
| 143 | + metadata.put("maxUsers", 0); |
|---|
| 144 | + metadata.put("maxInstances", 0); |
|---|
| 145 | + metadata.put("timeThreshold", 0); |
|---|
| 146 | + metadata.put("datasetPrefix", "BP"); |
|---|
| 147 | + metadata.put("extendedMode", true); |
|---|
| 141 | 148 | Date expirationDate = new Date(new Date().getTime() + (1000L * 3600 * 24 * 365 * 10)); |
|---|
| 142 | | - LicenseBean lic = LicenseGenerator.getInstance().generateLicense(req, metadata, expirationDate, "CD-01", "LIC-CURISTEC-0001"); |
|---|
| 143 | | - LicenseGenerator.getInstance().save(lic, new File("/Users/cproberto/Desktop/YanFei.lic")); |
|---|
| 149 | + LicenseBean lic = LicenseGenerator.getInstance().generateLicense(req, metadata, expirationDate, "CI-01", "LIC-CURISTEC-0001"); |
|---|
| 150 | + LicenseGenerator.getInstance().save(lic, new File("/Users/cproberto/Desktop/AxelLicCI.lic")); |
|---|
| 144 | 151 | |
|---|
| 145 | 152 | System.out.print("License expires at: " + expirationDate.getTime()); |
|---|
| 146 | 153 | |
|---|