Wednesday, September 26, 2012

Using HTMLUnit (Webcrawler Libs) For Login Into A Website



Notes :
  • Note that my HTMLUnit example below is not a web crawler scenario, because we have to know first the specific structure and fields of the page of a website that we try to log.
  • Note that HTMLUnit consume lot of memory. It's better to use other mechanism (not a crawler libs) such as Apache HTTPClient to complete the same task.
Preparation :
  • First, download HTMLUnit jars in here: HTMLUnit 2.12 
  • Put all the jars into your project's classpath.

Website login page source (login.html) :

 
Login codes:
That' its. Hope MyNotes helps.

Wednesday, September 5, 2012

Lambdaj - Usage Examples

Lambdaj simplify your task to manipulate Collection in Java programming. Convert from List into Map, select and filtering collection, joining String, grouping, etc, those are Lambdaj expertise. 

To use it you have to import Lambdaj library into your classpath and then import Lambda as a static class into your java program like this :
import static ch.lambdaj.Lambda.*;

Dummy Class (Person.java)
class Person {
private int id;
private String name;
private int age;
private String city;

public Person() {}

public Person(int id, String name, int age, String city) {
this.id = id;
this.name = name;
this.age = age;
this.city = city;
}

public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getDetails() {
return "ID= "+id+"; Name= "+name+"; Age= "+age+"; City= "+city;
}
}



And these are my Lambdaj usage examples :

USE JOIN FOR BUILDING QUERY
List<String> list = Arrays.asList("hari1","hari2","hari3","hari4");
//-------------------------------------------------------------------------------------------
String result = join(list,"','");
String query = "select t from T_PERSON t where "+(result.length()>0 ? "t.name in ('"+result+"')" : "0=0");
//-------------------------------------------------------------------------------------------
System.out.println(query);


CONVERT LIST OBJECT INTO MAP
List<Person> persons = Arrays.asList(
new Person(13,"Steve",22,"London, UK"),
new Person(25,"Greg",28,"New York, USA"),
new Person(5,"Emily",22,"Bali, Indonesia"),
new Person(9,"Malih",14,"Jakarta, Indonesia"));

//-------------------------------------------------------------------------------------------
Map<Integer,Person> personMap = index(persons,on(Person.class).getId());
//-------------------------------------------------------------------------------------------
System.out.println("Person name with ID 5: " + personMap.get(5).getName());


EXTRACTING FIELDS INTO NEW LIST
List<String> personNames = extract(persons, on(Person.class).getName());
//-------------------------------------------------------------------------------------------
System.out.println("personNames: "+personNames);


SELECTING PERSON LIST WHERE CITY CONTAINS "Indonesia"
List<Person> filteredPerson = select(persons, having(on(Person.class).getCity(), StringContains.containsString("Indonesia")));
//-------------------------------------------------------------------------------------------
for (Person person : filteredPerson)  
            System.out.println(person.getDetails());



COMBINE EXTRACT AND SELECT
List<String> filteredPersonNames = extract(select(persons,having(on(Person.class).getAge(), IsEqual.equalTo(22))), on(Person.class).getName());

//-------------------------------------------------------------------------------------------
System.out.println("filteredPersonNames: "+filteredPersonNames);


GROUPING
Group<Person> groupAgeOfPerson = group(persons,by(on(Person.class).getAge()));
Set<String> groupAgeKeys  = groupAgeOfPerson.keySet();
//-------------------------------------------------------------------------------------------
System.out.println("groupAgeKeys: "+groupAgeKeys);
System.out.println("groupOfAge: "+groupAgeOfPerson.find(22));


for (String ageKey : groupAgeKeys) 
for (Person person : groupAgeOfPerson.find(ageKey))
System.out.println(person.getDetails());


Tuesday, September 4, 2012

Convert Wallet Into Keystore (OHS to Weblogic SSL) - Verse 2

Completing my previous notes about Converting Wallet into Keystore, this is other steps to do the convert and it is more simple than before. For Identity Keystore, I create an empty keystore, actually I create a keystore with dummy certificate and then delete the dummy certificate. And for the Trust Keystore I'm using Firefox browser convert the CAs.


So, the steps:

IDENTITY KEYSTORE

CREATE AN EMPTY JKS KEYSTORE
keytool -genkey -alias dummy -keystore identity_keystore.jks
keytool -delete -alias dummy -keystore identity_keystore.jks

IMPORT wallet_server.p12 INTO identity_keystore.jks
keytool -v -importkeystore -srckeystore wallet_server.p12 -srcstoretype PKCS12 -destkeystore identity_keystore.jks -deststoretype JKS


TRUST KEYSTORE

