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.payload;
17  
18  import java.util.List;
19  
20  import org.apache.cxf.message.Message;
21  import org.apache.cxf.phase.AbstractPhaseInterceptor;
22  import static org.openehealth.ipf.commons.ihe.ws.cxf.payload.StringPayloadHolder.PayloadType.SOAP_BODY;
23  
24  import org.apache.cxf.phase.Phase;
25  import org.openehealth.ipf.commons.ihe.ws.cxf.audit.AuditInRequestInterceptor;
26  
27  /**
28   * CXF interceptor which inserts data of String content type  
29   * (it is supposed to be the XML payload of the incoming message)
30   * into the list of an operation's parameters or response values.
31   * 
32   * @author Dmytro Rud
33   */
34  public class InPayloadInjectorInterceptor extends AbstractPhaseInterceptor<Message> {
35      private final int position;
36      
37      /**
38       * Constructs an interceptor instance.
39       * @param position
40       *      position in the parameter list at which the collected 
41       *      message payload should be inserted.
42       */
43      public InPayloadInjectorInterceptor(int position) {
44          super(Phase.UNMARSHAL);
45          addBefore(AuditInRequestInterceptor.class.getName());
46          addAfter(InNamespaceMergeInterceptor.class.getName());
47          this.position = position;
48      }
49  
50      @SuppressWarnings("unchecked")
51      @Override
52      public void handleMessage(Message message) {
53          if (isGET(message)) {
54              return;
55          }
56  
57          List list = message.getContent(List.class);
58          StringPayloadHolder payloadHolder = message.getContent(StringPayloadHolder.class);
59          if ((list != null) && (payloadHolder != null)) {
60              list.set(position, payloadHolder.get(SOAP_BODY));
61          }
62      }
63  }
64