Coverage Report - org.mule.transport.cxf.support.MuleProtocolHeadersOutInterceptor
 
Classes in this File Line Coverage Branch Coverage Complexity
MuleProtocolHeadersOutInterceptor
94%
31/33
81%
13/16
0
 
 1  
 /*
 2  
  * $Id: MuleProtocolHeadersOutInterceptor.java 11664 2008-04-30 03:29:13Z dandiep $
 3  
  * --------------------------------------------------------------------------------------
 4  
  * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.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.transport.cxf.support;
 12  
 
 13  
 import org.mule.api.transport.MessageAdapter;
 14  
 import org.mule.transport.cxf.CxfConstants;
 15  
 import org.mule.transport.http.HttpConnector;
 16  
 import org.mule.transport.http.HttpConstants;
 17  
 
 18  
 import java.util.List;
 19  
 import java.util.Map;
 20  
 
 21  
 import org.apache.cxf.helpers.CastUtils;
 22  
 import org.apache.cxf.interceptor.AttachmentOutInterceptor;
 23  
 import org.apache.cxf.interceptor.Fault;
 24  
 import org.apache.cxf.message.Message;
 25  
 import org.apache.cxf.phase.AbstractPhaseInterceptor;
 26  
 import org.apache.cxf.phase.Phase;
 27  
 
 28  
 public class MuleProtocolHeadersOutInterceptor
 29  
     extends AbstractPhaseInterceptor<Message>
 30  
 {
 31  
 
 32  
     public MuleProtocolHeadersOutInterceptor()
 33  
     {
 34  732
         super(Phase.PRE_STREAM);
 35  732
         getAfter().add(AttachmentOutInterceptor.class.getName());
 36  732
     }
 37  
 
 38  
     public void handleMessage(Message message) throws Fault
 39  
     {
 40  186
         MessageAdapter muleMsg = (MessageAdapter) message.getExchange().get(CxfConstants.MULE_MESSAGE);
 41  
 
 42  186
         if (muleMsg == null)
 43  
         {
 44  4
             return;
 45  
         }
 46  182
         extractAndSet(message, muleMsg, Message.CONTENT_TYPE, HttpConstants.HEADER_CONTENT_TYPE);
 47  
 
 48  182
         String method = (String) message.get(Message.HTTP_REQUEST_METHOD);
 49  182
         if (method == null) method = HttpConstants.METHOD_POST;
 50  
         
 51  182
         muleMsg.setProperty(HttpConnector.HTTP_METHOD_PROPERTY, method);
 52  
         
 53  182
         Map<String, List<String>> reqHeaders = CastUtils.cast((Map<?, ?>) message.get(Message.PROTOCOL_HEADERS));
 54  182
         if (reqHeaders != null)
 55  
         {
 56  172
             for (Map.Entry<String, List<String>> e : reqHeaders.entrySet())
 57  
             {
 58  172
                 String key = e.getKey();
 59  172
                 String val = format(e.getValue());
 60  
                 
 61  172
                 muleMsg.setProperty(key, val);
 62  172
             }
 63  
         }   
 64  
         
 65  182
         if (!Boolean.TRUE.equals(message.containsKey(Message.REQUESTOR_ROLE)))
 66  
         {
 67  106
             message.getInterceptorChain().pause();
 68  
         }
 69  182
     }
 70  
 
 71  
     private void extractAndSet(Message message, MessageAdapter muleMsg, String cxfHeader, String muleHeader)
 72  
     {
 73  182
         String ct = (String) message.get(cxfHeader);
 74  182
         if (ct != null)
 75  
         {
 76  182
             muleMsg.setProperty(muleHeader, ct);
 77  
         }
 78  182
     }
 79  
 
 80  
     private String format(List<String> value)
 81  
     {
 82  172
         StringBuilder sb = new StringBuilder();
 83  172
         boolean first = true;
 84  
         
 85  172
         for (String s : value) {
 86  172
             if (!first) 
 87  
             {
 88  0
                 sb.append(", ");
 89  0
                 first = false;
 90  
             }
 91  
             else 
 92  
             {
 93  172
                 first = false;
 94  
             }
 95  
             
 96  172
             sb.append(s);
 97  
         }
 98  172
         return sb.toString();
 99  
     }
 100  
 }
 101  
 
 102