<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>locrian code</title>
    <link>http://blog.locrian.uk/</link>
    <description>Recent content on locrian code</description>
    <generator>Hugo -- gohugo.io</generator>
    <language>en-us</language>
    <lastBuildDate>Mon, 15 Aug 2016 17:46:53 +0100</lastBuildDate>
    <atom:link href="http://blog.locrian.uk/index.xml" rel="self" type="application/rss+xml" />
    
    <item>
      <title>Using Jetty form-based authentication with Dropwizard</title>
      <link>http://blog.locrian.uk/post/jetty-form-auth-dropwizard/</link>
      <pubDate>Mon, 15 Aug 2016 17:46:53 +0100</pubDate>
      
      <guid>http://blog.locrian.uk/post/jetty-form-auth-dropwizard/</guid>
      <description>&lt;p&gt;&lt;a href=&#34;http://www.dropwizard.io/&#34;&gt;Dropwizard&lt;/a&gt; is a rather nice Java REST framework. Although mainly designed
for web services, it can also be used for MVC-style web applications, with dropwizard-views
which supports both FreeMarker and Mustache templates on the front end.&lt;/p&gt;

&lt;p&gt;What&amp;rsquo;s missing out of the box is form-based authentication. The embedded Jetty web server does contain this functionality &amp;mdash; but there are a number of steps involved in getting it wired up.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;First, create /login and /logout pages&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Let&amp;rsquo;s create a simple Jersey resource for each page. We&amp;rsquo;ll also create a &lt;code&gt;/login/error&lt;/code&gt; page to
handle failed logins.&lt;/p&gt;
&lt;div class=&#34;highlight&#34; style=&#34;background: #f8f8f8&#34;&gt;&lt;pre style=&#34;line-height: 125%&#34;&gt;&lt;span style=&#34;color: #AA22FF&#34;&gt;@Path&lt;/span&gt;&lt;span style=&#34;color: #666666&#34;&gt;(&lt;/span&gt;&lt;span style=&#34;color: #BA2121&#34;&gt;&amp;quot;/login&amp;quot;&lt;/span&gt;&lt;span style=&#34;color: #666666&#34;&gt;)&lt;/span&gt;
&lt;span style=&#34;color: #008000; font-weight: bold&#34;&gt;public&lt;/span&gt; &lt;span style=&#34;color: #008000; font-weight: bold&#34;&gt;class&lt;/span&gt; &lt;span style=&#34;color: #0000FF; font-weight: bold&#34;&gt;LoginResource&lt;/span&gt; &lt;span style=&#34;color: #666666&#34;&gt;{&lt;/span&gt;

	&lt;span style=&#34;color: #AA22FF&#34;&gt;@GET&lt;/span&gt;
	&lt;span style=&#34;color: #008000; font-weight: bold&#34;&gt;public&lt;/span&gt; View &lt;span style=&#34;color: #0000FF&#34;&gt;login&lt;/span&gt;&lt;span style=&#34;color: #666666&#34;&gt;()&lt;/span&gt; &lt;span style=&#34;color: #666666&#34;&gt;{&lt;/span&gt;
		&lt;span style=&#34;color: #008000; font-weight: bold&#34;&gt;return&lt;/span&gt; &lt;span style=&#34;color: #008000; font-weight: bold&#34;&gt;new&lt;/span&gt; LoginView&lt;span style=&#34;color: #666666&#34;&gt;();&lt;/span&gt;
	&lt;span style=&#34;color: #666666&#34;&gt;}&lt;/span&gt;
	
	&lt;span style=&#34;color: #AA22FF&#34;&gt;@GET&lt;/span&gt; &lt;span style=&#34;color: #AA22FF&#34;&gt;@Path&lt;/span&gt;&lt;span style=&#34;color: #666666&#34;&gt;(&lt;/span&gt;&lt;span style=&#34;color: #BA2121&#34;&gt;&amp;quot;error&amp;quot;&lt;/span&gt;&lt;span style=&#34;color: #666666&#34;&gt;)&lt;/span&gt;
	&lt;span style=&#34;color: #008000; font-weight: bold&#34;&gt;public&lt;/span&gt; String &lt;span style=&#34;color: #0000FF&#34;&gt;error&lt;/span&gt;&lt;span style=&#34;color: #666666&#34;&gt;()&lt;/span&gt; &lt;span style=&#34;color: #666666&#34;&gt;{&lt;/span&gt;
		&lt;span style=&#34;color: #008000; font-weight: bold&#34;&gt;return&lt;/span&gt; &lt;span style=&#34;color: #BA2121&#34;&gt;&amp;quot;Error logging in.&amp;quot;&lt;/span&gt;&lt;span style=&#34;color: #666666&#34;&gt;;&lt;/span&gt;
	&lt;span style=&#34;color: #666666&#34;&gt;}&lt;/span&gt;
&lt;span style=&#34;color: #666666&#34;&gt;}&lt;/span&gt;

&lt;span style=&#34;color: #AA22FF&#34;&gt;@Path&lt;/span&gt;&lt;span style=&#34;color: #666666&#34;&gt;(&lt;/span&gt;&lt;span style=&#34;color: #BA2121&#34;&gt;&amp;quot;/logout&amp;quot;&lt;/span&gt;&lt;span style=&#34;color: #666666&#34;&gt;)&lt;/span&gt;
&lt;span style=&#34;color: #008000; font-weight: bold&#34;&gt;public&lt;/span&gt; &lt;span style=&#34;color: #008000; font-weight: bold&#34;&gt;class&lt;/span&gt; &lt;span style=&#34;color: #0000FF; font-weight: bold&#34;&gt;LogoutResource&lt;/span&gt; &lt;span style=&#34;color: #666666&#34;&gt;{&lt;/span&gt;

	&lt;span style=&#34;color: #AA22FF&#34;&gt;@GET&lt;/span&gt;
	&lt;span style=&#34;color: #008000; font-weight: bold&#34;&gt;public&lt;/span&gt; String &lt;span style=&#34;color: #0000FF&#34;&gt;logout&lt;/span&gt;&lt;span style=&#34;color: #666666&#34;&gt;(&lt;/span&gt;&lt;span style=&#34;color: #AA22FF&#34;&gt;@Context&lt;/span&gt; HttpServletRequest req&lt;span style=&#34;color: #666666&#34;&gt;)&lt;/span&gt; &lt;span style=&#34;color: #666666&#34;&gt;{&lt;/span&gt;
		
		req&lt;span style=&#34;color: #666666&#34;&gt;.&lt;/span&gt;&lt;span style=&#34;color: #7D9029&#34;&gt;getSession&lt;/span&gt;&lt;span style=&#34;color: #666666&#34;&gt;().&lt;/span&gt;&lt;span style=&#34;color: #7D9029&#34;&gt;invalidate&lt;/span&gt;&lt;span style=&#34;color: #666666&#34;&gt;();&lt;/span&gt;
		&lt;span style=&#34;color: #008000; font-weight: bold&#34;&gt;return&lt;/span&gt; &lt;span style=&#34;color: #BA2121&#34;&gt;&amp;quot;You have been logged out.&amp;quot;&lt;/span&gt;&lt;span style=&#34;color: #666666&#34;&gt;;&lt;/span&gt;
	&lt;span style=&#34;color: #666666&#34;&gt;}&lt;/span&gt;
