rsanchez
2015-01-28 da889d489da5d7fa8c71d9f21f24b1dc2e29d8e1
#0 refactoring - Removing Google Guice by CDI standard
2 files deleted
4 files added
24 files modified
changed files
securis/pom.xml patch | view | blame | history
securis/src/main/java/net/curisit/securis/AuthFilter.java patch | view | blame | history
securis/src/main/java/net/curisit/securis/BootstrapListener.java patch | view | blame | history
securis/src/main/java/net/curisit/securis/LicenseGenerator.java patch | view | blame | history
securis/src/main/java/net/curisit/securis/RestServicesApplication.java patch | view | blame | history
securis/src/main/java/net/curisit/securis/dao/UserDao.java patch | view | blame | history
securis/src/main/java/net/curisit/securis/db/common/SystemParams.java patch | view | blame | history
securis/src/main/java/net/curisit/securis/ioc/DatabaseProvider.java patch | view | blame | history
securis/src/main/java/net/curisit/securis/ioc/MyTrans.java patch | view | blame | history
securis/src/main/java/net/curisit/securis/ioc/RequestsModule.java patch | view | blame | history
securis/src/main/java/net/curisit/securis/ioc/SecurisModule.java patch | view | blame | history
securis/src/main/java/net/curisit/securis/ioc/TransactionsManager.java patch | view | blame | history
securis/src/main/java/net/curisit/securis/security/SecurityInterceptor.java patch | view | blame | history
securis/src/main/java/net/curisit/securis/services/ApiResource.java patch | view | blame | history
securis/src/main/java/net/curisit/securis/services/ApplicationResource.java patch | view | blame | history
securis/src/main/java/net/curisit/securis/services/BasicServices.java patch | view | blame | history
securis/src/main/java/net/curisit/securis/services/LicenseResource.java patch | view | blame | history
securis/src/main/java/net/curisit/securis/services/LicenseServices.java patch | view | blame | history
securis/src/main/java/net/curisit/securis/services/LicenseTypeResource.java patch | view | blame | history
securis/src/main/java/net/curisit/securis/services/OrganizationResource.java patch | view | blame | history
securis/src/main/java/net/curisit/securis/services/PackResource.java patch | view | blame | history
securis/src/main/java/net/curisit/securis/services/UserResource.java patch | view | blame | history
securis/src/main/java/net/curisit/securis/services/helpers/LicenseHelper.java patch | view | blame | history
securis/src/main/java/net/curisit/securis/services/helpers/UserHelper.java patch | view | blame | history
securis/src/main/java/net/curisit/securis/utils/CacheTTL.java patch | view | blame | history
securis/src/main/java/net/curisit/securis/utils/EmailManager.java patch | view | blame | history
securis/src/main/java/net/curisit/securis/utils/TokenHelper.java patch | view | blame | history
securis/src/main/resources/META-INF/persistence.xml patch | view | blame | history
securis/src/main/webapp/WEB-INF/beans.xml patch | view | blame | history
securis/src/main/webapp/WEB-INF/web.xml patch | view | blame | history
securis/pom.xml
....@@ -19,19 +19,24 @@
1919 </dependency>
2020 <dependency>
2121 <groupId>org.jboss.resteasy</groupId>
22
- <artifactId>resteasy-guice</artifactId>
23
- <version>3.0.9.Final</version>
24
- </dependency>
25
- <dependency>
26
- <groupId>org.jboss.resteasy</groupId>
2722 <artifactId>resteasy-multipart-provider</artifactId>
28
- <version>3.0.9.Final</version>
23
+ <version>3.0.10.Final</version>
2924 </dependency>
3025 <dependency>
3126 <groupId>org.jboss.resteasy</groupId>
3227 <artifactId>resteasy-jaxrs</artifactId>
33
- <version>3.0.9.Final</version>
28
+ <version>3.0.10.Final</version>
3429 </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>
3540 <dependency>
3641 <groupId>net.curisit</groupId>
3742 <artifactId>commons-curis</artifactId>
....@@ -48,11 +53,6 @@
4853 <version>1.0.4-SNAPSHOT</version>
4954 </dependency>
5055 <dependency>
51
- <groupId>org.hibernate</groupId>
52
- <artifactId>hibernate-c3p0</artifactId>
53
- <version>4.3.6.Final</version>
54
- </dependency>
55
- <dependency>
5656 <groupId>org.apache.httpcomponents</groupId>
5757 <artifactId>httpclient</artifactId>
5858 <version>4.4-beta1</version>
....@@ -65,12 +65,23 @@
6565 <dependency>
6666 <groupId>org.jboss.resteasy</groupId>
6767 <artifactId>resteasy-jackson2-provider</artifactId>
68
- <version>3.0.9.Final</version>
68
+ <version>3.0.10.Final</version>
6969 </dependency>
7070 <dependency>
7171 <groupId>org.apache.logging.log4j</groupId>
7272 <artifactId>log4j-web</artifactId>
7373 <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>
7485 </dependency>
7586 </dependencies>
7687 <build>
....@@ -117,6 +128,11 @@
117128 <url>http://archiva.curisit.net/archiva/repository/snapshots/</url>
118129 <layout>default</layout>
119130 </repository>
131
+ <repository>
132
+ <id>project.local</id>
133
+ <name>project</name>
134
+ <url>file:/opt/repo</url>
135
+ </repository>
120136 </repositories>
121137
122138 </project>
securis/src/main/java/net/curisit/securis/AuthFilter.java
....@@ -3,7 +3,7 @@
33 import java.io.IOException;
44 import java.security.Principal;
55
6
-import javax.inject.Singleton;
6
+import javax.enterprise.context.ApplicationScoped;
77 import javax.servlet.Filter;
88 import javax.servlet.FilterChain;
99 import javax.servlet.FilterConfig;
....@@ -17,7 +17,7 @@
1717 import org.apache.logging.log4j.LogManager;
1818 import org.apache.logging.log4j.Logger;
1919
20
-@Singleton
20
+@ApplicationScoped
2121 @WebFilter(urlPatterns = "/*")
2222 public class AuthFilter implements Filter {
2323
securis/src/main/java/net/curisit/securis/BootstrapListener.java
deleted file mode 100644
....@@ -1,39 +0,0 @@
1
-package net.curisit.securis;
2
-
3
-import java.util.List;
4
-
5
-import javax.servlet.ServletContext;
6
-import javax.servlet.ServletContextEvent;
7
-
8
-import org.jboss.resteasy.plugins.guice.GuiceResteasyBootstrapServletContextListener;
9
-
10
-import com.google.inject.Injector;
11
-import com.google.inject.Module;
12
-import com.google.inject.persist.PersistService;
13
-import com.google.inject.persist.jpa.JpaPersistModule;
14
-
15
-public class BootstrapListener extends GuiceResteasyBootstrapServletContextListener {
16
-
17
- @Override
18
- public void contextInitialized(ServletContextEvent event) {
19
- try {
20
- super.contextInitialized(event);
21
- } catch (Exception ex) {
22
- ex.printStackTrace();
23
- }
24
- }
25
-
26
- @SuppressWarnings("unchecked")
27
- @Override
28
- protected List<Module> getModules(ServletContext context) {
29
- List<Module> modules = (List<Module>) super.getModules(context);
30
- modules.add(new JpaPersistModule("localdb"));
31
- return modules;
32
- }
33
-
34
- @Override
35
- public void withInjector(Injector injector) {
36
- injector.getInstance(PersistService.class).start();
37
- }
38
-
39
-}
securis/src/main/java/net/curisit/securis/LicenseGenerator.java
....@@ -24,14 +24,12 @@
2424 import org.apache.logging.log4j.LogManager;
2525 import org.apache.logging.log4j.Logger;
2626
27
-import com.google.inject.Singleton;
28
-
2927 /**
3028 * License generator and signer
3129 *
3230 * @author roberto <roberto.sanchez@curisit.net>
3331 */
34
-@Singleton
32
+@javax.inject.Singleton
3533 public class LicenseGenerator {
3634
3735 private static final Logger LOG = LogManager.getLogger(LicenseGenerator.class);
securis/src/main/java/net/curisit/securis/RestServicesApplication.java
....@@ -3,28 +3,51 @@
33 import java.util.HashSet;
44 import java.util.Set;
55
6
+import javax.inject.Inject;
7
+import javax.ws.rs.ApplicationPath;
68 import javax.ws.rs.core.Application;
79
8
-import net.curisit.integrity.commons.Utils;
10
+import net.curisit.securis.ioc.DatabaseProvider;
11
+import net.curisit.securis.ioc.TransactionsManager;
12
+import net.curisit.securis.security.SecurityInterceptor;
13
+import net.curisit.securis.services.ApiResource;
14
+import net.curisit.securis.services.ApplicationResource;
15
+import net.curisit.securis.services.BasicServices;
16
+import net.curisit.securis.services.LicenseResource;
17
+import net.curisit.securis.services.LicenseTypeResource;
18
+import net.curisit.securis.services.OrganizationResource;
19
+import net.curisit.securis.services.PackResource;
20
+import net.curisit.securis.services.UserResource;
921
1022 import org.apache.logging.log4j.LogManager;
1123 import org.apache.logging.log4j.Logger;
1224
25
+@ApplicationPath("/")
1326 public class RestServicesApplication extends Application {
1427
1528 private static final Logger LOG = LogManager.getLogger(RestServicesApplication.class);
29
+
30
+ @Inject
31
+ private DatabaseProvider dp;
1632
1733 @Override
1834 public Set<Class<?>> getClasses() {
1935 Set<Class<?>> classes = new HashSet<>();
2036 // classes.add(LicenseServices.class);
21
- // classes.add(BasicServices.class);
37
+ classes.add(UserResource.class);
38
+ classes.add(ApiResource.class);
39
+ classes.add(LicenseResource.class);
40
+ classes.add(BasicServices.class);
41
+ classes.add(ApplicationResource.class);
42
+ classes.add(OrganizationResource.class);
43
+ classes.add(LicenseTypeResource.class);
44
+ classes.add(PackResource.class);
45
+ classes.add(SecurityInterceptor.class);
46
+ classes.add(TransactionsManager.class);
2247
23
- LOG.info("Returnes classes for services: {}", classes);
48
+ LOG.info("Returned classes for services: {}", classes);
49
+ LOG.info("dp: {}", dp);
2450 return classes;
2551 }
2652
27
- public static void main(String[] args) {
28
- System.out.println("sha256: " + Utils.sha256("jdoe"));
29
- }
3053 }
securis/src/main/java/net/curisit/securis/dao/UserDao.java
....@@ -3,15 +3,16 @@
33 import java.util.Arrays;
44 import java.util.Date;
55
6
+import javax.enterprise.context.ApplicationScoped;
67 import javax.inject.Inject;
78 import javax.inject.Provider;
8
-import javax.inject.Singleton;
99 import javax.persistence.EntityManager;
10
+import javax.transaction.Transactional;
1011
1112 import net.curisit.integrity.commons.Utils;
1213 import net.curisit.securis.db.User;
1314
14
-@Singleton
15
+@ApplicationScoped
1516 public class UserDao {
1617
1718 @Inject
....@@ -21,7 +22,7 @@
2122 @Inject
2223 Provider<EntityManager> emProvider;
2324
24
- @com.google.inject.persist.Transactional
25
+ @Transactional
2526 public User test(String username) {
2627 EntityManager em = emProvider.get();
2728 User user = new User();
securis/src/main/java/net/curisit/securis/db/common/SystemParams.java
....@@ -2,9 +2,10 @@
22
33 import java.util.Date;
44
5
-import javax.inject.Inject;
6
-import javax.inject.Singleton;
5
+import javax.enterprise.context.ApplicationScoped;
76 import javax.persistence.EntityManager;
7
+import javax.persistence.PersistenceContext;
8
+import javax.transaction.Transactional;
89
910 import net.curisit.integrity.commons.Utils;
1011 import net.curisit.securis.db.Settings;
....@@ -12,17 +13,14 @@
1213 import org.apache.logging.log4j.LogManager;
1314 import org.apache.logging.log4j.Logger;
1415
15
-import com.google.inject.Provider;
16
-import com.google.inject.persist.Transactional;
17
-
18
-@Singleton
16
+@ApplicationScoped
1917 public class SystemParams {
2018
2119 @SuppressWarnings("unused")
2220 private static final Logger LOG = LogManager.getLogger(SystemParams.class);
2321
24
- @Inject
25
- private Provider<EntityManager> emp;
22
+ @PersistenceContext(unitName = "localdb")
23
+ private EntityManager em;
2624
2725 /**
2826 * Returns the system parameter value for given key
....@@ -111,7 +109,6 @@
111109 * @return
112110 */
113111 public String getParam(String key, String defaultValue) {
114
- EntityManager em = emp.get();
115112 Settings p = em.find(Settings.class, key);
116113 return p == null ? defaultValue : p.getValue();
117114 }
....@@ -125,7 +122,6 @@
125122 */
126123 @Transactional
127124 public void setParam(String key, String value) {
128
- EntityManager em = this.emp.get();
129125 Settings p = em.find(Settings.class, key);
130126 if (p == null) {
131127 p = new Settings();
....@@ -187,7 +183,6 @@
187183 */
188184 @Transactional
189185 public void removeParam(String key) {
190
- EntityManager em = this.emp.get();
191186 Settings p = em.find(Settings.class, key);
192187 if (p != null) {
193188 em.remove(p);
securis/src/main/java/net/curisit/securis/ioc/DatabaseProvider.java
....@@ -0,0 +1,38 @@
1
+package net.curisit.securis.ioc;
2
+
3
+import javax.enterprise.context.ApplicationScoped;
4
+import javax.enterprise.inject.Default;
5
+import javax.enterprise.inject.Disposes;
6
+import javax.enterprise.inject.Produces;
7
+import javax.inject.Inject;
8
+import javax.persistence.EntityManager;
9
+import javax.persistence.EntityManagerFactory;
10
+
11
+import org.apache.logging.log4j.LogManager;
12
+import org.apache.logging.log4j.Logger;
13
+
14
+@ApplicationScoped
15
+public class DatabaseProvider {
16
+
17
+ private static final Logger LOG = LogManager.getLogger(DatabaseProvider.class);
18
+
19
+ @Inject
20
+ public DatabaseProvider() {
21
+ LOG.info("Contructor DatabaseProvider, emf: {}, em: {} ", entityManagerFactory, getEM());
22
+ }
23
+
24
+ private EntityManagerFactory entityManagerFactory = javax.persistence.Persistence.createEntityManagerFactory("localdb");
25
+
26
+ // EntityManagerFactory emfH2 =
27
+ // javax.persistence.Persistence.createEntityManagerFactory("localdb");
28
+ @Produces
29
+ private EntityManager getEM() {
30
+ return entityManagerFactory.createEntityManager();
31
+ }
32
+
33
+ public void dispose(@Disposes @Default EntityManager entityManager) {
34
+ if (entityManager.isOpen()) {
35
+ entityManager.close();
36
+ }
37
+ }
38
+}
securis/src/main/java/net/curisit/securis/ioc/MyTrans.java
....@@ -0,0 +1,17 @@
1
+package net.curisit.securis.ioc;
2
+
3
+import java.lang.annotation.ElementType;
4
+import java.lang.annotation.Retention;
5
+import java.lang.annotation.RetentionPolicy;
6
+import java.lang.annotation.Target;
7
+
8
+import javax.interceptor.InterceptorBinding;
9
+
10
+@Target({
11
+ ElementType.METHOD, ElementType.TYPE
12
+})
13
+@Retention(RetentionPolicy.RUNTIME)
14
+@InterceptorBinding
15
+public @interface MyTrans {
16
+
17
+}
securis/src/main/java/net/curisit/securis/ioc/RequestsModule.java
....@@ -5,24 +5,21 @@
55 import net.curisit.securis.services.ApplicationResource;
66 import net.curisit.securis.services.BasicServices;
77 import net.curisit.securis.services.LicenseResource;
8
-import net.curisit.securis.services.LicenseServices;
98 import net.curisit.securis.services.LicenseTypeResource;
109 import net.curisit.securis.services.OrganizationResource;
1110 import net.curisit.securis.services.PackResource;
1211 import net.curisit.securis.services.UserResource;
1312
14
-import org.jboss.resteasy.plugins.guice.ext.RequestScopeModule;
13
+import com.google.inject.AbstractModule;
1514
16
-public class RequestsModule extends RequestScopeModule {
15
+public class RequestsModule extends AbstractModule {
1716
1817 @Override
1918 protected void configure() {
20
- super.configure();
2119 // TODO: Make the bind using reflection dynamically
2220 bind(SecurityInterceptor.class);
2321
2422 bind(BasicServices.class);
25
- bind(LicenseServices.class);
2623 bind(UserResource.class);
2724
2825 bind(ApplicationResource.class);
....@@ -31,7 +28,9 @@
3128 bind(ApiResource.class);
3229 bind(LicenseResource.class);
3330 bind(PackResource.class);
31
+
3432 }
33
+
3534 // @Provides
3635 // @RequestScoped
3736 // public User provideUser() {
securis/src/main/java/net/curisit/securis/ioc/SecurisModule.java
....@@ -7,15 +7,13 @@
77 import java.util.List;
88 import java.util.Properties;
99
10
+import javax.enterprise.context.ApplicationScoped;
1011 import javax.inject.Named;
11
-import javax.inject.Singleton;
12
-import javax.sql.DataSource;
1312 import javax.ws.rs.core.UriBuilder;
1413 import javax.ws.rs.core.UriBuilderException;
1514
1615 import org.apache.logging.log4j.LogManager;
1716 import org.apache.logging.log4j.Logger;
18
-import org.h2.jdbcx.JdbcDataSource;
1917
2018 import com.google.inject.AbstractModule;
2119 import com.google.inject.Provides;
....@@ -46,7 +44,7 @@
4644
4745 @Named("base-uri")
4846 @Provides
49
- @Singleton
47
+ @ApplicationScoped
5048 public URI getBaseURI() {
5149 // TODO Read from configuration, where?
5250 try {
....@@ -81,7 +79,7 @@
8179
8280 @Named("temporary-dir")
8381 @Provides
84
- @Singleton
82
+ @ApplicationScoped
8583 public File getTemporaryDir() {
8684 String tmp = getAppDir().getAbsolutePath();
8785 tmp += File.separator + ".TEMP";
....@@ -98,7 +96,7 @@
9896
9997 @Named("app-dir")
10098 @Provides
101
- @Singleton
99
+ @ApplicationScoped
102100 public File getAppDir() {
103101 String appDir = System.getProperty("user.home", System.getProperty("user.dir"));
104102 if (appDir == null) {
....@@ -117,33 +115,21 @@
117115
118116 @Named("support-email")
119117 @Provides
120
- @Singleton
118
+ @ApplicationScoped
121119 public String getSupportEmail() {
122
- return "integrity@curistec.com";
120
+ return "support@curisit.net";
123121 }
124122
125123 @Named("hash-logo")
126124 @Provides
127
- @Singleton
125
+ @ApplicationScoped
128126 public String getHashLogo() {
129127 return "1b42616809d4cd8ccf109e3c30d0ab25067f160b30b7354a08ddd563de0096ba";
130128 }
131129
132
- @Provides
133
- @Singleton
134
- public DataSource getDataSource(@Named("app-dir") File appDir) {
135
-
136
- JdbcDataSource dataSource = new JdbcDataSource();
137
- dataSource.setURL(getUrl(appDir));
138
- dataSource.setUser("curis");
139
- dataSource.setPassword(getPassword());
140
- LOG.debug("JdbcDataSource: {}", dataSource);
141
- return dataSource;
142
- }
143
-
144130 @Named("db-files")
145131 @Provides
146
- @Singleton
132
+ @ApplicationScoped
147133 public List<String> getDbFiles() {
148134 return getAppDbFiles();
149135 }
securis/src/main/java/net/curisit/securis/ioc/TransactionsManager.java
....@@ -0,0 +1,80 @@
1
+package net.curisit.securis.ioc;
2
+
3
+import java.io.IOException;
4
+import java.lang.reflect.Method;
5
+
6
+import javax.inject.Inject;
7
+import javax.interceptor.AroundInvoke;
8
+import javax.interceptor.Interceptor;
9
+import javax.interceptor.InvocationContext;
10
+import javax.persistence.EntityManager;
11
+import javax.ws.rs.container.ContainerRequestContext;
12
+import javax.ws.rs.container.ContainerResponseContext;
13
+import javax.ws.rs.core.Response.Status;
14
+
15
+import org.apache.logging.log4j.LogManager;
16
+import org.apache.logging.log4j.Logger;
17
+import org.jboss.resteasy.core.ResourceMethodInvoker;
18
+import org.jboss.resteasy.spi.ResteasyProviderFactory;
19
+
20
+import com.google.inject.persist.Transactional;
21
+
22
+@Interceptor
23
+@MyTrans
24
+public class TransactionsManager {
25
+
26
+ private static final Logger LOG = LogManager.getLogger(TransactionsManager.class);
27
+
28
+ @Inject
29
+ private EntityManager em;
30
+
31
+ // @Inject
32
+ // private ThreadLocal<EntityManager> threadLocal;
33
+
34
+ @AroundInvoke
35
+ public Object invoke(InvocationContext context) throws Exception {
36
+ // EntityTransaction trx = manager.getTransaction();
37
+ if (!em.getTransaction().isActive()) {
38
+ em.getTransaction().begin();
39
+ LOG.info("INIT trans");
40
+ ResteasyProviderFactory.pushContext(EntityManager.class, em);
41
+ Object result = context.proceed();
42
+
43
+ em.getTransaction().commit();
44
+ LOG.info("COMMIT trans");
45
+ return result;
46
+ }
47
+ LOG.info("No trans");
48
+ Object result = context.proceed();
49
+ return result;
50
+ }
51
+
52
+ public void filter(ContainerRequestContext requestContext) throws IOException {
53
+ ResourceMethodInvoker methodInvoker = (ResourceMethodInvoker) requestContext.getProperty("org.jboss.resteasy.core.ResourceMethodInvoker");
54
+ Method method = methodInvoker.getMethod();
55
+
56
+ if (method.isAnnotationPresent(Transactional.class)) {
57
+ LOG.info("WE need transaction!!!");
58
+ em.getTransaction().begin();
59
+ }
60
+ }
61
+
62
+ public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext) throws IOException {
63
+ // TODO Auto-generated method stub
64
+
65
+ if (em.getTransaction().isActive()) {
66
+ LOG.info("There is transaction go ahead...");
67
+ if (responseContext.getStatus() == Status.OK.getStatusCode()) {
68
+ em.getTransaction().commit();
69
+ LOG.info("COMMIT");
70
+ } else {
71
+ em.getTransaction().rollback();
72
+ LOG.info("ROLLBACK");
73
+ }
74
+ } else {
75
+ LOG.info("There is NO transaction");
76
+
77
+ }
78
+ }
79
+
80
+}
securis/src/main/java/net/curisit/securis/security/SecurityInterceptor.java
....@@ -31,27 +31,27 @@
3131 import org.jboss.resteasy.spi.ResteasyProviderFactory;
3232
3333 @Provider
34
-// @PreMatching
3534 @Priority(Priorities.AUTHENTICATION)
3635 public class SecurityInterceptor implements javax.ws.rs.container.ContainerRequestFilter {
3736 private static final Logger LOG = LogManager.getLogger(SecurityInterceptor.class);
38
-
39
- @Inject
40
- private TokenHelper tokenHelper;
4137
4238 @Context
4339 private HttpServletRequest servletRequest;
4440
4541 @Inject
46
- CacheTTL cache;
47
-
48
- @Context
49
- Dispatcher dispatcher;
42
+ private CacheTTL cache;
5043
5144 @Inject
52
- com.google.inject.Provider<EntityManager> emProvider;
45
+ private TokenHelper tokenHelper;
46
+
47
+ @Context
48
+ private Dispatcher dispatcher;
49
+
50
+ @Inject
51
+ private EntityManager em;
5352
5453 public void filter(ContainerRequestContext containerRequestContext) throws IOException {
54
+
5555 ResourceMethodInvoker methodInvoker = (ResourceMethodInvoker) containerRequestContext
5656 .getProperty("org.jboss.resteasy.core.ResourceMethodInvoker");
5757 Method method = methodInvoker.getMethod();
....@@ -76,6 +76,7 @@
7676 containerRequestContext.setSecurityContext(scw);
7777 // Next line provide injection in resource methods
7878 ResteasyProviderFactory.pushContext(BasicSecurityContext.class, scw);
79
+ ResteasyProviderFactory.pushContext(EntityManager.class, em);
7980 LOG.debug("Added custom SecurityContext for user {}, orgs: {}", username, orgs);
8081 }
8182 }
....@@ -85,7 +86,6 @@
8586 Set<Integer> userOrgs = cache.get("orgs_" + username, Set.class);
8687 if (userOrgs == null) {
8788 // Theorically this shouldn't be never null, but just in case...
88
- EntityManager em = emProvider.get();
8989 User user = em.find(User.class, username);
9090 if (user != null) {
9191 userOrgs = user.getAllOrgsIds();
....@@ -103,7 +103,6 @@
103103 }
104104 Integer userRoles = cache.get("roles_" + username, Integer.class);
105105 if (userRoles == null) {
106
- EntityManager em = emProvider.get();
107106 User user = em.find(User.class, username);
108107 if (user != null) {
109108 userRoles = 0;
securis/src/main/java/net/curisit/securis/services/ApiResource.java
....@@ -5,8 +5,8 @@
55 import java.util.List;
66
77 import javax.inject.Inject;
8
-import javax.inject.Provider;
98 import javax.persistence.EntityManager;
9
+import javax.transaction.Transactional;
1010 import javax.ws.rs.Consumes;
1111 import javax.ws.rs.GET;
1212 import javax.ws.rs.HeaderParam;
....@@ -45,8 +45,6 @@
4545 import org.apache.logging.log4j.Logger;
4646 import org.jboss.resteasy.plugins.providers.multipart.MultipartFormDataInput;
4747
48
-import com.google.inject.persist.Transactional;
49
-
5048 /**
5149 * External API to be accessed by third parties
5250 *
....@@ -64,7 +62,7 @@
6462 private LicenseHelper licenseHelper;
6563
6664 @Inject
67
- Provider<EntityManager> emProvider;
65
+ EntityManager em;
6866
6967 @Inject
7068 LicenseGenerator licenseGenerator;
....@@ -124,7 +122,7 @@
124122 public Response createFromRequest(RequestBean request, @HeaderParam(LicenseManager.HEADER_LICENSE_NAME_OR_REFERENCE) String nameOrReference,
125123 @HeaderParam(LicenseManager.HEADER_LICENSE_EMAIL) String userEmail) throws IOException, SeCurisServiceException, SeCurisException {
126124 LOG.info("Request to get license: {}", request);
127
- SignedLicenseBean lic = createLicense(request, emProvider.get(), false, nameOrReference, userEmail);
125
+ SignedLicenseBean lic = createLicense(request, em, false, nameOrReference, userEmail);
128126
129127 return Response.ok(lic).build();
130128 }
....@@ -189,7 +187,7 @@
189187 throw new SeCurisServiceException(ErrorCodes.LICENSE_NOT_READY_FOR_RENEW, "The license is still valid, not ready for renew");
190188 }
191189
192
- EntityManager em = emProvider.get();
190
+ // EntityManager em = emProvider.get();
193191 License lic = License.findLicenseByCode(previousLic.getLicenseCode(), em);
194192 if (lic.getStatus() != LicenseStatus.ACTIVE) {
195193 throw new SeCurisServiceException(ErrorCodes.LICENSE_NOT_READY_FOR_RENEW, "Only licenses with status 'Active' can be renew");
....@@ -226,7 +224,7 @@
226224 throw new SeCurisServiceException(ErrorCodes.LICENSE_IS_EXPIRED, "The license is expired");
227225 }
228226
229
- EntityManager em = emProvider.get();
227
+ // EntityManager em = emProvider.get();
230228 try {
231229 SignatureHelper.getInstance().validateSignature(currentLic);
232230 } catch (SeCurisException ex) {
securis/src/main/java/net/curisit/securis/services/ApplicationResource.java
....@@ -6,10 +6,10 @@
66
77 import javax.annotation.security.RolesAllowed;
88 import javax.inject.Inject;
9
-import javax.inject.Provider;
109 import javax.persistence.EntityManager;
1110 import javax.persistence.TypedQuery;
1211 import javax.servlet.http.HttpServletRequest;
12
+import javax.transaction.Transactional;
1313 import javax.ws.rs.Consumes;
1414 import javax.ws.rs.DELETE;
1515 import javax.ws.rs.GET;
....@@ -39,8 +39,6 @@
3939 import org.apache.logging.log4j.LogManager;
4040 import org.apache.logging.log4j.Logger;
4141
42
-import com.google.inject.persist.Transactional;
43
-
4442 /**
4543 * Application resource, this service will provide methods to create, modify and
4644 * delete applications
....@@ -54,7 +52,7 @@
5452 TokenHelper tokenHelper;
5553
5654 @Inject
57
- Provider<EntityManager> emProvider;
55
+ EntityManager em;
5856
5957 private static final Logger LOG = LogManager.getLogger(ApplicationResource.class);
6058
....@@ -74,7 +72,7 @@
7472 public Response index() {
7573 LOG.info("Getting applications list ");
7674
77
- EntityManager em = emProvider.get();
75
+ // EntityManager em = emProvider.get();
7876 em.clear();
7977 TypedQuery<Application> q = em.createNamedQuery("list-applications", Application.class);
8078 List<Application> list = q.getResultList();
....@@ -100,7 +98,7 @@
10098 return Response.status(Status.NOT_FOUND).build();
10199 }
102100
103
- EntityManager em = emProvider.get();
101
+ // EntityManager em = emProvider.get();
104102 em.clear();
105103
106104 Application app = null;
....@@ -138,7 +136,7 @@
138136 @RolesAllowed(BasicSecurityContext.ROL_ADMIN)
139137 public Response create(Application app, @HeaderParam(TokenHelper.TOKEN_HEADER_PÀRAM) String token) {
140138 LOG.info("Creating new application");
141
- EntityManager em = emProvider.get();
139
+ // EntityManager em = emProvider.get();
142140 app.setCreationTimestamp(new Date());
143141 em.persist(app);
144142 LOG.info("App ID: {}", app.getId());
....@@ -167,7 +165,7 @@
167165 @RolesAllowed(BasicSecurityContext.ROL_ADMIN)
168166 public Response modify(Application app, @PathParam("appid") String appid, @HeaderParam(TokenHelper.TOKEN_HEADER_PÀRAM) String token) {
169167 LOG.info("Modifying application with id: {}", appid);
170
- EntityManager em = emProvider.get();
168
+ // EntityManager em = emProvider.get();
171169 Application currentapp = em.find(Application.class, Integer.parseInt(appid));
172170 if (currentapp == null) {
173171 LOG.error("Application with id {} not found in DB", appid);
....@@ -214,7 +212,7 @@
214212 @RolesAllowed(BasicSecurityContext.ROL_ADMIN)
215213 public Response delete(@PathParam("appid") String appid, @Context HttpServletRequest request) {
216214 LOG.info("Deleting app with id: {}", appid);
217
- EntityManager em = emProvider.get();
215
+ // EntityManager em = emProvider.get();
218216 Application app = em.find(Application.class, Integer.parseInt(appid));
219217 if (app == null) {
220218 LOG.error("Application with id {} can not be deleted, It was not found in DB", appid);
securis/src/main/java/net/curisit/securis/services/BasicServices.java
....@@ -3,9 +3,10 @@
33 import java.net.URI;
44 import java.util.Date;
55
6
+import javax.enterprise.context.ApplicationScoped;
67 import javax.inject.Inject;
7
-import javax.inject.Singleton;
88 import javax.servlet.http.HttpServletRequest;
9
+import javax.transaction.Transactional;
910 import javax.ws.rs.FormParam;
1011 import javax.ws.rs.GET;
1112 import javax.ws.rs.HeaderParam;
....@@ -27,15 +28,13 @@
2728 import org.apache.logging.log4j.LogManager;
2829 import org.apache.logging.log4j.Logger;
2930
30
-import com.google.inject.persist.Transactional;
31
-
3231 /**
3332 * Basic services for login and basic app wrkflow
3433 *
3534 * @author roberto <roberto.sanchez@curisit.net>
3635 */
3736 @Path("/")
38
-@Singleton
37
+@ApplicationScoped
3938 public class BasicServices {
4039
4140 private static final Logger LOG = LogManager.getLogger(BasicServices.class);
securis/src/main/java/net/curisit/securis/services/LicenseResource.java
....@@ -7,11 +7,9 @@
77 import java.util.List;
88
99 import javax.inject.Inject;
10
-import javax.inject.Provider;
1110 import javax.persistence.EntityManager;
12
-import javax.persistence.PersistenceContext;
13
-import javax.persistence.SynchronizationType;
1411 import javax.persistence.TypedQuery;
12
+import javax.transaction.Transactional;
1513 import javax.ws.rs.Consumes;
1614 import javax.ws.rs.DELETE;
1715 import javax.ws.rs.DefaultValue;
....@@ -62,7 +60,6 @@
6260 import com.fasterxml.jackson.annotation.JsonAutoDetect;
6361 import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
6462 import com.fasterxml.jackson.annotation.JsonProperty;
65
-import com.google.inject.persist.Transactional;
6663
6764 /**
6865 * License resource, this service will provide methods to create, modify and
....@@ -85,10 +82,7 @@
8582 private LicenseHelper licenseHelper;
8683
8784 @Inject
88
- private Provider<EntityManager> emProvider;
89
-
90
- @PersistenceContext(unitName = "localdb", synchronization = SynchronizationType.SYNCHRONIZED)
91
- private EntityManager entityManager;
85
+ EntityManager em;
9286
9387 @Inject
9488 private LicenseGenerator licenseGenerator;
....@@ -106,7 +100,7 @@
106100 public Response index(@QueryParam("packId") Integer packId, @Context BasicSecurityContext bsc) {
107101 LOG.info("Getting licenses list ");
108102
109
- EntityManager em = emProvider.get();
103
+ // EntityManager em = emProvider.get();
110104 em.clear();
111105
112106 if (!bsc.isUserInRole(BasicSecurityContext.ROL_ADMIN)) {
....@@ -141,7 +135,7 @@
141135 public Response get(@PathParam("licId") Integer licId, @Context BasicSecurityContext bsc) throws SeCurisServiceException {
142136 LOG.info("Getting organization data for id: {}: ", licId);
143137
144
- EntityManager em = emProvider.get();
138
+ // EntityManager em = emProvider.get();
145139 em.clear();
146140 License lic = getCurrentLicense(licId, bsc, em);
147141 return Response.ok(lic).build();
....@@ -161,7 +155,7 @@
161155 @Transactional
162156 public Response download(@PathParam("licId") Integer licId, @Context BasicSecurityContext bsc) throws SeCurisServiceException {
163157
164
- EntityManager em = emProvider.get();
158
+ // EntityManager em = emProvider.get();
165159 License lic = getCurrentLicense(licId, bsc, em);
166160
167161 if (lic.getLicenseData() == null) {
....@@ -198,7 +192,7 @@
198192 })
199193 public Response activate(@PathParam("licId") Integer licId, @Context BasicSecurityContext bsc) throws SeCurisServiceException {
200194
201
- EntityManager em = emProvider.get();
195
+ // EntityManager em = emProvider.get();
202196 License lic = getCurrentLicense(licId, bsc, em);
203197
204198 if (!License.Status.isActionValid(License.Action.ACTIVATION, lic.getStatus())) {
....@@ -247,7 +241,7 @@
247241 public Response send(@PathParam("licId") Integer licId, @DefaultValue("false") @FormParam("add_cc") Boolean addCC,
248242 @Context BasicSecurityContext bsc) throws SeCurisServiceException, SeCurisException {
249243
250
- EntityManager em = emProvider.get();
244
+ // EntityManager em = emProvider.get();
251245 License lic = getCurrentLicense(licId, bsc, em);
252246 Application app = lic.getPack().getLicenseType().getApplication();
253247 File licFile = null;
....@@ -303,7 +297,7 @@
303297 public Response cancel(@PathParam("licId") Integer licId, CancellationLicenseActionBean actionData, @Context BasicSecurityContext bsc)
304298 throws SeCurisServiceException {
305299
306
- EntityManager em = emProvider.get();
300
+ // EntityManager em = emProvider.get();
307301 License lic = getCurrentLicense(licId, bsc, em);
308302
309303 if (!License.Status.isActionValid(License.Action.CANCEL, lic.getStatus())) {
....@@ -348,7 +342,7 @@
348342 })
349343 @Transactional
350344 public Response create(License lic, @Context BasicSecurityContext bsc) throws SeCurisServiceException {
351
- EntityManager em = emProvider.get();
345
+ // EntityManager em = emProvider.get();
352346
353347 if (checkIfCodeExists(lic.getCode(), em)) {
354348 throw new SeCurisServiceException(ErrorCodes.INVALID_DATA, "The license code is already used in an existing license");
....@@ -476,7 +470,7 @@
476470 public Response modify(License lic, @PathParam("licId") Integer licId, @Context BasicSecurityContext bsc) throws SeCurisServiceException {
477471 LOG.info("Modifying organization with id: {}", licId);
478472
479
- EntityManager em = emProvider.get();
473
+ // EntityManager em = emProvider.get();
480474
481475 License currentLicense = getCurrentLicense(licId, bsc, em);
482476 currentLicense.setComments(lic.getComments());
....@@ -522,7 +516,7 @@
522516 })
523517 public Response delete(@PathParam("licId") Integer licId, @Context BasicSecurityContext bsc) throws SeCurisServiceException {
524518 LOG.info("Deleting license with id: {}", licId);
525
- EntityManager em = emProvider.get();
519
+ // EntityManager em = emProvider.get();
526520 License lic = getCurrentLicense(licId, bsc, em);
527521
528522 if (!License.Status.isActionValid(License.Action.DELETE, lic.getStatus())) {
....@@ -554,7 +548,7 @@
554548 })
555549 public Response block(@PathParam("licId") Integer licId, @Context BasicSecurityContext bsc) throws SeCurisServiceException {
556550 LOG.info("Blocking license with id: {}", licId);
557
- EntityManager em = emProvider.get();
551
+ // EntityManager em = emProvider.get();
558552 License lic = getCurrentLicense(licId, bsc, em);
559553
560554 if (!License.Status.isActionValid(License.Action.BLOCK, lic.getStatus())) {
....@@ -587,7 +581,7 @@
587581 })
588582 public Response unblock(@PathParam("licId") Integer licId, @Context BasicSecurityContext bsc) throws SeCurisServiceException {
589583 LOG.info("Unblocking license with id: {}", licId);
590
- EntityManager em = emProvider.get();
584
+ // EntityManager em = emProvider.get();
591585 License lic = getCurrentLicense(licId, bsc, em);
592586
593587 if (BlockedRequest.isRequestBlocked(lic.getRequestData(), em)) {
securis/src/main/java/net/curisit/securis/services/LicenseServices.java
deleted file mode 100644
....@@ -1,129 +0,0 @@
1
-package net.curisit.securis.services;
2
-
3
-import java.io.IOException;
4
-import java.net.URI;
5
-import java.text.MessageFormat;
6
-
7
-import javax.inject.Named;
8
-import javax.servlet.http.HttpServletRequest;
9
-import javax.ws.rs.Consumes;
10
-import javax.ws.rs.FormParam;
11
-import javax.ws.rs.GET;
12
-import javax.ws.rs.POST;
13
-import javax.ws.rs.Path;
14
-import javax.ws.rs.Produces;
15
-import javax.ws.rs.core.Context;
16
-import javax.ws.rs.core.MediaType;
17
-import javax.ws.rs.core.Response;
18
-
19
-import org.apache.commons.io.IOUtils;
20
-import org.apache.logging.log4j.LogManager;
21
-import org.apache.logging.log4j.Logger;
22
-import org.jboss.resteasy.annotations.providers.multipart.MultipartForm;
23
-import org.jboss.resteasy.annotations.providers.multipart.PartType;
24
-
25
-// The Java class will be hosted at the URI path "/myresource"
26
-@Path("/test")
27
-public class LicenseServices {
28
-
29
- private static final Logger LOG = LogManager.getLogger(LicenseServices.class);
30
-
31
- private static final int DEFAULT_LICENSE_EXPIRATION = 365;
32
- private static final String LICENSE_STRING = "CurisIntegrity Config Server v{0}.{1}";
33
-
34
- @com.google.inject.Inject
35
- @Named("base-uri")
36
- private URI uri;
37
-
38
- public LicenseServices() {
39
-
40
- }
41
-
42
- /**
43
- *
44
- * @return the server version in format majorVersion.minorVersion
45
- */
46
- @GET
47
- @Produces({
48
- MediaType.TEXT_HTML
49
- })
50
- public Response index() {
51
- try {
52
- String index = IOUtils.toString(this.getClass().getResourceAsStream("/static/index.html"));
53
- return Response.ok().entity(index).build();
54
- } catch (IOException e) {
55
- LOG.error("Error getting index.html", e);
56
- }
57
- return Response.ok().entity(MessageFormat.format(LICENSE_STRING, 0, 1)).build();
58
- }
59
-
60
- @GET
61
- @Path("/dummy")
62
- @Produces({
63
- MediaType.TEXT_PLAIN
64
- })
65
- public Response dummy(@Context HttpServletRequest request) {
66
- LOG.info("Request: " + request.getPathInfo());
67
- return Response.ok().entity((uri == null)).build();
68
- }
69
-
70
- /**
71
- * @return the version of the three entities that can be synchronized
72
- * (Users, DataSet and Settings)
73
- */
74
- @POST
75
- @Path("/upload1")
76
- @Consumes(MediaType.MULTIPART_FORM_DATA)
77
- @Produces({
78
- MediaType.APPLICATION_JSON
79
- })
80
- public Response testFile1(@MultipartForm FileUploadForm mfdi) {
81
- LOG.info("FORM: texto: {}, file: {}", mfdi.getTexto(), new String(mfdi.getFile()));
82
- return Response.ok("OK").build();
83
- }
84
-
85
-// @GET
86
-// @Path("/current/{license}")
87
-// @Produces({
88
-// MediaType.APPLICATION_JSON
89
-// })
90
-// public ServiceResponse<ServerConfigVersions> testFile(@PathParam("license") String license,
91
-// @DefaultValue("-1") @QueryParam("minorVersion") int minorVersion, @DefaultValue("-1") @QueryParam("majorVersion") int majorVersion) {
92
-//
93
-// LOG.info("Called 'current' service with license: {}", license);
94
-// ServiceResponse<ServerConfigVersions> response = new ServiceResponse<ServerConfigVersions>();
95
-//
96
-// return response;
97
-// }
98
-
99
- public static class FileUploadForm {
100
-
101
- @FormParam("file1")
102
- @PartType("application/octet-stream")
103
- private byte[] file;
104
-
105
- @FormParam("texto1")
106
- @PartType("text/plain")
107
- private String texto;
108
-
109
- public FileUploadForm() {
110
- }
111
-
112
- public byte[] getFile() {
113
- return file;
114
- }
115
-
116
- public void setFile(byte[] file) {
117
- this.file = file;
118
- }
119
-
120
- public void setTexto(final String texto) {
121
- this.texto = texto;
122
- }
123
-
124
- public String getTexto() {
125
- return texto;
126
- }
127
-
128
- }
129
-}
securis/src/main/java/net/curisit/securis/services/LicenseTypeResource.java
....@@ -6,10 +6,10 @@
66
77 import javax.annotation.security.RolesAllowed;
88 import javax.inject.Inject;
9
-import javax.inject.Provider;
109 import javax.persistence.EntityManager;
1110 import javax.persistence.TypedQuery;
1211 import javax.servlet.http.HttpServletRequest;
12
+import javax.transaction.Transactional;
1313 import javax.ws.rs.Consumes;
1414 import javax.ws.rs.DELETE;
1515 import javax.ws.rs.GET;
....@@ -39,8 +39,6 @@
3939 import org.apache.logging.log4j.LogManager;
4040 import org.apache.logging.log4j.Logger;
4141
42
-import com.google.inject.persist.Transactional;
43
-
4442 /**
4543 * LicenseType resource, this service will provide methods to create, modify and
4644 * delete license types
....@@ -56,7 +54,7 @@
5654 TokenHelper tokenHelper;
5755
5856 @Inject
59
- Provider<EntityManager> emProvider;
57
+ EntityManager em;
6058
6159 public LicenseTypeResource() {
6260 }
....@@ -74,7 +72,7 @@
7472 public Response index() {
7573 LOG.info("Getting license types list ");
7674
77
- EntityManager em = emProvider.get();
75
+ // EntityManager em = emProvider.get();
7876 em.clear();
7977 TypedQuery<LicenseType> q = em.createNamedQuery("list-license_types", LicenseType.class);
8078 List<LicenseType> list = q.getResultList();
....@@ -100,7 +98,7 @@
10098 return Response.status(Status.NOT_FOUND).build();
10199 }
102100
103
- EntityManager em = emProvider.get();
101
+ // EntityManager em = emProvider.get();
104102 em.clear();
105103 LicenseType lt = em.find(LicenseType.class, Integer.parseInt(ltid));
106104 if (lt == null) {
....@@ -121,7 +119,7 @@
121119 @RolesAllowed(BasicSecurityContext.ROL_ADMIN)
122120 public Response create(LicenseType lt, @HeaderParam(TokenHelper.TOKEN_HEADER_PÀRAM) String token) {
123121 LOG.info("Creating new license type");
124
- EntityManager em = emProvider.get();
122
+ // EntityManager em = emProvider.get();
125123
126124 try {
127125 setApplication(lt, lt.getApplicationId(), em);
....@@ -162,7 +160,7 @@
162160 @RolesAllowed(BasicSecurityContext.ROL_ADMIN)
163161 public Response modify(LicenseType lt, @PathParam("ltid") String ltid, @HeaderParam(TokenHelper.TOKEN_HEADER_PÀRAM) String token) {
164162 LOG.info("Modifying license type with id: {}", ltid);
165
- EntityManager em = emProvider.get();
163
+ // EntityManager em = emProvider.get();
166164 LicenseType currentlt = em.find(LicenseType.class, Integer.parseInt(ltid));
167165 if (currentlt == null) {
168166 LOG.error("LicenseType with id {} not found in DB", ltid);
....@@ -229,7 +227,7 @@
229227 @RolesAllowed(BasicSecurityContext.ROL_ADMIN)
230228 public Response delete(@PathParam("ltid") String ltid, @Context HttpServletRequest request) {
231229 LOG.info("Deleting app with id: {}", ltid);
232
- EntityManager em = emProvider.get();
230
+ // EntityManager em = emProvider.get();
233231 LicenseType app = em.find(LicenseType.class, Integer.parseInt(ltid));
234232 if (app == null) {
235233 LOG.error("LicenseType with id {} can not be deleted, It was not found in DB", ltid);
securis/src/main/java/net/curisit/securis/services/OrganizationResource.java
....@@ -6,11 +6,11 @@
66 import java.util.Set;
77
88 import javax.annotation.security.RolesAllowed;
9
-import javax.inject.Inject;
10
-import javax.inject.Provider;
9
+import javax.enterprise.context.RequestScoped;
1110 import javax.persistence.EntityManager;
1211 import javax.persistence.TypedQuery;
1312 import javax.servlet.http.HttpServletRequest;
13
+import javax.transaction.Transactional;
1414 import javax.ws.rs.Consumes;
1515 import javax.ws.rs.DELETE;
1616 import javax.ws.rs.GET;
....@@ -30,14 +30,13 @@
3030 import net.curisit.securis.SeCurisException;
3131 import net.curisit.securis.db.Organization;
3232 import net.curisit.securis.db.User;
33
+import net.curisit.securis.ioc.MyTrans;
3334 import net.curisit.securis.security.BasicSecurityContext;
3435 import net.curisit.securis.security.Securable;
3536 import net.curisit.securis.utils.TokenHelper;
3637
3738 import org.apache.logging.log4j.LogManager;
3839 import org.apache.logging.log4j.Logger;
39
-
40
-import com.google.inject.persist.Transactional;
4140
4241 /**
4342 * Organization resource, this service will provide methods to create, modify
....@@ -46,12 +45,16 @@
4645 * @author roberto <roberto.sanchez@curisit.net>
4746 */
4847 @Path("/organization")
48
+@RequestScoped
4949 public class OrganizationResource {
5050
5151 private static final Logger LOG = LogManager.getLogger(OrganizationResource.class);
5252
53
- @Inject
54
- private Provider<EntityManager> emProvider;
53
+ @Context
54
+ EntityManager em;
55
+
56
+ @Context
57
+ BasicSecurityContext bsc;
5558
5659 public OrganizationResource() {
5760 }
....@@ -66,10 +69,10 @@
6669 MediaType.APPLICATION_JSON
6770 })
6871 @Securable
69
- public Response index(@Context BasicSecurityContext bsc) {
72
+ public Response index() {
7073 LOG.info("Getting organizations list ");
7174
72
- EntityManager em = emProvider.get();
75
+ // EntityManager em = emProvider.get();
7376 em.clear();
7477 TypedQuery<Organization> q;
7578 if (bsc.isUserInRole(BasicSecurityContext.ROL_ADMIN)) {
....@@ -101,7 +104,7 @@
101104 return Response.status(Status.NOT_FOUND).build();
102105 }
103106
104
- EntityManager em = emProvider.get();
107
+ // EntityManager em = emProvider.get();
105108 em.clear();
106109 Organization org = em.find(Organization.class, Integer.parseInt(orgid));
107110 if (org == null) {
....@@ -128,12 +131,12 @@
128131 @Produces({
129132 MediaType.APPLICATION_JSON
130133 })
131
- @Transactional
134
+ @MyTrans
132135 @Securable
133136 @RolesAllowed(BasicSecurityContext.ROL_ADMIN)
134
- public Response create(Organization org, @HeaderParam(TokenHelper.TOKEN_HEADER_PÀRAM) String token) {
137
+ public Response create(Organization org) {
135138 LOG.info("Creating new organization");
136
- EntityManager em = emProvider.get();
139
+ // EntityManager em = emProvider.get();
137140
138141 try {
139142 this.setParentOrg(org, org.getParentOrgId(), em);
....@@ -159,7 +162,6 @@
159162 org.setUsers(users);
160163 org.setCreationTimestamp(new Date());
161164 em.persist(org);
162
-
163165 return Response.ok(org).build();
164166 }
165167
....@@ -205,7 +207,7 @@
205207 @RolesAllowed(BasicSecurityContext.ROL_ADMIN)
206208 public Response modify(Organization org, @PathParam("orgid") String orgid, @HeaderParam(TokenHelper.TOKEN_HEADER_PÀRAM) String token) {
207209 LOG.info("Modifying organization with id: {}", orgid);
208
- EntityManager em = emProvider.get();
210
+ // EntityManager em = emProvider.get();
209211 Organization currentOrg = em.find(Organization.class, Integer.parseInt(orgid));
210212 if (currentOrg == null) {
211213 LOG.error("Organization with id {} not found in DB", orgid);
....@@ -250,7 +252,7 @@
250252 @RolesAllowed(BasicSecurityContext.ROL_ADMIN)
251253 public Response delete(@PathParam("orgid") String orgid, @Context HttpServletRequest request) {
252254 LOG.info("Deleting organization with id: {}", orgid);
253
- EntityManager em = emProvider.get();
255
+ // EntityManager em = emProvider.get();
254256 Organization org = em.find(Organization.class, Integer.parseInt(orgid));
255257 if (org == null) {
256258 LOG.error("Organization with id {} can not be deleted, It was not found in DB", orgid);
securis/src/main/java/net/curisit/securis/services/PackResource.java
....@@ -7,9 +7,9 @@
77
88 import javax.annotation.security.RolesAllowed;
99 import javax.inject.Inject;
10
-import javax.inject.Provider;
1110 import javax.persistence.EntityManager;
1211 import javax.persistence.TypedQuery;
12
+import javax.transaction.Transactional;
1313 import javax.ws.rs.Consumes;
1414 import javax.ws.rs.DELETE;
1515 import javax.ws.rs.FormParam;
....@@ -46,8 +46,6 @@
4646 import org.apache.logging.log4j.LogManager;
4747 import org.apache.logging.log4j.Logger;
4848
49
-import com.google.inject.persist.Transactional;
50
-
5149 /**
5250 * Pack resource, this service will provide methods to create, modify and delete
5351 * packs
....@@ -63,7 +61,7 @@
6361 TokenHelper tokenHelper;
6462
6563 @Inject
66
- Provider<EntityManager> emProvider;
64
+ EntityManager em;
6765
6866 @Inject
6967 private LicenseHelper licenseHelper;
....@@ -81,7 +79,7 @@
8179 public Response index(@Context BasicSecurityContext bsc) {
8280 LOG.info("Getting packs list ");
8381
84
- EntityManager em = emProvider.get();
82
+ // EntityManager em = emProvider.get();
8583 em.clear();
8684
8785 TypedQuery<Pack> q;
....@@ -123,7 +121,7 @@
123121 return Response.status(Status.NOT_FOUND).build();
124122 }
125123
126
- EntityManager em = emProvider.get();
124
+ // EntityManager em = emProvider.get();
127125 em.clear();
128126 Pack pack = em.find(Pack.class, packId);
129127 if (pack == null) {
....@@ -148,7 +146,7 @@
148146 @Transactional
149147 public Response create(Pack pack, @Context BasicSecurityContext bsc) throws SeCurisServiceException {
150148 LOG.info("Creating new pack");
151
- EntityManager em = emProvider.get();
149
+ // EntityManager em = emProvider.get();
152150
153151 if (checkIfCodeExists(pack.getCode(), em)) {
154152 throw new SeCurisServiceException(ErrorCodes.INVALID_DATA, "The pack code is already used in an existing pack");
....@@ -213,7 +211,7 @@
213211 MediaType.TEXT_PLAIN
214212 })
215213 public Response getCodeSuffix(@PathParam("packId") Integer packId, @Context BasicSecurityContext bsc) throws SeCurisServiceException {
216
- EntityManager em = emProvider.get();
214
+ // EntityManager em = emProvider.get();
217215
218216 if (packId == null) {
219217 throw new SeCurisServiceException(ErrorCodes.INVALID_DATA, "The pack code is mandatory");
....@@ -250,7 +248,7 @@
250248 })
251249 public Response modify(Pack pack, @PathParam("packId") Integer packId) {
252250 LOG.info("Modifying pack with id: {}", packId);
253
- EntityManager em = emProvider.get();
251
+ // EntityManager em = emProvider.get();
254252 Pack currentPack = em.find(Pack.class, packId);
255253
256254 try {
....@@ -305,7 +303,7 @@
305303 })
306304 public Response activate(@PathParam("packId") Integer packId) throws SeCurisServiceException {
307305 LOG.info("Activating pack with id: {}", packId);
308
- EntityManager em = emProvider.get();
306
+ // EntityManager em = emProvider.get();
309307
310308 Pack currentPack = em.find(Pack.class, packId);
311309
....@@ -331,7 +329,7 @@
331329 })
332330 public Response onhold(@PathParam("packId") Integer packId) throws SeCurisServiceException {
333331 LOG.info("Putting On hold pack with id: {}", packId);
334
- EntityManager em = emProvider.get();
332
+ // EntityManager em = emProvider.get();
335333
336334 Pack currentPack = em.find(Pack.class, packId);
337335
....@@ -358,7 +356,7 @@
358356 public Response cancel(@PathParam("packId") Integer packId, @FormParam("reason") String reason, @Context BasicSecurityContext bsc)
359357 throws SeCurisServiceException {
360358 LOG.info("Cancelling pack with id: {}", packId);
361
- EntityManager em = emProvider.get();
359
+ // EntityManager em = emProvider.get();
362360
363361 Pack currentPack = em.find(Pack.class, packId);
364362
....@@ -401,7 +399,7 @@
401399 })
402400 public Response delete(@PathParam("packId") String packId) throws SeCurisServiceException {
403401 LOG.info("Deleting pack with id: {}", packId);
404
- EntityManager em = emProvider.get();
402
+ // EntityManager em = emProvider.get();
405403 Pack pack = em.find(Pack.class, Integer.parseInt(packId));
406404 if (pack == null) {
407405 LOG.error("Pack with id {} can not be deleted, It was not found in DB", packId);
securis/src/main/java/net/curisit/securis/services/UserResource.java
....@@ -6,12 +6,13 @@
66 import java.util.Set;
77
88 import javax.annotation.security.RolesAllowed;
9
+import javax.enterprise.context.RequestScoped;
910 import javax.inject.Inject;
10
-import javax.inject.Provider;
1111 import javax.persistence.EntityManager;
1212 import javax.persistence.PersistenceException;
1313 import javax.persistence.TypedQuery;
1414 import javax.servlet.http.HttpServletRequest;
15
+import javax.transaction.Transactional;
1516 import javax.ws.rs.Consumes;
1617 import javax.ws.rs.DELETE;
1718 import javax.ws.rs.FormParam;
....@@ -35,12 +36,12 @@
3536 import net.curisit.securis.db.User;
3637 import net.curisit.securis.security.BasicSecurityContext;
3738 import net.curisit.securis.security.Securable;
39
+import net.curisit.securis.services.exception.SeCurisServiceException;
40
+import net.curisit.securis.services.exception.SeCurisServiceException.ErrorCodes;
3841 import net.curisit.securis.utils.TokenHelper;
3942
4043 import org.apache.logging.log4j.LogManager;
4144 import org.apache.logging.log4j.Logger;
42
-
43
-import com.google.inject.persist.Transactional;
4445
4546 /**
4647 * User resource
....@@ -48,13 +49,14 @@
4849 * @author roberto <roberto.sanchez@curisit.net>
4950 */
5051 @Path("/user")
52
+@RequestScoped
5153 public class UserResource {
5254
5355 @Inject
5456 TokenHelper tokenHelper;
5557
5658 @Inject
57
- Provider<EntityManager> emProvider;
59
+ EntityManager em;
5860
5961 private static final Logger LOG = LogManager.getLogger(UserResource.class);
6062
....@@ -75,7 +77,7 @@
7577 public Response index() {
7678 LOG.info("Getting users list ");
7779
78
- EntityManager em = emProvider.get();
80
+ // EntityManager em = emProvider.get();
7981 em.clear();
8082 TypedQuery<User> q = em.createNamedQuery("list-users", User.class);
8183
....@@ -102,7 +104,7 @@
102104 return Response.status(Status.NOT_FOUND).build();
103105 }
104106
105
- EntityManager em = emProvider.get();
107
+ // EntityManager em = emProvider.get();
106108 em.clear();
107109 User lt = em.find(User.class, uid);
108110 if (lt == null) {
....@@ -123,7 +125,7 @@
123125 @RolesAllowed(BasicSecurityContext.ROL_ADMIN)
124126 public Response create(User user, @HeaderParam(TokenHelper.TOKEN_HEADER_PÀRAM) String token) {
125127 LOG.info("Creating new user");
126
- EntityManager em = emProvider.get();
128
+ // EntityManager em = emProvider.get();
127129 User currentUser = em.find(User.class, user.getUsername());
128130 if (currentUser != null) {
129131 LOG.info("User with id {} was found in DB, we'll try to modify it", user.getUsername());
....@@ -179,7 +181,7 @@
179181 @RolesAllowed(BasicSecurityContext.ROL_ADMIN)
180182 public Response modify(User user, @PathParam("uid") String uid, @HeaderParam(TokenHelper.TOKEN_HEADER_PÀRAM) String token) {
181183 LOG.info("Modifying user with id: {}", uid);
182
- EntityManager em = emProvider.get();
184
+ // EntityManager em = emProvider.get();
183185 User currentUser = em.find(User.class, uid);
184186 if (currentUser == null) {
185187 LOG.info("User with id {} not found in DB, we'll try to create it", uid);
....@@ -222,7 +224,7 @@
222224 @RolesAllowed(BasicSecurityContext.ROL_ADMIN)
223225 public Response delete(@PathParam("uid") String uid, @Context HttpServletRequest request) {
224226 LOG.info("Deleting app with id: {}", uid);
225
- EntityManager em = emProvider.get();
227
+ // EntityManager em = emProvider.get();
226228 User app = em.find(User.class, uid);
227229 if (app == null) {
228230 LOG.error("User with id {} can not be deleted, It was not found in DB", uid);
....@@ -238,23 +240,23 @@
238240 @Produces({
239241 MediaType.APPLICATION_JSON
240242 })
241
- public Response login(@FormParam("username") String username, @FormParam("password") String password, @Context HttpServletRequest request) {
243
+ public Response login(@FormParam("username") String username, @FormParam("password") String password, @Context HttpServletRequest request)
244
+ throws SeCurisServiceException {
242245 LOG.info("index session: " + request.getSession());
243246 LOG.info("user: {}, pass: {}", username, password);
244247 LOG.info("is user in role: {} == {} ? ", "advance", request.isUserInRole("advance"));
245248 LOG.info("is user in role: {} == {} ? ", "admin", request.isUserInRole("admin"));
246249
247
- EntityManager em = emProvider.get();
250
+ // EntityManager em = emProvider.get();
248251 User user = em.find(User.class, username);
249252 if (user == null) {
250253 LOG.error("Inknown username {} used in login service", username);
251
- return Response.status(Status.UNAUTHORIZED).build();
254
+ throw new SeCurisServiceException(ErrorCodes.UNAUTHORIZED_ACCESS, "Wrong credentials");
252255 }
253256 String securedPassword = Utils.sha256(password);
254257
255258 if (securedPassword == null || !securedPassword.equals(user.getPassword())) {
256
- // TODO: Code to test exception handling
257
- return Response.status(Status.UNAUTHORIZED).build();
259
+ throw new SeCurisServiceException(ErrorCodes.UNAUTHORIZED_ACCESS, "Wrong credentials");
258260 }
259261 user.setLastLogin(new Date());
260262 em.getTransaction().begin();
securis/src/main/java/net/curisit/securis/services/helpers/LicenseHelper.java
....@@ -8,8 +8,8 @@
88 import java.util.Map;
99 import java.util.Set;
1010
11
+import javax.enterprise.context.ApplicationScoped;
1112 import javax.inject.Inject;
12
-import javax.inject.Singleton;
1313 import javax.persistence.EntityManager;
1414 import javax.persistence.TypedQuery;
1515
....@@ -28,7 +28,7 @@
2828 import org.apache.logging.log4j.LogManager;
2929 import org.apache.logging.log4j.Logger;
3030
31
-@Singleton
31
+@ApplicationScoped
3232 public class LicenseHelper {
3333
3434 @SuppressWarnings("unused")
securis/src/main/java/net/curisit/securis/services/helpers/UserHelper.java
....@@ -1,6 +1,6 @@
11 package net.curisit.securis.services.helpers;
22
3
-import javax.inject.Singleton;
3
+import javax.enterprise.context.ApplicationScoped;
44 import javax.persistence.EntityManager;
55 import javax.ws.rs.core.Response.Status;
66
....@@ -8,7 +8,7 @@
88 import net.curisit.securis.security.BasicSecurityContext;
99 import net.curisit.securis.services.exception.SeCurisServiceException;
1010
11
-@Singleton
11
+@ApplicationScoped
1212 public class UserHelper {
1313
1414 public User getUser(BasicSecurityContext bsc, EntityManager em) throws SeCurisServiceException {
securis/src/main/java/net/curisit/securis/utils/CacheTTL.java
....@@ -6,8 +6,8 @@
66 import java.util.List;
77 import java.util.Map;
88
9
+import javax.enterprise.context.ApplicationScoped;
910 import javax.inject.Inject;
10
-import javax.inject.Singleton;
1111
1212 import org.apache.logging.log4j.LogManager;
1313 import org.apache.logging.log4j.Logger;
....@@ -18,7 +18,7 @@
1818 *
1919 * @author roberto <roberto.sanchez@curisit.net>
2020 */
21
-@Singleton
21
+@ApplicationScoped
2222 public class CacheTTL {
2323
2424 private static final Logger LOG = LogManager.getLogger(CacheTTL.class);
securis/src/main/java/net/curisit/securis/utils/EmailManager.java
....@@ -14,7 +14,7 @@
1414 import java.util.concurrent.Executor;
1515 import java.util.concurrent.Executors;
1616
17
-import javax.inject.Singleton;
17
+import javax.enterprise.context.ApplicationScoped;
1818
1919 import net.curisit.securis.SeCurisException;
2020 import net.curisit.securis.services.exception.SeCurisServiceException;
....@@ -45,7 +45,7 @@
4545 *
4646 * @author roberto <roberto.sanchez@curisit.net>
4747 */
48
-@Singleton
48
+@ApplicationScoped
4949 public class EmailManager {
5050
5151 private static final Logger LOG = LogManager.getLogger(EmailManager.class);
securis/src/main/java/net/curisit/securis/utils/TokenHelper.java
....@@ -7,8 +7,8 @@
77 import java.security.NoSuchAlgorithmException;
88 import java.util.Date;
99
10
+import javax.enterprise.context.ApplicationScoped;
1011 import javax.inject.Inject;
11
-import javax.inject.Singleton;
1212
1313 import net.curisit.integrity.commons.Utils;
1414
....@@ -17,7 +17,7 @@
1717 import org.apache.logging.log4j.Logger;
1818 import org.jboss.resteasy.util.Base64;
1919
20
-@Singleton
20
+@ApplicationScoped
2121 public class TokenHelper {
2222
2323 private static final Logger LOG = LogManager.getLogger(TokenHelper.class);
securis/src/main/resources/META-INF/persistence.xml
....@@ -2,19 +2,15 @@
22 <persistence version="2.0"
33 xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
44 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" transaction-type="RESOURCE_LOCAL">
5
+ <persistence-unit name="localdb" >
66 <description>SeCuris LocalDB</description>
77 <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
8
+ <non-jta-data-source>java:comp/env/SeCurisDS</non-jta-data-source>
89 <shared-cache-mode>NONE</shared-cache-mode>
9
-
10
-<!-- <jta-data-source>java:comp/env/jdbc/SeCurisDS</jta-data-source>
11
- -->
12
- <!-- <class>net.curisit.securis.db.UserSettingsUnits</class> -->
13
-
1410 <properties>
1511 <property name="hibernate.cache.provider_class" value="org.hibernate.cache.internal.NoCachingRegionFactory"/>
1612 <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect" />
17
- <property name="hibernate.connection.datasource" value="java:comp/env/jdbc/SeCurisDS" />
13
+ <!-- <property name="hibernate.connection.datasource" value="java:comp/env/jdbc/SeCurisDS" /> -->
1814
1915 <property name="hibernate.cache.use_second_level_cache" value="false" />
2016 <!-- <property name="hibernate.show_sql" value="true" /> -->
securis/src/main/webapp/WEB-INF/beans.xml
....@@ -0,0 +1,7 @@
1
+<beans xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2
+ xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/beans_1_1.xsd">
3
+<interceptors>
4
+ <class>net.curisit.securis.ioc.TransactionsManager</class>
5
+ </interceptors>
6
+</beans>
7
+
securis/src/main/webapp/WEB-INF/web.xml
....@@ -29,10 +29,6 @@
2929
3030
3131 <context-param>
32
- <param-name>resteasy.guice.modules</param-name>
33
- <param-value>net.curisit.securis.ioc.SecurisModule,net.curisit.securis.ioc.RequestsModule</param-value>
34
- </context-param>
35
- <context-param>
3632 <param-name>resteasy.role.based.security</param-name>
3733 <param-value>true</param-value>
3834 </context-param>
....@@ -40,27 +36,27 @@
4036 <param-name>resteasy.providers</param-name>
4137 <param-value>net.curisit.securis.DefaultExceptionHandler</param-value>
4238 </context-param>
39
+ <context-param>
40
+ <param-name>resteasy.injector.factory</param-name>
41
+ <param-value>org.jboss.resteasy.cdi.CdiInjectorFactory</param-value>
42
+ </context-param>
4343
44
-
44
+
4545 <filter>
4646 <filter-name>Resteasy</filter-name>
4747 <filter-class>
4848 org.jboss.resteasy.plugins.server.servlet.FilterDispatcher
4949 </filter-class>
50
-
50
+ <init-param>
51
+ <param-name>javax.ws.rs.Application</param-name>
52
+ <param-value>net.curisit.securis.RestServicesApplication</param-value>
53
+ </init-param>
5154 </filter>
5255
5356 <filter-mapping>
5457 <filter-name>Resteasy</filter-name>
5558 <url-pattern>/*</url-pattern>
5659 </filter-mapping>
57
-
58
-
59
- <listener>
60
- <listener-class>
61
- net.curisit.securis.BootstrapListener
62
- </listener-class>
63
- </listener>
6460
6561 <welcome-file-list>
6662 <welcome-file>/index.jsp</welcome-file>
....@@ -82,9 +78,13 @@
8278 </security-role>
8379
8480 <resource-env-ref>
85
- <resource-env-ref-name>jdbc/SeCurisDS</resource-env-ref-name>
81
+ <resource-env-ref-name>SeCurisDS</resource-env-ref-name>
8682 <resource-env-ref-type>javax.sql.DataSource</resource-env-ref-type>
87
- <res-auth>Container</res-auth>
83
+</resource-env-ref>
84
+
85
+<resource-env-ref>
86
+ <resource-env-ref-name>BeanManager</resource-env-ref-name>
87
+ <resource-env-ref-type>javax.enterprise.inject.spi.BeanManager</resource-env-ref-type>
8888 </resource-env-ref>
8989
9090 </web-app>