วันพุธที่ 28 กันยายน พ.ศ. 2554

HTTPS Java

HTTPS server on Windows
1. download "Win32 Binary including OpenSSL 0.9.8r"
(file.key = private key, file.cert = self signed certificate + public key)
3. re-config https, restart apache and verify


HTTPS client by Java
1. Since java truststore does not use the same format, then we have to convert self-signed certificate from server using this command
c:\> keytool -import -file file.cert -storepass $PASS -keystore server.keystore

2. Error:
Exception in thread "main" javax.net.ssl.SSLHandshakeException: java.security.cert.CertificateException: No subject alternative names present

3. Add anonymous class :

con.setHostnameVerifier(new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
return true;
}
});

----- java code for https client -----

import java.net.*;
import java.io.*;
import javax.net.ssl.*;

public class HttpsClient {

public static void main(String[] args) throws Exception {
System.setProperty("javax.net.ssl.trustStore", "server.keystore");
String httpsURL = "https://147.127.xxx.xx/serverCertificate";
URL myurl = new URL(httpsURL);


HttpsURLConnection con = (HttpsURLConnection) myurl.openConnection();
con.setHostnameVerifier(new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
return true;
}
});
con.connect();
InputStream ins = con.getInputStream();
InputStreamReader isr = new InputStreamReader(ins);
BufferedReader in = new BufferedReader(isr);
String inputLine;

while ((inputLine = in.readLine()) != null) {
System.out.println(inputLine);
}

in.close();
}
}

Note: Just use for testing, it is not suitable for real deployment.

วันอังคารที่ 27 กันยายน พ.ศ. 2554

Event Java


My Simple example with 4 classes for understanding how event and eventListener work together.

------------------------------
package myevents;
import java.util.EventObject;

public class MyEvent extends EventObject {

private static final long serialVersionUID = 5736098458773344230L;
private Object data;

public MyEvent(Object source, Object _data) {
super(source);
data = _data;
}

public Object getData() {
return data;
}
}
-------------------------------
package myevents;
import java.util.EventListener;

public interface MyEventListener extends EventListener {
public void myEventHappend(MyEvent e);
}
------------------------------
package myevents;
public class PracticalInterface implements MyEventListener {

@Override
public void myEventHappend(MyEvent e) {
System.out.println((String) e.getData());
}
}
------------------------------
package myevents;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextArea;

public class AppProgram extends JFrame {

private Vector listeners = new Vector();
private JTextArea _tar = new JTextArea(22,40);
private JButton _bt1 = new JButton("Yeah");
private JButton _bt2 = new JButton("Yoo");

public AppProgram() {
super("Application");
setLayout(new FlowLayout());
_bt1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
shootEvent("Yeah !!");
}
});
_bt2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
shootEvent("Yoo !?");
}
});
this.add(_tar);
this.add(_bt1);
this.add(_bt2);
}

public void addListener(MyEventListener l) {
listeners.add(l);
}

public void removeListener(MyEventListener l) {
listeners.remove(l);
}

public void shootEvent(String text) {
MyEvent me = new MyEvent(this, text);
if (listeners.size() > 0) {
((MyEventListener)listeners.firstElement()).myEventHappend(me);
}
}

public static void main(String[] args) {
AppProgram app = new AppProgram();
PracticalInterface lis = new PracticalInterface();
app.addListener(lis);
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
app.setSize(400,600);
app.setVisible(true);
}

}



จาก 
http://windygallery.wordpress.com/2010/02/18/events-and-eventlistener/

วันพุธที่ 14 กันยายน พ.ศ. 2554

Keystore for TLS/SSL

create keystore (public/private key):
keytool -genkeypair -alias certificatekey -keyalg RSA -validity 7 -keystore keystore.jks

verify keystore:
keytool -list -v -keystore keystore.jks

create self signed certificate:
keytool -export -alias certificatekey -keystore keystore.jks -rfc -file selfsignedcert.cer

import certificate to trust store (public key):
keytool -import -alias certificatekey -file selfsignedcert.cer -keystore truststore.jks

verify trust store:
keytool -list -v -keystore truststore.jks


ตัวอย่าง code TLS/SSL Java