&lt;span style=&#34;color: #666666&#34;&gt;}&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The login page needs to post to the special &lt;code&gt;/j_security_check&lt;/code&gt; endpoint, so your login
view should look something like this:&lt;/p&gt;
&lt;div class=&#34;highlight&#34; style=&#34;background: #f8f8f8&#34;&gt;&lt;pre style=&#34;line-height: 125%&#34;&gt;&amp;lt;&lt;span style=&#34;color: #008000; font-weight: bold&#34;&gt;html&lt;/span&gt;&amp;gt;
&amp;lt;&lt;span style=&#34;color: #008000; font-weight: bold&#34;&gt;head&lt;/span&gt;&amp;gt;&amp;lt;&lt;span style=&#34;color: #008000; font-weight: bold&#34;&gt;title&lt;/span&gt;&amp;gt;Log in&amp;lt;/&lt;span style=&#34;color: #008000; font-weight: bold&#34;&gt;title&lt;/span&gt;&amp;gt;&amp;lt;/&lt;span style=&#34;color: #008000; font-weight: bold&#34;&gt;head&lt;/span&gt;&amp;gt;
&amp;lt;&lt;span style=&#34;color: #008000; font-weight: bold&#34;&gt;body&lt;/span&gt;&amp;gt;

&amp;lt;&lt;span style=&#34;color: #008000; font-weight: bold&#34;&gt;form&lt;/span&gt; &lt;span style=&#34;color: #7D9029&#34;&gt;method&lt;/span&gt;&lt;span style=&#34;color: #666666&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color: #BA2121&#34;&gt;&amp;#39;POST&amp;#39;&lt;/span&gt; &lt;span style=&#34;color: #7D9029&#34;&gt;action&lt;/span&gt;&lt;span style=&#34;color: #666666&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color: #BA2121&#34;&gt;&amp;#39;/j_security_check&amp;#39;&lt;/span&gt;&amp;gt;
  &amp;lt;&lt;span style=&#34;color: #008000; font-weight: bold&#34;&gt;input&lt;/span&gt; &lt;span style=&#34;color: #7D9029&#34;&gt;type&lt;/span&gt;&lt;span style=&#34;color: #666666&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color: #BA2121&#34;&gt;&amp;#39;text&amp;#39;&lt;/span&gt; &lt;span style=&#34;color: #7D9029&#34;&gt;name&lt;/span&gt;&lt;span style=&#34;color: #666666&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color: #BA2121&#34;&gt;&amp;#39;j_username&amp;#39;&lt;/span&gt;/&amp;gt;
  &amp;lt;&lt;span style=&#34;color: #008000; font-weight: bold&#34;&gt;input&lt;/span&gt; &lt;span style=&#34;color: #7D9029&#34;&gt;type&lt;/span&gt;&lt;span style=&#34;color: #666666&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color: #BA2121&#34;&gt;&amp;#39;password&amp;#39;&lt;/span&gt; &lt;span style=&#34;color: #7D9029&#34;&gt;name&lt;/span&gt;&lt;span style=&#34;color: #666666&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color: #BA2121&#34;&gt;&amp;#39;j_password&amp;#39;&lt;/span&gt;/&amp;gt;
  &amp;lt;&lt;span style=&#34;color: #008000; font-weight: bold&#34;&gt;input&lt;/span&gt; &lt;span style=&#34;color: #7D9029&#34;&gt;type&lt;/span&gt;&lt;span style=&#34;color: #666666&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color: #BA2121&#34;&gt;&amp;#39;submit&amp;#39;&lt;/span&gt; &lt;span style=&#34;color: #7D9029&#34;&gt;value&lt;/span&gt;&lt;span style=&#34;color: #666666&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color: #BA2121&#34;&gt;&amp;#39;Login&amp;#39;&lt;/span&gt;/&amp;gt;
&amp;lt;/&lt;span style=&#34;color: #008000; font-weight: bold&#34;&gt;form&lt;/span&gt;&amp;gt;

