View Javadoc

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