View Javadoc

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