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));
    }

Thursday, February 20, 2014

Add words to file in linux

Following Linux commands can be used to add words to file.

1) open file in Linux vi editor
2) ESC and type following command as you referred

:/s/^/0/g
add zero(0) to in front of each line
^ defines the start of the line

:/s/$/0/g
add zero(0) to end of each line 
$ defines the end of the line

:/s/718/0/g
replace "718" with zero(0)

Wednesday, January 1, 2014

Update MySQL table appending text

Following SQL command can be used to update MySQL table with appending text "text" for field "field".

update table set field=concat('text',field);

Thursday, December 26, 2013

Execute procedure in informix

Following command can be used to execute procedure in informix database server.
In informix "dbaccess" shell, execute the following command. This will run the "this_is_it" procedure with three parameters "1", "2" and "3".

execute procedure this_is_it ('1','2','3');

create procedure this_is_it(this CHAR(3), that CHAR(4), other CHAR(3))
select * from inff_charge into temp this_is_it;
end procedure;