&amp;lt;/&lt;span style=&#34;color: #008000; font-weight: bold&#34;&gt;body&lt;/span&gt;&amp;gt;
&amp;lt;/&lt;span style=&#34;color: #008000; font-weight: bold&#34;&gt;html&lt;/span&gt;&amp;gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Automatically redirect 403 Forbidden responses to the login page&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;HTTP 403 responses are represented by the JAX-RS class &lt;code&gt;ForbiddenException&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;To handle these, we need an ExceptionMapper that will store the target URI in session and
then redirect the user to the login page. Once they have logged in, Jetty will retrieve the URI
and forward the user to their original destination.&lt;/p&gt;
&lt;div class=&#34;highlight&#34; style=&#34;background: #f8f8f8&#34;&gt;&lt;pre style=&#34;line-height: 125%&#34;&gt;&lt;span style=&#34;color: #008000; font-weight: bold&#34;&gt;public&lt;/span&gt; &lt;span style=&#34;color: #008000; font-weight: bold&#34;&gt;class&lt;/span&gt; &lt;span style=&#34;color: #0000FF; font-weight: bold&#34;&gt;ForbiddenExceptionMapper&lt;/span&gt; &lt;span style=&#34;color: #008000; font-weight: bold&#34;&gt;implements&lt;/span&gt; ExceptionMapper&lt;span style=&#34;color: #666666&#34;&gt;&amp;lt;&lt;/span&gt;ForbiddenException&lt;span style=&#34;color: #666666&#34;&gt;&amp;gt;&lt;/span&gt; &lt;span style=&#34;color: #666666&#34;&gt;{&lt;/span&gt;
	
	&lt;span style=&#34;color: #008000; font-weight: bold&#34;&gt;private&lt;/span&gt; UriInfo ui&lt;span style=&#34;color: #666666&#34;&gt;;&lt;/span&gt;
	&lt;span style=&#34;color: #008000; font-weight: bold&#34;&gt;private&lt;/span&gt; HttpServletRequest req&lt;span style=&#34;color: #666666&#34;&gt;;&lt;/span&gt;
	
	&lt;span style=&#34;color: #008000; font-weight: bold&#34;&gt;public&lt;/span&gt; &lt;span style=&#34;color: #0000FF&#34;&gt;ForbiddenExceptionMapper&lt;/span&gt;&lt;span style=&#34;color: #666666&#34;&gt;(&lt;/span&gt;&lt;span style=&#34;color: #AA22FF&#34;&gt;@Context&lt;/span&gt; UriInfo ui&lt;span style=&#34;color: #666666&#34;&gt;,&lt;/span&gt; &lt;span style=&#34;color: #AA22FF&#34;&gt;@Context&lt;/span&gt; HttpServletRequest req&lt;span style=&#34;color: #666666&#34;&gt;)&lt;/span&gt; &lt;span style=&#34;color: #666666&#34;&gt;{&lt;/span&gt;
		&lt;span style=&#34;color: #008000; font-weight: bold&#34;&gt;this&lt;/span&gt;&lt;span style=&#34;color: #666666&#34;&gt;.&lt;/span&gt;&lt;span style=&#34;color: #7D9029&#34;&gt;ui&lt;/span&gt; &lt;span style=&#34;color: #666666&#34;&gt;=&lt;/span&gt; ui&lt;span style=&#34;color: #666666&#34;&gt;;&lt;/span&gt;
		&lt;span style=&#34;color: #008000; font-weight: bold&#34;&gt;this&lt;/span&gt;&lt;span style=&#34;color: #666666&#34;&gt;.&lt;/span&gt;&lt;span style=&#34;color: #7D9029&#34;&gt;req&lt;/span&gt; &lt;span style=&#34;color: #666666&#34;&gt;=&lt;/span&gt; req&lt;span style=&#34;color: #666666&#34;&gt;;&lt;/span&gt;
	&lt;span style=&#34;color: #666666&#34;&gt;}&lt;/span&gt;

	&lt;span style=&#34;color: #AA22FF&#34;&gt;@Override&lt;/span&gt; &lt;span style=&#34;color: #008000; font-weight: bold&#34;&gt;public&lt;/span&gt; Response &lt;span style=&#34;color: #0000FF&#34;&gt;toResponse&lt;/span&gt;&lt;span style=&#34;color: #666666&#34;&gt;(&lt;/span&gt;ForbiddenException e&lt;span style=&#34;color: #666666&#34;&gt;)&lt;/span&gt; &lt;span style=&#34;color: #666666&#34;&gt;{&lt;/span&gt;

		String location &lt;span style=&#34;color: #666666&#34;&gt;=&lt;/span&gt; ui&lt;span style=&#34;color: #666666&#34;&gt;.&lt;/span&gt;&lt;span style=&#34;color: #7D9029&#34;&gt;getPath&lt;/span&gt;&lt;span style=&#34;color: #666666&#34;&gt;();&lt;/span&gt;
		
		&lt;span style=&#34;color: #008000; font-weight: bold&#34;&gt;if&lt;/span&gt; &lt;span style=&#34;color: #666666&#34;&gt;(&lt;/span&gt;location &lt;span style=&#34;color: #666666&#34;&gt;!=&lt;/span&gt; &lt;span style=&#34;color: #008000; font-weight: bold&#34;&gt;null&lt;/span&gt;&lt;span style=&#34;color: #666666&#34;&gt;)&lt;/span&gt; &lt;span style=&#34;color: #666666&#34;&gt;{&lt;/span&gt;
			req&lt;span style=&#34;color: #666666&#34;&gt;.&lt;/span&gt;&lt;span style=&#34;color: #7D9029&#34;&gt;getSession&lt;/span&gt;&lt;span style=&#34;color: #666666&#34;&gt;().&lt;/span&gt;&lt;span style=&#34;color: #7D9029&#34;&gt;setAttribute&lt;/span&gt;&lt;span style=&#34;color: #666666&#34;&gt;(&lt;/span&gt;FormAuthenticator&lt;span style=&#34;color: #666666&#34;&gt;.&lt;/span&gt;&lt;span style=&#34;color: #7D9029&#34;&gt;__J_URI&lt;/span&gt;&lt;span style=&#34;color: #666666&#34;&gt;,&lt;/span&gt; location&lt;span style=&#34;color: #666666&#34;&gt;);&lt;/span&gt;
		&lt;span style=&#34;color: #666666&#34;&gt;}&lt;/span&gt;
		&lt;span style=&#34;color: #008000; font-weight: bold&#34;&gt;return&lt;/span&gt; Response&lt;span style=&#34;color: #666666&#34;&gt;.&lt;/span&gt;&lt;span style=&#34;color: #7D9029&#34;&gt;temporaryRedirect&lt;/span&gt;&lt;span style=&#34;color: #666666&#34;&gt;(&lt;/span&gt;URI&lt;span style=&#34;color: #666666&#34;&gt;.&lt;/span&gt;&lt;span style=&#34;color: #7D9029&#34;&gt;create&lt;/span&gt;&lt;span style=&#34;color: #666666&#34;&gt;(&lt;/span&gt;&lt;span style=&#34;color: #BA2121&#34;&gt;&amp;quot;/login&amp;quot;&lt;/span&gt;&lt;span style=&#34;color: #666666&#34;&gt;)).&lt;/span&gt;&lt;span style=&#34;color: #7D9029&#34;&gt;build&lt;/span&gt;&lt;span style=&#34;color: #666666&#34;&gt;();&lt;/span&gt;
	&lt;span style=&#34;color: #666666&#34;&gt;}&lt;/span&gt;
	
