Search in sources :

Example 1 with MIMEConfig

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);
}
Also used : MIMEConfig(org.jvnet.mimepull.MIMEConfig) MIMEMessage(org.jvnet.mimepull.MIMEMessage) ParameterMap(org.glassfish.api.admin.ParameterMap) IOException(java.io.IOException) ParamsWithPayload(com.sun.enterprise.admin.remote.ParamsWithPayload) ActionReport(org.glassfish.api.ActionReport) Properties(java.util.Properties) RestPayloadImpl(com.sun.enterprise.admin.remote.RestPayloadImpl) MIMEPart(org.jvnet.mimepull.MIMEPart)

Example 2 with MIMEConfig

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;
}
Also used : MIMEConfig(org.jvnet.mimepull.MIMEConfig) IOException(java.io.IOException) File(java.io.File)

Aggregations

IOException (java.io.IOException)2 MIMEConfig (org.jvnet.mimepull.MIMEConfig)2 ParamsWithPayload (com.sun.enterprise.admin.remote.ParamsWithPayload)1 RestPayloadImpl (com.sun.enterprise.admin.remote.RestPayloadImpl)1 File (java.io.File)1 Properties (java.util.Properties)1 ActionReport (org.glassfish.api.ActionReport)1 ParameterMap (org.glassfish.api.admin.ParameterMap)1 MIMEMessage (org.jvnet.mimepull.MIMEMessage)1 MIMEPart (org.jvnet.mimepull.MIMEPart)1