Changeset 1fd3b33 in murachi
- Timestamp:
- Jul 29, 2015, 4:47:08 PM (9 years ago)
- Branches:
- master
- Children:
- 34d85b6
- Parents:
- 0111cff
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
murachi/src/main/java/ve/gob/cenditel/murachi/MurachiRESTWS.java
r0111cff r1fd3b33 8 8 import java.io.File; 9 9 import java.io.FileInputStream; 10 import java.io.FileNotFoundException; 10 11 import java.io.FileOutputStream; 11 12 import java.io.IOException; … … 38 39 import java.util.HashMap; 39 40 import java.util.List; 41 import java.util.Map; 40 42 import java.util.Map.Entry; 41 43 import java.util.UUID; … … 59 61 import org.bouncycastle.jce.provider.BouncyCastleProvider; 60 62 import org.bouncycastle.tsp.TimeStampToken; 63 import org.glassfish.jersey.media.multipart.FormDataBodyPart; 61 64 import org.glassfish.jersey.media.multipart.FormDataContentDisposition; 65 import org.glassfish.jersey.media.multipart.FormDataMultiPart; 62 66 import org.glassfish.jersey.media.multipart.FormDataParam; 63 67 import org.json.JSONArray; … … 2207 2211 2208 2212 try { 2209 Container deserializedContainer = deserialize r(serializedContainerId);2213 Container deserializedContainer = deserialize(serializedContainerId); 2210 2214 2211 2215 byte[] signatureInBytes = hexStringToByteArray(signature); … … 2335 2339 } 2336 2340 2341 @POST 2342 @Path("/bdocs/cargas") 2343 @Consumes(MediaType.MULTIPART_FORM_DATA) 2344 @Produces(MediaType.APPLICATION_JSON) 2345 public Response uploadFilesToBDOC(FormDataMultiPart formParams) throws MurachiException { 2346 2347 logger.info("/bdocs/cargas"); 2348 logger.debug("uploadFilesToBDOC..."); 2349 2350 // cadena con la respuesta 2351 String result = ""; 2352 2353 System.out.println("uploadFilesToBDOC..."); 2354 2355 Security.addProvider(new BouncyCastleProvider()); 2356 Container c = createBDOCContainer(); 2357 2358 Map<String, List<FormDataBodyPart>> fieldsByName = formParams.getFields(); 2359 2360 for (List<FormDataBodyPart> fields : fieldsByName.values()) 2361 { 2362 for (FormDataBodyPart field : fields) 2363 { 2364 InputStream is = field.getEntityAs(InputStream.class); 2365 2366 FormDataContentDisposition file = field.getFormDataContentDisposition(); 2367 String fileName = file.getFileName(); 2368 String mimeType = field.getMediaType().toString(); 2369 System.out.println("fileName " + fileName); 2370 System.out.println("mime " + mimeType); 2371 2372 addFileToBDOCContainer(is, fileName, mimeType, c); 2373 2374 } 2375 } 2376 // se debe serializar no guardar 2377 2378 String fileId = SERVER_UPLOAD_LOCATION_FOLDER + UUID.randomUUID().toString(); 2379 System.out.println("id contenedor serializado: "+fileId); 2380 2381 // establecer el nombre del archivo a serializar 2382 String serializedContainerId = fileId + "-serialized"; 2383 2384 // serializar el archivo 2385 try { 2386 serialize(c, serializedContainerId); 2387 } catch (IOException e1) { 2388 logger.error("error en la serializacion del contendor"); 2389 e1.printStackTrace(); 2390 2391 result = "error al serializar el archivo"+serializedContainerId; 2392 result = "\"error\":\"no se pudo crear el contenedor\""; 2393 return Response.status(500).entity(result).build(); 2394 } 2395 result = "\"containerId\":\""+ serializedContainerId +"\""; 2396 2397 2398 return Response.status(200).entity(result).build(); 2399 } 2400 2401 @POST 2402 @Path("/bdocs/cargas/{containerId}") 2403 @Consumes(MediaType.MULTIPART_FORM_DATA) 2404 @Produces(MediaType.APPLICATION_JSON) 2405 public Response appendFilesToBDOC(FormDataMultiPart formParams, @PathParam("containerId") String containerId) throws MurachiException { 2406 2407 logger.info("/bdocs/cargas/{containerId}"); 2408 logger.debug("appendFilesToBDOC..."); 2409 2410 // cadena con la respuesta 2411 String result = ""; 2412 // ruta del contenedor serializado 2413 String serializedContainerId = ""; 2414 2415 System.out.println("appendFilesToBDOC..."); 2416 2417 System.out.println("containerId: "+ containerId); 2418 2419 String containerToOpen = SERVER_UPLOAD_LOCATION_FOLDER + containerId + ".bin"; 2420 2421 System.out.println("containerToOpen: "+ containerToOpen); 2422 2423 File f = new File(containerToOpen); 2424 2425 if (!f.exists()) { 2426 logger.error("el contenedor "+ containerToOpen + " no existe."); 2427 result = "\"error\":\"el contenedor "+ containerId + " no existe\""; 2428 return Response.status(404).entity(result).build(); 2429 } 2430 2431 try { 2432 2433 Security.addProvider(new BouncyCastleProvider()); 2434 2435 Container c; 2436 2437 c = deserialize(containerToOpen); 2438 2439 Map<String, List<FormDataBodyPart>> fieldsByName = formParams.getFields(); 2440 2441 for (List<FormDataBodyPart> fields : fieldsByName.values()) 2442 { 2443 for (FormDataBodyPart field : fields) 2444 { 2445 InputStream is = field.getEntityAs(InputStream.class); 2446 2447 FormDataContentDisposition file = field.getFormDataContentDisposition(); 2448 String fileName = file.getFileName(); 2449 String mimeType = field.getMediaType().toString(); 2450 System.out.println("fileName " + fileName); 2451 System.out.println("mime " + mimeType); 2452 2453 addFileToBDOCContainer(is, fileName, mimeType, c); 2454 } 2455 } 2456 // se debe serializar no guardar 2457 2458 String[] array = containerId.split("\\.bin"); 2459 2460 // establecer el nombre del archivo a serializar 2461 serializedContainerId = SERVER_UPLOAD_LOCATION_FOLDER + array[0]; 2462 2463 serialize(c, serializedContainerId); 2464 logger.debug("contenedor " + containerToOpen + " serializado."); 2465 2466 2467 } catch (ClassNotFoundException e) { 2468 logger.error("error ClassNotFoundException"); 2469 e.printStackTrace(); 2470 2471 result = "\"error\":\"no se pudo pudo agregar el archivo al contenedor\""; 2472 e.printStackTrace(); 2473 return Response.status(500).entity(result).build(); 2474 2475 } catch (IOException e) { 2476 logger.error("error en la serializacion del contenedor"); 2477 e.printStackTrace(); 2478 2479 result = "\"error\":\"no se pudo agregar el archivo al contenedor\""; 2480 return Response.status(500).entity(result).build(); 2481 } 2482 2483 logger.debug("archivo agregado correctamente " + containerToOpen); 2484 2485 result = "\"containerId\":\""+ serializedContainerId +"\""; 2486 2487 return Response.status(200).entity(result).build(); 2488 } 2489 2490 2491 2337 2492 // ************************************************************************ 2338 2493 // ************************************************************************ … … 2434 2589 2435 2590 /** 2591 * Abre un contenedor BDOC 2592 * @param pathToContainer ruta absoluta al contenedor BDOC 2593 * @return contenedor BDOC creado 2594 */ 2595 private Container openBDOCContainer(String pathToContainer) { 2596 logger.debug("openBDOCContainer() "); 2597 System.out.println("openBDOCContainer()"); 2598 2599 Container container = null; 2600 2601 Configuration configuration = new Configuration(Configuration.Mode.PROD); 2602 configuration.loadConfiguration(DIGIDOC4J_CONFIGURATION); 2603 configuration.setTslLocation(DIGIDOC4J_TSL_LOCATION); 2604 2605 container = Container.open(pathToContainer, configuration); 2606 2607 System.out.println("container opened"); 2608 logger.debug("container "+ pathToContainer + " opened"); 2609 2610 return container; 2611 } 2612 2613 2614 /** 2436 2615 * Agrega un archivo a un contendor BDOC existente 2437 2616 * @param filePath ruta absoluta al archivo que se desea agregar … … 2449 2628 2450 2629 } 2630 2631 private void addFileToBDOCContainer(InputStream is, String fileName, String mimeType, Container c) { 2632 logger.debug("addFileToBDOCContainer(InputStream) "); 2633 System.out.println("addFileToBDOCContainer(InputStream)"); 2634 2635 2636 c.addDataFile(is, fileName, mimeType); 2637 System.out.println("agregado archivo a contenedor"); 2638 } 2639 2451 2640 2452 2641 /** … … 3201 3390 3202 3391 3392 3203 3393 /** 3204 3394 * Prueba de ejecucion de programa desde consola. Incompleta … … 3211 3401 public String executeProcess() throws InterruptedException { 3212 3402 3213 3403 String s = "9712f235-4dd4-4b09-957d-b06c33af482b-serialized.bin"; 3404 String[] array = s.split("-serialized\\.bin"); 3405 3406 return array[0]; 3407 3408 3409 3410 /* 3214 3411 String line = ""; 3215 3412 OutputStream stdin = null; … … 3242 3439 System.out.print("...saliendo"); 3243 3440 return line; 3441 */ 3244 3442 } 3245 3443 … … 3569 3767 * @throws ClassNotFoundException 3570 3768 */ 3571 private static Container deserialize r(String filePath) throws IOException, ClassNotFoundException {3769 private static Container deserialize(String filePath) throws IOException, ClassNotFoundException { 3572 3770 //FileInputStream fileIn = new FileInputStream("container.bin"); 3573 3771 FileInputStream fileIn = new FileInputStream(filePath);
Note: See TracChangeset
for help on using the changeset viewer.