protected WSRPMultiRequestContext(String characterEncoding, List<NamedString> formParams, List<UploadContext> uploadContexts) throws IOException, MessagingException
{
super(characterEncoding);
MimeMultipart parts = new MimeMultipart();
if (uploadContexts != null && !uploadContexts.isEmpty())
{
for (UploadContext uploadContext : uploadContexts)
{
InternetHeaders headers = new InternetHeaders();
headers.addHeader(FileUpload.CONTENT_TYPE, uploadContext.getMimeType());
List<NamedString> attributes = uploadContext.getMimeAttributes();
if (attributes != null && !attributes.isEmpty())
{
for (NamedString attribute : attributes)
{
headers.addHeader(attribute.getName(), attribute.getValue());
}
}
MimeBodyPart mimeBodyPart = new MimeBodyPart(headers, uploadContext.getUploadData());
parts.addBodyPart(mimeBodyPart);
}
}
final String paramContentDispositionHeader = FileUpload.FORM_DATA + "; name=\"";
if (formParams != null && !formParams.isEmpty())
{
Map<String, String[]> params = new HashMap<String, String[]>(formParams.size());
for (NamedString formParam : formParams)
{
InternetHeaders headers = new InternetHeaders();
headers.addHeader(FileUpload.CONTENT_DISPOSITION, paramContentDispositionHeader + formParam.getName() + "\"");
MimeBodyPart mimeBodyPart = new MimeBodyPart(headers, formParam.getValue().getBytes());
parts.addBodyPart(mimeBodyPart);
String paramName = formParam.getName();
String paramValue = formParam.getValue();
if (params.containsKey(paramName))
{
// handle multi-valued parameters...
String[] oldValues = params.get(paramName);
int valuesLength = oldValues.length;
String[] newValues = new String[valuesLength + 1];
System.arraycopy(oldValues, 0, newValues, 0, valuesLength);
newValues[valuesLength] = paramValue;
params.put(paramName, newValues);
}
else
{
params.put(paramName, new String[]{paramValue});
}
}
formParameters = new ParameterMap(params);
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
parts.writeTo(baos);
content = baos.toByteArray();
contentType = parts.getContentType();
}