Search in sources :

Example 1 with MinimalField

use of org.apache.http.entity.mime.MinimalField in project manifoldcf by apache.

the class ModifiedHttpMultipart method doWriteTo.

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);
        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);
}
Also used : FormBodyPart(org.apache.http.entity.mime.FormBodyPart) Header(org.apache.http.entity.mime.Header) MinimalField(org.apache.http.entity.mime.MinimalField) ByteArrayBuffer(org.apache.http.util.ByteArrayBuffer)

Example 2 with MinimalField

use of org.apache.http.entity.mime.MinimalField in project forest by dromara.

the class HttpclientLogBodyMessage method getBodyString.

@Override
public String getBodyString() {
    if (entity == null) {
        return null;
    }
    Header contentTypeHeader = entity.getContentType();
    ContentType contentType = new ContentType(contentTypeHeader.getValue());
    if (contentType.isMultipart()) {
        Class[] paramTypes = new Class[0];
        Object[] args = new Object[0];
        List<FormBodyPart> parts = null;
        try {
            Method getMultipartMethod = entity.getClass().getDeclaredMethod("getMultipart", paramTypes);
            getMultipartMethod.setAccessible(true);
            Object multipart = getMultipartMethod.invoke(entity, args);
            if (multipart != null) {
                Method getBodyPartsMethod = multipart.getClass().getDeclaredMethod("getBodyParts", paramTypes);
                getBodyPartsMethod.setAccessible(true);
                parts = (List<FormBodyPart>) getBodyPartsMethod.invoke(multipart, args);
            }
        } catch (NoSuchMethodException e) {
        } catch (IllegalAccessException e) {
        } catch (InvocationTargetException e) {
        }
        Long contentLength = null;
        try {
            contentLength = entity.getContentLength();
        } catch (Throwable th) {
        }
        if (parts == null) {
            String result = "[" + entity.getContentType().getValue();
            if (contentLength != null) {
                result += "; length=" + contentLength;
            }
            return result + "]";
        } else {
            StringBuilder builder = new StringBuilder();
            builder.append("[").append(entity.getContentType().getValue());
            if (contentLength != null) {
                builder.append("; length=").append(contentLength);
            }
            builder.append("] parts:");
            for (FormBodyPart part : parts) {
                ContentBody partBody = part.getBody();
                MinimalField disposition = part.getHeader().getField("Content-Disposition");
                builder.append("\n             -- [").append(disposition.getBody());
                if (partBody instanceof StringBody) {
                    Reader reader = ((StringBody) partBody).getReader();
                    BufferedReader bufferedReader = new BufferedReader(reader);
                    String value = null;
                    try {
                        value = getLogContentFormBufferedReader(bufferedReader);
                    } catch (IOException e) {
                    }
                    builder.append("; content-type=\"").append(((StringBody) partBody).getContentType()).append("\"");
                    builder.append("; value=\"").append(value).append("\"]");
                } else {
                    Long length = null;
                    length = partBody.getContentLength();
                    if (length != null) {
                        builder.append("; length=").append(length);
                    }
                    if (partBody instanceof HttpclientMultipartFileBody) {
                        builder.append("; content-type=\"").append(((HttpclientMultipartFileBody) partBody).getContentType()).append("\"");
                    }
                    builder.append("]");
                }
            }
            return builder.toString();
        }
    } else if (contentType.isBinary()) {
        return "[Binary length=" + entity.getContentLength() + "]";
    }
    return getLogContentForStringBody(entity);
}
Also used : ContentType(com.dtflys.forest.backend.ContentType) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) Method(java.lang.reflect.Method) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) HttpclientMultipartFileBody(com.dtflys.forest.backend.httpclient.body.HttpclientMultipartFileBody) FormBodyPart(org.apache.http.entity.mime.FormBodyPart) Header(org.apache.http.Header) ContentBody(org.apache.http.entity.mime.content.ContentBody) StringBody(org.apache.http.entity.mime.content.StringBody) MinimalField(org.apache.http.entity.mime.MinimalField) BufferedReader(java.io.BufferedReader)

Aggregations

FormBodyPart (org.apache.http.entity.mime.FormBodyPart)2 MinimalField (org.apache.http.entity.mime.MinimalField)2 ContentType (com.dtflys.forest.backend.ContentType)1 HttpclientMultipartFileBody (com.dtflys.forest.backend.httpclient.body.HttpclientMultipartFileBody)1 BufferedReader (java.io.BufferedReader)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 Reader (java.io.Reader)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1 Header (org.apache.http.Header)1 Header (org.apache.http.entity.mime.Header)1 ContentBody (org.apache.http.entity.mime.content.ContentBody)1 StringBody (org.apache.http.entity.mime.content.StringBody)1 ByteArrayBuffer (org.apache.http.util.ByteArrayBuffer)1