&lt;span style=&#34;color: #666666&#34;&gt;}&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Create a new server factory&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;To work, the auth service needs to be initialised at the correct point in Jetty&amp;rsquo;s lifecycle;
after the server has been created but before the app servlet is added. We&amp;rsquo;ll need a custom
&lt;code&gt;ServerFactory&lt;/code&gt; so we can hook into Jetty at the right time.&lt;/p&gt;
&lt;div class=&#34;highlight&#34; style=&#34;background: #f8f8f8&#34;&gt;&lt;pre style=&#34;line-height: 125%&#34;&gt;&lt;span style=&#34;color: #AA22FF&#34;&gt;@JsonTypeName&lt;/span&gt;&lt;span style=&#34;color: #666666&#34;&gt;(&lt;/span&gt;&lt;span style=&#34;color: #BA2121&#34;&gt;&amp;quot;jettyauthserver&amp;quot;&lt;/span&gt;&lt;span style=&#34;color: #666666&#34;&gt;)&lt;/span&gt;
&lt;span style=&#34;color: #008000; font-weight: bold&#34;&gt;public&lt;/span&gt; &lt;span style=&#34;color: #008000; font-weight: bold&#34;&gt;class&lt;/span&gt; &lt;span style=&#34;color: #0000FF; font-weight: bold&#34;&gt;JettyAuthServerFactory&lt;/span&gt; &lt;span style=&#34;color: #008000; font-weight: bold&#34;&gt;extends&lt;/span&gt; DefaultServerFactory &lt;span style=&#34;color: #666666&#34;&gt;{&lt;/span&gt;

    &lt;span style=&#34;color: #AA22FF&#34;&gt;@Override&lt;/span&gt;
    &lt;span style=&#34;color: #008000; font-weight: bold&#34;&gt;protected&lt;/span&gt; Handler &lt;span style=&#34;color: #0000FF&#34;&gt;createAppServlet&lt;/span&gt;&lt;span style=&#34;color: #666666&#34;&gt;(&lt;/span&gt;Server server&lt;span style=&#34;color: #666666&#34;&gt;,&lt;/span&gt; JerseyEnvironment jersey&lt;span style=&#34;color: #666666&#34;&gt;,&lt;/span&gt;
        ObjectMapper objectMapper&lt;span style=&#34;color: #666666&#34;&gt;,&lt;/span&gt; Validator validator&lt;span style=&#34;color: #666666&#34;&gt;,&lt;/span&gt; MutableServletContextHandler handler&lt;span style=&#34;color: #666666&#34;&gt;,&lt;/span&gt;
        Servlet jerseyContainer&lt;span style=&#34;color: #666666&#34;&gt;,&lt;/span&gt; MetricRegistry metricRegistry&lt;span style=&#34;color: #666666&#34;&gt;)&lt;/span&gt; &lt;span style=&#34;color: #666666&#34;&gt;{&lt;/span&gt;

        setupJettyAuth&lt;span style=&#34;color: #666666&#34;&gt;(&lt;/span&gt;handler&lt;span style=&#34;color: #666666&#34;&gt;);&lt;/span&gt;

        &lt;span style=&#34;color: #008000; font-weight: bold&#34;&gt;return&lt;/span&gt; &lt;span style=&#34;color: #008000; font-weight: bold&#34;&gt;super&lt;/span&gt;&lt;span style=&#34;color: #666666&#34;&gt;.&lt;/span&gt;&lt;span style=&#34;color: #7D9029&#34;&gt;createAppServlet&lt;/span&gt;&lt;span style=&#34;color: #666666&#34;&gt;(&lt;/span&gt;server&lt;span style=&#34;color: #666666&#34;&gt;,&lt;/span&gt; jersey&lt;span style=&#34;color: #666666&#34;&gt;,&lt;/span&gt; objectMapper&lt;span style=&#34;color: #666666&#34;&gt;,&lt;/span&gt; validator&lt;span style=&#34;color: #666666&#34;&gt;,&lt;/span&gt; handler&lt;span style=&#34;color: #666666&#34;&gt;,&lt;/span&gt;
            jerseyContainer&lt;span style=&#34;color: #666666&#34;&gt;,&lt;/span&gt; metricRegistry&lt;span style=&#34;color: #666666&#34;&gt;);&lt;/span&gt;
    &lt;span style=&#34;color: #666666&#34;&gt;}&lt;/span&gt;

    &lt;span style=&#34;color: #008000; font-weight: bold&#34;&gt;private&lt;/span&gt; &lt;span style=&#34;color: #008000; font-weight: bold&#34;&gt;static&lt;/span&gt; &lt;span style=&#34;color: #B00040&#34;&gt;void&lt;/span&gt; &lt;span style=&#34;color: #0000FF&#34;&gt;setupJettyAuth&lt;/span&gt;&lt;span style=&#34;color: #666666&#34;&gt;(&lt;/span&gt;MutableServletContextHandler context&lt;span style=&#34;color: #666666&#34;&gt;)&lt;/span&gt; &lt;span style=&#34;color: #666666&#34;&gt;{&lt;/span&gt;
    	
    	context&lt;span style=&#34;color: #666666&#34;&gt;.&lt;/span&gt;&lt;span style=&#34;color: #7D9029&#34;&gt;setSessionHandler&lt;/span&gt;&lt;span style=&#34;color: #666666&#34;&gt;(&lt;/span&gt;&lt;span style=&#34;color: #008000; font-weight: bold&#34;&gt;new&lt;/span&gt; SessionHandler&lt;span style=&#34;color: #666666&#34;&gt;());&lt;/span&gt;
    	
    	Constraint constraint &lt;span style=&#34;color: #666666&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color: #008000; font-weight: bold&#34;&gt;new&lt;/span&gt; Constraint&lt;span style=&#34;color: #666666&#34;&gt;();&lt;/span&gt;
    	constraint&lt;span style=&#34;color: #666666&#34;&gt;.&lt;/span&gt;&lt;span style=&#34;color: #7D9029&#34;&gt;setName&lt;/span&gt;&lt;span style=&#34;color: #666666&#34;&gt;(&lt;/span&gt;Constraint&lt;span style=&#34;color: #666666&#34;&gt;.&lt;/span&gt;&lt;span style=&#34;color: #7D9029&#34;&gt;__FORM_AUTH&lt;/span&gt;&lt;span style=&#34;color: #666666&#34;&gt;);&lt;/span&gt;
    	constraint&lt;span style=&#34;color: #666666&#34;&gt;.&lt;/span&gt;&lt;span style=&#34;color: #7D9029&#34;&gt;setRoles&lt;/span&gt;&lt;span style=&#34;color: #666666&#34;&gt;(&lt;/span&gt;&lt;span style=&#34;color: #008000; font-weight: bold&#34;&gt;new&lt;/span&gt; String&lt;span style=&#34;color: #666666&#34;&gt;[]{&lt;/span&gt;&lt;span style=&#34;color: #BA2121&#34;&gt;&amp;quot;user&amp;quot;&lt;/span&gt;&lt;span style=&#34;color: #666666&#34;&gt;,&lt;/span&gt;&lt;span style=&#34;color: #BA2121&#34;&gt;&amp;quot;admin&amp;quot;&lt;/span&gt;&lt;span style=&#34;color: #666666&#34;&gt;,&lt;/span&gt;&lt;span style=&#34;color: #BA2121&#34;&gt;&amp;quot;moderator&amp;quot;&lt;/span&gt;&lt;span style=&#34;color: #666666&#34;&gt;});&lt;/span&gt;
    	constraint&lt;span style=&#34;color: #666666&#34;&gt;.&lt;/span&gt;&lt;span style=&#34;color: #7D9029&#34;&gt;setAuthenticate&lt;/span&gt;&lt;span style=&#34;color: #666666&#34;&gt;(&lt;/span&gt;&lt;span style=&#34;color: #008000; font-weight: bold&#34;&gt;true&lt;/span&gt;&lt;span style=&#34;color: #666666&#34;&gt;);&lt;/span&gt;

    	ConstraintSecurityHandler securityHandler &lt;span style=&#34;color: #666666&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color: #008000; font-weight: bold&#34;&gt;new&lt;/span&gt; ConstraintSecurityHandler&lt;span style=&#34;color: #666666&#34;&gt;();&lt;/span&gt;

    	HashLoginService loginService &lt;span style=&#34;color: #666666&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color: #008000; font-weight: bold&#34;&gt;new&lt;/span&gt; HashLoginService&lt;span style=&#34;color: #666666&#34;&gt;();&lt;/span&gt;
    	loginService&lt;span style=&#34;color: #666666&#34;&gt;.&lt;/span&gt;&lt;span style=&#34;color: #7D9029&#34;&gt;putUser&lt;/span&gt;&lt;span style=&#34;color: #666666&#34;&gt;(&lt;/span&gt;&lt;span style=&#34;color: #BA2121&#34;&gt;&amp;quot;defaultuser&amp;quot;&lt;/span&gt;&lt;span style=&#34;color: #666666&#34;&gt;,&lt;/span&gt; &lt;span style=&#34;color: #008000; font-weight: bold&#34;&gt;new&lt;/span&gt; Password&lt;span style=&#34;color: #666666&#34;&gt;(&lt;/span&gt;&lt;span style=&#34;color: #BA2121&#34;&gt;&amp;quot;defaultpass&amp;quot;&lt;/span&gt;&lt;span style=&#34;color: #666666&#34;&gt;),&lt;/span&gt; &lt;span style=&#34;color: #008000; font-weight: bold&#34;&gt;new&lt;/span&gt; String&lt;span style=&#34;color: #666666&#34;&gt;[]&lt;/span&gt; &lt;span style=&#34;color: #666666&#34;&gt;{&lt;/span&gt;&lt;span style=&#34;color: #BA2121&#34;&gt;&amp;quot;admin&amp;quot;&lt;/span&gt;&lt;span style=&#34;color: #666666&#34;&gt;});&lt;/span&gt;
    	
    	FormAuthenticator authenticator &lt;span style=&#34;color: #666666&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color: #008000; font-weight: bold&#34;&gt;new&lt;/span&gt; FormAuthenticator&lt;span style=&#34;color: #666666&#34;&gt;(&lt;/span&gt;&lt;span style=&#34;color: #BA2121&#34;&gt;&amp;quot;/login&amp;quot;&lt;/span&gt;&lt;span style=&#34;color: #666666&#34;&gt;,&lt;/span&gt; &lt;span style=&#34;color: #BA2121&#34;&gt;&amp;quot;/login/error&amp;quot;&lt;/span&gt;&lt;span style=&#34;color: #666666&#34;&gt;,&lt;/span&gt; &lt;span style=&#34;color: #008000; font-weight: bold&#34;&gt;false&lt;/span&gt;&lt;span style=&#34;color: #666666&#34;&gt;);&lt;/span&gt;
    	securityHandler&lt;span style=&#34;color: #666666&#34;&gt;.&lt;/span&gt;&lt;span style=&#34;color: #7D9029&#34;&gt;setAuthenticator&lt;/span&gt;&lt;span style=&#34;color: #666666&#34;&gt;(&lt;/span&gt;authenticator&lt;span style=&#34;color: #666666&#34;&gt;);&lt;/span&gt;

    	context&lt;span style=&#34;color: #666666&#34;&gt;.&lt;/span&gt;&lt;span style=&#34;color: #7D9029&#34;&gt;setSecurityHandler&lt;/span&gt;&lt;span style=&#34;color: #666666&#34;&gt;(&lt;/span&gt;securityHandler&lt;span style=&#34;color: #666666&#34;&gt;);&lt;/span&gt;
    &lt;span style=&#34;color: #666666&#34;&gt;}&lt;/span&gt;

