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 org.apache.cxf.binding.soap.saaj.SAAJOutInterceptor;
19  import org.apache.cxf.message.Message;
20  import org.apache.cxf.phase.AbstractPhaseInterceptor;
21  import org.apache.cxf.phase.Phase;
22  import org.openehealth.ipf.commons.ihe.ws.utils.SoapUtils;
23  import static org.openehealth.ipf.commons.ihe.ws.cxf.payload.StringPayloadHolder.PayloadType.SOAP_BODY;
24  
25  /**
26   * CXF interceptor that reads outgoing payload collected by the output 
27   * stream proxy installed in {@link OutStreamSubstituteInterceptor} 
28   * and stores it in the message as String content type.
29   * 
30   * @author Dmytro Rud
31   */
32  public class OutPayloadExtractorInterceptor extends AbstractPhaseInterceptor<Message> {
33  
34      /**
35       * When the CXF message contains <code>Boolean.FALSE</code> in the property
36       * with this name, collecting message payload will not be deactivated
37       * after the SOAP part has been written.
38       */
39      public static final String PAYLOAD_COLLECTING_DEACTIVATION_ENABLED =
40              OutPayloadExtractorInterceptor.class.getName() + ".collecting.deactivation.enabled";
41  
42  
43      public OutPayloadExtractorInterceptor() {
44          super(Phase.PRE_PROTOCOL_ENDING);
45          addAfter(SAAJOutInterceptor.SAAJOutEndingInterceptor.class.getName());
46      }
47  
48  
49      @Override
50      public void handleMessage(Message message) {
51          if (isGET(message)) {
52              return;
53          }
54  
55          WrappedOutputStream wrapper = OutStreamSubstituteInterceptor.getStreamWrapper(message);
56          if (! Boolean.FALSE.equals(message.getContextualProperty(PAYLOAD_COLLECTING_DEACTIVATION_ENABLED))) {
57              wrapper.deactivate();
58          }
59  
60          String soapEnvelope = wrapper.getCollectedPayload();
61          String payload = SoapUtils.extractSoapBody(soapEnvelope);
62          StringPayloadHolder payloadHolder = new StringPayloadHolder();
63          payloadHolder.put(SOAP_BODY, payload);
64          message.setContent(StringPayloadHolder.class, payloadHolder);
65      }
66  }