<commit_before>---
layout:     post
title:      Setting up a HTTPS Restlet Webserver
date:       2015-08-31 16:58:21
categories: restlet webservice
---
For all of our services (such as [Haplogrep](http://haplogrep.uibk.ac.at)), we are using [Restlet](http://restlet.com/technical-resources/restlet-framework/guide/2.3) on server side. 
This blog entry summarizes the necessary steps to set up a working HTTPS Restlet Server with a valid certificate and a trusted connection. Check out the [Github Repo](https://github.com/seppinho/Restlet101/) for the code

### Set up a Java Keystore

- In a first step, a new Java Keystore needs to be created using the `keytool` of JDK (use at least Java 7 for this):
{% highlight bash %} 
    keytool -genkey -alias <your-alias> -keyalg RSA -keysize 2048 -keypass <your-passwd> -storepass <your-passwd> -keystore <your-keystore.jks>
{% endhighlight %}


-  Now, generate a new certificate signing request (CSR) and send it your certificate authority (CA). Alternatively, the CSR can also be [self-signed](http://restlet.com/technical-resources/restlet-framework/guide/2.3/core/security/https). 
{% highlight bash %} 
keytool -keystore <your-keystore.jks> -certreq -alias <your-alias> -keyalg RSA -keysize 2048 -file <signing-request.csr>
{% endhighlight %}


- Import the received root / intermediate webserver certificate files (CRTs) from your CA like this (I received two files):
{% highlight bash %} 
keytool -import -alias intermed -keystore <your-keystore.jks> -trustcacerts -file <intermed.crt>
keytool -import -alias <your-alias-from-the-csr> -file <received-file.crt> -trustcacerts -keystore <your-keystore.jks>
{% endhighlight %}

### Create a Restlet Webapp (Updated on Nov 24)
Now it's time to integrate your certificate into your Restlet App as described [here](http://restlet.com/technical-resources/restlet-framework/guide/2.5/core/security/https). 

{% highlight java %} 

public class SampleServer {

	public static void main(String[] args) {

		// Create a new Component.
		Component component = new Component();

		// Add a new HTTP server listening on port 8080
		Server server = component.getServers().add(Protocol.HTTP, 8080);

		// Add your HTTPS specifications
		String file = "location-to-your-file";
		String keystorePwd = "<PASSWORD>";
		String keyPwd = "<PASSWORD>";
		File keystoreFile = new File(file);
<commit_msg>improved post<commit_after>---
layout:     post
title:      Setting up a HTTPS Restlet Webserver
date:       2015-08-31 16:58:21
categories: restlet webservice
---
For all of our services (such as [Haplogrep](http://haplogrep.uibk.ac.at)), we are using [Restlet](http://restlet.com/technical-resources/restlet-framework/guide/2.3) on server side. 
This blog entry summarizes the necessary steps to set up a working HTTPS Restlet Server with a valid certificate and a trusted connection. Check out the [Github Repo](https://github.com/seppinho/Restlet101/) for the source code!

### Set up a Java Keystore

- In a first step, a new Java Keystore needs to be created using the `keytool` of JDK (use at least Java 7 for this):
{% highlight bash %} 
    keytool -genkey -alias <your-alias> -keyalg RSA -keysize 2048 -keypass <your-passwd> -storepass <your-passwd> -keystore <your-keystore.jks>
{% endhighlight %}


-  Now, generate a new certificate signing request (CSR) and send it your certificate authority (CA). Alternatively, the CSR can also be [self-signed](http://restlet.com/technical-resources/restlet-framework/guide/2.3/core/security/https). 
{% highlight bash %} 
keytool -keystore <your-keystore.jks> -certreq -alias <your-alias> -keyalg RSA -keysize 2048 -file <signing-request.csr>
{% endhighlight %}


- Import the received root / intermediate webserver certificate files (CRTs) from your CA like this (I received two files):
{% highlight bash %} 
keytool -import -alias intermed -keystore <your-keystore.jks> -trustcacerts -file <intermed.crt>
keytool -import -alias <your-alias-from-the-csr> -file <received-file.crt> -trustcacerts -keystore <your-keystore.jks>
{% endhighlight %}

### Create a Restlet Webapp (Updated on Nov 24)
Now it's time to integrate your certificate into your Restlet App as described [here](http://restlet.com/technical-resources/restlet-framework/guide/2.5/core/security/https). The running example can be found [here](https://github.com/seppinho/Restlet101/). 

{% highlight java %} 

public class SampleServer {

	public static void main(String[] args) {

		// Create a new Component.
		Component component = new Component();

		// Add a new HTTP server listening on port 8080
		Server server = component.getServers().add(Protocol.HTTP, 8080);

		// Add your HTTPS specifications
		String file = "location-to-your-file";
		String keystorePwd = "<PASSWORD>";
		String keyPwd = "<PASSWORD>";
		File keystoreFile = new File(file);

{'fraction_non_alphanumeric': 0.11557478368355995, 'fraction_numerical': 0.01792336217552534, 'mean_word_length': 4.105152471083071, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 31, 'https://': 3, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5803411', 'n_tokens_mistral': 1642, 'n_tokens_neox': 1454, 'n_words': 517}