コード例 #1
0
  /**
   * {@inheritDoc}
   *
   * @see
   *     org.apache.sling.api.servlets.SlingAllMethodsServlet#doPost(org.apache.sling.api.SlingHttpServletRequest,
   *     org.apache.sling.api.SlingHttpServletResponse)
   */
  @Override
  protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response)
      throws ServletException {
    final RequestParameter siteParam = request.getRequestParameter("site");
    if (siteParam == null || !siteParam.getString().startsWith("/")) {
      final String errorMessage =
          "A site must be specified, and it must be an absolute path pointing to a site.";
      sendError(
          HttpServletResponse.SC_BAD_REQUEST,
          errorMessage,
          new IllegalArgumentException(errorMessage),
          response);
      return;
    }
    final String sitePath = siteParam.getString();

    final RequestParameter[] files = request.getRequestParameters("Filedata");
    if (files == null || files.length < 1) {
      final String errorMessage = "Missing Filedata parameter.";
      sendError(
          HttpServletResponse.SC_BAD_REQUEST,
          errorMessage,
          new IllegalArgumentException(errorMessage),
          response);
      return;
    }
    final Session session = request.getResourceResolver().adaptTo(Session.class);
    for (RequestParameter p : files) {
      LOG.info(
          "Processing file: "
              + p.getFileName()
              + ": "
              + p.getContentType()
              + ": "
              + p.getSize()
              + " bytes");
      try {
        // create temporary local file of zip contents
        final File tempZip = File.createTempFile("siteArchive", ".zip");
        tempZip.deleteOnExit(); // just in case
        final InputStream in = p.getInputStream();
        final FileOutputStream out = new FileOutputStream(tempZip);
        final byte[] buf = new byte[4096];
        int len;
        while ((len = in.read(buf)) > 0) {
          out.write(buf, 0, len);
        }
        in.close();
        out.close();
        // process the zip file
        ZipFile zip = null;
        try {
          zip = new ZipFile(tempZip);
        } catch (ZipException e) {
          sendError(
              HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE,
              "Invalid zip file: "
                  + p.getFileName()
                  + ": "
                  + p.getContentType()
                  + ": "
                  + p.getSize(),
              null,
              response);
        }
        if (zip != null) {
          for (ZipEntry entry : Collections.list(zip.entries())) {
            if (entry.getName().startsWith("__MACOSX")
                || entry.getName().endsWith(".DS_Store")) {; // skip entry
            } else {
              if ("content.xml".equals(entry.getName())) {
                processContentXml(zip.getInputStream(entry), sitePath, session, zip);
              }
            }
          }
          zip.close();
        }
        // delete temporary file
        if (tempZip.delete()) {
          LOG.debug("{}: temporary zip file deleted.", tempZip.getAbsolutePath());
        } else {
          LOG.warn("Could not delete temporary file: {}", tempZip.getAbsolutePath());
        }
      } catch (IOException e) {
        sendError(
            HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.getLocalizedMessage(), e, response);
      } catch (XMLStreamException e) {
        sendError(
            HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.getLocalizedMessage(), e, response);
      }
    }
    sendError(HttpServletResponse.SC_OK, "All files processed without error.", null, response);
    return;
  }