Tuesday, May 7, 2013

Extracting public and private keys from a Java Key Store

Step 1: Creating the “public-private” key-pair.   
keytool -genkey -alias client -validity 365 -keystore keystore.jks 

Step 2: Validate the “public-private” key pair.
keytool -list -v -keystore keystore.jks

Step 3: Extract the “public key” from the “public-private”
keytool -export -alias client -keystore keystore.jks -rfc -file public.cert

Step 4: Check the extracted public key (public.cert)
type public.cert

Step 5: Time to create the truststore using the public key, which was extracted.
keytool -import -alias client -file public.cert -keystore server.truststore
keytool -list -v -keystore server.truststore

Steps Private Keys Export : It is required to save the private key in the PKCS#12 format
and we can convert that to a text file using openssl:
Step 1: keytool -v -importkeystore -srckeystore keystore.jks -srcalias client -destkeystore myp12file.p12 -deststoretype PKCS12
Step 2: openssl pkcs12 -in myp12file.p12 -out private.pem

Other Keytool Commands:
- keytool -delete -alias client -keystore keystore.jks
- keytool -storepasswd -new new_storepass -keystore keystore.jks
- keytool -list -v -keystore $JAVA_HOME/jre/lib/security/cacerts
- Import New CA into Trusted Certs
keytool -import -trustcacerts -file /path/to/ca/ca.pem -alias CA_ALIAS -keystore $JAVA_HOME/jre/lib/security/cacerts

Frequently used OpenSSL Commands:
- http://shib.kuleuven.be/docs/ssl_commands.shtml
 

Wednesday, May 1, 2013

Maven Basic

Maven Phases
Although hardly a comprehensive list, these are the most common default life cycle phases executed.
-validate: validate the project is correct and all necessary information is available
-compile: compile the source code of the project
-test: test the compiled source code using a suitable unit testing framework. These tests should not require the code be packaged or deployed
-package: take the compiled code and package it in its distributable format, such as a JAR.
-integration-test: process and deploy the package if necessary into an environment where integration tests can be run
-verify: run any checks to verify the package is valid and meets quality criteria
-install: install the package into the local repository, for use as a dependency in other projects locally
-deploy: done in an integration or release environment, copies the final package to the remote repository for sharing with other developers and projects.
There are two other Maven life-cycles of note beyond the default list above. They are
-clean: cleans up artifacts created by prior builds
-site: generates site documentation for this project

An interesting thing to note is that phases and goals may be executed in sequence.

-mvn clean dependency:copy-dependencies package
This command will clean the project, copy dependencies, and package the project
(executing all phases up to package, of course).

Maven Quickstart

  • cls
  • mvn -version
  • mvn archetype:generate
    Try running the following commands in serial order:
  • mvn validate
  • mvn compile / mvn clean compile (-o offline mode)
  • mvn package / mvn jar:jar / mvn war:war  (To build jar/war package)
  • mvn test  (Skipping test use property -Dmaven.test.skip=true)
    Just execute the following command,
  • mvn install / mvn clean install (It will encapsulate much of the default build life-cycle)
    (including compiling, testing, packaging, and installing the artifact in the local repository)
  • mvn -o compile / mvn -o install / mvn -o test (To Work Offline -o)
  • mvn dependency:analyze (To check your project for both unnecessary dependencies)
  • mvn dependency:tree
  • mvn clean dependency:copy-dependencies package
  • mvn site
  • mvn eclipse:eclipse -Dwtpversion=2.0 (To support Eclipse ide)
  • mvn archetype:generate -Dfilter=org.apache:struts
  • mvn archetype:generate > list.txt
  • mvn -f  <other pom file>
  • mvn help:help / mvn javadoc:help / mvn eclipse:help / mvn tomcat:help....etc
    mvn help:describe -Dplugin=eclipse
- Create a Simple Java Project from Maven Template
  • mvn archetype:generate -DgroupId=com.jini -DartifactId=Hello -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
  • mvn eclipse:eclipse
  • mvn package
- Running Jar
  • java -cp target/Hello-1.0-SNAPSHOT.jar com.jini.App 
  • mvn exec:java
    <plugin>
      <groupId>org.codehaus.mojo</groupId>
      <artifactId>exec-maven-plugin</artifactId>
      <version>1.4</version>
      <configuration>
        <mainClass>org.dhappy.test.NeoTraverse</mainClass>
      </configuration>
    </plugin>
 - MAVEN_OPTS Environment Variable 
  • (UNIX) export MAVEN_OPTS="-Xmx512m -XX:MaxPermSize=128m" 
  • (Windows) set MAVEN_OPTS=-Xmx512m -XX:MaxPermSize=128m

Maven Tips and Tricks

-Download all your dependencies to target/dependencies:
mvn dependency:copy-dependencies
-But note, you probably only want the dependencies that you actually need at runtime:
mvn dependency:copy-dependencies -DincludeScope=runtime
-Check all of your dependencies to see if newer versions are available (versions plugin):
mvn versions:display-dependency-updates
-And do the same for your plugins:
mvn versions:display-plugin-updates
-Purge all the project’s dependencies and re-download (dependency plugin):
mvn dependency:purge-local-repository
-Build a package but skip running the tests:
mvn package -DskipTests
-If Maven caches a failure to find a dependency, but you’ve fixed it, add a -U to your command line.
In a multi-module project, you need to build from the root pom to find dependencies.
If you just want to build a specific module/project
(Note that I’m just specifying it using the artifactId and you need to include a colon in front):
mvn package -pl :artifactId
-Maven Installing Jar in Local Repo.
mvn install:install-file -Dfile=
mvn install:install-file -Dfile= -DpomFile=
mvn install:install-file -DgroupId=org.springframework.roo -DartifactId=org.springframework.roo.osgi.bundle -Dversion=1.2.4.RELEASE

