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