Ejemplo n.º 1
0
  /**
   * Writes an entity to a given ZIP output stream with a given ZIP entry name.
   *
   * @param entity The entity to write.
   * @param out The ZIP output stream.
   * @param entryName The ZIP entry name.
   * @return True if the writing was successful.
   * @throws IOException
   */
  private boolean writeEntityStream(Representation entity, ZipOutputStream out, String entryName)
      throws IOException {
    if (entity != null && !entryName.endsWith("/")) {
      ZipEntry entry = new ZipEntry(entryName);
      if (entity.getModificationDate() != null)
        entry.setTime(entity.getModificationDate().getTime());
      else {
        entry.setTime(System.currentTimeMillis());
      }
      out.putNextEntry(entry);
      BioUtils.copy(new BufferedInputStream(entity.getStream()), out);
      out.closeEntry();
      return true;
    }

    out.putNextEntry(new ZipEntry(entryName));
    out.closeEntry();
    return false;
  }
Ejemplo n.º 2
0
  /**
   * Handles a PUT call.
   *
   * @param request The request to answer.
   * @param response The response to update.
   * @param file The Zip archive file.
   * @param entryName The Zip archive entry name.
   */
  protected void handlePut(Request request, Response response, File file, String entryName) {
    boolean zipExists = file.exists();
    ZipOutputStream zipOut = null;

    if ("".equals(entryName)
        && request.getEntity() != null
        && request.getEntity().getDisposition() != null) {
      entryName = request.getEntity().getDisposition().getFilename();
    }
    if (entryName == null) {
      response.setStatus(Status.CLIENT_ERROR_BAD_REQUEST, "Must specify an entry name.");
      return;
    }
    // boolean canAppend = true;
    boolean canAppend = !zipExists;
    boolean isDirectory = entryName.endsWith("/");
    boolean wrongReplace = false;
    try {
      if (zipExists) {
        ZipFile zipFile = new ZipFile(file);
        // Already exists ?
        canAppend &= null == zipFile.getEntry(entryName);
        // Directory with the same name ?
        if (isDirectory) {
          wrongReplace = null != zipFile.getEntry(entryName.substring(0, entryName.length() - 1));
        } else {
          wrongReplace = null != zipFile.getEntry(entryName + "/");
        }

        canAppend &= !wrongReplace;
        zipFile.close();
      }

      Representation entity;
      if (isDirectory) {
        entity = null;
      } else {
        entity = request.getEntity();
      }

      if (canAppend) {
        try {
          // zipOut = new ZipOutputStream(new BufferedOutputStream(new
          // FileOutputStream(file, true)));
          zipOut = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(file)));
          writeEntityStream(entity, zipOut, entryName);
          zipOut.close();
        } catch (Exception e) {
          response.setStatus(Status.SERVER_ERROR_INTERNAL, e);
          return;
        } finally {
          if (zipOut != null) zipOut.close();
        }
        response.setStatus(Status.SUCCESS_CREATED);
      } else {
        if (wrongReplace) {
          response.setStatus(
              Status.CLIENT_ERROR_BAD_REQUEST,
              "Directory cannot be replace by a file or file by a directory.");
        } else {
          File writeTo = null;
          ZipFile zipFile = null;
          try {
            writeTo = File.createTempFile("restlet_zip_", "zip");
            zipFile = new ZipFile(file);
            zipOut = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(writeTo)));
            Enumeration<? extends ZipEntry> entries = zipFile.entries();
            boolean replaced = false;
            while (entries.hasMoreElements()) {
              ZipEntry e = entries.nextElement();
              if (!replaced && entryName.equals(e.getName())) {
                writeEntityStream(entity, zipOut, entryName);
                replaced = true;
              } else {
                zipOut.putNextEntry(e);
                BioUtils.copy(new BufferedInputStream(zipFile.getInputStream(e)), zipOut);
                zipOut.closeEntry();
              }
            }
            if (!replaced) {
              writeEntityStream(entity, zipOut, entryName);
            }
            zipFile.close();
            zipOut.close();
          } finally {
            try {
              if (zipFile != null) zipFile.close();
            } finally {
              if (zipOut != null) zipOut.close();
            }
          }

          if (!(BioUtils.delete(file) && writeTo.renameTo(file))) {
            if (!file.exists()) file.createNewFile();
            FileInputStream fis = null;
            FileOutputStream fos = null;
            try {
              fis = new FileInputStream(writeTo);
              fos = new FileOutputStream(file);
              // ByteUtils.write(fis.getChannel(),
              // fos.getChannel());
              BioUtils.copy(fis, fos);
              response.setStatus(Status.SUCCESS_OK);
            } finally {
              try {
                if (fis != null) fis.close();
              } finally {
                if (fos != null) fos.close();
              }
            }
          } else {
            response.setStatus(Status.SUCCESS_OK);
          }
        }
      }
    } catch (Exception e) {
      response.setStatus(Status.SERVER_ERROR_INTERNAL, e);
      return;
    }
  }