Thursday, March 20, 2014

HTTP GET/POST Example

Following example can be used to send HTTP GET or POST request to web server using JAVA application.

// HTTP GET request
private void sendGet() throws Exception {

String url = "http://seguide.blogspot.com";

HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet(url);

request.addHeader("User-Agent", USER_AGENT);

HttpResponse response = client.execute(request);

System.out.println("\nSending 'GET' request to URL : " + url);
System.out.println("Response Code : " + 
                       response.getStatusLine().getStatusCode());

BufferedReader rd = new BufferedReader(
                       new InputStreamReader(response.getEntity().getContent()));

StringBuffer result = new StringBuffer();
String line = "";
while ((line = rd.readLine()) != null) {
result.append(line);
}

System.out.println(result.toString());

}

// HTTP POST request
private void sendPost() throws Exception {

String url = "http://seguide.blogspot.com";

HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(url);

post.setHeader("User-Agent", USER_AGENT);

List urlParameters = new ArrayList();
urlParameters.add(new BasicNameValuePair("name", "sujith"));
urlParameters.add(new BasicNameValuePair("pass", "abc123"));
urlParameters.add(new BasicNameValuePair("param1", "param1"));
urlParameters.add(new BasicNameValuePair("param2", "param2"));

post.setEntity(new UrlEncodedFormEntity(urlParameters));

HttpResponse response = client.execute(post);
System.out.println("\nSending 'POST' request to URL : " + url);
System.out.println("Post parameters : " + post.getEntity());
System.out.println("Response Code : " + 
                                    response.getStatusLine().getStatusCode());

BufferedReader rd = new BufferedReader(
                        new InputStreamReader(response.getEntity().getContent()));

StringBuffer result = new StringBuffer();
String line = "";
while ((line = rd.readLine()) != null) {
result.append(line);
}

System.out.println(result.toString());

}

Wednesday, March 19, 2014

Change permission for user in Linux

Following set of commands can be used to change the permission of a user.

Change directory
           sujithde@ewaps1:~> cd delight/

Change permission for user oracle
           sujithde@ewaps1:~/delight> setfacl –R -m user:oracle:rwx .

Add user a mask
           sujithde@ewaps1:~/delight> setfacl –R -m mask:rwx .

Note : –R will recursively change permission for sub folders. 

Wednesday, March 5, 2014

Run SQL script in Background in MySQL server

Following command can be used to run SQL script in background in MySQL server.

mysql --user=sujith --pass=123suji --database=postdb --host=localhost script_file results.out &

This will return output of script  to result.out file.

Tuesday, March 4, 2014

JAVA 8 Lambda Expression

Following example explain how Lambda Expression can be implemented in JAVA 8.

    interface MathOperation {

        int execute(int a, int b);
    }

    public int calculate(int a, int b, MathOperation op) {
        return op.execute(a, b);
    }

    public static void main(String[] args) {
        Lamda lamda = new Lamda();
        MathOperation addition = (a, b) -> (a + b);
        MathOperation substract = (a, b) -> (a - b);

        System.out.println(lamda.calculate(4, 5, addition));
        System.out.println(lamda.calculate(4, 5, substract));
    }