&lt;span style=&#34;color: #666666&#34;&gt;}&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Note: this example authenticates using a fixed, in-memory list of roles and user names.
For a real application, you&amp;rsquo;ll want to actually use a database.&lt;/p&gt;

&lt;p&gt;To do this, replace the &lt;code&gt;HashLoginService&lt;/code&gt; with a &lt;code&gt;JDBCLoginService&lt;/code&gt; (or your own login
service implementation).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Register the server factory&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Create a text file called &lt;code&gt;io.dropwizard.server.ServerFactory&lt;/code&gt; and save it in
&lt;code&gt;src/main/java/resources/META-INF/services/&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The file needs to contain the fully qualified name of the new server factory. For example:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;com.example.JettyAuthServerFactory
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Finally, register the server name in your application YAML config:&lt;/p&gt;
&lt;div class=&#34;highlight&#34; style=&#34;background: #f8f8f8&#34;&gt;&lt;pre style=&#34;line-height: 125%&#34;&gt;server:
  type:
    jettyauthserver
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Application setup&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;To hook everything up, these lines need to go in the &lt;code&gt;run()&lt;/code&gt; method of your Dropwizard application.&lt;/p&gt;
&lt;div class=&#34;highlight&#34; style=&#34;background: #f8f8f8&#34;&gt;&lt;pre style=&#34;line-height: 125%&#34;&gt;&lt;span style=&#34;color: #408080; font-style: italic&#34;&gt;// Enable the Jersey security annotations on resources &lt;/span&gt;
environment&lt;span style=&#34;color: #666666&#34;&gt;.&lt;/span&gt;&lt;span style=&#34;color: #7D9029&#34;&gt;jersey&lt;/span&gt;&lt;span style=&#34;color: #666666&#34;&gt;().&lt;/span&gt;&lt;span style=&#34;color: #7D9029&#34;&gt;getResourceConfig&lt;/span&gt;&lt;span style=&#34;color: #666666&#34;&gt;().&lt;/span&gt;&lt;span style=&#34;color: #7D9029&#34;&gt;register&lt;/span&gt;&lt;span style=&#34;color: #666666&#34;&gt;(&lt;/span&gt;RolesAllowedDynamicFeature&lt;span style=&#34;color: #666666&#34;&gt;.&lt;/span&gt;&lt;span style=&#34;color: #7D9029&#34;&gt;class&lt;/span&gt;&lt;span style=&#34;color: #666666&#34;&gt;);&lt;/span&gt;

