Created
April 16, 2011 19:39
-
-
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/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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 | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.