Skip to content

Instantly share code, notes, and snippets.

@filosganga
Created April 16, 2011 19:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save filosganga/923414 to your computer and use it in GitHub Desktop.
Save filosganga/923414 to your computer and use it in GitHub Desktop.
HTTP method override filter and request as described in http://blog.filippodeluca.com/2010/04/rest-method-override-filter/
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to you 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 org.filippodeluca.http;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
/**
*
* @author Filippo De Luca
*
* @version 1.0
*/
public class HttpMethodOverrideFilter implements Filter {
public static final String HEADER_PARAM = "methodOverrideHeader";
private String header;
public void init(FilterConfig filterConfig) throws ServletException {
header = filterConfig.getInitParameter(HEADER_PARAM);
if (header == null || header.length() == 0) {
header = HttpMethodOverrideServletRequest.DEFAULT_HEADER;
}
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
ServletRequest filteredRequest = request;
if (request instanceof HttpServletRequest) {
filteredRequest = processRequest(request);
}
chain.doFilter(filteredRequest, response);
}
protected ServletRequest processRequest(ServletRequest request) {
HttpServletRequest httpRequest = (HttpServletRequest) request;
return new HttpMethodOverrideServletRequest(httpRequest, header);
}
public void destroy() {
// Empty
}
}
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to you 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 org.filippodeluca.http;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
/**
* @author Filippo De Luca
*
* @version 1.0
*/
public class HttpMethodOverrideServletRequest extends HttpServletRequestWrapper {
public static final String DEFAULT_HEADER = "X-HTTP-Method-Override";
private final String methodOverrideHeader;
private transient String method;
/**
* @param request
*/
public HttpMethodOverrideServletRequest(
HttpServletRequest request, String methodOverrideHeader) {
super(request);
this.methodOverrideHeader = methodOverrideHeader;
}
@Override
public String getMethod() {
if (method == null) {
method = resolveMethod();
}
return method;
}
protected String resolveMethod() {
String headerValue = getHeader(methodOverrideHeader);
if (headerValue != null) {
return headerValue;
} else {
return super.getMethod();
}
}
}
@saurabh-sp-tripathi
Copy link

The given link : http://blog.filippodeluca.com/2010/04/rest-method-override-filter/ --- doesn't work anymore try the one given below
http://filippodeluca.com/programming/2010/04/21/REST-method-override-filter.

@filosganga you may like to update this reference in your gist. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment