use of nl.nn.adapterframework.stream.Message in project iaf by ibissource.
the class HttpSender method handleMultipartResponse.
/**
* return the first part as Message and put the other parts as InputStream in the PipeLineSession
*/
public static Message handleMultipartResponse(String mimeType, InputStream inputStream, PipeLineSession session) throws IOException {
Message result = null;
try {
// the entire InputStream will be read here!
InputStreamDataSource dataSource = new InputStreamDataSource(mimeType, inputStream);
MimeMultipart mimeMultipart = new MimeMultipart(dataSource);
for (int i = 0; i < mimeMultipart.getCount(); i++) {
BodyPart bodyPart = mimeMultipart.getBodyPart(i);
if (i == 0) {
result = new PartMessage(bodyPart);
} else {
session.put("multipart" + i, new PartMessage(bodyPart));
}
}
} catch (MessagingException e) {
throw new IOException("Could not read mime multipart response", e);
}
return result;
}
use of nl.nn.adapterframework.stream.Message in project iaf by ibissource.
the class HttpSender method elementToFormBodyPart.
protected FormBodyPart elementToFormBodyPart(Element element, PipeLineSession session) throws IOException {
// Name of the part
String partName = element.getAttribute("name");
// SessionKey to retrieve data from
String partSessionKey = element.getAttribute("sessionKey");
// MimeType of the part
String partMimeType = element.getAttribute("mimeType");
Message partObject = session.getMessage(partSessionKey);
if (partObject.isBinary()) {
return createMultipartBodypart(partSessionKey, partObject.asInputStream(), partName, partMimeType);
} else {
return createMultipartBodypart(partName, partObject.asString(), partMimeType);
}
}
use of nl.nn.adapterframework.stream.Message in project iaf by ibissource.
the class HttpSender method createMultiPartEntity.
protected HttpEntity createMultiPartEntity(String message, ParameterValueList parameters, PipeLineSession session) throws SenderException, IOException {
MultipartEntityBuilder entity = MultipartEntityBuilder.create();
entity.setCharset(Charset.forName(getCharSet()));
if (postType.equals(PostType.MTOM))
entity.setMtomMultipart();
if (StringUtils.isNotEmpty(getFirstBodyPartName())) {
entity.addPart(createMultipartBodypart(getFirstBodyPartName(), message));
if (log.isDebugEnabled())
log.debug(getLogPrefix() + "appended stringpart [" + getFirstBodyPartName() + "] with value [" + message + "]");
}
if (parameters != null) {
for (ParameterValue pv : parameters) {
String name = pv.getDefinition().getName();
// Skip parameters that are configured as ignored
if (skipParameter(name))
continue;
Message msg = pv.asMessage();
if (msg.isBinary()) {
InputStream fis = msg.asInputStream();
String fileName = null;
String sessionKey = pv.getDefinition().getSessionKey();
if (sessionKey != null) {
fileName = session.getMessage(sessionKey + "Name").asString();
}
entity.addPart(createMultipartBodypart(name, fis, fileName));
if (log.isDebugEnabled())
log.debug(getLogPrefix() + "appended filepart [" + name + "] with value [" + msg + "] and name [" + fileName + "]");
} else {
String value = msg.asString();
entity.addPart(createMultipartBodypart(name, value));
if (log.isDebugEnabled())
log.debug(getLogPrefix() + "appended stringpart [" + name + "] with value [" + value + "]");
}
}
}
if (StringUtils.isNotEmpty(getMultipartXmlSessionKey())) {
String multipartXml = session.getMessage(getMultipartXmlSessionKey()).asString();
log.debug(getLogPrefix() + "building multipart message with MultipartXmlSessionKey [" + multipartXml + "]");
if (StringUtils.isEmpty(multipartXml)) {
log.warn(getLogPrefix() + "sessionKey [" + getMultipartXmlSessionKey() + "] is empty");
} else {
Element partsElement;
try {
partsElement = XmlUtils.buildElement(multipartXml);
} catch (DomBuilderException e) {
throw new SenderException(getLogPrefix() + "error building multipart xml", e);
}
Collection<Node> parts = XmlUtils.getChildTags(partsElement, "part");
if (parts == null || parts.size() == 0) {
log.warn(getLogPrefix() + "no part(s) in multipart xml [" + multipartXml + "]");
} else {
Iterator<Node> iter = parts.iterator();
while (iter.hasNext()) {
Element partElement = (Element) iter.next();
entity.addPart(elementToFormBodyPart(partElement, session));
}
}
}
}
return entity.build();
}
use of nl.nn.adapterframework.stream.Message in project iaf by ibissource.
the class MultipartHttpSender method elementToFormBodyPart.
@Override
protected FormBodyPart elementToFormBodyPart(Element element, PipeLineSession session) throws IOException {
// File or otherwise
String partType = element.getAttribute("type");
// Name of the part
String partName = element.getAttribute("name");
// Name of the file
String fileName = (StringUtils.isNotEmpty(element.getAttribute("filename"))) ? element.getAttribute("filename") : element.getAttribute("fileName");
// SessionKey to retrieve data from
String sessionKey = element.getAttribute("sessionKey");
// MimeType of the part
String mimeType = element.getAttribute("mimeType");
// Value when SessionKey is empty or not set
String partValue = element.getAttribute("value");
Message partObject = session.getMessage(sessionKey);
if (partObject != null && partObject.isBinary()) {
InputStream fis = partObject.asInputStream();
if (StringUtils.isNotEmpty(fileName)) {
return createMultipartBodypart(partName, fis, fileName, mimeType);
} else if ("file".equalsIgnoreCase(partType)) {
return createMultipartBodypart(sessionKey, fis, partName, mimeType);
} else {
return createMultipartBodypart(partName, fis, null, mimeType);
}
} else {
String value = partObject.asString();
if (StringUtils.isEmpty(value))
value = partValue;
return createMultipartBodypart(partName, value, mimeType);
}
}
use of nl.nn.adapterframework.stream.Message in project iaf by ibissource.
the class ForEachAttachmentPipe method itemToMessage.
@Override
protected Message itemToMessage(A item) throws SenderException {
FS ifs = getFileSystem();
XmlBuilder result = new XmlBuilder("attachment");
try {
result.addAttribute("name", ifs.getAttachmentName(item));
result.addAttribute("filename", ifs.getAttachmentFileName(item));
result.addAttribute("contentType", ifs.getAttachmentContentType(item));
result.addAttribute("size", ifs.getAttachmentSize(item));
Map<String, Object> attachmentProperties = ifs.getAdditionalAttachmentProperties(item);
if (attachmentProperties != null) {
XmlBuilder properties = new XmlBuilder("properties");
Set<String> excludes = getExcludePropertiesSet();
Set<String> includes = getOnlyPropertiesSet();
if (excludes != null || includes == null) {
for (Entry<String, Object> entry : attachmentProperties.entrySet()) {
if (excludes == null || !excludes.contains(entry.getKey())) {
XmlBuilder property = new XmlBuilder("property");
property.addAttribute("name", entry.getKey());
if (entry.getValue() != null) {
property.setValue(entry.getValue().toString());
}
properties.addSubElement(property);
}
}
} else {
for (String key : includes) {
XmlBuilder property = new XmlBuilder("property");
property.addAttribute("name", key);
Object value = attachmentProperties.get(key);
if (value != null) {
property.setValue(value.toString());
}
properties.addSubElement(property);
}
}
result.addSubElement(properties);
}
} catch (Exception e) {
throw new SenderException("unable to read attachment attributes", e);
}
return new Message(result.toXML());
}
Aggregations