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