<?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>radikalFX &#187; Java</title>
	<atom:link href="http://radikalfx.com/tag/java/feed/" rel="self" type="application/rss+xml" />
	<link>http://radikalfx.com</link>
	<description>In full FX</description>
	<lastBuildDate>Tue, 27 Dec 2011 11:44:39 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>Spring 2.5 with annotations and REST requests</title>
		<link>http://radikalfx.com/2009/01/31/spring-25-with-annotations-and-rest-requests/</link>
		<comments>http://radikalfx.com/2009/01/31/spring-25-with-annotations-and-rest-requests/#comments</comments>
		<pubDate>Sat, 31 Jan 2009 17:09:09 +0000</pubDate>
		<dc:creator>cra5h</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[REST]]></category>
		<category><![CDATA[Spring]]></category>
		<category><![CDATA[Tomcat]]></category>

		<guid isPermaLink="false">http://radikalfx.com/?p=14</guid>
		<description><![CDATA[Spring 2.5 Web MVC is a really great framework, especially when using annotations you don&#8217;t have to spend to much time on getting everything up and running and separating MVC logic is really simple. One problem I recently came across, was something which was a bit annoying. When using Spring 2.5 with annotations, it&#8217;s not [...]]]></description>
			<content:encoded><![CDATA[<p>Spring 2.5 Web MVC is a really great framework, especially when using annotations you don&#8217;t have to spend to much time on getting everything up and running and separating MVC logic is really simple. One problem I recently came across, was something which was a bit annoying. When using Spring 2.5 with annotations, it&#8217;s not possible to define a URL pattern with a wild card in it. So creating REST full URL patterns can be a problem then. This is something that they will have build into Spring 3.0. But since that is not ready for production I wrote a small hack to at least be able to easily define URL patterns with wild cards.</p>
<p><strong>Creating a custom handler mapping Object</strong><br />
The first thing to do is, is to create a custom handler mapping Object. This Object can extend the Spring DefaultAnnotationHandlerMapping. This handler mapping won&#8217;t overwrite the DefaultAnnotationHandlerMapping. But we will just use this one to test for our REST url patterns. If no match is found. Then the method getHandlerInternal will return null. When null is returned, this means that Spring will go on and try to do this request in any of the other handler mappings configured. How this is configured in Spring comes up a little bit later in this post.</p>
<p>The code for the new RestHandlerMapping is as follows:<br />
<br/></p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.radikalfx.demo.handler</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.servlet.http.HttpServletRequest</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.springframework.beans.factory.annotation.Required</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.springframework.web.servlet.HandlerExecutionChain</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> RestHandlerMapping <span style="color: #000000; font-weight: bold;">extends</span> DefaultAnnotationHandlerMapping <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> requestNames<span style="color: #339933;">;</span>
&nbsp;
	@Override
	<span style="color: #000000; font-weight: bold;">public</span> HandlerExecutionChain getHandlerInternal<span style="color: #009900;">&#40;</span>HttpServletRequest request<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">Exception</span> <span style="color: #009900;">&#123;</span>
&nbsp;
		<span style="color: #003399;">String</span> uri <span style="color: #339933;">=</span> request.<span style="color: #006633;">getRequestURI</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> requestName <span style="color: #339933;">:</span> requestNames<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>uri.<span style="color: #006633;">startsWith</span><span style="color: #009900;">&#40;</span>requestName<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">new</span> HandlerExecutionChain<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">getHandlerMap</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span>requestName<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	@Required
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setRequestNames<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> requestNames<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">requestNames</span> <span style="color: #339933;">=</span> requestNames<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This RestHandlerMapping Object has the option to set one or more request names. This was we can just have this one Handler to set up multiple different URL patterns for different Controllers.</p>
<p><strong>The Controller</strong></p>
<p>Next is the controller. There is not much special that needs to happen to the controller. The only thing that we need to do, is make sure that we place a request mapping similar to that provided to the handler. And that this request mapping is placed on the class and not on the method.<br />
<br/></p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.radikalfx.demo.controller</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.servlet.http.HttpServletRequest</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.springframework.stereotype.Controller</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.springframework.web.bind.annotation.RequestMapping</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.springframework.web.servlet.ModelAndView</span><span style="color: #339933;">;</span>
&nbsp;
@Controller
@RequestMapping<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/action/&quot;</span><span style="color: #009900;">&#41;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ActionController <span style="color: #009900;">&#123;</span>
&nbsp;
	@RequestMapping
	<span style="color: #000000; font-weight: bold;">public</span> ModelAndView getAction<span style="color: #009900;">&#40;</span>HttpServletRequest request<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">new</span> ModelAndView<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;action&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p><strong>Spring configuration</strong><br />
Next thing we need to do, is that we need to change the Spring configuration a little bit. Normally when using annotated Web MVC you would probably use the DefaultAnnotationHandlerMapping, with this hack, we are still doing that. But creating an own handler mapping to be used before this one. In you configuration, this would become:<br />
<br/></p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;com.radikalfx.demo.handler.RestHandlerMapping&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;requestNames&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;list<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>/action/<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/list<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/property<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/bean<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span></pre></div></div>

<p><em>Don&#8217;t forget to also include in the Controller in the Spring configuration!</em></p>
<p><strong>Changing web.xml</strong><br />
The final thing that you need to do, to get this working, is change the web.xml so that the DispatcherServlet gets the requests for the URL pattern that we want to use:<br />
<br/></p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;url-pattern<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>/action/*<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/url-pattern<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>And that&#8217;s it. Of course you probably want to have some service that can handle the HttpServletRequest in the controller and extract the proper information from the request URI. But basically this is all you need to use wild cards in URL patterns with Spring 2.5 and annotations. </p>
]]></content:encoded>
			<wfw:commentRss>http://radikalfx.com/2009/01/31/spring-25-with-annotations-and-rest-requests/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Tomcat and if-modified-since header</title>
		<link>http://radikalfx.com/2008/03/23/what-to-do-with-google-seo-and-if-modified-since-header-in-your-servlet-jsp/</link>
		<comments>http://radikalfx.com/2008/03/23/what-to-do-with-google-seo-and-if-modified-since-header-in-your-servlet-jsp/#comments</comments>
		<pubDate>Sun, 23 Mar 2008 10:25:54 +0000</pubDate>
		<dc:creator>cra5h</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[SEO]]></category>
		<category><![CDATA[Tomcat]]></category>

		<guid isPermaLink="false">http://radikalfx.com/2008/03/23/what-to-do-with-google-seo-and-if-modified-since-header-in-your-servlet-jsp/</guid>
		<description><![CDATA[If you ever had to do something with web pages that need to be ranked the highest as possible, then you&#8217;ve probably read through the Google Webmaster Guidelines. But when I did that, I came across the following line: Make sure your web server supports the If-Modified-Since HTTP header. This feature allows your web server [...]]]></description>
			<content:encoded><![CDATA[<p>If you ever had to do something with web pages that need to be ranked the highest as possible, then you&#8217;ve probably read through the <a href="http://www.google.com/webmasters/guidelines.html" title="Google Webmaster Guidelines" target="_blank">Google Webmaster Guidelines</a>. But when I did that, I came across the following line:</p>
<blockquote><p>Make sure your web server supports the If-Modified-Since HTTP header. This feature allows your web server to tell Google whether your content has changed since we last crawled your site. Supporting this feature saves you bandwidth and overhead</p></blockquote>
<p>Because Tomcat&#8217;s default is to let the browser / crawler know that it doesn&#8217;t cache pages, GoogleBot will go and download the page every time it visits that page. In general this doesn&#8217;t have to be bad, but because the website I am working on contains a lot of pages in google (&gt;500.000) it might be wise to sometimes give this if-modified-since header back to Google and thereby spare some bandwidth and server load.</p>
<p>So then we need to take a look if GoogleBot gives back this header and then compare it and see if the information should be re crawled by Google. For this example I will be taking a period of 7 days. Of course, you could also let this depend on the data in your system. But the website I am working on makes it a bit difficult to do this, because looking up if there is new data, will slowdown the entire load process of the website (for each request), which is something I would like to avoid.</p>
<p>So how does the code look, to get this done:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #339933;">&lt;%</span>
<span style="color: #003399;">String</span> ifModifiedSince <span style="color: #339933;">=</span> request.<span style="color: #006633;">getHeader</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;if-modified-since&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>ifModifiedSince <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #339933;">!</span>ifModifiedSince.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// Format should be something like this: Thu, 10 Jan 2008 09:20:50 GMT</span>
    <span style="color: #003399;">SimpleDateFormat</span> sdf <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">SimpleDateFormat</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;EEE, d MMMM yyyy HH:mm:ss z&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #003399;">Date</span> modifiedDate <span style="color: #339933;">=</span> sdf.<span style="color: #006633;">parse</span><span style="color: #009900;">&#40;</span>ifModifiedSince<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #003399;">Calendar</span> modifiedCal <span style="color: #339933;">=</span> <span style="color: #003399;">Calendar</span>.<span style="color: #006633;">getInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    modifiedCal.<span style="color: #006633;">setTime</span><span style="color: #009900;">&#40;</span>modifiedDate<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #003399;">Calendar</span> nowCal <span style="color: #339933;">=</span> <span style="color: #003399;">Calendar</span>.<span style="color: #006633;">getInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">long</span> sevenDays <span style="color: #339933;">=</span> <span style="color: #cc66cc;">604800000</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>modifiedCal.<span style="color: #006633;">getTime</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getTime</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> sevenDays <span style="color: #339933;">&gt;</span> nowCal.<span style="color: #006633;">getTime</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getTime</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      response.<span style="color: #006633;">sendError</span><span style="color: #009900;">&#40;</span>HttpServletResponse.<span style="color: #006633;">SC_NOT_MODIFIED</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Exception</span> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    e.<span style="color: #006633;">printStackTrace</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
response.<span style="color: #006633;">setHeader</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Cache-Control&quot;</span>, <span style="color: #0000ff;">&quot;max-age=604800&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// 7 days in seconds</span>
response.<span style="color: #006633;">setDateHeader</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Expires&quot;</span>,
<span style="color: #003399;">System</span>.<span style="color: #006633;">currentTimeMillis</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><span style="color: #cc66cc;">604800000</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// 7 days in milliseconds</span>
response.<span style="color: #006633;">setDateHeader</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Last-Modified&quot;</span>, <span style="color: #003399;">System</span>.<span style="color: #006633;">currentTimeMillis</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Now</span>
<span style="color: #339933;">%&gt;</span></pre></div></div>

<p>The code above should be enough to get the job done. But if you feel that this can be done better or easier, feel free to comment on this post.</p>
]]></content:encoded>
			<wfw:commentRss>http://radikalfx.com/2008/03/23/what-to-do-with-google-seo-and-if-modified-since-header-in-your-servlet-jsp/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

