Adding Certificate files to Java KeyStore

Introduction SSL

Most of those sites use the Socket Layer (SSL) protocol to secure their Internet applications.

SSL allows the data from a client, such as a Web browser, to be encrypted prior to transmission so that someone trying to sniff the data is unable to decipher it.

Many Java application servers and Web servers support the use of keystores for SSL configuration.

If you’re building secure Java programs, learning to build a keystore is the first step.

Source :


What is Java Key Tool ?

Java Keytool is a key and certificate management utility. It allows users to manage their own public/private key pairs and certificates.
Java Keytool stores the keys and certificates in what is called a keystore. It protects private keys with a password.

Each certificate in a Java keystore is associated with a unique alias.
When creating a Java keystore you will first create the .jks file that will initially only contain the private key,
then generate a CSR. Then you will import the certificate to the keystore including any root certificates.



Steps to manually import certficicates into KeyStore


1. Create a KeyStore in machine

keytool -keystore <KeyStoreName> -genkey -alias <Key Store Alias>
keytool -keystore mykeystore -genkey -alias myalias




2. Get the Certificate File and keep it in a location

eg: Certificate file name is scarlet.cer


3. Import the Certificate as a Trusted Certificate to keystore

keytool -import -alias <AliasName> -keystore <KeyStoreName> -file <FilePath>
keytool -import -alias susan -keystore mykeystore -file scarlet.cer


Posted in Uncategorized

Java Program Code Obfuscation for Secure Software

What is the mean of Obfuscation?

Obfuscation (or beclouding) is the hiding of intended meaning in communication, making communication confusing, willfully ambiguous, and harder to interpret.[citation needed] The word comes from Latin obfuscatio, from obfuscare (“to darken”), from ob (“over”) and fuscare (“to make dark”), from fuscus (“dark”). Obfustication is a common variant, especially in British English.


Protect Your Java Code — Through Obfuscators And Beyond

Reverse engineering of your proprietary applications by unfair competition or malicious hackers may result in highly undesirable exposure of your algorithms and ideas, proprietary data formats, licensing and security mechanisms, and, most importantly, your customers’ data. Here is why Java is particularly weak in this respect compared to C++:


What is Progurad ?

ProGuard is a free Java class file shrinker, optimizer, obfuscator, and preverifier. It detects and removes unused classes, fields, methods, and attributes. It optimizes bytecode and removes unused instructions. It renames the remaining classes, fields, and methods using short meaningless names. Finally, it preverifies the processed code for Java 6 or higher, or for Java Micro Edition.



How to Use Progurad with Maven for code Obfuscation ? 

It is possible to obfuscate Java Code using Maven targets. It is need to use compatible Maven Plugin with compatible Proguard Version.

It is very easy to use Proguard with Maven. Just need to add correct versions in pom.xml under plugin section.



Following is working example for Obfuscate java code using Proguard. 

Here jdk version is jdk 1.6.0_45.

1. You have to define compatible versions of Progurad maven plugin and Progurad base plugin.

2. It is possible to set Classes are not to obfuscate. (Public Classes are set as non obfuscate since that classes act as entry point to the Services)


Progurad with Maven – Plugin Entry.

Example:- pom.xml

		<!-- Keep public classes non obfuscate mode-->
              <option>-keep public class * { public protected *; }</option> 




Tagged with: , , , , , , ,
Posted in Obfuscation

Test Driven Development


Posted in Uncategorized

Embedded jetty with Cuubez

In this tutorial, we show you how to develop a simple RESTfull web service application with embedded jetty server using cuubez framwork.

Technologies and Tools used in this article:

  1. cuubez 1.1.1
  2. JDK 1.7
  3. Maven 3.0.3
  4. Intellij IDEA 13.1.1

Note: If you want to know what and how REST works, just search on Google, ton of available resources.

1. Directory Structure

This is the final web project structure of this tutorial


2. Standard Java Project

Create a standard Maven java project structure.

mvn archetype:generate -DgroupId=com.cuubez -DartifactId=cuubez-jetty -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

3. Project Dependencies

Following maven dependencies should add to the pom.xml file.

File : pom.xml





4. REST Service


public class UserResource {

    private static Log log = LogFactory.getLog(UserResource.class);

