View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.module.cxf.support;
8   
9   import org.mule.api.MuleEvent;
10  import org.mule.api.MuleMessage;
11  import org.mule.module.cxf.CxfConstants;
12  import org.mule.transport.http.HttpConnector;
13  import org.mule.transport.http.HttpConstants;
14  
15  import java.util.List;
16  import java.util.Map;
17  
18  import org.apache.cxf.helpers.CastUtils;
19  import org.apache.cxf.interceptor.AttachmentOutInterceptor;
20  import org.apache.cxf.interceptor.Fault;
21  import org.apache.cxf.message.Exchange;
22  import org.apache.cxf.message.Message;
23  import org.apache.cxf.phase.AbstractPhaseInterceptor;
24  import org.apache.cxf.phase.Phase;
25  
26  public class MuleProtocolHeadersOutInterceptor
27      extends AbstractPhaseInterceptor<Message>
28  {
29  
30      public MuleProtocolHeadersOutInterceptor()
31      {
32          super(Phase.PRE_STREAM);
33          getAfter().add(AttachmentOutInterceptor.class.getName());
34      }
35  
36      public void handleMessage(Message message) throws Fault
37      {
38          MuleEvent event = (MuleEvent) message.getExchange().get(CxfConstants.MULE_EVENT);
39          
40          if (event == null) 
41          {
42              return;
43          }
44  
45          MuleMessage muleMsg = event.getMessage();
46          
47          if (muleMsg == null)
48          {
49              return;
50          }
51          extractAndSetContentType(message, muleMsg);
52          extractAndSet(message, muleMsg, Message.RESPONSE_CODE, HttpConnector.HTTP_STATUS_PROPERTY);
53  
54          String method = (String) message.get(Message.HTTP_REQUEST_METHOD);
55          if (method == null)
56          {
57              method = HttpConstants.METHOD_POST;
58          }
59  
60          muleMsg.setOutboundProperty(HttpConnector.HTTP_METHOD_PROPERTY, method);
61          
62          Map<String, List<String>> reqHeaders = CastUtils.cast((Map<?, ?>) message.get(Message.PROTOCOL_HEADERS));
63          if (reqHeaders != null)
64          {
65              for (Map.Entry<String, List<String>> e : reqHeaders.entrySet())
66              {
67                  String key = e.getKey();
68                  String val = format(e.getValue());
69  
70                  muleMsg.setOutboundProperty(key, val);
71              }
72          }   
73          
74          if (!Boolean.TRUE.equals(message.containsKey(Message.REQUESTOR_ROLE)))
75          {
76              message.getInterceptorChain().pause();
77          }
78      }
79  
80      private void extractAndSet(Message message, MuleMessage muleMsg, String cxfHeader, String muleHeader)
81      {
82          Object val = message.get(cxfHeader);
83          if (val != null)
84          {
85              muleMsg.setOutboundProperty(muleHeader, val);
86          }
87      }
88  
89      private void extractAndSetContentType(Message message, MuleMessage muleMsg)
90      {
91          String ct = (String) message.get(Message.CONTENT_TYPE);
92          if (ct != null)
93          {
94              String encoding = getEncoding(message);
95              if (ct.indexOf("charset") == -1)
96              {
97                  ct = ct + "; charset=" + encoding;
98              }
99              muleMsg.setOutboundProperty(HttpConstants.HEADER_CONTENT_TYPE, ct);
100         }
101     }
102 
103     private String getEncoding(Message message)
104     {
105         Exchange ex = message.getExchange();
106         String encoding = (String)message.get(Message.ENCODING);
107         if (encoding == null && ex.getInMessage() != null) {
108             encoding = (String) ex.getInMessage().get(Message.ENCODING);
109             message.put(Message.ENCODING, encoding);
110         }
111 
112         if (encoding == null) {
113             encoding = "UTF-8";
114             message.put(Message.ENCODING, encoding);
115         }
116         return encoding;
117     }
118 
119     private String format(List<String> value)
120     {
121         StringBuilder sb = new StringBuilder();
122         boolean first = true;
123         
124         for (String s : value) {
125             if (!first) 
126             {
127                 sb.append(", ");
128                 first = false;
129             }
130             else 
131             {
132                 first = false;
133             }
134             
135             sb.append(s);
136         }
137         return sb.toString();
138     }
139 }
140 
141