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

ENG-1213 adds an interceptor to verify the max upload size of each re… #53

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
@@ -0,0 +1,14 @@
package org.entando.entando.web.common.exceptions;

import com.agiletec.aps.system.exception.EntRuntimeException;

public class FileMaxSizeException extends EntRuntimeException {

public FileMaxSizeException(String message) {
super(message);
}

public FileMaxSizeException(String message, Throwable cause) {
super(message, cause);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,16 @@ public ErrorRestResponse processValidationError(MethodArgumentNotValidException
return processAllErrors(result);
}

@ExceptionHandler(value = FileMaxSizeException.class)
@ResponseStatus(HttpStatus.PAYLOAD_TOO_LARGE)
@ResponseBody
public ErrorRestResponse processTooLargePayload(FileMaxSizeException ex) {
logger.debug("Handling {} error", ex.getClass().getSimpleName());
ErrorRestResponse dto = new ErrorRestResponse();
dto.addError(new RestError("1", "File is too large"));
return dto;
}

private ErrorRestResponse processAllErrors(BindingResult result) {
return processAllErrors(result.getFieldErrors(), result.getGlobalErrors());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package org.entando.entando.web.common.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.entando.entando.web.common.exceptions.FileMaxSizeException;
import org.springframework.http.HttpMethod;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

public class FileBrowserSizeInterceptor extends HandlerInterceptorAdapter {

private int fileUploadMaxSize;

public FileBrowserSizeInterceptor(int maxSize) {
this.fileUploadMaxSize = maxSize;
}

public void setFileUploadMaxSize(int fileUploadMaxSize) {
this.fileUploadMaxSize = fileUploadMaxSize;
}

@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
return isValidRequest(request);
}

private boolean isValidRequest(HttpServletRequest request) {
if (isUploadMethod(request) && request.getContentLength() > this.fileUploadMaxSize) {
throw new FileMaxSizeException("Invalid max content-length");
}
return true;
}

private boolean isUploadMethod(HttpServletRequest request) {
HttpMethod method = HttpMethod.resolve(request.getMethod());
return HttpMethod.POST.equals(method) || HttpMethod.POST.equals(method);
Copy link
Contributor

Choose a reason for hiding this comment

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

guess this was meant to be POST and PUT
please fix

}
}
7 changes: 6 additions & 1 deletion src/main/resources/spring/web/servlet-context.xml
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,15 @@
class="org.entando.entando.web.common.interceptor.EntandoOauth2Interceptor" />
<bean id="activityStreamInterceptor"
class="org.entando.entando.web.common.interceptor.ActivityStreamInterceptor" />

<bean id="fileBrowserSizeInterceptor"
class="org.entando.entando.web.common.interceptor.FileBrowserSizeInterceptor">
<constructor-arg name="maxSize" value="${file.upload.maxSize}" />
</bean>

<mvc:interceptors>
<ref bean="entandoInterceptor" />
<ref bean="activityStreamInterceptor" />
<ref bean="fileBrowserSizeInterceptor" />
</mvc:interceptors>

</beans>