Beispiel #1
0
  @POST
  @Path("/downloadAsset")
  @Consumes(MediaType.APPLICATION_XML)
  @Produces(MediaType.APPLICATION_XML)
  public Response downloadAsset(Command command) throws IOException {

    Hidra hidra;
    Zipper zipper = new Zipper();
    String path = command.getRepositoryPath();
    String assetName = command.getAssetName();
    ResultMessage result = new ResultMessage();

    if (path.isEmpty() || assetName.isEmpty()) {
      throw new DataNotFoundException(
          "The absolute path of the repository Or Asset Name has not been set");
    }
    if (!new File(path).exists()) {
      throw new IOException("Could not find the informed repository");
    }
    if (!new File(path + separator + assetName).exists()) {
      throw new IOException("Asset informed was not found");
    }

    hidra = new Hidra(path);
    File assetFile = new File(path + separator + assetName);
    File destiny = new File(path + DOWNLOAD_PATH_TEMP + separator + assetName + extension);

    if (hidra.findAsset(assetName)) {
      zipper.criarZip(assetFile, assetFile.listFiles());
      zipper.getArquivoZipAtual().renameTo(new File(destiny, assetName));

      String downloadedFileLocation =
          System.getProperty("user.home") + separator + assetName + extension;

      InputStream in;
      int read = 0;
      byte[] bytes = new byte[1024];

      try {
        OutputStream out = new FileOutputStream(new File(downloadedFileLocation));
        in = new FileInputStream(zipper.getArquivoZipAtual());
        try {
          while ((read = in.read(bytes)) != -1) {
            out.write(bytes, 0, read);
          }
          out.flush();
          out.close();
          zipper.getArquivoZipAtual().delete();
          result.setMessage("File download successfully");
          result.setStatusMessage(Status.OK);
        } catch (IOException ex) {
          throw new DataNotFoundException(ex.getMessage());
        }
      } catch (FileNotFoundException ex) {
        throw new DataNotFoundException(ex.getMessage());
      }

      return Response.status(Status.OK).entity(result).build();
    }
    throw new DataNotFoundException("Canot find the informed Asset");
  }