Roberto Sánchez
2014-01-26 6d8aff7b3657332020ef215eb1b2fc16017e4cc8
securis/src/main/java/net/curisit/securis/services/LicenseResource.java
....@@ -1,5 +1,6 @@
11 package net.curisit.securis.services;
22
3
+import java.io.IOException;
34 import java.util.Date;
45 import java.util.List;
56
....@@ -21,6 +22,7 @@
2122 import javax.ws.rs.core.Response;
2223 import javax.ws.rs.core.Response.Status;
2324
25
+import net.curisit.integrity.commons.JsonUtils;
2426 import net.curisit.integrity.commons.Utils;
2527 import net.curisit.integrity.exception.CurisException;
2628 import net.curisit.securis.DefaultExceptionHandler;
....@@ -31,6 +33,7 @@
3133 import net.curisit.securis.security.Securable;
3234 import net.curisit.securis.utils.TokenHelper;
3335
36
+import org.jboss.resteasy.plugins.providers.multipart.MultipartFormDataInput;
3437 import org.slf4j.Logger;
3538 import org.slf4j.LoggerFactory;
3639
....@@ -124,7 +127,7 @@
124127 { MediaType.APPLICATION_JSON })
125128 @Transactional
126129 public Response create(License lic, @Context BasicSecurityContext bsc) {
127
- log.info("Creating new organization");
130
+ log.info("Creating new license from create()");
128131 EntityManager em = emProvider.get();
129132 Pack pack = null;
130133 if (lic.getPackId() != null) {
....@@ -143,7 +146,7 @@
143146 }
144147
145148 try {
146
- User createdBy = getUser(lic.getCreatedById(), em);
149
+ User createdBy = getUser(bsc.getUserPrincipal().getName(), em);
147150 lic.setCreatedBy(createdBy);
148151 } catch (CurisException ex) {
149152 String createdByUsername = lic.getCreatedById();
....@@ -161,6 +164,31 @@
161164 return Response.ok(lic).build();
162165 }
163166
167
+ @POST
168
+ @Path("/")
169
+ @Consumes(MediaType.MULTIPART_FORM_DATA)
170
+ @Securable
171
+ @Produces(
172
+ { MediaType.APPLICATION_JSON })
173
+ @Transactional
174
+ public Response createWithFile(MultipartFormDataInput mpfdi, @Context BasicSecurityContext bsc) throws IOException {
175
+ License lic = new License();
176
+ lic.setCode(mpfdi.getFormDataPart("code", String.class, null));
177
+ lic.setRequestData(mpfdi.getFormDataPart("request_data", String.class, null));
178
+ lic.setPackId(mpfdi.getFormDataPart("pack_id", Integer.class, null));
179
+ lic.setFullName(mpfdi.getFormDataPart("full_name", String.class, null));
180
+ lic.setEmail(mpfdi.getFormDataPart("email", String.class, null));
181
+ lic.setComments(mpfdi.getFormDataPart("comments", String.class, null));
182
+ try {
183
+ log.info("File content: {}", lic.getRequestData());
184
+ log.info("License read from multipart: {}", JsonUtils.toJSON(lic));
185
+ } catch (CurisException e) {
186
+ // TODO Auto-generated catch block
187
+ e.printStackTrace();
188
+ }
189
+ return create(lic, bsc);
190
+ }
191
+
164192 private User getUser(String username, EntityManager em) throws CurisException {
165193 User user = null;
166194 if (username != null) {