<?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>feadro.com</title>
	<atom:link href="http://feadro.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://feadro.com</link>
	<description>Oy Feadro Ab</description>
	<lastBuildDate>Thu, 07 Feb 2013 19:11:55 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.1</generator>
		<item>
		<title>Simple IP Access List for Play 2 with Java</title>
		<link>http://feadro.com/simple-ip-access-list-for-play-2-1-with-java/</link>
		<comments>http://feadro.com/simple-ip-access-list-for-play-2-1-with-java/#comments</comments>
		<pubDate>Thu, 07 Feb 2013 18:52:06 +0000</pubDate>
		<dc:creator>Franz Granlund</dc:creator>
				<category><![CDATA[Play! Framework 2]]></category>

		<guid isPermaLink="false">http://feadro.com/?p=172</guid>
		<description><![CDATA[Quite often we need to be able to grant access only for certain IPs to our application. The Play! Framework 2 doesn&#8217;t come with built in IP access lists, but &#8230;]]></description>
				<content:encoded><![CDATA[<p>Quite often we need to be able to grant access only for certain IPs to our application. The Play! Framework 2 doesn&#8217;t come with built in IP access lists, but it&#8217;s quite easy to develop such a function thanks to <a href="http://www.playframework.com/documentation/2.1.0/JavaActionsComposition">Action Composition</a>. We wanted to have the possibility to annotate a controller class or method, specifying a group of IPs that should have access to it.</p>
<p>This post demonstrates a way to have a simple access list for just that.</p>
<p><strong>Installation</strong></p>
<ul>
<li>1. Create a folder called &#8216;restrict&#8217; under your app folder.</li>
<li>2. Copy the files <a href="https://github.com/feadro/play2-restrict/tree/master/app/restrict">RestrictToHostGroup.java and RestrictToHostGroupAction.java</a> into the &#8216;restrict&#8217; folder.</li>
</ul>
<p><strong>Configuration</strong></p>
<p>You configure the groups in <code>applications.conf</code>. It is mandatory to have atleast one &#8216;default&#8217; group configured. </p>
<pre class="brush: jscript; title: ; notranslate">
restricttohostgroup {
    groups {
	    default = [&quot;0:0:0:0:0:0:0:1&quot;, &quot;127.0.0.1&quot;, &quot;10.0.0.&quot;, &quot;192.168.0.&quot;],
	    admin = [&quot;192.168.7.&quot;]
    },
    redirect = &quot;http://github.com&quot;
}
</pre>
<p>A group contains an array of IPs or a partial IP, like &#8220;192.168.0.&#8221;. At the time of writing, it does not support more advanced patterns like &#8220;192.168.0.0/16&#8243;. Basically it just tries to match the remote address against the pattern. Pull requests are welcome. <img src='http://feadro.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  </p>
<p>The &#8216;redirect&#8217; key is optional, and may contain an URL where the denied requestor will be redirected to.</p>
<p><strong>Usage</strong></p>
<p>In your code, simply annotate the controller class or a controller method. This will restrict the controller or method to the group specified, or to &#8216;default&#8217; if no group is specified. If a requestor is denied access, a <code>Logger.warn()</code> will be triggered with information about the denied request.</p>
<pre class="brush: java; title: ; notranslate">
package controllers;

import play.mvc.Controller;
import play.mvc.Result;
import restrict.RestrictToHostGroup;
import views.html.index;

@RestrictToHostGroup   // Same as @RestrictToHostGroup(&quot;default&quot;)
public class Application extends Controller {

    public static Result index() {
        return ok(index.render(&quot;User.&quot;));
    }

    @RestrictToHostGroup(&quot;admin&quot;)
    public static Result admin() {
        return ok(index.render(&quot;Admin.&quot;));
    }
}
</pre>
<p><strong>Example application</strong></p>
<p>We&#8217;ve put up an <a href="https://github.com/feadro/play2-restrict">sample application</a> so you can play around with it a bit. </p>
<p>Enjoy!</p>
]]></content:encoded>
			<wfw:commentRss>http://feadro.com/simple-ip-access-list-for-play-2-1-with-java/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Content Security Policy in Play! Framework 2</title>
		<link>http://feadro.com/content-security-policy-in-play-framework-2/</link>
		<comments>http://feadro.com/content-security-policy-in-play-framework-2/#comments</comments>
		<pubDate>Sat, 19 Jan 2013 20:00:12 +0000</pubDate>
		<dc:creator>Franz Granlund</dc:creator>
				<category><![CDATA[Play! Framework 2]]></category>

		<guid isPermaLink="false">http://feadro.com/?p=143</guid>
		<description><![CDATA[What is Content Security Policy (CSP)? CSP is a security concept, developed by the Mozilla Foundation, that helps to detect and mitigate certain types of attacks, including Cross Site Scripting &#8230;]]></description>
				<content:encoded><![CDATA[<p><strong>What is Content Security Policy (CSP)?</strong></p>
<p>CSP is a security concept, developed by the Mozilla Foundation, that helps to detect and mitigate certain types of attacks, including Cross Site Scripting (XSS) and data injection attacks. This post won&#8217;t go into details about CSP itself, but you can read all about it at <a href="http://www.html5rocks.com/en/tutorials/security/content-security-policy/">Html5Rocks</a>, <a href="http://en.wikipedia.org/wiki/Content_Security_Policy">Wikipedia</a> and <a href="http://www.w3.org/TR/CSP/#report-uri">W3</a>.</p>
<p><a href="http://feadro.com/content-security-policy-in-play-framework-2/chromeviolation/" rel="attachment wp-att-157"><img src="http://feadro.com/wp-content/uploads/2013/01/chromeviolation.png" alt="chromeviolation" width="600" class="alignnone size-full wp-image-157" /></a></p>
<p><strong>Adding support for CSP in your project</strong></p>
<p>1. Create a folder called <code>csp</code> under you app folder.<br />
2. Copy the files <a href="https://github.com/feadro/play2-csp/tree/master/app/csp">ContentSecurityPolicy.java and ContentSecurityPolicyAction.java</a> into the <code>csp</code> folder.<br />
3. In you <code>applications.conf</code>, add the following:</p>
<pre class="brush: jscript; title: ; notranslate">
csp {
    policy : &quot;default-src 'self'&quot;
}
</pre>
<p>4. Modify <code>csp.policy</code> key in <code>applications.conf</code> so that it matches the security you want for your application.</p>
<p><strong>Usage</strong></p>
<p>Simply annotate your controllers class or methods with <code>@ContentSecurityPolicy</code>. The header will be sent with each response from the server.</p>
<pre class="brush: java; title: ; notranslate">
import csp.ContentSecurityPolicy;

@ContentSecurityPolicy
public class Application extends Controller {
  
    public static Result index() {
        return ok(index.render(&quot;Hi from index.&quot;));
    }

    public static Result blog() {
    	return ok(blog.render(&quot;All about my cats&quot;));
    }
}
</pre>
<p>You can also override the default CSP, set in <code>applications.conf</code>, by setting the new CSP as a parameter to the annotation. </p>
<p>Example, allowing images from http://www.playframework.org on a controller method could look like this:</p>
<pre class="brush: java; title: ; notranslate">
    @ContentSecurityPolicy(&quot;default-src 'self' ; img-src http://www.playframework.org&quot;)
    public static Result anotherPolicy() {
        return ok(index.render(&quot;Hi from anotherPolicy.&quot;));
    }
</pre>
<p><strong>Getting reports</strong></p>
<p>CSP supports reporting with &#8220;report-uri&#8221; directive. Following this directive, the client browser can send (POST) a report (JSON format) to the server with directives that has been violated. A sample report may look like this:</p>
<pre class="brush: jscript; title: ; notranslate">
{
  &quot;csp-report&quot;: {
    &quot;document-uri&quot;: &quot;http://example.org/page.html&quot;,
    &quot;referrer&quot;: &quot;http://evil.example.com/haxor.html&quot;,
    &quot;blocked-uri&quot;: &quot;http://evil.example.com/image.png&quot;,
    &quot;violated-directive&quot;: &quot;default-src 'self'&quot;,
    &quot;original-policy&quot;: &quot;default-src 'self'; report-uri http://example.org/csp-report.cgi&quot;
  }
}
</pre>
<p>To catch these reports in you application, you first need to set up a route matching the report-uri directive. Say we have our CSP directive as follows:</p>
<pre class="brush: java; title: ; notranslate">
@ContentSecurityPolicy(&quot;default-src 'self' ; report-uri /report&quot;)
</pre>
<p>Create a route to catch it:</p>
<pre class="brush: java; title: ; notranslate">
POST    /report                     controllers.Application.cspReportParser()
</pre>
<p>Then create an controller action:</p>
<pre class="brush: java; title: ; notranslate">
    @BodyParser.Of(BodyParser.Json.class)
    public static Result cspReportParser() {
        JsonNode json = request().body().asJson();
        if(json == null) {
            Logger.debug(&quot;no JSON payload&quot;);
            return badRequest(&quot;Expecting Json data&quot;);
        } else {
            Logger.debug(json.toString());
            return ok();
        }
    }
</pre>
<p><strong>Example application</strong></p>
<p>We&#8217;ve put up an <a href="https://github.com/feadro/play2-csp">sample application</a> that you can download an play around with. Note that the sample uses Play 2.1-RC2. </p>
<p>To see the violations in your browser, bring forth the inspector.</p>
<p><a href="http://feadro.com/content-security-policy-in-play-framework-2/firefoxviolation/" rel="attachment wp-att-153"><img src="http://feadro.com/wp-content/uploads/2013/01/firefoxviolation.png" alt="firefoxviolation" width="600" class="alignnone size-full wp-image-153" /></a></p>
<p>If you just want to see the header sent in the server response, use cURL:</p>
<pre class="brush: bash; title: ; notranslate">
curl -i http://localhost:9000/
</pre>
<p><strong>Credits, References and Thanks</strong><br />
- <a href="http://www.html5rocks.com/en/tutorials/security/content-security-policy/">An Introduction to Content Security Policy</a> by Mike West<br />
- <a href="http://www.objectify.be/wordpress/?p=374">Writing Modules for Play 2, Interceptors</a> by Steve Chaloner<br />
- <a href="https://play.lighthouseapp.com/projects/82401-play-20/overview">Play! Framework Gurus</a></p>
<p>Also, to improve the security of your app further, check out <a href="https://github.com/orefalo/play2-authenticitytoken">AuthenticityToken for Play2!</a> by Olivier Refalo.</p>
]]></content:encoded>
			<wfw:commentRss>http://feadro.com/content-security-policy-in-play-framework-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Play! Framework 2.0 + DataTables + Server-side processing</title>
		<link>http://feadro.com/play-framework-2-0-datatables-server-side-processing/</link>
		<comments>http://feadro.com/play-framework-2-0-datatables-server-side-processing/#comments</comments>
		<pubDate>Thu, 05 Jul 2012 21:14:12 +0000</pubDate>
		<dc:creator>Franz Granlund</dc:creator>
				<category><![CDATA[Play! Framework 2]]></category>

		<guid isPermaLink="false">http://feadro.com/?p=86</guid>
		<description><![CDATA[This post will describe how to set up a Play! Framework 2.0(.2) Java project with DataTables and server-side processing. The source code used in this example can be found on &#8230;]]></description>
				<content:encoded><![CDATA[<p>This post will describe how to set up a Play! Framework 2.0(.2) Java project with <a title="DataTables" href="http://datatables.net/" target="_blank">DataTables</a> and <a title="server-side processing" href="http://datatables.net/examples/data_sources/server_side.html" target="_blank">server-side processing</a>. <a href="https://github.com/franzgranlund/dtapp">The source code used in this example can be found on github</a>.</p>
<p style="text-align: center"><a href="http://feadro.com/wp-content/uploads/2012/07/datatables_result.png"><img class="wp-image-89 aligncenter" src="http://feadro.com/wp-content/uploads/2012/07/datatables_result.png" alt="" width="524" height="367" /></a></p>
<p><strong>Create a new project</strong></p>
<p>Create a new Play! application and choose <em>Create a simple Java application</em>. In this example we will call our app <em>dtapp</em>.</p>
<pre class="brush: bash; title: ; notranslate">
$ play new dtapp
</pre>
<p><strong>Download needed files</strong></p>
<p>We are going to use DataTables with the jQuery style, so we need to download those packages.</p>
<p>1. Download <a title="DataTables" href="http://datatables.net/download/" target="_blank">DataTables</a> (this post uses version 1.9.2)</p>
<ul>
<li>Copy DataTables-1.9.2/media/css/demo_table_jui.css to dtapp/public/stylesheets/</li>
<li>Copy DataTables-1.9.2/media/js/jquery.dataTables.min.js to dtapp/public/javascripts/</li>
</ul>
<p>2. Download a <a title="jQuery UI" href="http://jqueryui.com/download/" target="_blank">jQuery UI</a> (this post uses version 1.8.21). You can choose what theme you like, this post will use UI lightness.</p>
<ul>
<li>Copy jquery-ui/js/jquery-1.7.2.min.js to dtapp/public/javascripts/ (delete the old jquery-1.7.1.min.js)</li>
<li>Copy jquery-ui/js/jquery-ui-1.8.21.custom.min.js to dtapp/public/javascripts/</li>
<li>Copy query-ui/css/ui-lightness to dtapp/public/stylesheets/</li>
</ul>
<p>3. Modify main.scala.html to include the new files:</p>
<pre class="brush: xml; title: ; notranslate">
@(title: String)(content: Html)

&lt;!DOCTYPE html&gt;

&lt;html&gt;
 &lt;head&gt;
 &lt;title&gt;@title&lt;/title&gt;
 &lt;link rel=&quot;stylesheet&quot; media=&quot;screen&quot; href=&quot;@routes.Assets.at(&quot;stylesheets/demo_table_jui.css&quot;)&quot;&gt;
 &lt;link rel=&quot;stylesheet&quot; media=&quot;screen&quot; href=&quot;@routes.Assets.at(&quot;stylesheets/ui-lightness/jquery-ui-1.8.21.custom.css&quot;)&quot;&gt;
 &lt;link rel=&quot;shortcut icon&quot; type=&quot;image/png&quot; href=&quot;@routes.Assets.at(&quot;images/favicon.png&quot;)&quot;&gt;
 &lt;script src=&quot;@routes.Assets.at(&quot;javascripts/jquery-1.7.2.min.js&quot;)&quot; type=&quot;text/javascript&quot;&gt;&lt;/script&gt;
 &lt;script src=&quot;@routes.Assets.at(&quot;javascripts/jquery-ui-1.8.21.custom.min.js&quot;)&quot; type=&quot;text/javascript&quot;&gt;&lt;/script&gt;
 &lt;script src=&quot;@routes.Assets.at(&quot;javascripts/jquery.dataTables.min.js&quot;)&quot; type=&quot;text/javascript&quot;&gt;&lt;/script&gt;
 &lt;/head&gt;
 &lt;body&gt;
 @content
 &lt;/body&gt;
&lt;/html&gt;
</pre>
<p><strong>Create the Model</strong></p>
<p>Our table will display information about contacts, so we need to create a Contact model. Create a file dtapp/app/models/Contact.java with the following content:</p>
<pre class="brush: java; title: ; notranslate">package models;

import java.util.*;
import javax.persistence.*;

import play.api.libs.Crypto;
import play.db.ebean.*;
import play.data.format.*;
import play.data.validation.*;

import play.Logger;

import com.avaje.ebean.*;

@Entity
public class Contact extends Model {

  @Id
  public Long id;

  @Constraints.Required
  public String name;

  public String title;
  public String email;

  public static Model.Finder&lt;Long,Contact&gt; find = new Model.Finder(Long.class, Contact.class);

  public static List&lt;Contact&gt; findAll() {
    return find.all();
  }

  public String toString() {
    return name;
  }
}</pre>
<p><strong>Create the view</strong></p>
<p>Add a HTML-table and the needed javascript to index.scala.html. The javascript-snippet initializes DataTable and sets so that it will use server-side processing and jQuery UI. <em>Note the sAjaxSource setting, that points to an application route</em>.</p>
<pre class="brush: xml; title: ; notranslate">
@(message: String)

@main(&quot;Play!ing with DataTables&quot;) {

&lt;script type=&quot;text/javascript&quot;&gt;
  /* Table initialisation */
  $(document).ready(function() {
    $('#contacts_table').dataTable( {
      &quot;bProcessing&quot;: true,
      &quot;aaSorting&quot;: [[ 0, &quot;asc&quot; ]],
      &quot;bServerSide&quot;: true,
      &quot;bJQueryUI&quot;: true,
      &quot;sPaginationType&quot;: &quot;full_numbers&quot;,
      &quot;sAjaxSource&quot;: &quot;@routes.Application.list()&quot;
    });
  });
&lt;/script&gt;

&lt;h2&gt;Play!ing with DataTables&lt;/h2&gt;

&lt;table id=&quot;contacts_table&quot; class='display'&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Name&lt;/th&gt;
      &lt;th&gt;Title&lt;/th&gt;
      &lt;th&gt;E-mail&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Row 1 Data 1&lt;/td&gt;
      &lt;td&gt;Row 1 Data 2&lt;/td&gt;
      &lt;td&gt;etc&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;
}
</pre>
<p><strong>Set up the route</strong><br />
Now we need to set up a route for the Ajax-call. Add the following line to your dtapp/conf/routes:</p>
<pre class="brush: java; title: ; notranslate">GET /search controllers.Application.list()</pre>
<p><strong>Set up the controller</strong></p>
<p>This is where the tricky part is. DataTables with server-side processing sends a long query-string to the Application.list(). The query-string contains information like search-string, sorting, pagination etc. We need to get the query-string, find out what it wants, get the data it wants from the database and return it. DataTables expects Application.list() to give back a JSON-string containing the needed information to be displayed. The real magic here is <a href="http://www.avaje.org/static/javadoc/pub/com/avaje/ebean/Page.html" target="_blank">EBeans Page&lt;T&gt;</a>, a class that will help us to easily get the data we need.</p>
<p>Your dtapp/app/controllers/Application.java should look like this:</p>
<pre class="brush: java; title: ; notranslate">
package controllers;

import play.*;
import play.libs.Json;
import play.mvc.*;
import play.data.*;

import models.*;
import views.html.*;

import java.util.*;

import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.node.ArrayNode;
import org.codehaus.jackson.node.ObjectNode;

import com.avaje.ebean.Expr;
import com.avaje.ebean.Page;

public class Application extends Controller {

  public static Result index() {
    return ok(index.render(&quot;Your new application is ready.&quot;));
  }

  public static Result list() {
    /**
     * Get needed params
     */
    Map&lt;String, String[]&gt; params = request().queryString();

    Integer iTotalRecords = Contact.find.findRowCount();
    String filter = params.get(&quot;sSearch&quot;)[0];
    Integer pageSize = Integer.valueOf(params.get(&quot;iDisplayLength&quot;)[0]);
    Integer page = Integer.valueOf(params.get(&quot;iDisplayStart&quot;)[0]) / pageSize;

    /**
     * Get sorting order and column
     */
    String sortBy = &quot;name&quot;;
    String order = params.get(&quot;sSortDir_0&quot;)[0];

    switch(Integer.valueOf(params.get(&quot;iSortCol_0&quot;)[0])) {
      case 0 : sortBy = &quot;name&quot;; break;
      case 1 : sortBy = &quot;title&quot;; break;
      case 2 : sortBy = &quot;email&quot;; break;
    }

    /**
     * Get page to show from database
     * It is important to set setFetchAhead to false, since it doesn't benefit a stateless application at all.
     */
    Page&lt;Contact&gt; contactsPage = Contact.find.where(
      Expr.or(
        Expr.ilike(&quot;name&quot;, &quot;%&quot;+filter+&quot;%&quot;),
        Expr.or(
          Expr.ilike(&quot;title&quot;, &quot;%&quot;+filter+&quot;%&quot;),
          Expr.ilike(&quot;email&quot;, &quot;%&quot;+filter+&quot;%&quot;)
        )
      )
    )
    .orderBy(sortBy + &quot; &quot; + order + &quot;, id &quot; + order)
    .findPagingList(pageSize).setFetchAhead(false)
    .getPage(page);

    Integer iTotalDisplayRecords = contactsPage.getTotalRowCount();

    /**
     * Construct the JSON to return
     */
    ObjectNode result = Json.newObject();

    result.put(&quot;sEcho&quot;, Integer.valueOf(params.get(&quot;sEcho&quot;)[0]));
    result.put(&quot;iTotalRecords&quot;, iTotalRecords);
    result.put(&quot;iTotalDisplayRecords&quot;, iTotalDisplayRecords);

    ArrayNode an = result.putArray(&quot;aaData&quot;);

    for(Contact c : contactsPage.getList()) {
      ObjectNode row = Json.newObject();
      row.put(&quot;0&quot;, c.name);
      row.put(&quot;1&quot;, c.title);
      row.put(&quot;2&quot;, c.email);
      an.add(row);
    }

    return ok(result);
 }
}
</pre>
<p><strong>Set up the sample data</strong></p>
<p>Now we just need to create some sample data so that we can try out our shiny new table. Download <a href="https://github.com/franzgranlund/dtapp/blob/master/conf/initial-data.yml">initial-data.yml</a> and place it in your dtapp/conf/. Then create dtapp/app/Global.java, that will import the sample data, with the following content:</p>
<pre class="brush: java; title: ; notranslate">
import play.*;
import play.libs.*;
import java.util.*;
import com.avaje.ebean.*;
import models.*;
import java.util.concurrent.*;

public class Global extends GlobalSettings {

  @Override
  public void onStart(Application app) {

    /**
     * Here we load the initial data into the database
     */
    if(Ebean.find(Contact.class).findRowCount() == 0) {
      Map&lt;String,List&lt;Object&gt;&gt; all = (Map&lt;String,List&lt;Object&gt;&gt;)Yaml.load(&quot;initial-data.yml&quot;);
      Ebean.save(all.get(&quot;contacts&quot;));
    }
  }
}
</pre>
<p>The last thing you have to do is enable H2 database for your application. Uncomment the following lines in dtapp/conf/application.conf:</p>
<pre class="brush: plain; title: ; notranslate">
db.default.driver=org.h2.Driver
db.default.url=&quot;jdbc:h2:mem:play&quot;
ebean.default=&quot;models.*&quot;
</pre>
<p><strong>Ready to run!</strong></p>
<p>You should now be able to run your application. DataTables will only fetch the needed data. Try searching for different terms, pagination etc. The table should update directly. Try adding more sample data to the yaml-file to see how it behaves. Try bringing front your web browsers Inspector and see what query and data is sent.</p>
<p>Enjoy!</p>
]]></content:encoded>
			<wfw:commentRss>http://feadro.com/play-framework-2-0-datatables-server-side-processing/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>
