Changeset 1fd3b33 in murachi


Ignore:
Timestamp:
Jul 29, 2015, 4:47:08 PM (9 years ago)
Author:
antonioaraujob <aaraujo@…>
Branches:
master
Children:
34d85b6
Parents:
0111cff
Message:

Agregado los siguientes recursos: /bdocs/cargas que crea un contenedor BDOC, inserta los archivos subidos y retorna el identificador único del contenedor. /bdocs/cargas/{idContainer} agrega los archivos subidos al contenedor BDOC existente que tiene identificador idContainer.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • murachi/src/main/java/ve/gob/cenditel/murachi/MurachiRESTWS.java

    r0111cff r1fd3b33  
    88import java.io.File;
    99import java.io.FileInputStream;
     10import java.io.FileNotFoundException;
    1011import java.io.FileOutputStream;
    1112import java.io.IOException;
     
    3839import java.util.HashMap;
    3940import java.util.List;
     41import java.util.Map;
    4042import java.util.Map.Entry;
    4143import java.util.UUID;
     
    5961import org.bouncycastle.jce.provider.BouncyCastleProvider;
    6062import org.bouncycastle.tsp.TimeStampToken;
     63import org.glassfish.jersey.media.multipart.FormDataBodyPart;
    6164import org.glassfish.jersey.media.multipart.FormDataContentDisposition;
     65import org.glassfish.jersey.media.multipart.FormDataMultiPart;
    6266import org.glassfish.jersey.media.multipart.FormDataParam;
    6367import org.json.JSONArray;
     
    22072211               
    22082212                try {
    2209                         Container deserializedContainer = deserializer(serializedContainerId);
     2213                        Container deserializedContainer = deserialize(serializedContainerId);
    22102214                       
    22112215                        byte[] signatureInBytes = hexStringToByteArray(signature);
     
    23352339        }
    23362340       
     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       
    23372492        // ************************************************************************
    23382493        // ************************************************************************
     
    24342589       
    24352590        /**
     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        /**
    24362615         * Agrega un archivo a un contendor BDOC existente
    24372616         * @param filePath ruta absoluta al archivo que se desea agregar
     
    24492628               
    24502629        }
     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       
    24512640       
    24522641        /**
     
    32013390       
    32023391       
     3392       
    32033393        /**
    32043394         * Prueba de ejecucion de programa desde consola. Incompleta
     
    32113401        public String executeProcess() throws InterruptedException {
    32123402               
    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                /*
    32143411                String line = "";
    32153412                OutputStream stdin = null;
     
    32423439                System.out.print("...saliendo");
    32433440                return line;
     3441                */
    32443442        }
    32453443       
     
    35693767           * @throws ClassNotFoundException
    35703768           */
    3571           private static Container deserializer(String filePath) throws IOException, ClassNotFoundException {
     3769          private static Container deserialize(String filePath) throws IOException, ClassNotFoundException {
    35723770                  //FileInputStream fileIn = new FileInputStream("container.bin");
    35733771                  FileInputStream fileIn = new FileInputStream(filePath);
Note: See TracChangeset for help on using the changeset viewer.