1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.openehealth.ipf.commons.ihe.ws.cxf;
17
18 import java.util.ArrayList;
19 import java.util.Collection;
20 import java.util.Map;
21
22 import javax.activation.DataHandler;
23
24 import org.apache.cxf.attachment.AttachmentImpl;
25 import org.apache.cxf.binding.soap.SoapMessage;
26 import org.apache.cxf.binding.soap.interceptor.AbstractSoapInterceptor;
27 import org.apache.cxf.interceptor.AttachmentOutInterceptor;
28 import org.apache.cxf.interceptor.Fault;
29 import org.apache.cxf.jaxws.interceptors.HolderOutInterceptor;
30 import org.apache.cxf.jaxws.interceptors.WrapperClassOutInterceptor;
31 import org.apache.cxf.message.Attachment;
32 import org.apache.cxf.phase.Phase;
33
34
35
36
37
38 public class ProvidedAttachmentOutInterceptor extends AbstractSoapInterceptor {
39
40
41
42
43 public static final String ATTACHMENTS =
44 ProvidedAttachmentOutInterceptor.class.getName() + ".provided_attachments";
45
46
47
48
49 public ProvidedAttachmentOutInterceptor() {
50 super(Phase.PRE_LOGICAL);
51 addAfter(HolderOutInterceptor.class.getName());
52 addBefore(WrapperClassOutInterceptor.class.getName());
53 }
54
55 @Override
56 public void handleMessage(SoapMessage message) throws Fault {
57 Map<?, ?> providedAttachments = (Map<?, ?>) message.getContextualProperty(ATTACHMENTS);
58 if (providedAttachments.isEmpty()) {
59 return;
60 }
61
62 Collection<Attachment> attachments = message.getAttachments();
63 if (attachments == null) {
64 attachments = new ArrayList<>();
65 message.setAttachments(attachments);
66 }
67
68 for (Map.Entry<?, ?> entry : providedAttachments.entrySet()) {
69 attachments.add(new AttachmentImpl((String)entry.getKey(), (DataHandler)entry.getValue()));
70 }
71
72 message.put(AttachmentOutInterceptor.WRITE_ATTACHMENTS, Boolean.TRUE);
73 }
74 }