Eclipse: Configure HTTP Proxy Settings for All Run Configurations

There are many cases nowadays when you have to go through HTTP proxy in order to access the internet. Eclipse provides an option to configure HTTP proxy settings – either by manual configuration or loading native system settings. (It does not support auto-configuration scripts, though, so if this is the way you configure your OS, you’ll have to provide proxy address to Eclipse manually.)

However. the settings configured via Eclipse preferences are not propagated to the Java programs that you develop. Obviously, you can provide proxy settings via VM arguments, but doing that for each and every run configuration may be painful. Fortunately, this can be solved with the help of “Default VM Arguments” setting under JRE/JDK definition. System properties configured there will be in effect for every Java program you are going to run or debug. Continue reading “Eclipse: Configure HTTP Proxy Settings for All Run Configurations”

Summary of Cache-Related HTTP Headers

Long ago (long before the first post in this blog!) I’ve composed a list of cache-related HTTP headers, so I would not need to go through the trial-and-error process of guessing the right combination more than once. Recently I got another question about caching and it took me a lot of time to recall where I saw this list last time. So now I’m placing it here.

Please treat the explanations below as quick and incomplete summary. For full specification of “Pragma”, “Cache-Control” and “Expires” headers refer to HTTP/1.1 specification.

Caching in HTTP 1.1

Following directive does not prevent caching despite its name. It allows caching of the page, but specifies that the cache must ask the originating web server if the page is up-to-date before serving the cached version. So the cached page can still be served up if the originating web server says so. Applies to all caches.

Cache-Control: no-cache

Following directive tells the browser that the page has expired and must be treated as stale. Should be good news as long as the caches obey.

Expires: Thu, 01 Jan 1970 00:00:00 GMT

Following directive specifies that the page contains information intended for a single user only and must not be cached by a shared cache (e.g. a proxy server).

Cache-Control: private

Following directive specifies that a cache must not store any part of the response or the request that elicited it.

Cache-Control: no-store

Following directive tells the cache that the maximum acceptable staleness of a page is 0 seconds.

Cache-Control: max-stale=0

Caching in HTTP 1.0

Following directive is the only cache control directive for HTTP 1.0, so use it in addition to any HTTP 1.1 cache control headers you include.

Pragma: no-cache

Continue reading “Summary of Cache-Related HTTP Headers”

How to Use CDATA in Spring Configuration Entries

Today I had to create a map in Spring XML configuration file, where both keys and values ought to be XML elements by their own. Obviously, using CDATA is the most readable way to achieve that – but it was not immediately clear how to use CDATA for entry’s key and value attributes.

Here is what I ended with:

<util:map id="patterns">
  <entry>
    <key><value><![CDATA[<original>old-value</original>]]></value></key>
    <value><![CDATA[<replacement>new-value</replacement>]]></value>
  </entry>
</util:map>

Enjoy!

How to Download Eclipse Update Site for Offline Use

Unlike the early Eclipse days, now most of Eclipse plugins are distributed via Update Manager. Installation of new software is only a few clicks away – unless your development environment is not connected to the internet! Some vendors publish update site archives for offline use, but most of them are not. Eclipse tries to provide a solution for that with site mirroring, but is not as easy as it could be and forces you to install full-fledged Eclipse on a machine connected to the internet. So, what can you do about it?

Let’s take run-jetty-run – excellent plugin that allows you to run Jetty in Eclipse with a single click, including source attachment for debugging, – and prepare an offline update site for it. Continue reading “How to Download Eclipse Update Site for Offline Use”

Spring MVC: return view or send error from the same handler

There are many Spring MVC primers on the web that explains Spring MVC basics to some extent. Example of request handler that creates some model data and returns view name is definitely one of the basics and appears in almost each and every primer. Many of those primers also mention HttpServletResponse.sendError() call as a way to produce custom HTTP error codes. However, I did not find any comprehensive example that combines the two and demonstrates the typical REST flow – respond with data object in some cases and send “204 No Content” in others.

So, here comes the example:

	@RequestMapping( value = "/get/{name}", method = RequestMethod.GET )
	public ModelAndView get( @PathVariable( "name" ) String name, HttpServletResponse resp ) throws IOException
	{
		// Check if we have value
		Object value = map.get( name );
		if ( value == null )
		{
			resp.sendError( HttpServletResponse.SC_NO_CONTENT );
			return null; // no further processing needed
		}
		
		// Continue to the view
		ModelAndView mv = new ModelAndView( "show-value" );
		mv.addObject( "value", value );
		return mv;
	}

Note that it is OK to return null in line 9 – at this point there is enough data in the response object to generate valid output.