Example usage for java.util.zip ZipException getLocalizedMessage

List of usage examples for java.util.zip ZipException getLocalizedMessage

Introduction

In this page you can find the example usage for java.util.zip ZipException getLocalizedMessage.

Prototype

public String getLocalizedMessage() 

Source Link

Document

Creates a localized description of this throwable.

Usage

From source file:org.sakaiproject.nakamura.importer.ImportSiteArchiveServlet.java

/**
 * {@inheritDoc}/*from www.j  a v  a  2  s.com*/
 * 
 * @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);
        }
    }
    response.setStatus(HttpServletResponse.SC_OK);
    return;
}

From source file:org.sead.repositories.reference.util.RefLocalContentProvider.java

@Override
public InputStream getInputStreamMatchingHash(String algorithm, String hash) {
    // If algorithm matches
    if (algorithm.equals(hashtype) && zf != null) {

        // Look up path based on hash
        if (hashToPathMap.containsKey(hash)) {

            String relPath = hashToPathMap.get(hash);
            log.trace("Retrieving stream for hash: " + hash + "from : " + relPath);

            try {
                ZipEntry archiveEntry1 = zf.getEntry(relPath);
                if (archiveEntry1 != null) {
                    return new BufferedInputStream(zf.getInputStream(archiveEntry1));
                }//from   ww  w  .  j a v  a2 s. c  o  m

            } catch (ZipException e) {
                log.error(relPath + " : " + e.getLocalizedMessage());
                e.printStackTrace();
            } catch (IOException e) {
                log.error(relPath + " : " + e.getLocalizedMessage());
                e.printStackTrace();
            }
        }
    }

    return null;
}