Search in sources :

Example 1 with ImmutableByteArray

use of password.pwm.http.bean.ImmutableByteArray in project pwm by pwm-project.

the class ResourceServletConfiguration method makeMemoryFileMapFromZipInput.

private static Map<String, FileResource> makeMemoryFileMapFromZipInput(final ImmutableByteArray content) throws IOException {
    final ZipInputStream stream = new ZipInputStream(new ByteArrayInputStream(content.getBytes()));
    final Map<String, FileResource> memoryMap = new HashMap<>();
    ZipEntry entry;
    while ((entry = stream.getNextEntry()) != null) {
        if (!entry.isDirectory()) {
            final String name = entry.getName();
            final long lastModified = entry.getTime();
            final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            IOUtils.copy(stream, byteArrayOutputStream);
            final ImmutableByteArray contents = new ImmutableByteArray(byteArrayOutputStream.toByteArray());
            memoryMap.put(name, new MemoryFileResource(name, contents, lastModified));
            LOGGER.trace("discovered file in configured resource bundle: " + entry.getName());
        }
    }
    return memoryMap;
}
Also used : ZipInputStream(java.util.zip.ZipInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) HashMap(java.util.HashMap) ZipEntry(java.util.zip.ZipEntry) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ImmutableByteArray(password.pwm.http.bean.ImmutableByteArray)

Example 2 with ImmutableByteArray

use of password.pwm.http.bean.ImmutableByteArray in project pwm by pwm-project.

the class PwmRequest method readFileUploads.

public Map<String, FileUploadItem> readFileUploads(final int maxFileSize, final int maxItems) throws IOException, ServletException, PwmUnrecoverableException {
    final Map<String, FileUploadItem> returnObj = new LinkedHashMap<>();
    try {
        if (ServletFileUpload.isMultipartContent(this.getHttpServletRequest())) {
            final ServletFileUpload upload = new ServletFileUpload();
            final FileItemIterator iter = upload.getItemIterator(this.getHttpServletRequest());
            while (iter.hasNext() && returnObj.size() < maxItems) {
                final FileItemStream item = iter.next();
                final InputStream inputStream = item.openStream();
                final ByteArrayOutputStream baos = new ByteArrayOutputStream();
                final long length = IOUtils.copyLarge(inputStream, baos, 0, maxFileSize + 1);
                if (length > maxFileSize) {
                    final ErrorInformation errorInformation = new ErrorInformation(PwmError.ERROR_UNKNOWN, "upload file size limit exceeded");
                    LOGGER.error(this, errorInformation);
                    respondWithError(errorInformation);
                    return Collections.emptyMap();
                }
                final byte[] outputFile = baos.toByteArray();
                final FileUploadItem fileUploadItem = new FileUploadItem(item.getName(), item.getContentType(), new ImmutableByteArray(outputFile));
                returnObj.put(item.getFieldName(), fileUploadItem);
            }
        }
    } catch (Exception e) {
        LOGGER.error("error reading file upload: " + e.getMessage());
    }
    return Collections.unmodifiableMap(returnObj);
}
Also used : InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ServletException(javax.servlet.ServletException) PwmUnrecoverableException(password.pwm.error.PwmUnrecoverableException) IOException(java.io.IOException) LinkedHashMap(java.util.LinkedHashMap) ErrorInformation(password.pwm.error.ErrorInformation) ServletFileUpload(org.apache.commons.fileupload.servlet.ServletFileUpload) FileItemStream(org.apache.commons.fileupload.FileItemStream) ImmutableByteArray(password.pwm.http.bean.ImmutableByteArray) FileItemIterator(org.apache.commons.fileupload.FileItemIterator)

Aggregations

ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 ImmutableByteArray (password.pwm.http.bean.ImmutableByteArray)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 ZipEntry (java.util.zip.ZipEntry)1 ZipInputStream (java.util.zip.ZipInputStream)1 ServletException (javax.servlet.ServletException)1 FileItemIterator (org.apache.commons.fileupload.FileItemIterator)1 FileItemStream (org.apache.commons.fileupload.FileItemStream)1 ServletFileUpload (org.apache.commons.fileupload.servlet.ServletFileUpload)1 ErrorInformation (password.pwm.error.ErrorInformation)1 PwmUnrecoverableException (password.pwm.error.PwmUnrecoverableException)1