View Javadoc

1   /*
2    * $Id: FormTransformer.java 22409 2011-07-14 05:14:27Z 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.transport.http.transformers;
12  
13  import org.mule.api.MuleMessage;
14  import org.mule.api.transformer.TransformerException;
15  import org.mule.transformer.AbstractMessageTransformer;
16  
17  import java.net.URLDecoder;
18  import java.util.ArrayList;
19  import java.util.HashMap;
20  import java.util.List;
21  import java.util.Map;
22  import java.util.StringTokenizer;
23  
24  /**
25   * Converts HTML forms POSTs into a Map of parameters. Each key can have multiple
26   * values, in which case the value will be a List<String>. Otherwise, it will
27   * be a String.
28   */
29  public class FormTransformer extends AbstractMessageTransformer
30  {
31  
32      @Override
33      public Object transformMessage(MuleMessage message, String outputEncoding) throws TransformerException
34      {
35          try
36          {
37              String v = message.getPayloadAsString();
38              Map<String, Object> values = new HashMap<String, Object>();
39  
40              for (StringTokenizer st = new StringTokenizer(v, "&"); st.hasMoreTokens();)
41              {
42                  String token = st.nextToken();
43                  int idx = token.indexOf('=');
44                  if (idx < 0)
45                  {
46                      add(values, URLDecoder.decode(token, outputEncoding), null);
47                  }
48                  else if (idx > 0)
49                  {
50                      add(values, URLDecoder.decode(token.substring(0, idx), outputEncoding),
51                          URLDecoder.decode(token.substring(idx + 1), outputEncoding));
52                  }
53              }
54              return values;
55          }
56          catch (Exception e)
57          {
58              throw new TransformerException(this, e);
59          }
60      }
61  
62      @SuppressWarnings("unchecked")
63      private void add(Map<String, Object> values, String key, String value)
64      {
65          Object existingValue = values.get(key);
66          if (existingValue == null)
67          {
68              values.put(key, value);
69          }
70          else if (existingValue instanceof List)
71          {
72              List<String> list = (List<String>) existingValue;
73              list.add(value);
74          }
75          else if (existingValue instanceof String)
76          {
77              List<String> list = new ArrayList<String>();
78              list.add((String) existingValue);
79              list.add(value);
80              values.put(key, list);
81          }
82      }
83  }