Skip to content
This repository has been archived by the owner on Nov 9, 2017. It is now read-only.

Commit

Permalink
Allow multiple different values for same parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
seanf committed May 6, 2016
1 parent e472845 commit fe5ae00
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import com.google.common.collect.ImmutableMap;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
Expand All @@ -33,15 +34,17 @@
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import java.io.IOException;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Map.Entry;

import static java.util.Collections.addAll;
import static javax.servlet.DispatcherType.*;

/**
* Replaces any duplicated request parameter keys with a single parameter
* (using the first parameter value). Intended only as a workaround for
* https://github.com/ocpsoft/rewrite/issues/223
* Removes duplicated values for each parameter name (multiple values are
* preserved in order, as long as the values are diferent). Intended only as
* a workaround for https://github.com/ocpsoft/rewrite/issues/223
* @author Sean Flanigan <a href="mailto:sflaniga@redhat.com">sflaniga@redhat.com</a>
*/
@WebFilter(filterName = "DuplicateParamFilter", urlPatterns = "/*", dispatcherTypes = {
Expand Down Expand Up @@ -72,23 +75,29 @@ public FilteredRequest(ServletRequest request) {
super((HttpServletRequest) request);
}

private boolean isEmpty(@Nullable String[] values) {
return values == null || values.length == 0;
}

@Override
public @Nullable String getParameter(String paramName) {
@Nullable String[] values = getRequest().getParameterValues(paramName);
return isEmpty(values) ? null : values[0];
return getRequest().getParameter(paramName);
}

// Return array of first element, if any, otherwise null.
private @Nullable String[] head(@Nullable String[] values) {
return isEmpty(values) ? null : new String[] { values[0] };
// Remove duplicate strings, preserving order
@Nonnull static String[] deduplicate(@Nonnull String[] values) {
// premature optimisation:
if (values.length < 2) {
return values;
}
LinkedHashSet<String> set = new LinkedHashSet<>();
addAll(set, values);
return set.toArray(new String[set.size()]);
}

@Override
public @Nullable String[] getParameterValues(String paramName) {
@Nullable String[] values = getRequest().getParameterValues(paramName);
return head(values);
if (values == null) {
return null;
}
return deduplicate(values);
}

@Override
Expand All @@ -102,9 +111,9 @@ public Map<String, String[]> getParameterMap() {
ImmutableMap.builder();
for (Entry<String, String[]> e : origMap.entrySet()) {
String key = e.getKey();
String[] values = head(e.getValue());
String[] values = e.getValue();
if (values != null) {
mapBuilder.put(key, values);
mapBuilder.put(key, deduplicate(values));
}
}
lazyMap = mapBuilder.build();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright 2016, Red Hat, Inc. and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
* distribution for a full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.zanata.servlet;

import org.junit.Test;

import static org.assertj.core.api.Assertions.assertThat;
import static org.zanata.servlet.DuplicateParamFilter.FilteredRequest.deduplicate;

/**
* @author Sean Flanigan <a href="mailto:sflaniga@redhat.com">sflaniga@redhat.com</a>
*/
public class DuplicateParamFilterTest {
@Test
public void testDeduplicateNull() {
assertThat(deduplicate(null)).isNull();
}

@Test
public void testDeduplicateEmpty() {
String[] vals = new String[0];
assertThat(deduplicate(vals)).isEmpty();
}

@Test
public void testDeduplicateOneString() {
String[] vals = new String[] {"Testing"};
assertThat(deduplicate(vals)).containsExactly(vals);
}

@Test
public void testDeduplicateTwoIdentical() {
String[] vals = new String[] {"Testing", "Testing"};
assertThat(deduplicate(vals)).containsExactly("Testing");
}

@Test
public void testDeduplicateMixed() {
String[] vals = new String[] {"a", "b", "a", "c", "c", "", "", "d"};
assertThat(deduplicate(vals)).containsExactly("a", "b", "c", "", "d");
}

}

0 comments on commit fe5ae00

Please sign in to comment.