Changeset 34d85b6 in murachi
- Timestamp:
- Aug 6, 2015, 11:11:21 AM (9 years ago)
- Branches:
- master
- Children:
- a93a57c
- Parents:
- 1fd3b33
- Location:
- murachi/src/main/java/ve/gob/cenditel/murachi
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
murachi/src/main/java/ve/gob/cenditel/murachi/MurachiRESTWS.java
r1fd3b33 r34d85b6 2246 2246 2247 2247 /** 2248 * Retorna el numero de dataFile que se encuentran en un contenedor 2249 * 2250 * @param containerId 2251 * @return 2252 */ 2253 @GET 2254 @Path("/bdocs/archivos/{containerId}") 2255 @Produces(MediaType.APPLICATION_JSON) 2256 public Response getDataFileNumber(@PathParam("containerId") String containerId) { 2257 logger.info("/bdocs/archivos/"+containerId); 2258 2259 String fullPathBdocFile = SERVER_UPLOAD_LOCATION_FOLDER + containerId + ".bin"; 2260 logger.debug(fullPathBdocFile); 2261 2262 2263 JSONObject json = new JSONObject(); 2264 int dataFileNumber = 0; 2265 2266 Response response; 2267 2268 // Retrieve the file 2269 File file = new File(fullPathBdocFile); 2270 if (file.exists()) { 2271 2272 Security.addProvider(new BouncyCastleProvider()); 2273 2274 Configuration configuration = new Configuration(Configuration.Mode.PROD); 2275 configuration.loadConfiguration(DIGIDOC4J_CONFIGURATION); 2276 configuration.setTslLocation(DIGIDOC4J_TSL_LOCATION); 2277 2278 Container c; 2279 2280 try { 2281 c = deserialize(fullPathBdocFile); 2282 2283 dataFileNumber = c.getDataFiles().size(); 2284 logger.debug("dataFileNumber: "+ Integer.toString(dataFileNumber)); 2285 json.put("dataFileNumber", Integer.toString(dataFileNumber)); 2286 2287 response = Response.status(200).entity(json.toString()).build(); 2288 2289 } catch (ClassNotFoundException e) { 2290 // TODO Auto-generated catch block 2291 e.printStackTrace(); 2292 2293 json.put("error", "error interno al leer el contenedor"); 2294 response = Response.status(500).entity(json.toString()).build(); 2295 2296 } catch (IOException e) { 2297 // TODO Auto-generated catch block 2298 e.printStackTrace(); 2299 json.put("error", "no se pudo leer el contenedor"); 2300 response = Response.status(500).entity(json.toString()).build(); 2301 } 2302 } else { 2303 logger.error("El contenedor con id: "+containerId+ " no existe."); 2304 json.put("error", "El contenedor con id: "+containerId+ " no existe."); 2305 response = Response.status(404).entity(json.toString()).build(); 2306 } 2307 return response; 2308 } 2309 2310 2311 /** 2312 * Retorna lista de los dataFile que se encuentran en un contenedor 2313 * 2314 * @param containerId 2315 * @return lista de los dataFile que se encuentran en un contenedor 2316 */ 2317 @GET 2318 @Path("/bdocs/archivos/lista/{containerId}") 2319 @Produces(MediaType.APPLICATION_JSON) 2320 public Response getDataFileList(@PathParam("containerId") String containerId) { 2321 2322 2323 logger.info("/bdocs/archivos/lista/"+containerId); 2324 2325 String fullPathBdocFile = SERVER_UPLOAD_LOCATION_FOLDER + containerId + ".bin"; 2326 logger.debug(fullPathBdocFile); 2327 2328 JSONObject jsonDataFile = new JSONObject(); 2329 int dataFileNumber = 0; 2330 2331 Response response = null; 2332 2333 // Retrieve the file 2334 File file = new File(fullPathBdocFile); 2335 if (file.exists()) { 2336 2337 Security.addProvider(new BouncyCastleProvider()); 2338 2339 Configuration configuration = new Configuration(Configuration.Mode.PROD); 2340 configuration.loadConfiguration(DIGIDOC4J_CONFIGURATION); 2341 configuration.setTslLocation(DIGIDOC4J_TSL_LOCATION); 2342 2343 Container c; 2344 String dataFileName = ""; 2345 Long dataFileSize = (long) 0; 2346 2347 try { 2348 c = deserialize(fullPathBdocFile); 2349 2350 dataFileNumber = c.getDataFiles().size(); 2351 2352 if (dataFileNumber > 0) 2353 { 2354 for (int i=0; i<dataFileNumber; i++) 2355 { 2356 // el dataFile es valido 2357 DataFile df = c.getDataFile(i); 2358 dataFileName = df.getName(); 2359 dataFileSize = df.getFileSize(); 2360 logger.debug("obtenido DataFile: "+Integer.toString(i)); 2361 logger.debug("DataFile name: "+ dataFileName); 2362 logger.debug("DataFile size: "+ Long.toString(dataFileSize)); 2363 } 2364 jsonDataFile.put("dataFiles", getJSONFromBDOCDataFiles(c.getDataFiles())); 2365 response = Response.status(200).entity(jsonDataFile.toString()).build(); 2366 } 2367 else 2368 { 2369 logger.debug("dataFileNumber: "+ Integer.toString(dataFileNumber)); 2370 jsonDataFile.put("dataFileNumber", Integer.toString(dataFileNumber)); 2371 response = Response.status(200).entity(jsonDataFile.toString()).build(); 2372 } 2373 2374 } catch (ClassNotFoundException e) { 2375 // TODO Auto-generated catch block 2376 e.printStackTrace(); 2377 2378 jsonDataFile.put("error", "error interno al leer el contenedor"); 2379 response = Response.status(500).entity(jsonDataFile.toString()).build(); 2380 2381 } catch (IOException e) { 2382 // TODO Auto-generated catch block 2383 e.printStackTrace(); 2384 jsonDataFile.put("error", "no se pudo leer el contenedor"); 2385 response = Response.status(500).entity(jsonDataFile.toString()).build(); 2386 } 2387 } else { 2388 logger.error("El contenedor con id: "+containerId+ " no existe."); 2389 jsonDataFile.put("error", "El contenedor con id: "+containerId+ " no existe."); 2390 response = Response.status(404).entity(jsonDataFile.toString()).build(); 2391 } 2392 return response; 2393 } 2394 2395 2396 2397 2398 2399 /** 2248 2400 * Descarga un archivo que se encuentra dentro de un contenedor BDOC. 2249 2401 * … … 2278 2430 */ 2279 2431 @GET 2280 @Path("/bdocs/archivos/{ fileId}/{dataFileId}")2432 @Path("/bdocs/archivos/{containerId}/{dataFileId}") 2281 2433 @Produces(MediaType.APPLICATION_OCTET_STREAM) 2282 public Response downloadDataFileFromBDOC(@PathParam(" fileId") String fileId, @PathParam("dataFileId") int dataFileId) {2283 logger.info("/bdocs/archivos/"+ fileId+"/"+Integer.toString(dataFileId));2434 public Response downloadDataFileFromBDOC(@PathParam("containerId") String containerId, @PathParam("dataFileId") int dataFileId) { 2435 logger.info("/bdocs/archivos/"+containerId+"/"+Integer.toString(dataFileId)); 2284 2436 2285 2437 logger.debug("dataFileId: " + Integer.toString(dataFileId)); 2286 2438 2287 String fullPathBdocFile = SERVER_UPLOAD_LOCATION_FOLDER + fileId;2439 String fullPathBdocFile = SERVER_UPLOAD_LOCATION_FOLDER + containerId; 2288 2440 2289 2441 Response response; … … 2332 2484 2333 2485 } else { 2334 logger.error("El archivo con id: "+ fileId+ " no existe.");2486 logger.error("El archivo con id: "+containerId+ " no existe."); 2335 2487 response = Response.status(404).entity("{\"fileExist\": false}").type("text/plain").build(); 2336 2488 } … … 2339 2491 } 2340 2492 2493 2494 2495 /** 2496 * Retorna el numero de firmas que tiene en un contenedor 2497 * 2498 * @param containerId 2499 * @return 2500 */ 2501 @GET 2502 @Path("/bdocs/firmas/{containerId}") 2503 @Produces(MediaType.APPLICATION_JSON) 2504 public Response getSignatureNumber(@PathParam("containerId") String containerId) { 2505 logger.info("/bdocs/archivos/"+containerId); 2506 2507 String fullPathBdocFile = SERVER_UPLOAD_LOCATION_FOLDER + containerId + ".bin"; 2508 logger.debug(fullPathBdocFile); 2509 2510 2511 JSONObject json = new JSONObject(); 2512 int signatureNumber = 0; 2513 2514 Response response; 2515 2516 // Retrieve the file 2517 File file = new File(fullPathBdocFile); 2518 if (file.exists()) { 2519 2520 Security.addProvider(new BouncyCastleProvider()); 2521 2522 Configuration configuration = new Configuration(Configuration.Mode.PROD); 2523 configuration.loadConfiguration(DIGIDOC4J_CONFIGURATION); 2524 configuration.setTslLocation(DIGIDOC4J_TSL_LOCATION); 2525 2526 Container c; 2527 2528 try { 2529 c = deserialize(fullPathBdocFile); 2530 2531 signatureNumber = c.getSignatures().size(); 2532 2533 logger.debug("signatureNumber: "+ Integer.toString(signatureNumber)); 2534 json.put("signatureNumber", Integer.toString(signatureNumber)); 2535 2536 response = Response.status(200).entity(json.toString()).build(); 2537 2538 } catch (ClassNotFoundException e) { 2539 // TODO Auto-generated catch block 2540 e.printStackTrace(); 2541 2542 json.put("error", "error interno al leer el contenedor"); 2543 response = Response.status(500).entity(json.toString()).build(); 2544 2545 } catch (IOException e) { 2546 // TODO Auto-generated catch block 2547 e.printStackTrace(); 2548 json.put("error", "no se pudo leer el contenedor"); 2549 response = Response.status(500).entity(json.toString()).build(); 2550 } 2551 } else { 2552 logger.error("El contenedor con id: "+containerId+ " no existe."); 2553 json.put("error", "El contenedor con id: "+containerId+ " no existe."); 2554 response = Response.status(404).entity(json.toString()).build(); 2555 } 2556 return response; 2557 } 2558 2559 /** 2560 * Crea un contenedor BDOC con los archivos subidos del formulario, lo serializa y 2561 * retorna el identificados unico del contenedor. 2562 * 2563 * @param formParams parametros del formulario 2564 * 2565 * @return 2566 */ 2341 2567 @POST 2342 2568 @Path("/bdocs/cargas") … … 2376 2602 // se debe serializar no guardar 2377 2603 2378 String fileId = SERVER_UPLOAD_LOCATION_FOLDER +UUID.randomUUID().toString();2604 String fileId = UUID.randomUUID().toString(); 2379 2605 System.out.println("id contenedor serializado: "+fileId); 2380 2606 2381 2607 // establecer el nombre del archivo a serializar 2382 String serializedContainerId = fileId + "-serialized";2608 String serializedContainerId = SERVER_UPLOAD_LOCATION_FOLDER + fileId + "-serialized"; 2383 2609 2384 2610 // serializar el archivo … … 2389 2615 e1.printStackTrace(); 2390 2616 2391 result = "error al serializar el archivo"+ serializedContainerId;2617 result = "error al serializar el archivo"+fileId; 2392 2618 result = "\"error\":\"no se pudo crear el contenedor\""; 2393 2619 return Response.status(500).entity(result).build(); 2394 2620 } 2395 result = "\"containerId\":\""+ serializedContainerId +"\"";2621 result = "\"containerId\":\""+ fileId +"\""; 2396 2622 2397 2623 … … 2399 2625 } 2400 2626 2627 /** 2628 * Recibe los archivos enviador a través del formulario, deserializa el contenedor identificado 2629 * con containerId, agrega los archivos al contenedor, serializa de nuevo el contenedor y retorna 2630 * el identificador del contendor. 2631 * 2632 * @param formParams parametros del formulario 2633 * @param containerId identificador del contenedor pasado en la URL 2634 * 2635 * @return 2636 */ 2401 2637 @POST 2402 2638 @Path("/bdocs/cargas/{containerId}") … … 2417 2653 System.out.println("containerId: "+ containerId); 2418 2654 2419 String containerToOpen = SERVER_UPLOAD_LOCATION_FOLDER + containerId + " .bin";2655 String containerToOpen = SERVER_UPLOAD_LOCATION_FOLDER + containerId + "-serialized.bin"; 2420 2656 2421 2657 System.out.println("containerToOpen: "+ containerToOpen); … … 2459 2695 2460 2696 // establecer el nombre del archivo a serializar 2461 serializedContainerId = SERVER_UPLOAD_LOCATION_FOLDER + array[0];2697 serializedContainerId = SERVER_UPLOAD_LOCATION_FOLDER + containerId + "-serialized"; 2462 2698 2463 2699 serialize(c, serializedContainerId); … … 2483 2719 logger.debug("archivo agregado correctamente " + containerToOpen); 2484 2720 2485 result = "\"containerId\":\""+ serializedContainerId +"\"";2721 result = "\"containerId\":\""+ containerId +"\""; 2486 2722 2487 2723 return Response.status(200).entity(result).build(); 2724 } 2725 2726 2727 /** 2728 * Ejecuta la prefirma de un contenedor BDOC calculando el hash y enviandolo al cliente para que lo firme. 2729 * 2730 * @param presignPar 2731 * @param req 2732 * @return 2733 * @throws MurachiException 2734 */ 2735 @POST 2736 @Path("/bdocs/firmas/pre") 2737 @Consumes(MediaType.APPLICATION_JSON) 2738 @Produces(MediaType.APPLICATION_JSON) 2739 public Response presignBDOCContainer(PresignParametersBdoc presignPar) throws MurachiException { 2740 2741 logger.info("/bdocs/firmas/pre"); 2742 2743 PresignHash presignHash = new PresignHash(); 2744 2745 // obtener el id del archivo a firmaer 2746 String containerId = presignPar.getFileId(); 2747 2748 // cadena con el certificado 2749 String certHex = presignPar.getCertificate(); 2750 System.out.println("certificado en Hex: " + certHex); 2751 2752 String city = presignPar.getCity(); 2753 logger.debug("city: " + city); 2754 2755 String state = presignPar.getState(); 2756 logger.debug("state: " + state); 2757 2758 String postalCode = presignPar.getPostalCode(); 2759 logger.debug("postalCode: " + postalCode); 2760 2761 String country = presignPar.getCountry(); 2762 logger.debug("country: " + country); 2763 2764 String role = presignPar.getRole(); 2765 logger.debug("role: " + role); 2766 2767 Boolean addSignature = presignPar.getAddSignature(); 2768 logger.debug("addSignature: " + addSignature.toString()); 2769 2770 CertificateFactory cf; 2771 X509Certificate signerCert; 2772 2773 // 2774 String hashToSign = ""; 2775 2776 String result = ""; 2777 2778 SignedInfo signedInfo; 2779 2780 String fullPathContainerId = SERVER_UPLOAD_LOCATION_FOLDER + containerId + "-serialized.bin"; 2781 System.out.println("archivo a firmar: " + fullPathContainerId); 2782 logger.debug("archivo a firmar: " + fullPathContainerId); 2783 2784 String sourceFileMimeType = getMimeTypeWithTika(fullPathContainerId); 2785 logger.debug("mimeType del archivo a firmar: " + sourceFileMimeType); 2786 2787 certHex = presignPar.getCertificate(); 2788 System.out.println("certificado en Hex: " + certHex); 2789 logger.debug("certificado firmante en Hex: " + certHex); 2790 2791 File f = new File(fullPathContainerId); 2792 2793 if (!f.exists()) { 2794 logger.error("el contenedor "+ fullPathContainerId + " no existe."); 2795 result = "\"error\":\"el contenedor "+ fullPathContainerId + " no existe\""; 2796 return Response.status(404).entity(result).build(); 2797 } 2798 2799 try 2800 { 2801 Security.addProvider(new BouncyCastleProvider()); 2802 Container container = null; 2803 2804 Configuration configuration = new Configuration(Configuration.Mode.PROD); 2805 2806 configuration.loadConfiguration(DIGIDOC4J_CONFIGURATION); 2807 2808 configuration.setTslLocation(DIGIDOC4J_TSL_LOCATION); 2809 2810 // deserializar el contenedor 2811 container = deserialize(fullPathContainerId); 2812 logger.debug("deserializado el contenedor: " + fullPathContainerId); 2813 2814 SignatureParameters signatureParameters = new SignatureParameters(); 2815 SignatureProductionPlace productionPlace = new SignatureProductionPlace(); 2816 productionPlace.setCity(city); 2817 productionPlace.setStateOrProvince(state); 2818 productionPlace.setPostalCode(postalCode); 2819 productionPlace.setCountry(country); 2820 signatureParameters.setProductionPlace(productionPlace); 2821 logger.debug("container setProductionPlace"); 2822 2823 signatureParameters.setRoles(asList(role)); 2824 container.setSignatureParameters(signatureParameters); 2825 container.setSignatureProfile(SignatureProfile.B_BES); 2826 2827 cf = CertificateFactory.getInstance("X.509"); 2828 2829 InputStream in = new ByteArrayInputStream(hexStringToByteArray(certHex)); 2830 2831 signerCert = (X509Certificate) cf.generateCertificate(in); 2832 2833 signedInfo = container.prepareSigning(signerCert); 2834 2835 hashToSign = byteArrayToHexString(signedInfo.getDigest()); 2836 2837 System.out.println("presignBdoc - hash: " + hashToSign); 2838 logger.debug("hash to sign: " + hashToSign); 2839 2840 2841 String[] array = containerId.split("\\.bin"); 2842 2843 // establecer el nombre del archivo a serializar 2844 String serializedContainerId = SERVER_UPLOAD_LOCATION_FOLDER + containerId + "-serialized"; 2845 logger.debug("serializedContainerId: " + serializedContainerId); 2846 2847 // serializar el archivo 2848 serialize(container, serializedContainerId); 2849 logger.debug("serializado el contenedor: " + serializedContainerId); 2850 2851 } catch(IOException e) 2852 { 2853 presignHash.setError(e.getMessage()); 2854 presignHash.setHash(""); 2855 return Response.status(500).entity(presignHash).build(); 2856 } catch(CertificateException e) 2857 { 2858 presignHash.setError(e.getMessage()); 2859 presignHash.setHash(""); 2860 return Response.status(500).entity(presignHash).build(); 2861 } catch (ClassNotFoundException e) { 2862 presignHash.setError(e.getMessage()); 2863 presignHash.setHash(""); 2864 return Response.status(500).entity(presignHash).build(); 2865 } 2866 2867 2868 2869 // creacion del json 2870 JSONObject jsonHash = new JSONObject(); 2871 jsonHash.put("hashToSign", hashToSign); 2872 2873 presignHash.setHash(hashToSign); 2874 presignHash.setError(""); 2875 2876 logger.debug("presignBDOCContainer: "+ presignHash.getHash()); 2877 return Response.status(200).entity(presignHash).build(); 2878 } 2879 2880 2881 2882 2883 @POST 2884 @Path("/bdocs/firmas/post") 2885 @Consumes(MediaType.APPLICATION_JSON) 2886 @Produces(MediaType.APPLICATION_JSON) 2887 public Response postsignBDOCContainer(PostsignParameters postsignPar) throws IOException, MurachiException { 2888 2889 logger.info("/bdocs/resenas"); 2890 2891 // cadena con la firma 2892 String signature = postsignPar.getSignature(); 2893 System.out.println("firma en Hex: " + signature); 2894 logger.info("firma en Hex: " + signature); 2895 2896 // obtener el id del archivo a firmar 2897 String containerId = postsignPar.getContainerId(); 2898 2899 /* 2900 HttpSession session = req.getSession(false); 2901 2902 String fileId = (String) session.getAttribute("fileId"); 2903 System.out.println("fileId: " + fileId); 2904 logger.debug("fileId: " + fileId); 2905 */ 2906 2907 String signedBdoc = containerId + ".bdoc"; 2908 logger.debug("sigendBdoc: " + signedBdoc); 2909 2910 String serializedContainerId = SERVER_UPLOAD_LOCATION_FOLDER + containerId + "-serialized.bin"; 2911 2912 System.out.println("serializedContainerId: " + serializedContainerId); 2913 logger.debug("serializedContainerId: " + serializedContainerId); 2914 2915 try { 2916 Container deserializedContainer = deserialize(serializedContainerId); 2917 logger.debug("deserializado el contenedor"); 2918 2919 byte[] signatureInBytes = hexStringToByteArray(signature); 2920 2921 deserializedContainer.signRaw(signatureInBytes); 2922 logger.debug("asignada firma al contenedor"); 2923 2924 2925 deserializedContainer.save(SERVER_UPLOAD_LOCATION_FOLDER + signedBdoc); 2926 logger.debug("guardado el contenedor: " + signedBdoc); 2927 2928 // 2929 File f = new File(serializedContainerId); 2930 f.delete(); 2931 2932 logger.debug("archivo firmado escrito en: " + signedBdoc); 2933 } catch (ClassNotFoundException e) { 2934 //e.printStackTrace(); 2935 2936 JSONObject jsonError = new JSONObject(); 2937 2938 System.out.println("ClassNotFoundException e: " + e.getMessage()); 2939 logger.error("error: " + e.getMessage()); 2940 2941 jsonError.put("error", e.getMessage()); 2942 return Response.status(500).entity(jsonError).build(); 2943 } 2944 2945 // en este punto el archivo bdoc debe estar disponible en la ruta 2946 // SERVER_UPLOAD_LOCATION_FOLDER + fileId; 2947 System.out.println("Archivo firmado correctamente"); 2948 logger.debug("Archivo firmado correctamente"); 2949 2950 PostsignMessage message = new PostsignMessage(); 2951 message.setMessage("{\"signedFile\":"+ signedBdoc); 2952 2953 JSONObject jsonFinalResult = new JSONObject(); 2954 jsonFinalResult.put("signedFileId", signedBdoc); 2955 2956 logger.info(jsonFinalResult.toString()); 2957 return Response.status(200).entity(jsonFinalResult.toString()).build(); 2488 2958 } 2489 2959 … … 2618 3088 */ 2619 3089 private void addFileToBDOCContainer(String filePath, Container c) { 3090 2620 3091 logger.debug("addFileToBDOCContainer() "); 2621 3092 System.out.println("addFileToBDOCContainer()"); 2622 3093 /* 2623 3094 String mime = getMimeTypeWithTika(filePath); 2624 3095 System.out.println("mimeType: " + mime); … … 2626 3097 c.addDataFile(filePath, mime); 2627 3098 System.out.println("agregado archivo a contenedor"); 3099 */ 2628 3100 2629 3101 } -
murachi/src/main/java/ve/gob/cenditel/murachi/PostsignParameters.java
rb3b099f r34d85b6 13 13 private String signature; 14 14 15 private String containerId; 16 15 17 public String getSignature() { 16 18 return signature; … … 21 23 } 22 24 25 public String getContainerId() { 26 return containerId; 27 } 28 29 public void setContainerId(String containerId) { 30 this.containerId = containerId; 31 } 32 23 33 }
Note: See TracChangeset
for help on using the changeset viewer.