use of org.jvnet.mimepull.MIMEConfig in project Payara by payara.
the class MultipartProprietaryReader method readFrom.
@Override
public ParamsWithPayload readFrom(final InputStream is, final String contentType) throws IOException {
RestPayloadImpl.Inbound payload = null;
ActionReport actionReport = null;
ParameterMap parameters = null;
Properties mtProps = parseHeaderParams(contentType);
final String boundary = mtProps.getProperty("boundary");
if (!StringUtils.ok(boundary)) {
throw new IOException("ContentType does not define boundary");
}
final MIMEMessage mimeMessage = new MIMEMessage(is, boundary, new MIMEConfig());
// Parse
for (MIMEPart mimePart : mimeMessage.getAttachments()) {
String cd = getFirst(mimePart.getHeader("Content-Disposition"));
if (!StringUtils.ok(cd)) {
cd = "file";
}
cd = cd.trim();
Properties cdParams = parseHeaderParams(cd);
// 3 types of content disposition
if (cd.startsWith("form-data")) {
// COMMAND PARAMETER
if (!StringUtils.ok(cdParams.getProperty("name"))) {
throw new IOException("Form-data Content-Disposition does not contains name parameter.");
}
if (parameters == null) {
parameters = new ParameterMap();
}
parameters.add(cdParams.getProperty("name"), stream2String(mimePart.readOnce()));
} else if (mimePart.getContentType() != null && mimePart.getContentType().startsWith("application/json")) {
// ACTION REPORT
actionReport = actionReportReader.readFrom(mimePart.readOnce(), "application/json");
} else {
// PAYLOAD
String name = "noname";
if (cdParams.containsKey("name")) {
name = cdParams.getProperty("name");
} else if (cdParams.containsKey("filename")) {
name = cdParams.getProperty("filename");
}
if (payload == null) {
payload = new RestPayloadImpl.Inbound();
}
String ct = mimePart.getContentType();
if (!StringUtils.ok(ct) || ct.trim().startsWith("text/plain")) {
payload.add(name, stream2String(mimePart.readOnce()), mimePart.getAllHeaders());
} else {
payload.add(name, mimePart.read(), ct, mimePart.getAllHeaders());
}
}
}
// Result
return new ParamsWithPayload(payload, parameters, actionReport);
}
use of org.jvnet.mimepull.MIMEConfig in project jersey by jersey.
the class MultiPartReaderClientSide method createMimeConfig.
private MIMEConfig createMimeConfig(final MultiPartProperties properties) {
final MIMEConfig mimeConfig = new MIMEConfig();
// Set values defined by user.
mimeConfig.setMemoryThreshold(properties.getBufferThreshold());
final String tempDir = properties.getTempDir();
if (tempDir != null) {
mimeConfig.setDir(tempDir);
}
if (properties.getBufferThreshold() != MultiPartProperties.BUFFER_THRESHOLD_MEMORY_ONLY) {
// Validate - this checks whether it's possible to create temp files in currently set temp directory.
try {
//noinspection ResultOfMethodCallIgnored
File.createTempFile("MIME", null, tempDir != null ? new File(tempDir) : null).delete();
} catch (final IOException ioe) {
LOGGER.log(Level.WARNING, LocalizationMessages.TEMP_FILE_CANNOT_BE_CREATED(properties.getBufferThreshold()), ioe);
}
}
return mimeConfig;
}
Aggregations