Wednesday, December 8, 2010

Swing applications to web

This is my first post on WEBc Developers Blog :)

Hi All, This is my first post on Developers blog of WEBc Project. I'm proud to be a team member of the such a big and research based project. First of all, I want to say litle bit about me. :)

In my profession, I'm a java based web application developer. But I have a big interest in law-level languages like C and C++. Because those languages are very powerful than high level languages. Currently I'm doing some researches in compilers, web services and middlewares.

These days I'm working on a specific sub project, which wants to display swing based stand-alone applications on web browser and working as real swing application on web. I did a survey about existing developments on this area and I found some proper implementations.

Actually our main target is display swing applications using AJAX/HTML scripts on web browser and handle communications with front-end and back-end using HTTP-Requests and HTTP-Responses. Most important thing is, It must be efficient communication between web browser and server.

There are several projects for this area. Some are open-source and some are proprietary. Ajax Swing is a good commercial product for this. AjaxSwing is supports for the awt and swing based application. this project is based on free, Web-Cream project. It has both free and commercial versions. Using Web-Cream, we can convert awt application to AJAX/HTML based web sites automatically. Most Important thing is, It converts almost all of awt components such as button,table,frames to Ajax/Javascrypt based applications.

Another one of the good project is XML11, An Abstract window protocol. This project is fully opensource project with GPL licence. XML11 is researched by Arno Puder and who is a good researcher on these areas. This project is also using AJAX/Javascript based user interface to display awt applications on user's web browser. The main idea is that a web browser serves as a generic client just like an X-Server can render any user interface. This is bit different from Web-Cream project. Most important thing is, using XML11 protocol to communicate with server.


Architecture of XML11

XML11 has several important components to work successfully .
  • SessionManager
  • Awt Manager
  • Model Manager
  • event Manager
Above components are most important to manage AWT applications as AJAX/Javascript applications on web. you can find out more details from XML11 Documentation.

I found several another solutions called SwingWeb and this also a Opensource project. Bringing to Swing to web is an another solution for this problem. Unfortunately I hadn't enough time to search all those. Next time I will publish a more detailed article.

Cheers.!!

Monday, December 14, 2009

How to integrate google wave for your blog.

I have been interested in google wave for some time now. once I want to integrate google wave for this blog post. I surfed the Internet but I didn't found a proper solution. Google wave embed API is not warking for blogspot blog.

Here is my Solution. ( Actually I used wave API)

1. Go to Layout -> Edit HTM in your Blogspot Blog.
Insert this code to your between <head> elements of your Blog.

<script type="text/javascript" src="http://wave-api.appspot.com/public/embed.js">
</script>
<script type="text/javascript">
function waveScrypt() {

var div = document.getElementsByTagName('div');
for (var i = 0; i < div.length; i++) {
if( div[i].id.substr(0, 5) == 'wave_' ) {
var id = div[i].id.substr(5, div[i].id.length-5);

var wave = new WavePanel('https://wave.google.com/wave/');
wave.setUIConfig('white', 'black', 'Arial', '13px');
wave.loadWave('googlewave.com!w+' + id);
wave.init(document.getElementById(div[i].id));
}
}
}
</script>


2. find the <body> Tag and replace it as like this.

<body onload="waveScrypt()">

3. Now you can simply add a wave to your posting area using this code.

<div id="wave_waveid" style="width: 400px; height: 420px"></div>

waveid is your wave id which you want to publish.

Example:
<div id="wave_GCW5356" style="width: 400px; height: 420px"></div>

Sunday, November 29, 2009

How to convert PublicKey as a String and How to decode it.

I was suffered with a big trouble when I am going to develop a simple secure chat application. I have to convert it as a String and send to third party. Public Key can not send through socket because of that is in as a object. Then I'm surf the Internet and their are no proper answer to send it as a String. Finally I found a method that can can Convert it.


I've used two steps to Encode Public key to String.
1. Convert it to Byte array.
byte array = publicKey.getEncoded();
2. Convert Byte array to String.
BASE64Encoder encoder = new BASE64Encoder();
string = encoder.encode(byte array);
In this step I used BASE64Encoder to encode byte stream to String. BASE64Encoder is a proprietary software of Sun. you can see that while compile source file ;)


Then we can distribute a public key to the third party.

Decode:

This can do as two steps.

