View Javadoc

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          super();
26          this.registerSourceType(String.class);
27          this.registerSourceType(byte[].class);
28          this.setReturnClass(NameString.class);
29      }
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          return new NameString(extractNameValue(extractRequestQuery(convertRequestToString(src, encoding))));
39          
40      }
41      
42      private String convertRequestToString(Object src, String encoding)
43      {
44          String srcAsString = null;
45          
46          if (src instanceof byte[])
47          {
48              if (encoding != null)
49              {
50                  try
51                  {
52                      srcAsString = new String((byte[])src, encoding);
53                  }
54                  catch (UnsupportedEncodingException ex)
55                  {
56                      srcAsString = new String((byte[])src);
57                  }
58              }
59              else
60              {
61                  srcAsString = new String((byte[])src);
62              }
63          }
64          else
65          {
66              srcAsString = src.toString();
67          }        
68          
69          return srcAsString;
70      }
71      
72      private String extractRequestQuery(String request)
73      {
74          String requestQuery = null;
75          
76          if (request != null && request.length() > 0 && request.indexOf('?') != -1)
77          {
78              requestQuery = request.substring(request.indexOf('?') + 1).trim();
79          }
80  
81          return requestQuery;
82      }
83      
84      private String extractNameValue(String requestQuery) throws TransformerException
85      {
86          String nameValue = null;
87          
88          if (requestQuery != null && requestQuery.length() > 0)
89          {
90              int nameParameterPos = requestQuery.indexOf(NAME_REQUEST_PARAMETER);
91              if (nameParameterPos != -1)
92              {
93                  int nextParameterValuePos = requestQuery.indexOf('&'); 
94                  if (nextParameterValuePos == -1 || nextParameterValuePos < nameParameterPos)
95                  {
96                      nextParameterValuePos = requestQuery.length();
97                  }
98  
99                  nameValue = requestQuery.substring(nameParameterPos + NAME_REQUEST_PARAMETER.length(), nextParameterValuePos);
100             }
101             
102             if (nameValue != null && nameValue.length() > 0)
103             {
104                 try
105                 {
106                     nameValue = URLDecoder.decode(nameValue, "UTF-8");
107                 }
108                 catch (UnsupportedEncodingException uee)
109                 {
110                     logger.error(uee.getMessage());
111                 }
112             }
113         }
114 
115         if (nameValue == null)
116         {
117             nameValue = "";
118         }
119         
120         return nameValue;
121     }
122 }