For CAs (which mine is in DER format), I tried a different way. I'm using Firefox browser to convert DER into PEM format.

IMPORT CA CERTIFICATES INTO FIREFOX
options -> Advanced -> Encryption -> View Certificates -> Import 

EXPORT THE CAs INTO PEM FORMAT (X.509 Certificate - PEM)

CREATE TRUSTED CA - CERTIFICATE CHAIN KEYSTORE
keytool -import -trustcacerts -file Inter1-CA.pem -keystore trust_keystore.jks -alias inter1
keytool -import -trustcacerts -file Inter2-CA.pem -keystore trust_keystore.jks -alias inter2
keytool -import -trustcacerts -file Root-CA.pem -keystore trust_keystore.jks -alias root


That's it, simple than the previous. Of course, there are many other ways to complete the task, but hope mine helps.

Monday, September 3, 2012

Convert Wallet Into Keystore (OHS to Weblogic SSL) - Verse 1

Recently I've been trying to migrate my SSL configuration from OHS (Oracle HTTP Server) to Weblogic Server. For information, OHS is using Wallet to store certificates for SSL authentication and at the other side, Weblogic using Keystore (Identity and Trusted Keystore). So, I need to convert my Wallet (PKCS12 format) into Keystore.

My certificates description :
  • One Root CA (Root-CA)
  • Two Intermediate CAs (inter1-CA and inter2-CA)
  • A pair of certificate (public and private key) for Server
  • inter1-CA issued by Root-CA
  • inter2-CA issued by inter1-CA
  • and a Wallet contain server certificate which issued by inter2-CA

Identity and Trust Keystore as Oracle documentation describes, Identity contains public and private key of the server and Trust contains trusted CA Certificates (mine is a chain: Root-CA, inter1CA and inter2-CA).

To complete this task I'm using this tools:


More description:

  • My certificates are in DER format
  • Keytool only accept certificate in PEM format (hope I'm not wrong), so I have to convert it first before import it into the keystore
  • Identity Keystore must fill with server public and private key, that's way I need ImportKey.class to do this task
  • ImportKey class only accept DER format, so I have to convert it too using Openssl

So here it is, the steps:

IDENTITY KEYSTORE

SPLIT WALLET (PKCS12) INTO KEY AND CERT

openssl pkcs12 -nocerts -in wallet_server.p12 -out wallet_serverkey.pem -nodes

openssl pkcs12 -clcerts -nokeys -in wallet_server.p12 -out wallet_servercert.pem

openssl rsa -in wallet_serverkey.pem -out wallet_serverkey2.pem

CONVERT KEY AND CERT PEM FORMAT INTO DER FORMAT
openssl pkcs8 -topk8 -nocrypt -in wallet_serverkey2.pem -inform PEM -out wallet_serverkey.der -outform DER
openssl x509 -in wallet_servercert.pem -inform PEM -out wallet_servercert.der -outform DER
USING ImportKey.class TO IMPORT PRIVATE KEY INTO KEYSTORE
java ImportKey walletkey.der walletcert.der

RENAME KEYSTORE FILE keystore.importKey INTO identity_keystore.jks

CHANGE KEYSTORE PASSWORD importkey INTO somepass
keytool -keystore identity_keystore.jks -storepasswd

CHANGE CERTIFICATE PASSWORD importkey INTO somepass
keytool -keypasswd -keypass importkey -new somepass -alias importkey -keystore identity_keystore.jks

CHANGE ALIAS importkey INTO somekey
keytool -keystore identity_keystore.jks -keyclone -alias importkey -dest somekey

DELETE OLD ALIAS importkey
keytool -keystore identity_keystore.jks -delete -alias importkey 


TRUST KEYSTORE
USING WALLET MANAGER, EXPORT ALL CAs AND SAVE IT INTO YOUR LOCAL DISC
CONVERT CA CERTIFICATES DER TO PEM
openssl x509 -in Inter1-CA.cer -inform DER -out Inter1-CA.pem -outform PEM
openssl x509 -in Inter2-CA.cer -inform DER -out Inter2-CA.pem -outform PEM
openssl x509 -in Root-CA.cer -inform DER -out Root-CA.pem -outform PEM
CREATE TRUSTED CA - CERTIFICATE CHAIN KEYSTORE
keytool -import -trustcacerts -file Inter1-CA.pem -keystore trust_keystore.jks -alias inter1
keytool -import -trustcacerts -file Inter2-CA.pem -keystore trust_keystore.jks -alias inter2
keytool -import -trustcacerts -file Root-CA.pem -keystore trust_keystore.jks -alias root


That's it. Hope my notes helps.

By the way, there's a simple way to do the task, you can find it here.