Coverage Report - org.mule.module.cxf.support.MuleProtocolHeadersOutInterceptor
 
Classes in this File Line Coverage Branch Coverage Complexity
MuleProtocolHeadersOutInterceptor
0%
0/52
0%
0/26
0
 
 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  0
         super(Phase.PRE_STREAM);
 37  0
         getAfter().add(AttachmentOutInterceptor.class.getName());
 38  0
     }
 39  
 
 40  
     public void handleMessage(Message message) throws Fault
 41  
     {
 42  0
         MuleEvent event = (MuleEvent) message.getExchange().get(CxfConstants.MULE_EVENT);
 43  0
         MuleMessage muleMsg = event.getMessage();
 44  
         
 45  0
         if (muleMsg == null)
 46  
         {
 47  0
             return;
 48  
         }
 49  0
         extractAndSetContentType(message, muleMsg);
 50  0
         extractAndSet(message, muleMsg, Message.RESPONSE_CODE, HttpConnector.HTTP_STATUS_PROPERTY);
 51  
 
 52  0
         String method = (String) message.get(Message.HTTP_REQUEST_METHOD);
 53  0
         if (method == null)
 54  
         {
 55  0
             method = HttpConstants.METHOD_POST;
 56  
         }
 57  
 
 58  0
         muleMsg.setOutboundProperty(HttpConnector.HTTP_METHOD_PROPERTY, method);
 59  
         
 60  0
         Map<String, List<String>> reqHeaders = CastUtils.cast((Map<?, ?>) message.get(Message.PROTOCOL_HEADERS));
 61  0
         if (reqHeaders != null)
 62  
         {
 63  0
             for (Map.Entry<String, List<String>> e : reqHeaders.entrySet())
 64  
             {
 65  0
                 String key = e.getKey();
 66  0
                 String val = format(e.getValue());
 67  
 
 68  0
                 muleMsg.setOutboundProperty(key, val);
 69  0
             }
 70  
         }   
 71  
         
 72  0
         if (!Boolean.TRUE.equals(message.containsKey(Message.REQUESTOR_ROLE)))
 73  
         {
 74  0
             message.getInterceptorChain().pause();
 75  
         }
 76  0
     }
 77  
 
 78  
     private void extractAndSet(Message message, MuleMessage muleMsg, String cxfHeader, String muleHeader)
 79  
     {
 80  0
         Object val = message.get(cxfHeader);
 81  0
         if (val != null)
 82  
         {
 83  0
             muleMsg.setOutboundProperty(muleHeader, val);
 84  
         }
 85  0
     }
 86  
 
 87  
     private void extractAndSetContentType(Message message, MuleMessage muleMsg)
 88  
     {
 89  0
         String ct = (String) message.get(Message.CONTENT_TYPE);
 90  0
         if (ct != null)
 91  
         {
 92  0
             String encoding = getEncoding(message);
 93  0
             if (ct.indexOf("charset") == -1)
 94  
             {
 95  0
                 ct = ct + "; charset=" + encoding;
 96  
             }
 97  0
             muleMsg.setOutboundProperty(HttpConstants.HEADER_CONTENT_TYPE, ct);
 98  
         }
 99  0
     }
 100  
 
 101  
     private String getEncoding(Message message)
 102  
     {
 103  0
         Exchange ex = message.getExchange();
 104  0
         String encoding = (String)message.get(Message.ENCODING);
 105  0
         if (encoding == null && ex.getInMessage() != null) {
 106  0
             encoding = (String) ex.getInMessage().get(Message.ENCODING);
 107  0
             message.put(Message.ENCODING, encoding);
 108  
         }
 109  
 
 110  0
         if (encoding == null) {
 111  0
             encoding = "UTF-8";
 112  0
             message.put(Message.ENCODING, encoding);
 113  
         }
 114  0
         return encoding;
 115  
     }
 116  
 
 117  
     private String format(List<String> value)
 118  
     {
 119  0
         StringBuilder sb = new StringBuilder();
 120  0
         boolean first = true;
 121  
         
 122  0
         for (String s : value) {
 123  0
             if (!first) 
 124  
             {
 125  0
                 sb.append(", ");
 126  0
                 first = false;
 127  
             }
 128  
             else 
 129  
             {
 130  0
                 first = false;
 131  
             }
 132  
             
 133  0
             sb.append(s);
 134  
         }
 135  0
         return sb.toString();
 136  
     }
 137  
 }
 138  
 
 139