3 Aug 2015

Configuring Virtual Hosts as Proxy Server on Linux Mint with Apache2.4

Configuring Virtual Hosts as Proxy Server on Linux Mint with Apache2.4

To configure a virtual host as proxy server we use Apache as a virtual host and as a proxy server to redirect requests to JBOSS application server. In this regard we separate the  whole process into multiple steps as below

  1. Installing Apache
  2. Configuring Virtual Hosts in Apache
  3. Enabling and configuring Proxy server in Apache

1. Installing Apache

              As specified in the header we are doing the configuration in Mint, so we user apt-get to install Apache2 as below

#apt-get install apache2

2. Configuring Virtual Hosts in Apache

        Copy the following block in to the file "/etc/apache2/sites-enabled/000-default.conf"

         <VirtualHost *:80>
                  ServerName www.example.com
                  ServerAdmin webmaster@localhost
                  DocumentRoot /var/www/html
         </VirtualHost>

      "*" in the above configuration implies all the requests to this host i.e all the requests to this server with 80 port number will be served by this virtual host.
      "ServerName" directive specifies the name server identifies itself with.

3. Enabling and configuring Proxy server in Apache

                To enable Proxy in Apache use the command a2enmod as shown below

#a2enmod
        The above line will show you modules that are supported by Apache and lets you select the modules to be enabled. So enter the following options to enable 
proxy proxy_ajp proxy_http rewrite deflate headers proxy_balancer proxy_connect proxy_html

              To configure the above Virtual host as proxy change the Virtual Host block to the following


<VirtualHost *:80>
    ServerName www.example.com
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html
   
    ProxyPreserveHost On
    ProxyPass /examples http://localhost:8080/examples
    ProxyPassReverse /examples http://localhost:8080/examples 
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
         "ProxyPreserveHost" directive uses the incoming request's header for proxy redirections.
         "ProxyPass" directive maps /examples to our local server URL "http://localhost:8080/examples"
          "ProxyPassReverse" adjusts the response header URL sent from the reverse proxied server.


Reference docs:

https://www.digitalocean.com/community/tutorials/how-to-use-apache-http-server-as-reverse-proxy-using-mod_proxy-extension

https://httpd.apache.org/docs/2.2/vhosts/examples.html

7 May 2015

JSON library Usage

JSON Library usage for replacing the value of particular key in JSON object.


JSONObject: JSONObject is an unordered collection of key value pairs.

Ex:{
         "Employee1": {"Name":"John", "EmpId":"1002", "Address":"California" }
         "Employee2": {"Name":"Alex", "EmpId":"1001", "Address":"Mexico" }
         "Employee3": {"Name":"Luci", "EmpId":"1003", "Address":"New york" }
     }

With the above JSONString create a JSONObject.

String strEmployees ="{
         \"Employee1\": {\"Name\":\"John\", \"EmpId\":\"1002\", \"Address\":\"California\" },
         \"Employee2\": {\"Name\":\"Alex\", \"EmpId\":\"1001\", \"Address\":\"Mexico\" },
         \"Employee3\": {\"Name\":\"Luci\", \"EmpId\":\"1003\",\"Address\":\"New york\" }
     }";

JSONObject employess = new JSONObject(strEmployees);

To replace the details of "Employee1" in the above JSONString, use the JSONObject employees and method put with key value pair, which replaces if the key is already present in the JSON, otherwise it puts the new entry into the JSONObject.

System.out.println(employees.getJSONObject("Employee1"));

Output:

{"Name":"John", "EmpId":"1002", "Address":"California" }

Now to change Employee1 to {"Name":"Stacy", "EmpId":"1005", "Address":"Texos" }

Create a new JSONObject with the above details.

JSONObject employee1 = new JSONObject("{\"Name\":\"Stacy\", \"EmpId\":\"1005\", \"Address\":\"Texos\" }");

employees.put("Employee1", employee1);

Now print the output of employee1

System.out.println(employees.getJSONObject("Employee1"));

The output will be as below.

 {"Name":"Stacy", "EmpId":"1005", "Address":"Texos" }




7 Jan 2015

JAVA Management package

Management package in java consists of classes that has managed beans for environment of JAVA in the system. The following is a small example of what this Management package can do:


import java.lang.management.ManagementFactory;
import java.lang.management.OperatingSystemMXBean;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;


public class Toworld {
        public static void main(String args[]){
            OperatingSystemMXBean os =  ManagementFactory.getOperatingSystemMXBean();
            Method[] methods = OperatingSystemMXBean.class.getDeclaredMethods();
            for( int nMethodCntr = 0; nMethodCntr < methods.length ; nMethodCntr++ )
            {
                Method method = methods[nMethodCntr];
                try
                {
                    System.out.println(method.getName()+": "+method.invoke(os));                 
                }
                catch( IllegalArgumentException e )
                {
                    e.printStackTrace();
                }
                catch( IllegalAccessException e )
                {
                    e.printStackTrace();
                }
                catch( InvocationTargetException e )
                {
                    e.printStackTrace();
                }
            }
           
    }
}


The following will be the output:

getName: Linux
getAvailableProcessors: 4
getArch: amd64
getVersion: 3.6.11-4.fc16.x86_64
getSystemLoadAverage: 0.25