Coverage Report - org.mule.transport.http.transformers.HttpRequestBodyToParamMap
 
Classes in this File Line Coverage Branch Coverage Complexity
HttpRequestBodyToParamMap
0%
0/46
0%
0/22
0
 
 1  
 /*
 2  
  * $Id: HttpRequestBodyToParamMap.java 19829 2010-10-05 02:16:56Z dirk.olmes $
 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.transport.http.transformers;
 12  
 
 13  
 import org.mule.api.MuleMessage;
 14  
 import org.mule.api.transformer.TransformerException;
 15  
 import org.mule.transformer.AbstractMessageTransformer;
 16  
 import org.mule.transformer.types.DataTypeFactory;
 17  
 import org.mule.transport.http.HttpConstants;
 18  
 import org.mule.util.StringUtils;
 19  
 
 20  
 import java.io.UnsupportedEncodingException;
 21  
 import java.net.URI;
 22  
 import java.net.URLDecoder;
 23  
 import java.util.Arrays;
 24  
 import java.util.HashMap;
 25  
 import java.util.List;
 26  
 import java.util.Map;
 27  
 
 28  
 public class HttpRequestBodyToParamMap extends AbstractMessageTransformer
 29  
 {
 30  
     public HttpRequestBodyToParamMap()
 31  0
     {
 32  0
         registerSourceType(DataTypeFactory.OBJECT);
 33  0
         setReturnDataType(DataTypeFactory.OBJECT);
 34  0
     }
 35  
 
 36  
     @Override
 37  
     public Object transformMessage(MuleMessage message, String outputEncoding) throws TransformerException
 38  
     {
 39  0
         Map<String, Object> paramMap = new HashMap<String, Object>();
 40  
 
 41  
         try
 42  
         {
 43  0
             String httpMethod = message.getInboundProperty("http.method");
 44  0
             String contentType = message.getInboundProperty("Content-Type");
 45  
             
 46  0
             boolean isGet = HttpConstants.METHOD_GET.equalsIgnoreCase(httpMethod);
 47  0
             boolean isPost = HttpConstants.METHOD_POST.equalsIgnoreCase(httpMethod);
 48  0
             boolean isUrlEncoded = false;
 49  0
             if (contentType != null)
 50  
             {
 51  0
                 isUrlEncoded = contentType.startsWith("application/x-www-form-urlencoded");
 52  
             }
 53  
 
 54  0
             if (!(isGet || (isPost && isUrlEncoded)))
 55  
             {
 56  0
                 throw new Exception("The HTTP method or content type is unsupported!");
 57  
             }
 58  
 
 59  0
             String queryString = null;
 60  0
             if (isGet)
 61  
             {
 62  0
                 URI uri = new URI(message.getPayloadAsString(outputEncoding));
 63  0
                 queryString = uri.getRawQuery();
 64  0
             }
 65  0
             else if (isPost)
 66  
             {
 67  0
                 queryString = new String(message.getPayloadAsBytes());
 68  
             }
 69  
 
 70  0
             if (StringUtils.isNotBlank(queryString))
 71  
             {
 72  0
                 addQueryStringToParameterMap(queryString, paramMap, outputEncoding);
 73  
             }
 74  
         }
 75  0
         catch (Exception e)
 76  
         {
 77  0
             throw new TransformerException(this, e);
 78  0
         }
 79  
 
 80  0
         return paramMap;
 81  
     }
 82  
 
 83  
     protected void addQueryStringToParameterMap(String queryString, Map<String, Object> paramMap,
 84  
         String outputEncoding) throws UnsupportedEncodingException
 85  
     {
 86  0
         String[] pairs = queryString.split("&");
 87  0
         for (String pair : pairs)
 88  
         {
 89  0
             String[] nameValue = pair.split("=");
 90  0
             if (nameValue.length == 2)
 91  
             {
 92  0
                 String key = URLDecoder.decode(nameValue[0], outputEncoding);
 93  0
                 String value = URLDecoder.decode(nameValue[1], outputEncoding);
 94  0
                 addToParameterMap(paramMap, key, value);
 95  
             }
 96  
         }
 97  0
     }
 98  
 
 99  
     @SuppressWarnings("unchecked")
 100  
     protected void addToParameterMap(Map<String, Object> paramMap, String key, String value)
 101  
     {
 102  0
         Object existingValue = paramMap.get(key);
 103  0
         if (existingValue != null)
 104  
         {
 105  
             List<Object> values;
 106  0
             if (existingValue instanceof List<?>)
 107  
             {
 108  0
                 values = (List<Object>) existingValue;
 109  
             }
 110  
             else
 111  
             {
 112  0
                 values = Arrays.asList(existingValue);
 113  
             }
 114  
 
 115  0
             values.add(value);
 116  0
             paramMap.put(key, values);
 117  0
         }
 118  
         else
 119  
         {
 120  0
             paramMap.put(key, value);
 121  
         }
 122  0
     }
 123  
 
 124  
     @Override
 125  
     public boolean isAcceptNull()
 126  
     {
 127  0
         return false;
 128  
     }
 129  
 }