Skip to content

Commit

Permalink
Servlet 3 multipart request implements getParameterMap defensively as…
Browse files Browse the repository at this point in the history
… well

Issue: SPR-11074
(cherry picked from commit c7d1c49)
  • Loading branch information
jhoeller committed May 20, 2014
1 parent d4e1b9e commit e3e71ba
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;
Expand Down Expand Up @@ -75,22 +75,6 @@ public DefaultMultipartHttpServletRequest(HttpServletRequest request) {
}


@Override
public Enumeration<String> getParameterNames() {
Map<String, String[]> multipartParameters = getMultipartParameters();
if (multipartParameters.isEmpty()) {
return super.getParameterNames();
}

Set<String> paramNames = new LinkedHashSet<String>();
Enumeration<String> paramEnum = super.getParameterNames();
while (paramEnum.hasMoreElements()) {
paramNames.add(paramEnum.nextElement());
}
paramNames.addAll(multipartParameters.keySet());
return Collections.enumeration(paramNames);
}

@Override
public String getParameter(String name) {
String[] values = getMultipartParameters().get(name);
Expand All @@ -109,11 +93,32 @@ public String[] getParameterValues(String name) {
return super.getParameterValues(name);
}

@Override
public Enumeration<String> getParameterNames() {
Map<String, String[]> multipartParameters = getMultipartParameters();
if (multipartParameters.isEmpty()) {
return super.getParameterNames();
}

Set<String> paramNames = new LinkedHashSet<String>();
Enumeration<String> paramEnum = super.getParameterNames();
while (paramEnum.hasMoreElements()) {
paramNames.add(paramEnum.nextElement());
}
paramNames.addAll(multipartParameters.keySet());
return Collections.enumeration(paramNames);
}

@Override
public Map<String, String[]> getParameterMap() {
Map<String, String[]> paramMap = new HashMap<String, String[]>();
Map<String, String[]> multipartParameters = getMultipartParameters();
if (multipartParameters.isEmpty()) {
return super.getParameterMap();
}

Map<String, String[]> paramMap = new LinkedHashMap<String, String[]>();
paramMap.putAll(super.getParameterMap());
paramMap.putAll(getMultipartParameters());
paramMap.putAll(multipartParameters);
return paramMap;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
import java.util.Collection;
import java.util.Collections;
import java.util.Enumeration;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.Part;
Expand Down Expand Up @@ -138,6 +140,8 @@ public Enumeration<String> getParameterNames() {
return super.getParameterNames();
}

// Servlet 3.0 getParameterNames() not guaranteed to include multipart form items
// (e.g. on WebLogic 12) -> need to merge them here to be on the safe side
Set<String> paramNames = new LinkedHashSet<String>();
Enumeration<String> paramEnum = super.getParameterNames();
while (paramEnum.hasMoreElements()) {
Expand All @@ -147,6 +151,27 @@ public Enumeration<String> getParameterNames() {
return Collections.enumeration(paramNames);
}

@Override
public Map<String, String[]> getParameterMap() {
if (this.multipartParameterNames == null) {
initializeMultipart();
}
if (this.multipartParameterNames.isEmpty()) {
return super.getParameterMap();
}

// Servlet 3.0 getParameterMap() not guaranteed to include multipart form items
// (e.g. on WebLogic 12) -> need to merge them here to be on the safe side
Map<String, String[]> paramMap = new LinkedHashMap<String, String[]>();
paramMap.putAll(super.getParameterMap());
for (String paramName : this.multipartParameterNames) {
if (!paramMap.containsKey(paramName)) {
paramMap.put(paramName, getParameterValues(paramName));
}
}
return paramMap;
}

@Override
public String getMultipartContentType(String paramOrFileName) {
try {
Expand Down

0 comments on commit e3e71ba

Please sign in to comment.