Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upload observing instead of intercepting #256

Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,15 @@
*/
package br.com.caelum.vraptor.interceptor.multipart;

import static org.slf4j.LoggerFactory.getLogger;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Collection;
import java.util.List;

import javax.enterprise.context.RequestScoped;
import javax.enterprise.event.Observes;
import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;

Expand All @@ -32,15 +35,10 @@
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.io.FilenameUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import br.com.caelum.vraptor.Intercepts;
import br.com.caelum.vraptor.controller.ControllerMethod;
import br.com.caelum.vraptor.core.InterceptorStack;
import br.com.caelum.vraptor.http.InvalidParameterException;
import br.com.caelum.vraptor.http.MutableRequest;
import br.com.caelum.vraptor.interceptor.Interceptor;
import br.com.caelum.vraptor.interceptor.ParametersInstantiatorInterceptor;
import br.com.caelum.vraptor.validator.I18nMessage;
import br.com.caelum.vraptor.validator.Validator;

Expand All @@ -51,17 +49,17 @@
import com.google.common.collect.Multiset;

/**
* A multipart interceptor based on Apache Commons Upload. Provided parameters are injected through
* A multipart observer based on Apache Commons Upload. Provided parameters are injected through
* {@link HttpServletRequest#setAttribute(String, Object)} and uploaded files are made available through.
*
* @author Guilherme Silveira
* @author Otávio Scherer Garcia
* @author Rodrigo Turini
*/
@Intercepts(before=ParametersInstantiatorInterceptor.class)
@RequestScoped
public class CommonsUploadMultipartInterceptor implements Interceptor {
public class CommonsUploadMultipartObserver {

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

private final MutableRequest request;
private final MultipartConfig config;
Expand All @@ -70,30 +68,24 @@ public class CommonsUploadMultipartInterceptor implements Interceptor {
private Multiset<String> indexes;
private Multimap<String, String> params;

/**
/**
* @deprecated CDI eyes only
*/
protected CommonsUploadMultipartInterceptor() {
protected CommonsUploadMultipartObserver() {
this(null, null, null);
}

@Inject
public CommonsUploadMultipartInterceptor(MutableRequest request, MultipartConfig cfg, Validator validator) {
public CommonsUploadMultipartObserver(MutableRequest request, MultipartConfig cfg, Validator validator) {
this.request = request;
this.validator = validator;
this.config = cfg;
}

/**
* Will intercept the request if apache file upload says that this request is multipart
*/
@Override
public boolean accepts(ControllerMethod method) {
return ServletFileUpload.isMultipartContent(request);
}

@Override
public void intercept(InterceptorStack stack, ControllerMethod method, Object controllerInstance) {
public void upload(@Observes ControllerMethod controllerMethod) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this the correct event? wasn't something like ReadyToExecute?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we need run it before ParameterInstantiatorInterceptor, ReadyToExecute is fired after

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok.. where is it being fired? what else is observing this event?

ControllerMethod is not an event, it's a product of one. Maybe we should wrap it in another class...

Just maybe ;)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok.. where is it being fired? what else is observing this event?

fired by ControllerLookupInterceptor,
observed by ControllerMethodFactory, NullMultipart and CommonsUploadMultipart

Maybe we should wrap it in another class...

I think about that before using ControllerMethod directly, do you have a good name for this new event?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

something like ControllerMethodDiscovered

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done here 775a81b


if (!ServletFileUpload.isMultipartContent(request)) return;

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

indexes = HashMultiset.create();
Expand Down Expand Up @@ -136,8 +128,6 @@ public void intercept(InterceptorStack stack, ControllerMethod method, Object co
logger.warn("There was some problem parsing this multipart request, "
+ "or someone is not sending a RFC1867 compatible multipart request.", e);
}

stack.next(method, controllerInstance);
}

private boolean isNotEmpty(FileItem item) {
Expand All @@ -146,8 +136,6 @@ private boolean isNotEmpty(FileItem item) {

/**
* This method is called when the {@link SizeLimitExceededException} was thrown.
*
* @param e
*/
protected void reportSizeLimitExceeded(final SizeLimitExceededException e) {
validator.add(new I18nMessage("upload", "file.limit.exceeded", e.getActualSize(), e.getPermittedSize()));
Expand All @@ -172,7 +160,7 @@ protected ServletFileUpload createServletFileUpload(MultipartConfig config) {
factory.setRepository(config.getDirectory());

logger.debug("Using repository {} for file upload", factory.getRepository());

return new ServletFileUpload(factory);
}

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/***
* Copyright (c) 2009 Caelum - www.caelum.com.br/opensource
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package br.com.caelum.vraptor.interceptor.multipart;

import static com.google.common.base.Strings.nullToEmpty;
import static org.slf4j.LoggerFactory.getLogger;

import javax.enterprise.context.RequestScoped;
import javax.enterprise.event.Observes;
import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;

import br.com.caelum.vraptor.controller.ControllerMethod;

/**
* A null implementation of {@link MultipartInterceptor}. This interceptor will
* be activated when no commons-fileupload was found in classpath. If application
* try to upload any files, this interceptor will warn a message in console.
*
* @author Otávio Scherer Garcia
* @author Rodrigo Turini
* @since 3.1.3
*/
@RequestScoped
public class NullMultipartObserver {

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

private final HttpServletRequest request;

/**
* @deprecated CDI eyes only
*/
protected NullMultipartObserver() {
this(null);
}

@Inject
public NullMultipartObserver(HttpServletRequest request) {
this.request = request;
}

public void nullUpload(@Observes ControllerMethod controllerMethod) {
if (request.getMethod().toUpperCase().equals("POST")
&& nullToEmpty(request.getContentType()).startsWith("multipart/form-data")) {
logger.warn("There is no file upload handlers registered. If you are willing to "
+ "upload a file, please add the commons-fileupload in your classpath");
}
}
}
Loading