Saturday, November 30, 2013

Some Interesting gradle commands.

once the gradle is installed, now it is time to explore the functionalities of the gradle.


1. generating new java project with gradle.

   it is possible to generate simple java project with maven as follows.
 
   maven
   mvn archetype:generate -DarchetypeArtifactId=maven-archetype-quickstart

  the gradle also supported to generate project as follows.

  gradle
  gradle init --type java-library


2. converting the maven project to gradle.

it is possible to convert the maven project to gradle without any problem. it will create the build.gradle file and declare all the dependencies and other plugins declared in the pom.xml into the build.gradle file. In order to convert the maven project to gradle, you need to execute the following command  from the same directory of the project where the pom.xml file is located.

 gradle init



Hope this will helpful for you!

Thanks
Chathuranga Tennakoon
chathuranga.t@gmail.com
http://www.chathurangaonline.com







Thursday, November 28, 2013

How to install Gradle on Linux (Ubuntu)

if you dont know gradle, please go though the following website to get an understanding about gradle.

Gradle project home page :- http://www.gradle.org


follow the steps give below to install the gradle on your develop environment. (Linux based)

1. download the gradle distribution from the gradle website.

    http://www.gradle.org/downloads


2. extract the downloaded gradle distribution in any directory in your PC.

 
3. then add the GRADLE_HOME environmental variable. to add the environmental variable, follow the below instructions.

    3.1 sudo  gedit .bashrc
 
    3.2  add the following to the bottom of the .bashrc file

    GRADLE_HOME=<path to gradle bin file>  (e.g. /opt/gradle/gradle-1.5/bin )
    export GRADLE_HOME
    PATH=$PATH:$GRADLE_HOME
    export PATH
  
    3.2  source .bashrc



4. once the above changes are done, run gradle in the terminal to check whether gradle is properly installed.




Thanks
Chathuranga Tennakoon
chathuranga.t@gmail.com
http://www.chathurangaonline.com


 

Sunday, November 10, 2013

How to configure Tomcat to support SSL or https

first you need to create a keystore with following command.

 keytool -genkey -keyalg RSA -keystore /home/chathuranga/test_chathu.keystore

then answers for the questions that prompts sequentially. once the keystore is created, you can use the following command to check whether your keystore is there.

keytool -list -keystore /home/chathuranga/test_chathu.keystore


Now it is the time to do the tocat SSL configuration.

In tomcatHome/conf/server.xml file, change the SSL configuration as follows.

 <Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true"
               maxThreads="150" scheme="https" secure="true"
               clientAuth="false" sslProtocol="TLS"
           keystoreFile="/home/chathuranga/test_chathu.keystore"
           keystorePass="password" />


Then restart the tomcat server and try to access the following URL.

https://locahost:8443


you will notice that, your tomcat installation supports SSL(HTTPS) now.


Thanks
Chathuranga Tennakoon
chathuranga.t@gmail.com

Saturday, November 2, 2013

Application Authentication for JAX-WS web services

 the fully source code for this example can be found at  following gitHub repository.

Download code From GitHub

clone the project from gitHub and use the maven to build the project.
then deploy the web service in the tomcat server. (just copy the war file and i have already done the required web.xml and sun-haxws.xml configurations.)

if you want  to know, how to deploy the web service in tomcat, you can refer my previous blog post here

once the web service is deployed, you can run the web service client to test the web service and see how it works.


In application authentication, then authentication logic will be implemented there in the web service. therefore the web service will be responsible for handling the user authentication.

the web service client will send the user credentials (username and password) to the web service. please refer the following WebService Client.


package com.chathurangaonline.jaxws.samples.client;

import com.chathurangaonline.jaxws.samples.impl.*;
import javax.xml.ws.BindingProvider;
import javax.xml.ws.handler.MessageContext;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class WebServiceClientImpl {

    public static void main(String [] args){

        CalculatorServiceImplService calculatorServiceImplService = new CalculatorServiceImplService();
        CalculatorServiceImpl calculatorService = calculatorServiceImplService.getCalculatorServiceImplPort();

        Map<String, Object> req_ctx = ((BindingProvider)calculatorService).getRequestContext();
        Map<String, List<String>> headers = new HashMap<String, List<String>>();

        //setting up the username and password 
        headers.put("Username", Collections.singletonList("chathuranga"));
        headers.put("Password", Collections.singletonList("chathu@123"));
        req_ctx.put(MessageContext.HTTP_REQUEST_HEADERS, headers);

        //in order to invoke the add method, you need to have valid login credentials
        double answer =  calculatorService.add(45,10);
        System.out.println(" answer is ["+answer+"]");
    }
}


The web service will extract the user login credentials (username and password) from the HTTP Request Headers and  will perform the user authentication.
(here we have hard coded the username and password for the demostration purpose and to make it more simple. in the production mode, you need to move then  database) 
 if the user authentication is successful, he will be able to access the web service. otherwise it will throw a HttpException as implemented.  refer the following web service implementation.


package com.chathurangaonline.jaxws.samples.impl;

import com.chathurangaonline.jaxws.samples.CalculatorService;
import javax.annotation.Resource;
import javax.jws.WebService;
import javax.xml.ws.WebServiceContext;
import javax.xml.ws.handler.MessageContext;
import javax.xml.ws.http.HTTPException;
import java.util.List;
import java.util.Map;


@WebService
public class CalculatorServiceImpl implements CalculatorService{

    @Resource
    private WebServiceContext webServiceContext;

    @Override
    public double add(double num1, double num2) {
        //todo username and password was hardcoded only for the demonstration purpose. this should be configured to look up from database or somewhere else
        if(isAuthenticated("chathuranga","chathu@123")){
            //allowing the operation for the authenticated user
            return num1 + num2;
        }
        else{
            //non-authenticated user.
            throw  new HTTPException(401);
        }
    }

    @Override
    public double multiply(double num1, double num2) {
        return num1 * num2;
    }


    /**
     * <p>
     *     method for checking the application level authentication using the username and password provided.
     * </p>
     * @param username - username provided as {@link java.lang.String}
     * @param password - password provided as {@link java.lang.String}
     * @return {@link java.lang.Boolean} (true if user authenticated, otherwise false)
     */
    private boolean isAuthenticated(String username, String password){
        if(username!=null && password!=null){
            MessageContext messageContext = webServiceContext.getMessageContext();
            Map httpHeaders  = (Map) messageContext.get(MessageContext.HTTP_REQUEST_HEADERS);

            List usernameList  = (List) httpHeaders.get("username");
            List passwordList = (List) httpHeaders.get("password");

            if((usernameList!=null && usernameList.contains(username)) && (passwordList!=null && passwordList.contains(password))){
                return true;
            }
        }
        return false;
    }
}


The main problem with Application Authentication is the mix of security logic with the business logic might mess the code. it add some unnecessary complexity for the code with tight coupling. as a solution for this, we can go for the Container Managed Authentication and that will be my next blog post ;)

Thanks
Chathuranga Tennakoon
chathuranga.t@gmail.com