Saturday, 9 June 2018

Generating free server ssl certificate using letsencrypt

Generating and maintaining SSL certificates is costly and time-consuming.  Thanks to the letsencrypt, we can generate SSL certificate free of cost and in the really easy way.

For generating web certificates for apache or nginx we have awesome tool certbot, hence I will not bother writing anything about it. All steps needed for certbot are available here.

For generating SSL certificate with custom key size below are the steps
Let's generate a certificate for domain rnd.njoshi.com.

  1.  Make sure http://rnd.njoshi.com is reachable
  2.  For this I installed nginx
    • #mkdir -p /usr/share/nginx/html/.well-known/acme-challenge
  3. Downlaod letsencrypt-auto script
    • #curl -o letsencrypt-auto https://raw.githubusercontent.com/certbot/certbot/master/letsencrypt-auto-source/letsencrypt-auto
  4. Generate the certificate
    • ./letsencrypt-auto certonly --agree-tos --rsa-key-size 4096 --renew-by-default -m youremail@gmail.com --webroot -w /usr/share/nginx/html -d rnd.njoshi.com
  5. We have freshly minted certificate at /etc/letsencrypt/live/rnd.njoshi.com/

Saturday, 2 September 2017

Loading properties from config file

While loading properties from config file we face folder path issue. I have written very simple helper class which will load config files from base path and jars location, enabling same code to run in IDE and also in standalone deployments.

Saturday, 13 May 2017

REST service using Embedded Jetty Server and Jersey ServletContainer

I wanted to developed embedded solution that provides REST service without using external container like JBoss.
This can be done using Jetty Server, Jersey ServletContainer and simple maven project.
Code as below

Full source code

Friday, 12 May 2017

Saving instance state of the Fragment while screen rotation

To give seamless experience to user, it important to keep user in same state of operation. For example if user is typing something in EditText and rotates device, text entered by user should be kept.

When device rotates activity gets recreated forcing Fragment to recreate.
Here we can maintain state of fragment so that after rotation fragment can restore data using saved instance state.

Full source code


 

Thursday, 8 September 2016

Making sequential requests using Volley

Volley by nature is async as on completion of request callback gets triggered. We add request by using RequestQueue.add(Request) which gets added in internal priority blocking queue. By default 4 dispatcher or network threads run in background which pick request from queue and process it. As soon as processing is complete dispatcher picks next request in from the queue.

To make volley request sequential we need to restrict dispatcher threads to single thread instated of four. By doing this we restrict Volley from executing next request before complication of current request.

We can use RequestQueue constructor
/**
* Creates the worker pool. Processing will not begin until {@link #start()} is called.
*
* @param cache A Cache to use for persisting responses to disk
* @param network A Network interface for performing HTTP requests
* @param threadPoolSize Number of network dispatcher threads to create
*/
public RequestQueue(Cache cache, Network network, int threadPoolSize) {
...
}

Saturday, 20 February 2016

Sorting string array using SetTree

Code is straight forward
import java.util.TreeSet;
import java.util.Set;
import java.util.Collections;
public class Sort{

public static void main(String [] args){
String [] input = new String[] {"this", "is", "a", "test", "string"};
Set<String> stringSet = new TreeSet<>();
Collections.addAll(stringSet, input);
System.out.println(stringSet);
}
}

 

Friday, 4 December 2015

Accessing H2 in memory database

Accessing H2 in memory database integrated in spring application

Add below 2 beans in spring config. H2 console will be accessible on http://localhost:8082/

<bean id="h2Server" class="org.h2.tools.Server" factory-method="createTcpServer" init-method="start" destroy-method="stop" depends-on="h2WebServer">
<constructor-arg value="-tcp,-tcpAllowOthers,-tcpPort,9092"/>
</bean>
<bean id="h2WebServer" class="org.h2.tools.Server" factory-method="createWebServer" init-method="start" destroy-method="stop">
<constructor-arg value="-web,-webAllowOthers,-webPort,8082"/>
</bean>