Example usage for org.apache.http.entity.mime MIME CONTENT_TYPE

List of usage examples for org.apache.http.entity.mime MIME CONTENT_TYPE

Introduction

In this page you can find the example usage for org.apache.http.entity.mime MIME CONTENT_TYPE.

Prototype

String CONTENT_TYPE

To view the source code for org.apache.http.entity.mime MIME CONTENT_TYPE.

Click Source Link

Usage

From source file:neembuu.uploader.uploaders.common.FormBodyPartUtils.java

/**
 * Create an empty form body part.// w  w w. j  a  v a  2  s . co m
 * @param name the name of the field.
 * @param body the content of the field.
 * @return Return the new empty form body part.
 * @throws UnsupportedEncodingException 
 */
public static FormBodyPart createEmptyFileFormBodyPart(final String name, StringBody body)
        throws UnsupportedEncodingException {
    return new FormBodyPart(name, body) {
        @Override
        protected void generateContentDisp(final ContentBody body) {
            StringBuilder buffer = new StringBuilder();
            buffer.append("form-data; name=\"").append(name).append("\"");
            buffer.append("; filename=\"\"");
            buffer.append("\n");
            addField(MIME.CONTENT_DISPOSITION, buffer.toString());
            addField(MIME.CONTENT_TYPE, "application/octet-stream");
        }

    };
}

From source file:org.apache.manifoldcf.agents.output.solr.ModifiedHttpMultipart.java

private void doWriteTo(final HttpMultipartMode mode, final OutputStream out, boolean writeContent)
        throws IOException {

    ByteArrayBuffer boundary = encode(this.charset, getBoundary());
    for (FormBodyPart part : this.parts) {
        writeBytes(TWO_DASHES, out);//  ww  w  . j a  va2s  . co m
        writeBytes(boundary, out);
        writeBytes(CR_LF, out);

        Header header = part.getHeader();

        switch (mode) {
        case STRICT:
            for (MinimalField field : header) {
                writeField(field, this.charset, out);
            }
            break;
        case BROWSER_COMPATIBLE:
            // Only write Content-Disposition
            // Use content charset
            MinimalField cd = part.getHeader().getField(MIME.CONTENT_DISPOSITION);
            writeField(cd, this.charset, out);
            String filename = part.getBody().getFilename();
            if (filename != null) {
                MinimalField ct = part.getHeader().getField(MIME.CONTENT_TYPE);
                writeField(ct, this.charset, out);
            }
            break;
        }
        writeBytes(CR_LF, out);

        if (writeContent) {
            part.getBody().writeTo(out);
        }
        writeBytes(CR_LF, out);
    }
    writeBytes(TWO_DASHES, out);
    writeBytes(boundary, out);
    writeBytes(TWO_DASHES, out);
    writeBytes(CR_LF, out);
}

From source file:org.trancecode.xproc.step.RequestParser.java

private FormBodyPart getContentBody(final XdmNode node, final Processor processor) {
    final String contentTypeAtt = node.getAttributeValue(XProcXmlModel.Attributes.CONTENT_TYPE);
    final String encoding = node.getAttributeValue(XProcXmlModel.Attributes.ENCODING);
    final ContentType contentType = Steps.getContentType(contentTypeAtt, node);
    final String contentString = getContentString(node, contentType, encoding, processor);
    final StringBody body;
    try {/*from  w  w w  .j  a  va  2  s  .  c o m*/
        body = new StringBody(contentString, contentType.toString(),
                Steps.getCharset(contentType.getParameter("charset")));
    } catch (final UnsupportedEncodingException e) {
        throw XProcExceptions.xc0020(node);
    }

    final String id = node.getAttributeValue(XProcXmlModel.Attributes.ID);
    final String description = node.getAttributeValue(XProcXmlModel.Attributes.DESCRIPTION);
    final String disposition = node.getAttributeValue(XProcXmlModel.Attributes.DISPOSITION);
    final FormBodyPart bodyPart = new FormBodyPart("body", body) {
        @Override
        protected void generateContentDisp(final ContentBody body) {
            if (disposition != null) {
                addField(MIME.CONTENT_DISPOSITION, disposition);
            }
        }

        @Override
        protected void generateTransferEncoding(final ContentBody body) {
            if (encoding != null) {
                addField(MIME.CONTENT_TRANSFER_ENC, encoding);
            }
        }

        @Override
        protected void generateContentType(final ContentBody body) {
            final StringBuilder buffer = new StringBuilder();
            buffer.append(body.getMimeType());
            if (body.getCharset() != null) {
                try {
                    final String testCharset = new ContentType(body.getMimeType()).getParameter("charset");
                    if (testCharset != null) {
                        final Charset charset = Charset.forName(testCharset);
                        if (!StringUtils.equalsIgnoreCase(charset.displayName(), body.getCharset())) {
                            buffer.append("; charset=").append(body.getCharset().toLowerCase());
                        }
                    } else {
                        buffer.append("; charset=utf-8");
                    }
                } catch (final ParseException | IllegalCharsetNameException e) {
                    throw XProcExceptions.xc0020(node);
                }
            }
            addField(MIME.CONTENT_TYPE, buffer.toString());
        }
    };
    if (id != null) {
        bodyPart.addField("Content-ID", id);
    }
    if (description != null) {
        bodyPart.addField("Content-Description", description);
    }

    return bodyPart;
}