java -Djavax.net.ssl.keyStore=mySrvKeystore -Djavax.net.ssl.keyStorePassword=123456 EchoServer

เราสามารถใช้ option -Djavax.net.ssl.keyStore=... ใน netbeans ได้ โดยกำหนดที่ Run parameter และ ป้อนค่า options นี้ ลงไปใน VM options (ไม่ใช่ใน arguments อันนี้ สำหรับ args[] ของฟังก์ชัน main) จากนั้น ให้เก็บ file keyStore ใน project folder (parent ของ src)




วันอังคารที่ 26 กรกฎาคม พ.ศ. 2554

UML on NB 6.9

Download:

UML plugin for NB6.9

Installation Instructions:
  1. Extract the contents of the archive into your Netbeans installation directory.
  2. In your Netbeans directory you should now have a folder called UML.
  3. Restart or open Netbeans.
  4. To verify its working, try to create a new project. In the list of project types, there should be the option to create a UML project. If you have that option, then it is all working!

Usage:

  1. Open an existing Netbeans project.
  2. In the projects pane, right click on the project your working on and click Reverse Engineer.
  3. You will be presented with a window on what to reverse engineer. Choose only the source files and tell it to create a new UML project for you.
  4. It will tick away for a little while, then you should now have a new UML project in the projects pane on the left.
  5. The UML project should now be full of all the components your using.
  6. Expand your UML project, then expand the Model.
  7. You should have folders for each package thats in the Netbeans project you generated from. Example, I had one package called assign2, so i had one folder called assign2 in the UML project. Plus several other files that arent important.
  8. Right click this folder and choose Create Diagram From Selected Elements.
  9. Choose to create a Class Diagram, and push Finish.
  10. You might be prompted with a message saying: The selected element has one or more scoped elements. Do you want to include the scope elements on the diagram in addition to the selected element?
  11. Click Yes.
  12. You should now have a completed UML class diagram.

Notes:

  • The modeler does a good job of presenting the data, however it tends to push it to the right side of the window. Which wastes a lot of printing room. The only way i found to fix this, was to move every element manually to the middle. It seems the middle is the best place for printing.
  • I also couldn’t find a way to save the UML diagram as an image or a pdf. So I used Cute PDF Writer to do the job!

วันพุธที่ 13 กรกฎาคม พ.ศ. 2554

IPSec Windows XP

การ Setup IPSec บน Windows XP

การทำงานก็คล้าย ๆ กับ Windows 7 แค่สลับหมายเลข IP กัน และ ใส่ Security parameter ให้ตรงกัน เป็นอันใช้ได้ ตามรูปครับ

เริ่มต้น เปิด Run -> secpol.msc

แล้วทำตามรูปเลยครับ






















IPSec Windows7

Setup IPSec on Windows 7

วิธีการใช้งาน IPSec ระหว่าง Windows 7 และ Windows XP แบบ ง่าย ๆ
โดย กำหนด ให้ IP 147.127.240.90 (Windows7) และ 147.127.240.91 (Windows XP) โดยใช้ preshared-key ในการเข้ารหัส ทั้งส่วนของ AH และ ESP

เข้าไปที่ Local security policy โดย -> Run (Windows+R) -> secpol.msc

เริ่มสร้าง security policy ก่อน



ไม่ต้อง activate default response





IP Tunnel ใช้ในกรณีที่ ติดต่อระหว่าง IP Private กับ IP Public










กำหนด Mirrored สำหรับ การส่ง packet กลับมา





ตรงนี้สำคัญต้อง กำหนดให้เหมือนกัน ทั้ง 2 ฝั่ง


แบบ ง่ายที่สุด กำหนด preshared key


สั่ง enable IPSec


เห็น message IPSec วิ่งแล้ว เย้ :-)

วันพฤหัสบดีที่ 16 มิถุนายน พ.ศ. 2554

POJO Cooker

Pojo Cooker เป็น Data oriented framework ที่ใช้สำหรับสร้าง share data กันหลาย ๆ platform หลาย ๆ ภาษา โดยใช้ concept ของ Object XML mapping ซึ่งมีเป้าหมายหลัก ๆ ดังนี้
  • Cross-platform exchanges
  • Multi-languages
  • Multi-devices
  • Persistency layer
  • Full text search
  • Zero-conf
