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;
}
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);
}
Aggregations