Coverage Report - org.mule.util.UriParamFilter
 
Classes in this File Line Coverage Branch Coverage Complexity
UriParamFilter
0%
0/38
0%
0/24
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.util;
 8  
 
 9  
 import java.util.Map;
 10  
 import java.util.TreeMap;
 11  
 
 12  
 /**
 13  
  * filters key value pairs out of a query string
 14  
  */
 15  
 //TODO: this should really be done with RegEx but I was having a lot of difficulty. See the history for this file for a solution
 16  
 // that almost works, maybe someone else can figure out the last piece
 17  0
 public class UriParamFilter
 18  
 {
 19  
     public String filterParamsByValue(String uri, String paramValue)
 20  
     {
 21  0
         int i = uri.indexOf("?");
 22  0
         if (i == -1)
 23  
         {
 24  0
             return uri;
 25  
         }
 26  0
         String query = uri.substring(i + 1);
 27  0
         String base = uri.substring(0, i + 1);
 28  0
         StringBuffer newQuery = new StringBuffer();
 29  
 
 30  0
         TreeMap<String, String> p = getPropertiesFromQueryString(query);
 31  0
         for (Map.Entry<String, String> entry : p.entrySet())
 32  
         {
 33  0
             if (!paramValue.equals(entry.getValue()))
 34  
             {
 35  0
                 newQuery.append(entry.getKey()).append("=").append(entry.getValue()).append("&");
 36  
             }
 37  
         }
 38  0
         String result = base + newQuery.toString();
 39  0
         if (result.endsWith("?") || result.endsWith("&"))
 40  
         {
 41  0
             result = result.substring(0, result.length() - 1);
 42  
         }
 43  0
         return result;
 44  
     }
 45  
 
 46  
     private TreeMap<String, String> getPropertiesFromQueryString(String query)
 47  
     {
 48  0
         TreeMap<String, String> props = new TreeMap<String, String>();
 49  
 
 50  0
         if (query == null)
 51  
         {
 52  0
             return props;
 53  
         }
 54  
 
 55  0
         query = new StringBuffer(query.length() + 1).append('&').append(query).toString();
 56  
 
 57  0
         int x = 0;
 58  0
         while ((x = addProperty(query, x, '&', props)) != -1)
 59  
         {
 60  
             // run
 61  
         }
 62  
 
 63  0
         return props;
 64  
     }
 65  
 
 66  
     private int addProperty(String query, int start, char separator, TreeMap<String, String> properties)
 67  
     {
 68  0
         int i = query.indexOf(separator, start);
 69  0
         int i2 = query.indexOf(separator, i + 1);
 70  
         String pair;
 71  0
         if (i > -1 && i2 > -1)
 72  
         {
 73  0
             pair = query.substring(i + 1, i2);
 74  
         }
 75  0
         else if (i > -1)
 76  
         {
 77  0
             pair = query.substring(i + 1);
 78  
         }
 79  
         else
 80  
         {
 81  0
             return -1;
 82  
         }
 83  0
         int eq = pair.indexOf('=');
 84  
 
 85  0
         if (eq <= 0)
 86  
         {
 87  0
             String value = "";
 88  0
             properties.put(pair, value);
 89  0
         }
 90  
         else
 91  
         {
 92  0
             String key = pair.substring(0, eq);
 93  0
             String value = (eq == pair.length() ? "" : pair.substring(eq + 1));
 94  0
             properties.put(key, value);
 95  
         }
 96  0
         return i2;
 97  
     }
 98  
 
 99  
 }