    public Response userGet(@PathParam(value = "userId") String id, @QueryParam(value = "name") String name, @QueryParam(value = "age") int age) {

        User user = new User(id, age, name);
        return Response.ok().entity(user).build();

    public void userPost(User user) {"POST = [" + user + "]");

    @Consumes({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
    public void userPut(User user) {"PUT = [" + user + "]");




5. Embedded Jetty Implementation

public class JettyServer {

    public static void main(String args[]) {

        Server server = new Server(8080);
        ServletContextHandler handler = new ServletContextHandler(ServletContextHandler.SESSIONS);
        handler.addEventListener(new BootstrapContextListener());  //cuubez bootstrap context listner
        handler.addServlet(HttpServletDispatcher.class, "/rest/*"); //servlet filter

        try {


        } catch (Exception e) {



6. Demo

In this example, web request from projectURL/rest/users/id-1003 will match to UserResource, via @Path(“/users/{userId}”). {userId} will match to parameter annotated with @PathParam and age and name will match to parameters annotated with @QueryParam.

URL : http://localhost:8080/rest/users/id-1003?name=jhone&age=30



Download this example –

Posted in Uncategorized

Mocking Final Classes with Mockito

I have used PowerMock with Mockito for mocking final Class.

Following are Maven dependencies for PowerMock and Mockito.
Eg. pom.xml



We have final class called with public method called Send(String parameter).
Need to mock Send method.

How we do that?

Eg: Final Class is as follows

final class TestFinal{

       public String send(String input){

       //method to process the input
       String status=Processor.getResult(input);

       return status;



Mocking send method

import java.util.HashMap;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

public class RequestFlowTest {

    public void shouldTestRequestData() throws Exception {
 	TestFinal testFinal = PowerMockito.mock(TestFinal.class);

 	String result=testFinal.send("testInput");


Tagged with: , , , , , ,
Posted in Uncategorized

REST Web Application with Cuubez

In this tutorial, we show you how to develop a simple REST web application with Cuubez.

Technologies and Tools used in this article:

1.Cuubez 1.0.0
2.JDK 1.6
3.Tomcat 6.0
4.Maven 3.0.3
5.Intellij IDEA 13.1.1

Note If you want to know what and how REST works, just search on Google, ton of available resources.






2. Standard Web Project
Create a standard Maven web project structure.


mvn archetype:generate -DgroupId=com.cuubez -DartifactId=Employee-example
        -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false

Note To support IDEA, use Maven command :

mvn idea:idea

3. Project Dependencies
The recommended way to get started using cuubez-framework in your project is with a dependency management system – the snippet below can be copied and pasted into your build(pom.xml). Need help? See our getting started guides on building with Maven.


File : pom.xml


4. REST Service

Simple REST service with Cuubez

import com.cuubez.example.entity.Employee;

public class EmployeeResource {

    public Employee getEmployee(@PathParam("empId") final String empId) {

        Employee employee = new Employee(empId, "jhon powel", "marketing", "No 321, Colombo 4", "+94775993720");
        return employee;

5. web.xml
The ContextLoaderListner context listener has to be deployed in order to create the registry for cuubez ,while the ServiceInitiator servlet is used so that incoming requests are correctly routed to the appropriate services. We have configured the specific servlet, named cuubez, to intercept requests under the /rest/ path.


File : web.xml


  <display-name>Employee Example</display-name>





6. Demo

In this example, web request from projectURL/rest/employees will match to EmployeeResource, via @Path(“/employees”). And the {empId}from projectURL/rest/employees/{empId} will match to parameter annotated with @PathParam.
URL :  http://localhost:8080/employee-example-1.0.0/rest/employees/eId-0001_




Download above Employee example –

Tagged with: , , , , , , , ,
Posted in Uncategorized

Spring AOP

Spring Framework contains built-in AOP infrastructure.
It is defined in org.springframework.aop.* packages.
Following are the explanations of the Vocabulary which plays around AOP.
This will help you to understand basic concepts around AOP.

Think of this as the general feature you want to apply globally to your application
(logging, performance monitoring, exception handling, transaction management, etc).


A chunk of code that is invoked during program execution,
and is a piece of the logic for implementing your aspect.
This is the first important piece of a Spring AOP aspect implementation

A *single* location in the code where an advice should be executed (such as field access, method invocation , constructor invocation, etc.).
Spring’s built-in AOP only supports method invocation currently.

A pointcut is a set of many joinpoints where an advice should be executed.
So if, in Spring, a joinpoint is always a method invocation, then a pointcut is just a set of methods that, when called, should have advices invoked around them.
This is the second important pieces of a Spring AOP aspect implementation!

Targets/Target Objects
The objects you want to apply an aspect or set of aspects to


This is the ability to add methods to an object.
This is closely tied to, and is almost analogous to the term ‘mixins’.
It’s really just a way to make an object of type A also an object of type B.
Introduction in Spring is limited to interfaces.

Tagged with: , , , , ,
Posted in Uncategorized