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