How to Get mulipart-attachment in SOAP Response using CXF ?
niteshjain132 Mar 5, 2015 12:43 AMI am trying to get the multi-part attachment in soapui response. Using apache cxf webservices i am reading multipart content from database and sending the attachment as response for the soap request.
I tried using Interceptor as below , but the Intercetor "AttachmentOutInterceptor" is not being invoked while request/response, (cxf interceptor configuration configured in cxf bus).
any help would be appreciated.
import java.io.IOException;
import java.io.OutputStream;
import org.apache.cxf.attachment.AttachmentSerializer;
import org.apache.cxf.message.Message;
import org.apache.cxf.message.MessageUtils;
import org.apache.cxf.phase.AbstractPhaseInterceptor;
import org.apache.cxf.phase.Phase;
public class AttachmentOutInterceptor extends AbstractPhaseInterceptor<Message> {
public static final String WRITE_ATTACHMENTS = "write.attachments";
private static final ResourceBundle BUNDLE = BundleUtils.getBundle(AttachmentOutInterceptor.class);
private AttachmentOutEndingInterceptor ending = new AttachmentOutEndingInterceptor();
public AttachmentOutInterceptor() {
super(Phase.PRE_STREAM);
}
public void handleMessage(Message message) {
// Make it possible to step into this process in spite of Eclipse
// by declaring the Object.
Object prop = message.getContextualProperty(org.apache.cxf.message.Message.MTOM_ENABLED);
boolean mtomEnabled = MessageUtils.isTrue(prop);
boolean writeAtts = MessageUtils.isTrue(message.getContextualProperty(WRITE_ATTACHMENTS))
|| (message.getAttachments() != null && !message.getAttachments().isEmpty());
if (!mtomEnabled && !writeAtts) {
return;
}
if (message.getContent(OutputStream.class) == null) {
return;
}
AttachmentSerializer serializer = new AttachmentSerializer(message);
serializer.setXop(mtomEnabled);
try {
serializer.writeProlog();
} catch (IOException e) {
e.printStackTrace();
throw new Fault(new org.apache.cxf.common.i18n.Message("WRITE_ATTACHMENTS", BUNDLE), e);
}
message.setContent(AttachmentSerializer.class, serializer);
// Add a final interceptor to write attachements
message.getInterceptorChain().add(ending);
}
protected String getMultipartType() {
return "multipart/related";
}
protected Map<String, List getRootHeaders() {
return Collections.emptyMap();
}
public class AttachmentOutEndingInterceptor extends AbstractPhaseInterceptor<Message> {
public AttachmentOutEndingInterceptor() {
super(Phase.PRE_STREAM_ENDING);
}
public void handleMessage(Message message) {
AttachmentSerializer ser = message.getContent(AttachmentSerializer.class);
if (ser != null) {
try {
ser.writeAttachments();
} catch (IOException e) {
e.printStackTrace();
throw new Fault(new org.apache.cxf.common.i18n.Message("WRITE_ATTACHMENTS", BUNDLE), e);
}
}
}
}
}
Thanks,
Nitesh