1. Decode to byte stream using BASE64Decoder
BASE64Decoder decoder = new BASE64Decoder();
= decoder.decodeBuffer();
2. Convert is to Public Key using X509EncodedKeySpec
X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(sigBytes2);
KeyFactory keyFact = KeyFactory.getInstance("RSA", "BC");
pubKey2 = keyFact.generatePublic(x509KeySpec);




Here is the Program.


import java.security.*;
import java.security.spec.*;
import javax.crypto.Cipher;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;


public class RSAEncryption {
public static void main(String[] args) throws Exception {
RSAEncryption en = new RSAEncryption();
en.DistributePubKey();
}

Key pubKey2;

public void DistributePubKey() throws Exception {
Cipher cipher = Cipher.getInstance("RSA/NONE/PKCS1Padding", "BC");
//SecureRandom random = Utils.createFixedRandom();

// create the keys
KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA", "BC");
generator.initialize(256, new SecureRandom());

KeyPair pair = generator.generateKeyPair();
Key pubKey = pair.getPublic();
Key privKey = pair.getPrivate();


// Send the public key bytes to the other party...
byte[] publicKeyBytes = pubKey.getEncoded();

//Convert Public key to String
BASE64Encoder encoder = new BASE64Encoder();
String pubKeyStr = encoder.encode(publicKeyBytes);




//Convert PublicKeyString to Byte Stream
BASE64Decoder decoder = new BASE64Decoder();
byte[] sigBytes2 = decoder.decodeBuffer(pubKeyStr);



// Convert the public key bytes into a PublicKey object
X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(sigBytes2);
KeyFactory keyFact = KeyFactory.getInstance("RSA", "BC");
pubKey2 = keyFact.generatePublic(x509KeySpec);




// encryption step
cipher.init(Cipher.ENCRYPT_MODE, pubKey2, new SecureRandom());
byte[] cipherText = cipher.doFinal(input.getBytes());
System.out.println("cipher: " + Utils.toHex(cipherText));

// decryption step
cipher.init(Cipher.DECRYPT_MODE, privKey);
byte[] plainText = cipher.doFinal(cipherText);
System.out.println("plain : " +new String(plainText));
}
}


Encryption code by Dr. Nandika kasun.

Friday, November 27, 2009

A simple socket based chat application.

Run Server. java
after that run Client.java and give a IP address using command line.

Server.java


/**
*
* @author janith
*/

import java.net.*;
import java.io.*;

public class Server {
public static void main(String[] args) throws IOException{
int PORT = 8134;
InputStream inStream;
DataInputStream inDataStream;
OutputStream outStream;
DataOutputStream outDataStream;
String message="";
String received="";

System.out.println("Chat Server Started");

ServerSocket sock = new ServerSocket(PORT);
Socket conn = sock.accept();
do{
inStream = conn.getInputStream ();
inDataStream = new DataInputStream ( inStream );
message = inDataStream.readUTF();
System.out.println("Client sent: "+message);

DataInputStream dis = new DataInputStream(System.in);
message = dis.readLine();

outStream = conn.getOutputStream();
outDataStream = new DataOutputStream (outStream);
System.out.println("Enter your message here: ");
outDataStream.writeUTF(message);
}while(!message.equals("bye"));
conn.close();
}

}


Client.java



/**
*
* @author janith
*/

import java.io.*;
import java.net.*;

public class Client{
public static void main(String[] args) throws IOException {

int PORT = 8134;
InputStream inStream;
DataInputStream inDataStream;
OutputStream outStream;
DataOutputStream outDataStream;
String message = "";

InetAddress host = InetAddress.getLocalHost();
String diffHost = args[0];
Socket sock = new Socket(diffHost,PORT);
System.out.println("Chat Client Started");
do{
System.out.println("Enter your message here: ");
DataInputStream dis = new DataInputStream(System.in);
message = dis.readLine();
outStream = sock.getOutputStream();
outDataStream = new DataOutputStream (outStream);
outDataStream.writeUTF(message);

inStream = sock.getInputStream ();
inDataStream = new DataInputStream ( inStream );
message = inDataStream.readUTF();
System.out.println("Server Sent: "+message);
}while(!message.equals("bye"));
}
}

Saturday, October 31, 2009

welcome

Hi, This is my first blog post of my "Code of Conduct" blog. I am living together with much of program languages since my school time. I'm mostly interested in open source programming languages and little bit commercial. Hey, I'm not an expert. ok. hope this place use to share my coding experiences with you.

I have a another blog called linux hug which is about only linux and open source softwares. :)

Hope you Enjoy. :)