View Javadoc

1   /*
2    * $Id: MuleProtocolHeadersOutInterceptor.java 20109 2010-11-07 05:20:30Z mike.schilling $
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.apache.cxf.message.Exchange;
14  import org.mule.api.MuleEvent;
15  import org.mule.api.MuleMessage;
16  import org.mule.module.cxf.CxfConstants;
17  import org.mule.transport.http.HttpConnector;
18  import org.mule.transport.http.HttpConstants;
19  
20  import java.util.List;
21  import java.util.Map;
22  
23  import org.apache.cxf.helpers.CastUtils;
24  import org.apache.cxf.interceptor.AttachmentOutInterceptor;
25  import org.apache.cxf.interceptor.Fault;
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          MuleMessage muleMsg = event.getMessage();
44          
45          if (muleMsg == null)
46          {
47              return;
48          }
49          extractAndSetContentType(message, muleMsg);
50          extractAndSet(message, muleMsg, Message.RESPONSE_CODE, HttpConnector.HTTP_STATUS_PROPERTY);
51  
52          String method = (String) message.get(Message.HTTP_REQUEST_METHOD);
53          if (method == null)
54          {
55              method = HttpConstants.METHOD_POST;
56          }
57  
58          muleMsg.setOutboundProperty(HttpConnector.HTTP_METHOD_PROPERTY, method);
59          
60          Map<String, List<String>> reqHeaders = CastUtils.cast((Map<?, ?>) message.get(Message.PROTOCOL_HEADERS));
61          if (reqHeaders != null)
62          {
63              for (Map.Entry<String, List<String>> e : reqHeaders.entrySet())
64              {
65                  String key = e.getKey();
66                  String val = format(e.getValue());
67  
68                  muleMsg.setOutboundProperty(key, val);
69              }
70          }   
71          
72          if (!Boolean.TRUE.equals(message.containsKey(Message.REQUESTOR_ROLE)))
73          {
74              message.getInterceptorChain().pause();
75          }
76      }
77  
78      private void extractAndSet(Message message, MuleMessage muleMsg, String cxfHeader, String muleHeader)
79      {
80          Object val = message.get(cxfHeader);
81          if (val != null)
82          {
83              muleMsg.setOutboundProperty(muleHeader, val);
84          }
85      }
86  
87      private void extractAndSetContentType(Message message, MuleMessage muleMsg)
88      {
89          String ct = (String) message.get(Message.CONTENT_TYPE);
90          if (ct != null)
91          {
92              String encoding = getEncoding(message);
93              if (ct.indexOf("charset") == -1)
94              {
95                  ct = ct + "; charset=" + encoding;
96              }
97              muleMsg.setOutboundProperty(HttpConstants.HEADER_CONTENT_TYPE, ct);
98          }
99      }
100 
101     private String getEncoding(Message message)
102     {
103         Exchange ex = message.getExchange();
104         String encoding = (String)message.get(Message.ENCODING);
105         if (encoding == null && ex.getInMessage() != null) {
106             encoding = (String) ex.getInMessage().get(Message.ENCODING);
107             message.put(Message.ENCODING, encoding);
108         }
109 
110         if (encoding == null) {
111             encoding = "UTF-8";
112             message.put(Message.ENCODING, encoding);
113         }
114         return encoding;
115     }
116 
117     private String format(List<String> value)
118     {
119         StringBuilder sb = new StringBuilder();
120         boolean first = true;
121         
122         for (String s : value) {
123             if (!first) 
124             {
125                 sb.append(", ");
126                 first = false;
127             }
128             else 
129             {
130                 first = false;
131             }
132             
133             sb.append(s);
134         }
135         return sb.toString();
136     }
137 }
138 
139