Sunday, January 25, 2015

Apache Proxy for Tomcat

ADDING APACHE WEB SERVER AS PROXY FOR TOMCAT
1. Configure the copy of Apache so that it includes the mod_proxy module. In httpd.conf file enable these:
LoadModule proxy_module {path-to-module}/mod_proxy.so and
LoadModule proxy_http_module {path-to-module}/mod_proxy_http.so

2. Next Add Two Directives in the httpd.conf file for each web application that need to forward to the Tomcat as:
ProxyPass /myapp http://tomcat-ip:8081/myapp
ProxyPassReverse /myapp http://tomcat-ip:8081/myapp

More on: http://tomcat.apache.org.tomcat-6.0-doc/proxy-howto.html#Apache_2.0_Proxy_Support

Configuring SSL in Tomcat

CONFIGURE SIMPLE SSL USING TOMCAT
1. Create simple KeyStore file in your machine using following command:

%JAVA_HOME%\bin\keytool -genkey -alias tomcat -keyalg RSA
(default it stores in your \Users directory as .keystore file

OR

%JAVA_HOME%\bin\keytool -genkey -alias tomcat -keyalg RSA \
  -keystore \path\to\my\keystore
 
2. Once the keystore file is created, add the following line in your Tomcat Server.xml file as:
       
<!-- Define a SSL HTTP/1.1 Connector on port 8443
     This connector uses the BIO implementation that requires the JSSE
     style configuration. When using the APR/native implementation, the
     OpenSSL style configuration is required as described in the APR/native
     documentation -->

<Connector port="8443" protocol="org.apache.coyote.http11.Http11Protocol"
        maxThreads="150" SSLEnabled="true" scheme="https" secure="true"
        clientAuth="false" sslProtocol="TLS" keystoreFile="\path\to\my\keystore\.keystore" keystorePass="your_password"/>
       

3. Add Security setting in your application's web.xml file as:

<security-constraint>
    <web-resource-collection>
        <web-resource-name>your_app_name</web-resource-name>
        <url-pattern>/*</url-pattern>
    </web-resource-collection>
    <user-data-constraint>
        <transport-guarantee>CONFIDENTIAL</transport-guarantee>
    </user-data-constraint>
</security-constraint>

4. Access your app using https://localhost:8443/your_app_name
if you access using http://localhost:8080/your_app_name, it will redirect to https because of the web.xml configurations

5. For more information check the Apache Tomcat Document Page

URL REWRITING in TOMCAT

TOMCAT URL REWRITE USING URLREWRITEFILTER BY TUCKEY.ORG
In order to rewrite URL directly from the TOMCAT ROOT context:

1. Copy the urlrewritefilter-4.0.3.jar file to {TOMCAT_HOME}/lib folder

2. Add urlrewrite.xml file in the {TOMCAT_HOME}/webapps/ROOT/WEB-INF folder and write your own rule something like:

<urlrewrite>
    <rule>
        <from>^/([a-z]+)/some_app</from>
        <to type="redirect">/other_app/LoginServlet?param1=$1</to>
    </rule>
</urlrewrite>

3. If you are redirecting the url from your app, then add these line at the top of the web.xml file's Servlet mapping, for ROOT redirect, add these to ROOT/WEB-INF/web.xml file as:
<filter>
    <filter-name>UrlRewriteFilter</filter-name>
    <filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>UrlRewriteFilter</filter-name>
    <url-pattern>/*</url-pattern>
    <dispatcher>REQUEST</dispatcher>
    <dispatcher>FORWARD</dispatcher>
</filter-mapping>

4. Now access the page using http://localhost:8090/abc/some_app, this will redirect the page to http://localhost:8090/other_app/LoginServlet?param1=abc

More information and tutorials on: http://www.tuckey.org/urlrewrite/

Friday, October 4, 2013

Lambda in Python (Map, Reduce and Filter)

Map, Reduce and Filter using Lambda in Python

1. Map
>>>map(lambda x:x*x, range(1,10))
>>>[1, 4, 9, 16, 25, 36, 49, 64, 81]

2. Reduce
>>>reduce(lambda x,y: x+y, range(1,10))
>>>45

3. Filter
>>>filter(lambda x: x % 2 != 0, range(1,20))
>>>[1, 3, 5, 7, 9, 11, 13, 15, 17, 19]

ex1: Add sum of all even number from 1 to 20
>>>reduce(lambda x,y: x+y, filter(lambda x: x % 2 == 0, range(1,20)))
>>>90

ex2: lambda as return function
>>>g = lambda x: x**2
>>>print g(2)
>>>4

ex3: like partial function
>>>def increament(n):
>>>    return lambda x: x + n
>>>f = increment(10)
>>>print f(6)
>>>16
or
>>>print increament(6)(10)
>>>16

ex4: prime numbers between 1-100
>>>nums = range(2,100)
>>>for i in range(2,8):
>>>    nums = filter(lambda x: x == i or x % i, nums)
>>>print nums
>>>[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]

ex5: Letter count
>>>print map(lambda w: len(w), 'It has been raining since this morning'.split())
>>>[2, 3, 4, 7, 5, 4, 7]

ex6: Functional Style of adding
>>> from operator import add
>>> expr = "28+32+++32++39"
>>> print reduce(add, map(int, filter(bool, expr.split("+"))))
>>> 131

Wednesday, September 4, 2013

Creating Simple 'Hello World' Django web application

Creating Simple Hello World Django/python web application

# Download the Django from: https://www.djangoproject.com/download/

# untar the application tar
$ tar xvf Django-1.5.2.tar

$ cd Django-1.5.2

# Setup django
$ python setup.py install
- this will install django web framework

# Create the new site using following command
$ django-admin.py startproject mysite
- after you run this command, it will create the following folder structure
- mysite
--- manage.py
--- mysite
------ urls.py
------ __init__.py
------ wsgi.py
------ settings.py
------ views.py (add new file as shown below)

# Create new python file in mysite/ directory as views.py and add following line
#!/usr/bin/python
from django.http import HttpResponse
def hello(request):
    html = "<html><body><b>Hello World!</b></body></html>"
    return HttpResponse(html)

# Update the urls.py file in mysite/ directory as
#!/usr/bin/python
from django.conf.urls import patterns, include, url
from mysite.views import hello
urlpatterns = patterns('',
    url(r'^$', hello),
)

# Finally from inside mysite/ directory run this command
$ python manage.py runserver

# Open the browser and type this url http://localhost:8000

Friday, August 23, 2013

Hot Deployment/Republishing the JSF Facelet in Tomcat/Eclipse Environment

Today I was trying to update/republish the JSF pages without redeploying in the tomcat server again and again but couldn't get success.

After few minutes of google, I figured what was the issue. In my web application web.xml file, the FACELETS_REFRES_PERIOD is set as -1 as:

    <context-param>
      <param-name>javax.faces.FACELETS_REFRESH_PERIOD</param-name>
      <param-value>-1</param-value>
    </context-param>

I changed it to '1' and also changed
    <context-param>
      <param-name>javax.faces.PROJECT_STAGE</param-name>
      <param-value>Production</param-value>
    </context-param>



to

    <context-param>
      <param-name>javax.faces.PROJECT_STAGE</param-name>
      <param-value>Development</param-value>
    </context-param>

and VOILA! it worked and I am happy. Now I can change the static resources without module redeploy.




Tuesday, August 20, 2013

Configure ssh login information in config file

How to configure the ssh login information in Config file

Usually we connect the remote host using ssh by giving following command in terminal

$ssh -i filename.pem user@ipaddress 

(if there is the pem file for the security key)

Or

$ssh user@ipaddress

Instead of doing this we can add these information in Config file and just type in

$ssh <host>

The config file reside in .ssh/ folder as .ssh/config and add the following:
Host <HostName/AnyName>
HostName <ipaddress>
User <username>
IdentityFile <if there are any pem file> (Optional)

eg:
Host MyHost
HostName 192.168.1.101
User user