View Javadoc

1   /*
2    * $Id: MuleProtocolHeadersOutInterceptor.java 11664 2008-04-30 03:29:13Z dandiep $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.transport.cxf.support;
12  
13  import org.mule.api.transport.MessageAdapter;
14  import org.mule.transport.cxf.CxfConstants;
15  import org.mule.transport.http.HttpConnector;
16  import org.mule.transport.http.HttpConstants;
17  
18  import java.util.List;
19  import java.util.Map;
20  
21  import org.apache.cxf.helpers.CastUtils;
22  import org.apache.cxf.interceptor.AttachmentOutInterceptor;
23  import org.apache.cxf.interceptor.Fault;
24  import org.apache.cxf.message.Message;
25  import org.apache.cxf.phase.AbstractPhaseInterceptor;
26  import org.apache.cxf.phase.Phase;
27  
28  public class MuleProtocolHeadersOutInterceptor
29      extends AbstractPhaseInterceptor<Message>
30  {
31  
32      public MuleProtocolHeadersOutInterceptor()
33      {
34          super(Phase.PRE_STREAM);
35          getAfter().add(AttachmentOutInterceptor.class.getName());
36      }
37  
38      public void handleMessage(Message message) throws Fault
39      {
40          MessageAdapter muleMsg = (MessageAdapter) message.getExchange().get(CxfConstants.MULE_MESSAGE);
41  
42          if (muleMsg == null)
43          {
44              return;
45          }
46          extractAndSet(message, muleMsg, Message.CONTENT_TYPE, HttpConstants.HEADER_CONTENT_TYPE);
47  
48          String method = (String) message.get(Message.HTTP_REQUEST_METHOD);
49          if (method == null) method = HttpConstants.METHOD_POST;
50          
51          muleMsg.setProperty(HttpConnector.HTTP_METHOD_PROPERTY, method);
52          
53          Map<String, List<String>> reqHeaders = CastUtils.cast((Map<?, ?>) message.get(Message.PROTOCOL_HEADERS));
54          if (reqHeaders != null)
55          {
56              for (Map.Entry<String, List<String>> e : reqHeaders.entrySet())
57              {
58                  String key = e.getKey();
59                  String val = format(e.getValue());
60                  
61                  muleMsg.setProperty(key, val);
62              }
63          }   
64          
65          if (!Boolean.TRUE.equals(message.containsKey(Message.REQUESTOR_ROLE)))
66          {
67              message.getInterceptorChain().pause();
68          }
69      }
70  
71      private void extractAndSet(Message message, MessageAdapter muleMsg, String cxfHeader, String muleHeader)
72      {
73          String ct = (String) message.get(cxfHeader);
74          if (ct != null)
75          {
76              muleMsg.setProperty(muleHeader, ct);
77          }
78      }
79  
80      private String format(List<String> value)
81      {
82          StringBuilder sb = new StringBuilder();
83          boolean first = true;
84          
85          for (String s : value) {
86              if (!first) 
87              {
88                  sb.append(", ");
89                  first = false;
90              }
91              else 
92              {
93                  first = false;
94              }
95              
96              sb.append(s);
97          }
98          return sb.toString();
99      }
100 }
101 
102