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