โครงการนี้ มีส่วนประกอบ ส่วนสำคัญ ๆ คือ Pojo, Fridge โดยที่ Pojo คือตัว โครงสร้าง XML (key/value) แล้วเราสามารถป้อน data เข้าไปเอง และ สามารถเก็บลงใน Fridge (หรือ มองว่าเก็บข้อมูลลง disk ผ่านการ persistence) นั่นเอง

Phase แรก จะเริ่มใช้งานกับ Java เป็นหลัก จากนั้น ก็จะมีส่วนที่พัฒนาของ Php, Flex, iPhone รวมไปถึง Android ด้วย ซึ่งเราสามารถ generate และ share data ผ่าน RESTlet Web framework

ส่วนสุดท้ายของโครงงานนี้คือ เราสามารถค้นหา ข้อมูลใน object (Pojo) ที่เราเก็บไว้ใน fridge ได้ ผ่านการทำงานของ Berkeley DB ซึ่งมีขนาดเล็ก และ ทำงานได้อย่างรวดเร็ว

เนื่องจากโครงการนี้ ถูก implement โดยใช้ Maven เราจึงต้องมาเริ่มติดตั้ง Maven กันก่อน ดังนี้


การติดตั้ง Maven (3 นาที)
1) ติดตั้ง maven โดย download จาก http://maven.apache.org/download.html แล้วก็ unzip ออกมา
2) กำหนด PATH ของตัวแปร ให้เรียบร้อย ตามนี้ http://maven.apache.org/download.html#Installation
3) ทดสอบดู โดยสั่ง mvn --version ถ้าเป็น version ตรงตามที่เรา download มา แสดงว่า OK. เสร็จ


หลังจากติดตั้ง Maven เรียบร้อยแล้ว ก็มาติดตั้งตัวโครงงาน



การติดตั้ง PojoCooker (1 นาที)
เพิ่ม config ตาม http://www.pojocooker.net/install.html
ใน MAVEN_HOME/conf/settings.xml

การใช้งาน PojoCooker
- mvn pojocooker:version
-
mvn pojocooker:kitchen
- cd kitchen (จะมี file pom.xml)
- 
mvn pojocooker:java
- mvn pojocooker:server

เปิด web browser เข้าไปที่ http://locahost:8080/ เป็นอันเสร็จพิธี
(อ้างอิง http://www.pojocooker.net/start.html)

การเพิ่ม Own POJO:
เข้าไปที่ .\PojoKitchen\src\main\java\org\pojocooker\demo\pojokitchen\pojos
จะมี file ที่ชื่อว่า GeoPlace.java เป็นตัวอย่างของ Pojo เราสามารถสร้าง Pojo ขึ้นใหม่ได้ โดยเลียนแบบจาก GeoPlace.java  ข้อควรระวังคือ ห้ามใช้ build-in variable type ต้องใช้ Wrapper type เท่านั้น หลังจากที่ เข้าไป define Pojo แล้ว  ให้ทำการ compile ใหม่ โดยสั่ง mvn pojocooker:java , mvn pojocooker:java ตามลำดับ
เข้าไปที่ http://localhost:8080/pojo เราจะ เห็น file POJO ที่เรา add ไว้ กดที่ 1 ใต้ XML Sample ก็จะได้ ตัวอย่าง XML เราสามารถแก้ไขข้อมูล แล้วกด save ลงใน server ได้ จากนั้น เราสามารถ search ข้อมูลที่เรา add ไว้ (ได้ผลลัพธ์เป็น XML) และ สามารถใช้ ID ของ object จะส่ง output เป็น XML มาให้ ตาม ID ของ object ที่เราต้องการหา

วิธีลบ Own Pojo
mvn pojocooker:clear (เพื่อทำการลบ java source files ออก)
mvn clear  (เพื่อลบ class file ออก)  
 แล้วจึงสั่ง recompile ใหม่ ด้วย mvn pojocooker:java 

สรุป
คิดว่า framework นี้ ออกแบบมาให้ share XML data ผ่าน
RESTlet web framework (XML output)