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