&lt;span style=&#34;color: #408080; font-style: italic&#34;&gt;// Register custom exception mapper to redirect 403 errors to the login page&lt;/span&gt;
environment&lt;span style=&#34;color: #666666&#34;&gt;.&lt;/span&gt;&lt;span style=&#34;color: #7D9029&#34;&gt;jersey&lt;/span&gt;&lt;span style=&#34;color: #666666&#34;&gt;().&lt;/span&gt;&lt;span style=&#34;color: #7D9029&#34;&gt;register&lt;/span&gt;&lt;span style=&#34;color: #666666&#34;&gt;(&lt;/span&gt;ForbiddenExceptionMapper&lt;span style=&#34;color: #666666&#34;&gt;.&lt;/span&gt;&lt;span style=&#34;color: #7D9029&#34;&gt;class&lt;/span&gt;&lt;span style=&#34;color: #666666&#34;&gt;);&lt;/span&gt;

&lt;span style=&#34;color: #408080; font-style: italic&#34;&gt;// Register the Login and Logout resources&lt;/span&gt;
environment&lt;span style=&#34;color: #666666&#34;&gt;.&lt;/span&gt;&lt;span style=&#34;color: #7D9029&#34;&gt;jersey&lt;/span&gt;&lt;span style=&#34;color: #666666&#34;&gt;().&lt;/span&gt;&lt;span style=&#34;color: #7D9029&#34;&gt;register&lt;/span&gt;&lt;span style=&#34;color: #666666&#34;&gt;(&lt;/span&gt;&lt;span style=&#34;color: #008000; font-weight: bold&#34;&gt;new&lt;/span&gt; LoginResource&lt;span style=&#34;color: #666666&#34;&gt;());&lt;/span&gt;
environment&lt;span style=&#34;color: #666666&#34;&gt;.&lt;/span&gt;&lt;span style=&#34;color: #7D9029&#34;&gt;jersey&lt;/span&gt;&lt;span style=&#34;color: #666666&#34;&gt;().&lt;/span&gt;&lt;span style=&#34;color: #7D9029&#34;&gt;register&lt;/span&gt;&lt;span style=&#34;color: #666666&#34;&gt;(&lt;/span&gt;&lt;span style=&#34;color: #008000; font-weight: bold&#34;&gt;new&lt;/span&gt; LogoutResource&lt;span style=&#34;color: #666666&#34;&gt;());&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Try it out&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;With all the pieces in place, you can now use Jersey&amp;rsquo;s &lt;a href=&#34;https://jersey.java.net/documentation/latest/security.html#annotation-based-security&#34;&gt;declarative security annotations&lt;/a&gt; to protect resources:&lt;/p&gt;
&lt;div class=&#34;highlight&#34; style=&#34;background: #f8f8f8&#34;&gt;&lt;pre style=&#34;line-height: 125%&#34;&gt;&lt;span style=&#34;color: #AA22FF&#34;&gt;@Path&lt;/span&gt;&lt;span style=&#34;color: #666666&#34;&gt;(&lt;/span&gt;&lt;span style=&#34;color: #BA2121&#34;&gt;&amp;quot;/topsecret&amp;quot;&lt;/span&gt;&lt;span style=&#34;color: #666666&#34;&gt;)&lt;/span&gt;
&lt;span style=&#34;color: #AA22FF&#34;&gt;@RolesAllowed&lt;/span&gt;&lt;span style=&#34;color: #666666&#34;&gt;(&lt;/span&gt;&lt;span style=&#34;color: #BA2121&#34;&gt;&amp;quot;admin&amp;quot;&lt;/span&gt;&lt;span style=&#34;color: #666666&#34;&gt;)&lt;/span&gt;
&lt;span style=&#34;color: #008000; font-weight: bold&#34;&gt;public&lt;/span&gt; &lt;span style=&#34;color: #008000; font-weight: bold&#34;&gt;class&lt;/span&gt; &lt;span style=&#34;color: #0000FF; font-weight: bold&#34;&gt;TopSecretResource&lt;/span&gt; &lt;span style=&#34;color: #666666&#34;&gt;{&lt;/span&gt;

    &lt;span style=&#34;color: #408080; font-style: italic&#34;&gt;/* ... */&lt;/span&gt;

&lt;span style=&#34;color: #666666&#34;&gt;}&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
</description>
    </item>
    
    <item>
      <title>Contact</title>
      <link>http://blog.locrian.uk/contact/</link>
      <pubDate>Sun, 14 Aug 2016 22:15:33 +0100</pubDate>
      
      <guid>http://blog.locrian.uk/contact/</guid>
      <description>&lt;div style=&#34;text-align:center; margin: 2em&#34;&gt;
Get in touch by &lt;a id=&#34;otliam&#34; href=&#34;click_to_reveal&#34; onclick=ku() onmouseover=ku()&gt;sending me an email&lt;/a&gt;.
&lt;/div&gt;

