<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Filippo De Luca Workshop</title>
	<atom:link href="http://blog.filippodeluca.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.filippodeluca.com</link>
	<description>Just Another Java Enthusiast</description>
	<lastBuildDate>Thu, 06 May 2010 11:26:20 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>REST method override filter</title>
		<link>http://blog.filippodeluca.com/2010/04/rest-method-override-filter/</link>
		<comments>http://blog.filippodeluca.com/2010/04/rest-method-override-filter/#comments</comments>
		<pubDate>Tue, 20 Apr 2010 22:02:18 +0000</pubDate>
		<dc:creator>Filippo De Luca</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[REST]]></category>

		<guid isPermaLink="false">http://blog.filippodeluca.com/?p=13</guid>
		<description><![CDATA[The  actual trend of developing application based on REST service can find an obstacle in the proxy and firewall configurations. Most of these drops requests other than GET and POST. The best practices of REST API design involve the use of all HTTP method to describe the action, rather than changing the URI. The URI [...]]]></description>
			<content:encoded><![CDATA[<p>The  actual trend of developing application based on REST service can find an obstacle in the proxy and firewall configurations. Most of these drops requests other than GET and POST. The best practices of REST API design involve the use of all HTTP method to describe the action, rather than changing the URI. The URI should identify the resources nor the actions.</p>
<p>An URI as <code>http://example.com/users/delete/6</code> is a very bad example of REST API implementation. It should be <code>http://example.com/users/6</code> and handle the DELETE method to remove the user with id 6.</p>
<p>A smart solution to keep the API clean, is overriding the original method with one specified in a request header. So a POST method can act as PUT or DELETE method adding a known header to the request. The <code>X-HTTP-Method-Override</code> is the header that is used to to that</p>
<p>The java sourcecode:</p>
<pre class="brush: java;">
/**
 *
 * @author Filippo De Luca
 *
 * @version $Id$
 */
public class MethodOverrideFilter implements Filter {

	public static final String HEADER_PARAM = &quot;methodOverrideHeader&quot;;

	public static final String DEFAULT_HEADER = &quot;X-HTTP-Method-Override&quot;;

	private String header = DEFAULT_HEADER;

	public void init(FilterConfig filterConfig) throws ServletException {

		header = filterConfig.getInitParameter(HEADER_PARAM);
		if(StringUtils.isBlank(header)) {
			header = DEFAULT_HEADER;
		}

	}

	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {

		ServletRequest filteredRequest = request;

		if(request instanceof HttpServletRequest) {

			HttpServletRequest httpRequest = (HttpServletRequest)request;
			filteredRequest = new MethodOverrideHttpServletRequestDecorator(httpRequest, header);
		}

		chain.doFilter(filteredRequest, response);
	}

	public void destroy() {
		// Empty
	}

}

/**
 * @author Filippo De Luca
 *
 * @version $Id$
 */
public class MethodOverrideHttpServletRequestDecorator extends
		HttpServletRequestWrapper {

	public static final String DEFAULT_HEADER = &quot;X-HTTP-Method-Override&quot;;

	private final String methodOverrideHeader;

	private transient String method;

	/**
	 * @param request
	 */
	public MethodOverrideHttpServletRequestDecorator(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();
		}
	}

}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://blog.filippodeluca.com/2010/04/rest-method-override-filter/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk
Page Caching using disk (enhanced)

Served from: blog.filippodeluca.com @ 2010-09-05 02:55:40 -->