securis/pom.xml
.. .. @@ -1,100 +1,101 @@ 1 -<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">2 - <modelVersion>4.0.0</modelVersion>3 - <groupId>net.curisit</groupId>4 - <artifactId>securis-server</artifactId>5 - <version>0.0.9-SNAPSHOT</version>6 - <name>SeCuris</name>7 - <description>CurisTEC Server Licenses</description>8 - <dependencies>9 - <dependency>10 - <groupId>commons-lang</groupId>11 - <artifactId>commons-lang</artifactId>12 - <version>2.6</version>13 - </dependency>14 - <dependency>15 - <groupId>javax.servlet</groupId>16 - <artifactId>javax.servlet-api</artifactId>17 - <version>3.1.0</version>18 - <scope>provided</scope>19 - </dependency>20 - <dependency>21 - <groupId>org.jboss.resteasy</groupId>22 - <artifactId>resteasy-multipart-provider</artifactId>23 - <version>3.0.10.Final</version>24 - </dependency>25 - <dependency>26 - <groupId>org.jboss.resteasy</groupId>27 - <artifactId>resteasy-jaxrs</artifactId>28 - <version>3.0.10.Final</version>29 - </dependency>30 -<dependency>31 - <groupId>org.jboss.weld.servlet</groupId>32 - <artifactId>weld-servlet</artifactId>33 - <version>2.2.9.Final</version>34 -</dependency>35 -<dependency>36 - <groupId>org.jboss</groupId>37 - <artifactId>jandex</artifactId>38 - <version>1.2.2.Final</version>39 -</dependency>40 - <dependency>41 - <groupId>net.curisit</groupId>42 - <artifactId>commons-curis</artifactId>43 - <version>0.0.1-SNAPSHOT</version>44 - </dependency>45 - <dependency>46 - <groupId>org.hibernate</groupId>47 - <artifactId>hibernate-entitymanager</artifactId>48 - <version>4.3.6.Final</version>49 - </dependency>50 - <dependency>51 - <groupId>net.curisit</groupId>52 - <artifactId>securis-client</artifactId>53 - <version>1.0.4-SNAPSHOT</version>54 - </dependency>55 - <dependency>56 - <groupId>org.apache.httpcomponents</groupId>57 - <artifactId>httpclient</artifactId>58 - <version>4.4-beta1</version>59 - </dependency>60 - <dependency>61 - <groupId>org.apache.httpcomponents</groupId>62 - <artifactId>httpmime</artifactId>63 - <version>4.4-beta1</version>64 - </dependency>65 - <dependency>66 - <groupId>org.jboss.resteasy</groupId>67 - <artifactId>resteasy-jackson2-provider</artifactId>68 - <version>3.0.10.Final</version>69 - </dependency>70 - <dependency>71 - <groupId>org.apache.logging.log4j</groupId>72 - <artifactId>log4j-web</artifactId>73 - <version>2.1</version>74 - </dependency>75 - <dependency>76 - <groupId>javax.enterprise</groupId>77 - <artifactId>cdi-api</artifactId>78 - <version>1.2</version>79 - <scope>provided</scope>80 -</dependency>81 - <dependency>82 - <groupId>org.jboss.resteasy</groupId>83 - <artifactId>resteasy-cdi</artifactId>84 - <version>3.0.10.Final</version>85 - </dependency>86 - </dependencies>87 - <build>1 +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"2 + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">3 + <modelVersion>4.0.0</modelVersion>4 + <groupId>net.curisit</groupId>5 + <artifactId>securis-server</artifactId>6 + <version>1.0-SNAPSHOT</version>7 + <name>SeCuris</name>8 + <description>CurisTEC Server Licenses</description>9 + <dependencies>10 + <dependency>11 + <groupId>commons-lang</groupId>12 + <artifactId>commons-lang</artifactId>13 + <version>2.6</version>14 + </dependency>15 + <dependency>16 + <groupId>javax.servlet</groupId>17 + <artifactId>javax.servlet-api</artifactId>18 + <version>3.1.0</version>19 + <scope>provided</scope>20 + </dependency>21 + <dependency>22 + <groupId>org.jboss.resteasy</groupId>23 + <artifactId>resteasy-multipart-provider</artifactId>24 + <version>3.0.10.Final</version>25 + </dependency>26 + <dependency>27 + <groupId>org.jboss.resteasy</groupId>28 + <artifactId>resteasy-jaxrs</artifactId>29 + <version>3.0.10.Final</version>30 + </dependency>31 + <dependency>32 + <groupId>org.jboss.weld.servlet</groupId>33 + <artifactId>weld-servlet</artifactId>34 + <version>2.2.9.Final</version>35 + </dependency>36 + <dependency>37 + <groupId>org.jboss</groupId>38 + <artifactId>jandex</artifactId>39 + <version>1.2.2.Final</version>40 + </dependency>41 + <dependency>42 + <groupId>net.curisit</groupId>43 + <artifactId>commons-curis</artifactId>44 + <version>0.0.1-SNAPSHOT</version>45 + </dependency>46 + <dependency>47 + <groupId>org.hibernate</groupId>48 + <artifactId>hibernate-entitymanager</artifactId>49 + <version>4.3.6.Final</version>50 + </dependency>51 + <dependency>52 + <groupId>net.curisit</groupId>53 + <artifactId>securis-client</artifactId>54 + <version>1.0.5-SNAPSHOT</version>55 + </dependency>56 + <dependency>57 + <groupId>org.apache.httpcomponents</groupId>58 + <artifactId>httpclient</artifactId>59 + <version>4.4-beta1</version>60 + </dependency>61 + <dependency>62 + <groupId>org.apache.httpcomponents</groupId>63 + <artifactId>httpmime</artifactId>64 + <version>4.4-beta1</version>65 + </dependency>66 + <dependency>67 + <groupId>org.jboss.resteasy</groupId>68 + <artifactId>resteasy-jackson2-provider</artifactId>69 + <version>3.0.10.Final</version>70 + </dependency>71 + <dependency>72 + <groupId>org.apache.logging.log4j</groupId>73 + <artifactId>log4j-web</artifactId>74 + <version>2.1</version>75 + </dependency>76 + <dependency>77 + <groupId>javax.enterprise</groupId>78 + <artifactId>cdi-api</artifactId>79 + <version>1.2</version>80 + <scope>provided</scope>81 + </dependency>82 + <dependency>83 + <groupId>org.jboss.resteasy</groupId>84 + <artifactId>resteasy-cdi</artifactId>85 + <version>3.0.10.Final</version>86 + </dependency>87 + </dependencies>88 + <build>88 89 <plugins> 89 - <plugin>90 - <groupId>org.apache.maven.plugins</groupId>91 - <artifactId>maven-compiler-plugin</artifactId>92 - <configuration>93 - <source>1.8</source>94 - <target>1.8</target>95 - </configuration>96 - </plugin>97 - <plugin>90 + <plugin>91 + <groupId>org.apache.maven.plugins</groupId>92 + <artifactId>maven-compiler-plugin</artifactId>93 + <configuration>94 + <source>1.8</source>95 + <target>1.8</target>96 + </configuration>97 + </plugin>98 + <plugin>98 99 <artifactId>maven-assembly-plugin</artifactId> 99 100 <version>2.4</version> 100 101 <configuration> .. .. @@ -108,31 +109,31 @@ 108 109 <appendAssemblyId>false</appendAssemblyId> 109 110 </configuration> 110 111 </plugin> 111 - </plugins>112 + </plugins>112 113 </build> 113 - <repositories>114 - <repository>115 - <id>internal</id>116 - <name>CurisIT Repository</name>117 - <url>http://archiva.curisit.net/archiva/repository/internal</url>118 - </repository>119 - <repository>120 - <id>curistec</id>121 - <name>CurisTec Corporate Repository</name>122 - <url>http://archiva.curisit.net/archiva/repository/internal/</url>123 - <layout>default</layout>124 - </repository>125 - <repository>126 - <id>curistecSnapshot</id>127 - <name>CurisTec Snapshots</name>128 - <url>http://archiva.curisit.net/archiva/repository/snapshots/</url>129 - <layout>default</layout>130 - </repository>131 - <repository>132 - <id>project.local</id>133 - <name>project</name>134 - <url>file:/opt/repo</url>135 - </repository>136 - </repositories>137 -114 + <repositories>115 + <repository>116 + <id>internal</id>117 + <name>CurisIT Repository</name>118 + <url>http://archiva.curisit.net/archiva/repository/internal</url>119 + </repository>120 + <repository>121 + <id>curistec</id>122 + <name>CurisTec Corporate Repository</name>123 + <url>http://archiva.curisit.net/archiva/repository/internal/</url>124 + <layout>default</layout>125 + </repository>126 + <repository>127 + <id>curistecSnapshot</id>128 + <name>CurisTec Snapshots</name>129 + <url>http://archiva.curisit.net/archiva/repository/snapshots/</url>130 + <layout>default</layout>131 + </repository>132 + <repository>133 + <id>project.local</id>134 + <name>project</name>135 + <url>file:/opt/repo</url>136 + </repository>137 + </repositories>138 +138 139 </project> securis/src/main/java/net/curisit/securis/DefaultExceptionHandler.java
.. .. @@ -1,5 +1,6 @@ 1 1 package net.curisit.securis; 2 2 3 +import javax.persistence.EntityManager;3 4 import javax.servlet.http.HttpServletRequest; 4 5 import javax.ws.rs.ForbiddenException; 5 6 import javax.ws.rs.core.Context; .. .. @@ -32,23 +33,22 @@ 32 33 HttpServletRequest request; 33 34 @Context 34 35 SecurityContext bsc; 36 + @Context37 + EntityManager em;35 38 36 39 @Override 37 40 public Response toResponse(Exception e) { 41 + releaseEntityManager();38 42 if (e instanceof ForbiddenException) { 39 43 LOG.warn("Unauthorized access to {}, user: {}", request.getPathInfo(), bsc.getUserPrincipal()); 40 - return Response.status(Status.UNAUTHORIZED)41 - .header(ERROR_CODE_MESSAGE_HEADER, ErrorCodes.INVALID_CREDENTIALS)42 - .header(ERROR_MESSAGE_HEADER, "Unathorized access to the application")43 - .type(MediaType.APPLICATION_JSON).build();44 + return Response.status(Status.UNAUTHORIZED).header(ERROR_CODE_MESSAGE_HEADER, ErrorCodes.INVALID_CREDENTIALS)45 + .header(ERROR_MESSAGE_HEADER, "Unathorized access to the application").type(MediaType.APPLICATION_JSON).build();44 46 } 45 47 46 48 if (e instanceof SeCurisServiceException) { 47 49 LOG.warn("SeCurisServiceException accessing to {}, user: {}", request.getPathInfo(), bsc.getUserPrincipal()); 48 - return Response.status(DEFAULT_APP_ERROR_STATUS_CODE)49 - .header(ERROR_CODE_MESSAGE_HEADER, ((SeCurisServiceException) e).getStatus())50 - .header(ERROR_MESSAGE_HEADER, e.getMessage())51 - .type(MediaType.APPLICATION_JSON).build();50 + return Response.status(DEFAULT_APP_ERROR_STATUS_CODE).header(ERROR_CODE_MESSAGE_HEADER, ((SeCurisServiceException) e).getStatus())51 + .header(ERROR_MESSAGE_HEADER, e.getMessage()).type(MediaType.APPLICATION_JSON).build();52 52 } 53 53 54 54 LOG.error("Unexpected error accesing to '{}' by user: {}", request.getPathInfo(), bsc.getUserPrincipal()); .. .. @@ -56,4 +56,20 @@ 56 56 LOG.error("Request url: " + request.getRequestURL(), e); 57 57 return Response.serverError().header(ERROR_MESSAGE_HEADER, "Unexpected error: " + e.toString()).type(MediaType.APPLICATION_JSON).build(); 58 58 } 59 +60 + private void releaseEntityManager() {61 + if (em != null && em.isOpen()) {62 + LOG.debug("CLOSING EM: {}, trans: {}", em, em.isJoinedToTransaction());63 + try {64 + if (em.isJoinedToTransaction()) {65 + em.getTransaction().rollback();66 + LOG.info("ROLLBACK");67 + }68 + em.close();69 + } catch (Exception ex) {70 + ex.printStackTrace();71 + LOG.error("Error closing EM: {}, {}", em, ex);72 + }73 + }74 + }59 75 } securis/src/main/java/net/curisit/securis/RestServicesApplication.java
.. .. @@ -27,7 +27,6 @@ 27 27 @Override 28 28 public Set<Class<?>> getClasses() { 29 29 Set<Class<?>> classes = new HashSet<>(); 30 - // classes.add(LicenseServices.class);31 30 classes.add(UserResource.class); 32 31 classes.add(ApiResource.class); 33 32 classes.add(LicenseResource.class); .. .. @@ -37,9 +36,11 @@ 37 36 classes.add(LicenseTypeResource.class); 38 37 classes.add(PackResource.class); 39 38 classes.add(RequestsInterceptor.class); 39 + // classes.add(TransactionsInterceptor.class);40 + // classes.add(JpaJacksonProvider.class);41 + // classes.add(JacksonConfig.class);40 42 41 43 LOG.info("JAX-RS classes for services: {}", classes); 42 44 return classes; 43 45 } 44 -45 46 } securis/src/main/java/net/curisit/securis/db/Application.java
.. .. @@ -101,7 +101,6 @@ 101 101 102 102 @JsonProperty("metadata") 103 103 public Set<ApplicationMetadata> getApplicationMetadata() { 104 - LOG.info("Getting metadata from app: {}", metadata);105 104 return metadata; 106 105 } 107 106 securis/src/main/java/net/curisit/securis/db/ApplicationMetadata.java
.. .. @@ -20,6 +20,7 @@ 20 20 import com.fasterxml.jackson.annotation.JsonInclude; 21 21 import com.fasterxml.jackson.annotation.JsonInclude.Include; 22 22 import com.fasterxml.jackson.annotation.JsonProperty; 23 +import com.google.common.base.Objects;23 24 24 25 /** 25 26 * Entity implementation class for Entity: application_metadata .. .. @@ -102,13 +103,13 @@ 102 103 if (!(obj instanceof ApplicationMetadata)) 103 104 return false; 104 105 ApplicationMetadata other = (ApplicationMetadata) obj; 105 - return key.equals(other.key) && (application == null || application.equals(other.application));106 + return Objects.equal(key, other.key) && Objects.equal(application, other.application);106 107 } 107 108 108 109 @Override 109 110 public int hashCode() { 110 111 111 - return key.hashCode() + (application == null ? 0 : application.hashCode());112 + return (key == null ? 0 : key.hashCode()) + (application == null ? 0 : application.hashCode());112 113 } 113 114 114 115 @Override securis/src/main/java/net/curisit/securis/ioc/RequestsInterceptor.java
.. .. @@ -9,16 +9,17 @@ 9 9 import javax.inject.Inject; 10 10 import javax.persistence.EntityManager; 11 11 import javax.servlet.http.HttpServletRequest; 12 +import javax.servlet.http.HttpServletResponse;12 13 import javax.ws.rs.Priorities; 13 14 import javax.ws.rs.WebApplicationException; 14 15 import javax.ws.rs.container.ContainerRequestContext; 15 16 import javax.ws.rs.container.ContainerRequestFilter; 16 -import javax.ws.rs.container.ContainerResponseContext;17 -import javax.ws.rs.container.ContainerResponseFilter;18 17 import javax.ws.rs.core.Context; 19 18 import javax.ws.rs.core.Response; 20 19 import javax.ws.rs.core.Response.Status; 21 20 import javax.ws.rs.ext.Provider; 21 +import javax.ws.rs.ext.WriterInterceptor;22 +import javax.ws.rs.ext.WriterInterceptorContext;22 23 23 24 import net.curisit.securis.db.User; 24 25 import net.curisit.securis.security.BasicSecurityContext; .. .. @@ -37,8 +38,11 @@ 37 38 38 39 @Provider 39 40 @Priority(Priorities.AUTHENTICATION) 40 -public class RequestsInterceptor implements ContainerRequestFilter, ContainerResponseFilter {41 +public class RequestsInterceptor implements ContainerRequestFilter, WriterInterceptor {41 42 private static final Logger LOG = LogManager.getLogger(RequestsInterceptor.class); 43 +44 + @Context45 + private HttpServletResponse servletResponse;42 46 43 47 @Context 44 48 private HttpServletRequest servletRequest; .. .. @@ -58,6 +62,8 @@ 58 62 @Override 59 63 public void filter(ContainerRequestContext containerRequestContext) throws IOException { 60 64 EntityManager em = emProvider.getEntityManager(); 65 + LOG.info("GETTING EM: {}", em);66 +61 67 ResteasyProviderFactory.pushContext(EntityManager.class, em); 62 68 63 69 ResourceMethodInvoker methodInvoker = (ResourceMethodInvoker) containerRequestContext .. .. @@ -68,12 +74,11 @@ 68 74 69 75 boolean next = checkSecurableMethods(containerRequestContext, method); 70 76 if (next) { 71 - prepareTransaction(containerRequestContext, method);77 + prepareTransaction(containerRequestContext, method, em);72 78 } 73 79 } 74 80 75 - private void prepareTransaction(ContainerRequestContext containerRequestContext, Method method) {76 - EntityManager em = ResteasyProviderFactory.getContextData(EntityManager.class);81 + private void prepareTransaction(ContainerRequestContext containerRequestContext, Method method, EntityManager em) {77 82 78 83 if (method.isAnnotationPresent(EnsureTransaction.class)) { 79 84 LOG.debug("Beginning a new transaction"); .. .. @@ -155,17 +160,30 @@ 155 160 } 156 161 157 162 @Override 158 - public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext) throws IOException {163 + public void aroundWriteTo(WriterInterceptorContext context) throws IOException, WebApplicationException {164 + context.proceed();159 165 EntityManager em = ResteasyProviderFactory.getContextData(EntityManager.class); 160 -161 - if (em != null && em.getTransaction().isActive()) {162 - LOG.debug("Transaction is active, ready to finish it");163 - if (responseContext.getStatus() == Status.OK.getStatusCode()) {164 - em.getTransaction().commit();165 - LOG.debug("COMMIT");166 - } else {167 - em.getTransaction().rollback();168 - LOG.debug("ROLLBACK");166 + try {167 + if (em != null && em.getTransaction().isActive()) {168 + if (servletResponse.getStatus() == Status.OK.getStatusCode()) {169 + em.getTransaction().commit();170 + LOG.debug("COMMIT");171 + } else {172 + // This code is never executed if there is an error the173 + // filter chain is broken174 + em.getTransaction().rollback();175 + LOG.debug("ROLLBACK");176 + }177 + }178 + } finally {179 + if (em.isOpen()) {180 + LOG.info("CLOSING EM: {}, trans: {}", em, em.isJoinedToTransaction());181 + try {182 + em.close();183 + } catch (Exception ex) {184 + ex.printStackTrace();185 + LOG.error("Error closing EM: {}, {}", em, ex);186 + }169 187 } 170 188 } 171 189 } securis/src/main/java/net/curisit/securis/services/ApplicationResource.java
.. .. @@ -1,7 +1,9 @@ 1 1 package net.curisit.securis.services; 2 2 3 3 import java.util.Date; 4 +import java.util.HashMap;4 5 import java.util.List; 6 +import java.util.Map;5 7 import java.util.Set; 6 8 7 9 import javax.annotation.security.RolesAllowed; .. .. @@ -177,19 +179,24 @@ 177 179 currentapp.setDescription(app.getDescription()); 178 180 179 181 Set<ApplicationMetadata> newMD = app.getApplicationMetadata(); 180 - for (ApplicationMetadata currentMd : currentapp.getApplicationMetadata()) {181 - if (newMD == null || !newMD.contains(currentMd)) {182 + Set<ApplicationMetadata> oldMD = currentapp.getApplicationMetadata();183 + Map<String, ApplicationMetadata> directOldMD = getMapMD(oldMD);184 + Map<String, ApplicationMetadata> directNewMD = getMapMD(newMD);185 + for (ApplicationMetadata currentMd : oldMD) {186 + if (newMD == null || !directNewMD.containsKey(currentMd.getKey())) {182 187 em.remove(currentMd); 183 188 } 184 189 } 185 190 186 191 if (newMD != null) { 187 - Set<ApplicationMetadata> oldMD = currentapp.getApplicationMetadata();188 192 for (ApplicationMetadata md : newMD) { 189 - if (oldMD.contains(md)) {190 - em.merge(md);193 + if (directOldMD.containsKey(md.getKey())) {194 + ApplicationMetadata amd = directOldMD.get(md.getKey());195 + amd.setValue(md.getValue());196 + amd.setMandatory(md.isMandatory());197 + em.merge(amd);191 198 } else { 192 - md.setApplication(app);199 + md.setApplication(currentapp);193 200 if (md.getCreationTimestamp() == null) { 194 201 md.setCreationTimestamp(app.getCreationTimestamp()); 195 202 } .. .. @@ -202,6 +209,16 @@ 202 209 return Response.ok(currentapp).build(); 203 210 } 204 211 212 + private Map<String, ApplicationMetadata> getMapMD(Set<ApplicationMetadata> amd) {213 + Map<String, ApplicationMetadata> map = new HashMap<String, ApplicationMetadata>();214 + if (amd != null) {215 + for (ApplicationMetadata applicationMetadata : amd) {216 + map.put(applicationMetadata.getKey(), applicationMetadata);217 + }218 + }219 + return map;220 + }221 +205 222 @DELETE 206 223 @Path("/{appid}") 207 224 @EnsureTransaction securis/src/main/resources/META-INF/persistence.xml
.. .. @@ -2,7 +2,7 @@ 2 2 <persistence version="2.0" 3 3 xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 4 xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"> 5 - <persistence-unit name="localdb" >5 + <persistence-unit name="localdb" transaction-type="RESOURCE_LOCAL">6 6 <description>SeCuris LocalDB</description> 7 7 <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider> 8 8 <non-jta-data-source>java:comp/env/SeCurisDS</non-jta-data-source> securis/src/main/webapp/WEB-INF/web.xml
.. .. @@ -40,8 +40,7 @@ 40 40 <param-name>resteasy.injector.factory</param-name> 41 41 <param-value>org.jboss.resteasy.cdi.CdiInjectorFactory</param-value> 42 42 </context-param> 43 -44 -43 +45 44 <filter> 46 45 <filter-name>Resteasy</filter-name> 47 46 <filter-class> .. .. @@ -58,6 +57,9 @@ 58 57 <url-pattern>/*</url-pattern> 59 58 </filter-mapping> 60 59 60 +61 +62 +61 63 <welcome-file-list> 62 64 <welcome-file>/index.jsp</welcome-file> 63 65 </welcome-file-list>