Skip to content

Commit

Permalink
Merge pull request #317 from caelum/ot-appscopedupload
Browse files Browse the repository at this point in the history
Upload as application scoped
  • Loading branch information
garcia-jj committed Jan 17, 2014
2 parents 40a7244 + be24097 commit d8627fc
Showing 1 changed file with 8 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import java.util.Collection;
import java.util.List;

import javax.enterprise.context.RequestScoped;
import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.event.Observes;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
Expand Down Expand Up @@ -56,14 +56,11 @@
* @author Otávio Scherer Garcia
* @author Rodrigo Turini
*/
@RequestScoped
@ApplicationScoped
public class CommonsUploadMultipartObserver {

private static final Logger logger = getLogger(CommonsUploadMultipartObserver.class);

private Multiset<String> indexes;
private Multimap<String, String> params;

public void upload(@Observes ControllerMethodDiscovered event, MutableRequest request,
MultipartConfig config, Validator validator) {

Expand All @@ -73,8 +70,8 @@ public void upload(@Observes ControllerMethodDiscovered event, MutableRequest re

logger.info("Request contains multipart data. Try to parse with commons-upload.");

indexes = HashMultiset.create();
params = LinkedListMultimap.create();
final Multiset<String> indexes = HashMultiset.create();
final Multimap<String, String> params = LinkedListMultimap.create();

ServletFileUpload uploader = createServletFileUpload(config);
uploader.setSizeMax(config.getSizeLimit());
Expand All @@ -86,7 +83,7 @@ public void upload(@Observes ControllerMethodDiscovered event, MutableRequest re

for (FileItem item : items) {
String name = item.getFieldName();
name = fixIndexedParameters(name);
name = fixIndexedParameters(name, indexes);

if (item.isFormField()) {
logger.debug("{} is a field", name);
Expand Down Expand Up @@ -161,12 +158,13 @@ protected String getValue(FileItem item, ServletRequest request) {
return item.getString();
}

protected String fixIndexedParameters(String name) {
protected String fixIndexedParameters(String name, Multiset<String> indexes) {
if (name.contains("[]")) {
String newName = name.replace("[]", "[" + (indexes.count(name)) + "]");
indexes.add(name);
logger.debug("{} was renamed to {}", name, newName);
name = newName;

return newName;
}
return name;
}
Expand Down

0 comments on commit d8627fc

Please sign in to comment.