View Javadoc
1   /*
2    * Copyright 2009 the original author or authors.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *     
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * Interceptor to add provided outgoing attachments for SwA on the client side.
36   * @author Jens Riemschneider
37   */
38  public class ProvidedAttachmentOutInterceptor extends AbstractSoapInterceptor {
39      /**
40       * Context property that contains the provided attachments that need to be
41       * added to the message.
42       */
43      public static final String ATTACHMENTS = 
44          ProvidedAttachmentOutInterceptor.class.getName() + ".provided_attachments";
45  
46      /**
47       * Constructs the interceptor.
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  }