Skip to content
This repository has been archived by the owner on Apr 6, 2021. It is now read-only.

Commit

Permalink
Merge pull request #46 from carne-iot/0.0.5.RELEASE
Browse files Browse the repository at this point in the history
0.0.5.release
  • Loading branch information
juanmbellini committed Nov 21, 2017
2 parents f08913c + 6de438f commit e2c54ca
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 7 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

<groupId>ar.edu.itba.iot.carne-iot</groupId>
<artifactId>server</artifactId>
<version>0.0.4.RELEASE</version>
<version>0.0.5.RELEASE</version>
<packaging>pom</packaging>
<name>${project.groupId}:${project.artifactId}</name>

Expand Down
2 changes: 1 addition & 1 deletion server-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<parent>
<groupId>ar.edu.itba.iot.carne-iot</groupId>
<artifactId>server</artifactId>
<version>0.0.4.RELEASE</version>
<version>0.0.5.RELEASE</version>
</parent>

<artifactId>server-core</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion server-persistence-interfaces/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<parent>
<groupId>ar.edu.itba.iot.carne-iot</groupId>
<artifactId>server</artifactId>
<version>0.0.4.RELEASE</version>
<version>0.0.5.RELEASE</version>
</parent>

<artifactId>server-persistence-interfaces</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion server-persistence/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<parent>
<groupId>ar.edu.itba.iot.carne-iot</groupId>
<artifactId>server</artifactId>
<version>0.0.4.RELEASE</version>
<version>0.0.5.RELEASE</version>
</parent>

<artifactId>server-persistence</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion server-services-interfaces/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<parent>
<groupId>ar.edu.itba.iot.carne-iot</groupId>
<artifactId>server</artifactId>
<version>0.0.4.RELEASE</version>
<version>0.0.5.RELEASE</version>
</parent>

<artifactId>server-services-interfaces</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion server-services/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<parent>
<groupId>ar.edu.itba.iot.carne-iot</groupId>
<artifactId>server</artifactId>
<version>0.0.4.RELEASE</version>
<version>0.0.5.RELEASE</version>
</parent>

<artifactId>server-services</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion server-webapp/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<parent>
<groupId>ar.edu.itba.iot.carne-iot</groupId>
<artifactId>server</artifactId>
<version>0.0.4.RELEASE</version>
<version>0.0.5.RELEASE</version>
</parent>

<artifactId>server-webapp</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package ar.edu.itba.iot.carne_iot.server.web.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.filter.OncePerRequestFilter;
import org.springframework.web.util.WebUtils;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Locale;

/**
* A Custom {@link javax.servlet.Filter} to create a request given a method in a header from a POST request.
* Idea taken from {@link org.springframework.web.filter.HiddenHttpMethodFilter},
* using headers instead of form fields.
*/
@Component
@Order(Ordered.HIGHEST_PRECEDENCE + 1)
public class CustomHiddenMethodFilter extends OncePerRequestFilter {

/**
* Default method parameter: {@code _method}
*/
private static final String DEFAULT_METHOD_PARAM = "X-Hidden-Method";

private static final Logger LOGGER = LoggerFactory.getLogger(CustomHiddenMethodFilter.class);

@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
throws ServletException, IOException {


HttpServletRequest requestToUse = request;

if ("POST".equals(request.getMethod()) && request.getAttribute(WebUtils.ERROR_EXCEPTION_ATTRIBUTE) == null) {
String paramValue = request.getHeader(DEFAULT_METHOD_PARAM);
if (StringUtils.hasLength(paramValue)) {
LOGGER.debug("Changing hidden method request to a {} request", paramValue);
requestToUse = new CustomHiddenMethodFilter.HttpMethodRequestWrapper(request, paramValue);
}
}
filterChain.doFilter(requestToUse, response);
}

/**
* Simple {@link HttpServletRequest} wrapper that returns the supplied method for
* {@link HttpServletRequest#getMethod()}.
*/
private static class HttpMethodRequestWrapper extends HttpServletRequestWrapper {

private final String method;

private HttpMethodRequestWrapper(HttpServletRequest request, String method) {
super(request);
this.method = method.toUpperCase(Locale.ENGLISH);
}

@Override
public String getMethod() {
return this.method;
}
}
}

0 comments on commit e2c54ca

Please sign in to comment.