&lt;!-- FoxyForm is great in principle but the emails never turn up!
&lt;div style=&#34;text-align:center; margin: 2em&#34;&gt;
&lt;a id=&#34;foxyform_embed_link_805693&#34; href=&#34;http://www.foxyform.com/&#34;&gt;foxyform&lt;/a&gt;
&lt;script type=&#34;text/javascript&#34;&gt;
(function(d, t){
   var g = d.createElement(t),
       s = d.getElementsByTagName(t)[0];
   g.src = &#34;http://www.foxyform.com/js.php?id=805693&amp;sec_hash=0f4b4663c7e&amp;width=350px&#34;;
   s.parentNode.insertBefore(g, s);
}(document, &#34;script&#34;));
&lt;/script&gt;
&lt;/div&gt;
--&gt;

&lt;script type=&#34;text/javascript&#34;&gt;
window.ku=function(){(function(a,b,c,d) {
var e=document.getElementById(d);
e.setAttribute(&#39;href&#39;,(a+&#39;.&#39;+b+&#39;@&#39;+c+&#39;:&#39;+d).split(&#39;&#39;).reverse().join(&#39;&#39;));
}(&#39;ku&#39;,&#39;naircol&#39;,&#39;golb&#39;,&#39;otliam&#39;))};
&lt;/script&gt;
</description>
    </item>
    
    <item>
      <title>About</title>
      <link>http://blog.locrian.uk/about/</link>
      <pubDate>Sun, 14 Aug 2016 17:47:21 +0100</pubDate>
      
      <guid>http://blog.locrian.uk/about/</guid>
      <description></description>
    </item>
    
    <item>
      <title>New Beginnings</title>
      <link>http://blog.locrian.uk/post/new-beginnings/</link>
      <pubDate>Sun, 14 Aug 2016 13:39:40 +0100</pubDate>
      
      <guid>http://blog.locrian.uk/post/new-beginnings/</guid>
      <description>&lt;p&gt;So I decided it was finally time to resurrect this blog and bring it into the 21st century. It&amp;rsquo;s
now powered by &lt;a href=&#34;https://gohugo.io/&#34;&gt;Hugo&lt;/a&gt;, a static site generator written in
&lt;a href=&#34;http://golang.org&#34;&gt;Go&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;I promise there will be a few more posts soon!&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>Closure Exposure</title>
      <link>http://blog.locrian.uk/post/closure-exposure/</link>
      <pubDate>Tue, 21 Dec 2010 00:00:00 +0000</pubDate>
      
      <guid>http://blog.locrian.uk/post/closure-exposure/</guid>
      <description>

&lt;p&gt;These days people are becoming more aware that you can write real object oriented code in Javascript. Even though the language doesn&amp;rsquo;t support classes with private members directly, you can achieve the same thing quite easily using closures.&lt;/p&gt;
&lt;div class=&#34;highlight&#34; style=&#34;background: #f8f8f8&#34;&gt;&lt;pre style=&#34;line-height: 125%&#34;&gt;&lt;span style=&#34;color: #008000; font-weight: bold&#34;&gt;function&lt;/span&gt; MyClass() {
    &lt;span style=&#34;color: #408080; font-style: italic&#34;&gt;// Private members&lt;/span&gt;
    &lt;span style=&#34;color: #008000; font-weight: bold&#34;&gt;var&lt;/span&gt; internalCounter &lt;span style=&#34;color: #666666&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color: #666666&#34;&gt;0&lt;/span&gt;;
    &lt;span style=&#34;color: #008000; font-weight: bold&#34;&gt;var&lt;/span&gt; somePrivateFunction &lt;span style=&#34;color: #666666&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color: #008000; font-weight: bold&#34;&gt;function&lt;/span&gt;(input) {
        &lt;span style=&#34;color: #008000; font-weight: bold&#34;&gt;return&lt;/span&gt; input &lt;span style=&#34;color: #666666&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color: #BA2121&#34;&gt;&amp;quot; foo!&amp;quot;&lt;/span&gt;;
    };

    &lt;span style=&#34;color: #408080; font-style: italic&#34;&gt;// Public members&lt;/span&gt;
    &lt;span style=&#34;color: #008000; font-weight: bold&#34;&gt;this&lt;/span&gt;.getCounter &lt;span style=&#34;color: #666666&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color: #008000; font-weight: bold&#34;&gt;function&lt;/span&gt;() {
        &lt;span style=&#34;color: #008000; font-weight: bold&#34;&gt;return&lt;/span&gt; internalCounter;
    };
    &lt;span style=&#34;color: #008000; font-weight: bold&#34;&gt;this&lt;/span&gt;.increment &lt;span style=&#34;color: #666666&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color: #008000; font-weight: bold&#34;&gt;function&lt;/span&gt;() {
        internalCounter&lt;span style=&#34;color: #666666&#34;&gt;++&lt;/span&gt;;
    };
}
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Here, the first two members are effectively private, as they can only be accessed from functions declared within MyClass.&lt;/p&gt;

&lt;p&gt;This is all well and good, and makes me feel much better when I have to write Javascript, but if you&amp;rsquo;re a serious Javascript developer you&amp;rsquo;ll want to unit test your code as you go along. How are you going to test those pesky private variables and functions?&lt;/p&gt;

&lt;h3 id=&#34;dude-let-me-in&#34;&gt;Dude, let me in!&lt;/h3&gt;

&lt;p&gt;The problem is, you can&amp;rsquo;t break into a closure because it&amp;rsquo;s enforced by the language. You can try as hard as you like, but there&amp;rsquo;s no way you&amp;rsquo;re getting access to somePrivateFunction() without modifying the code of MyClass. This is a good thing - it means the private functions are truly encapsulated. But it does make testing a bit difficult.&lt;/p&gt;

&lt;p&gt;One trick I like is to add the following line to the end of the class constructor.&lt;/p&gt;
&lt;div class=&#34;highlight&#34; style=&#34;background: #f8f8f8&#34;&gt;&lt;pre style=&#34;line-height: 125%&#34;&gt;&lt;span style=&#34;color: #008000; font-weight: bold&#34;&gt;this&lt;/span&gt;.debug &lt;span style=&#34;color: #666666&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color: #008000; font-weight: bold&#34;&gt;function&lt;/span&gt;(code) { &lt;span style=&#34;color: #008000; font-weight: bold&#34;&gt;return&lt;/span&gt; &lt;span style=&#34;color: #008000&#34;&gt;eval&lt;/span&gt;(code); };
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Because of the way the eval command works, this now means that any of the private members can be extracted from within the class. For example, we can now write a unit test for somePrivateFunction. (This is a &lt;a href=&#34;http://docs.jquery.com/Qunit&#34;&gt;QUnit&lt;/a&gt; test, which is the test framework jQuery uses).&lt;/p&gt;
&lt;div class=&#34;highlight&#34; style=&#34;background: #f8f8f8&#34;&gt;&lt;pre style=&#34;line-height: 125%&#34;&gt;test(&lt;span style=&#34;color: #BA2121&#34;&gt;&amp;quot;somePrivateFunction&amp;quot;&lt;/span&gt;, &lt;span style=&#34;color: #008000; font-weight: bold&#34;&gt;function&lt;/span&gt;() {
    &lt;span style=&#34;color: #008000; font-weight: bold&#34;&gt;var&lt;/span&gt; my &lt;span style=&#34;color: #666666&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color: #008000; font-weight: bold&#34;&gt;new&lt;/span&gt; MyClass();

    &lt;span style=&#34;color: #408080; font-style: italic&#34;&gt;// extract the private function from the closure&lt;/span&gt;
    &lt;span style=&#34;color: #008000; font-weight: bold&#34;&gt;var&lt;/span&gt; somePrivateFunction &lt;span style=&#34;color: #666666&#34;&gt;=&lt;/span&gt; my.debug(&lt;span style=&#34;color: #BA2121&#34;&gt;&amp;quot;somePrivateFunction&amp;quot;&lt;/span&gt;);

    &lt;span style=&#34;color: #408080; font-style: italic&#34;&gt;// call the function&lt;/span&gt;
    equals(somePrivateFunction(&lt;span style=&#34;color: #BA2121&#34;&gt;&amp;quot;hello&amp;quot;&lt;/span&gt;), &lt;span style=&#34;color: #BA2121&#34;&gt;&amp;quot;hello foo!&amp;quot;&lt;/span&gt;);
});
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;We have slightly polluted the original class with this extra line. If it worries you, the &amp;lsquo;debug&amp;rsquo; lines can easily be stripped out before the code goes into production.&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>Static classes and unit testing</title>
      <link>http://blog.locrian.uk/post/static-classes-unit-testing/</link>
      <pubDate>Fri, 25 Jun 2010 00:00:00 +0000</pubDate>
      
      <guid>http://blog.locrian.uk/post/static-classes-unit-testing/</guid>
      <description>

&lt;p&gt;If you unit test your code (and I hope you do), you&amp;rsquo;ll quickly discover that you need to mock out parts of the system. This is much easier if you design with testability in mind from the start of the project. In particular, as you&amp;rsquo;ll see, you need to be very careful about where you use static classes.&lt;/p&gt;

&lt;p&gt;Generally when people first learn about object oriented programming it takes a while for the concepts to sink in. If you&amp;rsquo;re used to procedural languages the world of objects can seem strange and full of unnecessary bloat: why are my functions called &amp;lsquo;methods&amp;rsquo;, and why do I have to put them all in this thing called a &amp;lsquo;class&amp;rsquo;? Beginners end up creating classes full of static methods, mimicking the style of procedural programs. Unfortunately a lot of experienced programmers do the same thing. A static method can look like a deceptively simple solution but often there are good reasons to create a fully fledged class instead.&lt;/p&gt;

&lt;h3 id=&#34;when-it-s-ok&#34;&gt;When it&amp;rsquo;s OK&lt;/h3&gt;

&lt;p&gt;But first of all, let&amp;rsquo;s look at the situations when it really is OK to use a static method. To do that I&amp;rsquo;m going to borrow a term from functional programming called &amp;lsquo;referential transparency&amp;rsquo;. For example, here&amp;rsquo;s a method which is referentially transparent:&lt;/p&gt;
&lt;div class=&#34;highlight&#34; style=&#34;background: #f8f8f8&#34;&gt;&lt;pre style=&#34;line-height: 125%&#34;&gt;&lt;span style=&#34;color: #B00040&#34;&gt;boolean&lt;/span&gt; &lt;span style=&#34;color: #0000FF&#34;&gt;emailAddressIsValid&lt;/span&gt;&lt;span style=&#34;color: #666666&#34;&gt;(&lt;/span&gt;String email&lt;span style=&#34;color: #666666&#34;&gt;)&lt;/span&gt; &lt;span style=&#34;color: #666666&#34;&gt;{&lt;/span&gt; &lt;span style=&#34;color: #666666&#34;&gt;...&lt;/span&gt; &lt;span style=&#34;color: #666666&#34;&gt;}&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;It carries out some tests on the email address and returns a boolean value. Perhaps it checks that it contains the @ and at least one dot in the domain section. The important part is that it doesn&amp;rsquo;t do anything else - it doesn&amp;rsquo;t interact with the screen or the keyboard, call a database, or modify any values elsewhere in the program. All these things are &amp;lsquo;side effects&amp;rsquo;. &lt;a href=&#34;http://www.haskell.org/&#34;&gt;Pure functional programmers&lt;/a&gt; believe that functions should never, ever, cause side effects, but if you followed this at work your programs wouldn&amp;rsquo;t ever do anything and your boss might not be happy.&lt;/p&gt;

&lt;p&gt;You can put all your methods into two categories: ones that cause side effects, and ones that don&amp;rsquo;t. If a method is side-effect free, all its dependencies (the methods it calls) have to be side-effect free as well. You&amp;rsquo;ll never need to mock these methods, so they &lt;em&gt;can&lt;/em&gt; be made static. To look at it another way, consider that a method like this doesn&amp;rsquo;t change the state of the program or it&amp;rsquo;s environment. As object-oriented classes are essentially a representation of state, there&amp;rsquo;s a sense in which this method exists independently, and doesn&amp;rsquo;t need to be associated with an instance of an class.&lt;/p&gt;

&lt;h3 id=&#34;when-it-s-not&#34;&gt;When it&amp;rsquo;s not&lt;/h3&gt;

&lt;p&gt;All the other methods cause side effects. If a method calls a database, or sends an email, it&amp;rsquo;s pretty clear that you&amp;rsquo;ll need to mock it at some point down the line. Even methods that print output to the screen or read keyboard input should be mockable, if you ever want to test their behaviour.&lt;/p&gt;

&lt;p&gt;The object-oriented solution to this is polymorphism. This means creating a base class, or preferably an interface for more flexibility, and coding against the interface rather than the implementation. This is the open/closed principle in action. You&amp;rsquo;ll still need to create the concrete class at some point, and dependency injection and inversion of control are useful techniques here. But if the class was static, none of this would be possible and you would lose all the benefits of OOP.&lt;/p&gt;
</description>
    </item>
    
  </channel>
</rss>