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

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

Introduction

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

Prototype

String CONTENT_TRANSFER_ENC

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

Click Source Link

Usage

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 {//  ww w  .  j a v  a  2 s . c om
        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;
}