Tuesday, April 30, 2013

Recursively List File Name in DOS/Windows

for /r %i in (*) do @echo %~ni >> list.txt

If you end up wanting the extensions, change %~ni to %~nxi

To use in a batch file, change all the % to %%

Wednesday, April 24, 2013

Date and Time Method

    public static String getCurrentDateTime(){
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH.mm.ss");
        Date date = new Date();
        log.info("Current System Date_Time as : " + dateFormat.format(date));
        return dateFormat.format(date);
    } // end of method
   
    public static void storeCurrentDateTimeinPropertyFile(String currentDateTime) {
        //long hour = 3600 * 1000; // 3600 seconds times 1000 milliseconds
        Properties prop = new Properties();
        try {
            // set the properties value
            prop.setProperty("CurrentDateTime", currentDateTime);
            prop.store(new FileOutputStream(getCurrentDirPath()+"//" +"time.properties"), null);
            log.info("store current start_Date_Time to property file : " +  currentDateTime);
        } catch (IOException ex) {
            ex.printStackTrace();
        } // end of catch
    }
   
    // load from property file and parse to date and then increment it by one second
    public static void incDateTimeByOneSecoud(){
    try {
        Properties prop = loadProperty();
        String startTime = prop.getProperty("StartDateTime");
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH.mm.ss");
        Date date = dateFormat.parse(startTime);
        Date newDt = new Date(date.getTime() + 1000L);
        String newDT = dateFormat.format(newDt).toString();
        prop.setProperty("StartDateTime", newDT);
        prop.store(new FileOutputStream(getCurrentDirPath()+"//" +"time.properties"), null);
        log.info("increment C1_START_DATE_TIME  to property file : " + newDT);
        } catch (Exception ex) {
            ex.printStackTrace();
        } // end of catch
    } // end of method

 // load property file if missing then create new and reload it
private static Properties loadProperty(){
        Properties prop = new Properties();
        try {
            // load a properties file from class path, inside static method
            //prop.load(SyncTimeProperty.class.getClassLoader().getResourceAsStream("config.properties"));
             prop.load(new FileInputStream("time.properties"));
            log.info("loading property file : ");
        } catch (IOException ex) {
            ex.printStackTrace();
            log.info("Creating new property file : ");
            storeCurrentDateTimeinPropertyFile(getCurrentDateTime());
            return loadProperty();
        } // end of catch
        return prop;
    } // end of method

/shift the given Date by exactly 24 hours. 
private final static long MILLISECONDS_PER_DAY = 1000L * 60 * 60 * 24; 
public static void shiftDate(Date d) {
    long time = d.getTime();
    time += MILLISECONDS_PER_DAY;
    d.setTime(time);
}

Thursday, April 18, 2013

Generating JAX-WS Portable Artifacts and Webservices Tutorials

wsimport tool (client):
The wsimport tool is used to parse an existing Web Services Description Language (WSDL) file and generate required files (JAX-WS portable artifacts) for web service client to access the published web services.
Example :
  • clientjar option for wsimport, which automatically downloads the wsdl and schema and packages all the generated client-side artifacts into a jar file.
    wsimport -clientjar wsclient.jar http://example.com/service/hello?WSDL
  • wsimport -keep wsdl_URL
  • wsimport -keep -verbose -wsdllocation /META-INF/wsdl/MyService.wsdl
  • wsimport -verbose -p ec.estat.edit -target 2.1 -Xnocompile editWS.wsdl
wsgen tool (server):
The wsgen tool is used to parse an existing web service implementation class and generates required files (JAX-WS portable artifacts) for web service deployment.
Example:
  • Generating service endfpoints artifacts (target\classes>) folder
    wsgen -verbose -keep -cp com.jini.HelloWorld
  • Generating wsdl
    wsgen -verbose -keep -cp . com.jini.HelloWorld -wsdl

Tutorials:
- JAX-WS Tutorial @ Mykyong
- Developerworks -Top 10 SOA and web services tutorials and articles
- Oracle - Creating and Using SOAP Message Handlers
- Pedict 8
- JAXWS using Maven
Online Webservice:
- Online service registry
- WebserviceX.NET

Artifacts and Client Generation using Maven:
    <build>
        <finalName>HelloService</finalName>
        <plugins>
            <plugin>
                <groupId>org.jvnet.jax-ws-commons</groupId>
                <artifactId>jaxws-maven-plugin</artifactId>
                <version>2.1</version>
                <executions>

                    <execution>
                        <id>generate-wsdl</id>
                        <phase>process-classes</phase>
                        <goals>
                            <goal>wsgen</goal>
                        </goals>
                        <configuration>
                            <sei>com.jini.service.Hello</sei>
                            <sourceDestDir>src/main/java</sourceDestDir>
                            <keep>true</keep>
                            <genWsdl>true</genWsdl>
                            <wsdlDirectory>src/resources</wsdlDirectory>
                            <verbose>true</verbose>
                        </configuration>
                    </execution>

                    <execution>
                        <id>generate-stubs</id>
                        <phase>process-classes</phase>
                        <goals>
                            <goal>wsimport</goal>
                        </goals>
                        <configuration>
                            <keep>true</keep>
                            <verbose>true</verbose>
                            <wsdlDirectory>target/jaxws/wsgen/wsdl</wsdlDirectory>
                            <wsdlFiles>
                                <wsdlFile>
                                    HelloService.wsdl
                                </wsdlFile>
                            </wsdlFiles>
<!--                         <wsdlLocation>http://localhost:8080/test</wsdlLocation> -->
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>