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