Wednesday, May 27, 2015

Set default JAVA path

Sometimes you need to use several JAVA versions in a single machine. But you can't change default running JAVA version. Changing PATH variable doesn't affect for this. You can remove system32 JAVA exe file and reset JAVA path for new version to run newly installed JAVA version.

exe running directory :- C:\Windows\System32
application exe name :- java.exe

Now, try version in command prompt using java -version command. This will show new JAVA version.

Tuesday, May 26, 2015

Set email priority in JAVA

Following code can be used to set priority in email server. This is tested and worked fine.JAVA mail API is used for the development.

// Setting the priority of an email is simply a matter of setting the X-Priority header field.
// Values of 1 through 5 are acceptable
// 1 = highest priority, 3 = normal, and 5 = lowest priority.

// Set the email's priority to high:
email.AddHeaderField("X-Priority","1");

Tuesday, May 19, 2015

CURL Commands

Here are some valuable curl commands.

GET JSON Request
curl -i -H "Accept: application/json" -H "Content-Type: application/json" -X GET http://hostname/resource

GET XML Request
curl -H "Accept: application/xml" -H "Content-Type: application/xml" -X GET http://hostname/resource

POST data
curl --data "param1=value1&param2=value2" http://hostname/resource

For file upload
curl --form "fileupload=@filename.txt" http://hostname/resource

RESTful HTTP Post
curl -X POST -d @filename http://hostname/resource

For logging into a site (auth)
curl -d "username=admin&password=admin&submit=Login" --dump-header headers http://localhost/Login
curl -L -b headers http://localhost/

Reference Site :-

http://stackoverflow.com/questions/14978411/http-post-and-get-using-curl-in-linux
http://www.w3schools.com/json/json_syntax.asp

Wednesday, May 13, 2015

SFTP client with private key authentication

SFTP ( FTP over SSH ) is a secure way for file transferring. In JAVA application, you can use the following code to build a SFTP client with private key authentication. Here instead of password pass phase is used.

For this code, JSch JAVA library is used. Following jar files also used for code building.

  • jsch-0.1.51.jar
  • commons-vfs2-2.0.jar
  • commons-logging-1.2.jar

         JSch jSch = new JSch();
        final byte[] privatekey = readMyPrivateKeyFromFile(); // Private key must be byte array
        final byte[] passPhrase = "pass phase".getBytes();

        jSch.addIdentity(
            "myusername",    // String userName
            privatekey,          // byte[] privateKey 
            null,            // byte[] publicKey
            passPhrase  // byte[] passPhrase
        );
        Session session = jSch.getSession("myusername", "hostname", 22);
        UserInfo ui = new UserInfoImpl(); // UserInfoImpl implements UserInfo
        session.setUserInfo(ui);
        session.connect();
        Channel channel = session.openChannel("sftp");
        ChannelSftp sftp = (ChannelSftp) channel;
        sftp.connect();

// Now you can execute any SFTP command for the sftp channel.
        final Vector files = sftp.ls(".");
        for (Object obj : files) {
            System.out.println(obj.toString());
        }
        sftp.disconnect();
        session.disconnect();

Wednesday, May 6, 2015

SFTPconnection using RSA public key

Password encrypted RSA public key file can be used to authenticate user access to FTP server. Following SFTP command describes how to create secure connectivity using RSA public key with SFTP server.

sftp -o IdentityFile=/home/sujith/.ssh/id_rsa_pub_key sujith@192.168.1.23

I tried this on Linux server and worked fine.

Wednesday, February 18, 2015

Copy directory from server to server in Linux

You can use following scp command to copy directory with files from one server to another.

scp -rp directory sujith@192.176.87.32:./

Above command will copy the 'directory" directory into home folder of sujith at 192.176.87.32.

Monday, January 12, 2015

Check sever status using Socket in JAVA

In some cases, you have to check server status whether server is running or not using telnet command before executing commands on the server application. This code can be used to check whether there is a running something on the given IP and Port.

import java.io.DataInputStream;
import java.io.InputStream;
import java.net.*;

public class Telnet {

    public static void main(String args[]) {
        try {
            String ip =  "192.168.*.*";           
            int port = 23;
            Socket s1 = new Socket(ip, port);
            InputStream is = s1.getInputStream();
            DataInputStream dis = new DataInputStream(is);
            if (dis != null) {
                System.out.println("Connected IP : " + ip + ", Port : " + port);
            } else {
                System.out.println("Connection Invalid.");
            }

            dis.close();
            s1.close();

        } catch (Exception e) {
            System.out.println("Not Connected, check Ip and Port.");

        }

    }
}