use of com.dtflys.forest.backend.httpclient.body.HttpclientMultipartFileBody 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);
}
Aggregations