Coverage Report - org.mule.example.hello.HttpRequestToNameString
 
Classes in this File Line Coverage Branch Coverage Complexity
HttpRequestToNameString
0%
0/43
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.example.hello;
 8  
 
 9  
 import org.mule.api.transformer.TransformerException;
 10  
 import org.mule.transformer.AbstractTransformer;
 11  
 import org.mule.transformer.types.DataTypeFactory;
 12  
 import org.mule.util.IOUtils;
 13  
 
 14  
 import java.io.InputStream;
 15  
 import java.io.UnsupportedEncodingException;
 16  
 import java.net.URLDecoder;
 17  
 
 18  
 public class HttpRequestToNameString extends AbstractTransformer
 19  
 {
 20  
     private static final String NAME_REQUEST_PARAMETER = "name=";
 21  
     
 22  
     public HttpRequestToNameString()
 23  
     {
 24  0
         super();
 25  0
         this.registerSourceType(DataTypeFactory.STRING);
 26  0
         this.registerSourceType(DataTypeFactory.BYTE_ARRAY);
 27  0
         this.registerSourceType(DataTypeFactory.INPUT_STREAM);
 28  0
         this.setReturnDataType(DataTypeFactory.create(NameString.class));
 29  0
     }
 30  
 
 31  
     @Override
 32  
     public Object doTransform(Object src, String encoding) throws TransformerException
 33  
     {
 34  0
         return new NameString(extractNameValue(extractRequestQuery(convertRequestToString(src, encoding))));
 35  
     }
 36  
     
 37  
     private String convertRequestToString(Object src, String encoding)
 38  
     {
 39  0
         String srcAsString = null;
 40  
         
 41  0
         if (src instanceof byte[])
 42  
         {
 43  0
             if (encoding != null)
 44  
             {
 45  
                 try
 46  
                 {
 47  0
                     srcAsString = new String((byte[])src, encoding);
 48  
                 }
 49  0
                 catch (UnsupportedEncodingException ex)
 50  
                 {
 51  0
                     srcAsString = new String((byte[])src);
 52  0
                 }
 53  
             }
 54  
             else
 55  
             {
 56  0
                 srcAsString = new String((byte[])src);
 57  
             }
 58  
         }
 59  0
         else if (src instanceof InputStream)
 60  
         {
 61  0
             InputStream input = (InputStream) src;
 62  
             try
 63  
             {
 64  0
                 srcAsString = IOUtils.toString(input);
 65  
             }
 66  
             finally
 67  
             {
 68  0
                 IOUtils.closeQuietly(input);
 69  0
             }
 70  0
         }
 71  
         else
 72  
         {
 73  0
             srcAsString = src.toString();
 74  
         }
 75  
         
 76  0
         return srcAsString;
 77  
     }
 78  
     
 79  
     private String extractRequestQuery(String request)
 80  
     {
 81  0
         String requestQuery = null;
 82  
         
 83  0
         if (request != null && request.length() > 0 && request.indexOf('?') != -1)
 84  
         {
 85  0
             requestQuery = request.substring(request.indexOf('?') + 1).trim();
 86  
         }
 87  
 
 88  0
         return requestQuery;
 89  
     }
 90  
     
 91  
     private String extractNameValue(String requestQuery) throws TransformerException
 92  
     {
 93  0
         String nameValue = null;
 94  
         
 95  0
         if (requestQuery != null && requestQuery.length() > 0)
 96  
         {
 97  0
             int nameParameterPos = requestQuery.indexOf(NAME_REQUEST_PARAMETER);
 98  0
             if (nameParameterPos != -1)
 99  
             {
 100  0
                 int nextParameterValuePos = requestQuery.indexOf('&');
 101  0
                 if (nextParameterValuePos == -1 || nextParameterValuePos < nameParameterPos)
 102  
                 {
 103  0
                     nextParameterValuePos = requestQuery.length();
 104  
                 }
 105  
 
 106  0
                 nameValue = requestQuery.substring(nameParameterPos + NAME_REQUEST_PARAMETER.length(), nextParameterValuePos);
 107  
             }
 108  
             
 109  0
             if (nameValue != null && nameValue.length() > 0)
 110  
             {
 111  
                 try
 112  
                 {
 113  0
                     nameValue = URLDecoder.decode(nameValue, "UTF-8");
 114  
                 }
 115  0
                 catch (UnsupportedEncodingException uee)
 116  
                 {
 117  0
                     logger.error(uee.getMessage());
 118  0
                 }
 119  
             }
 120  
         }
 121  
 
 122  0
         if (nameValue == null)
 123  
         {
 124  0
             nameValue = "";
 125  
         }
 126  
         
 127  0
         return nameValue;
 128  
     }
 129  
 }