jump to navigation

Java HTTP proxy settings February 18, 2011

Posted by Anoop Somasundaran in Java, Linux, Tomcat, Unix.
Tags: , , , ,
add a comment

Some of you might have come across http connectivity issues especially when you setup your java application on staging or production servers. Due to security reasons, access to internet/external URLs within an organization is often through proxy servers. When I was trying to setup one of the java web applications on staging and production servers, the application was throwing an exception – ‘java.net.ConnectException: Connection refused’ when it tries to make an HTTP connection to get the content. However we were not getting this error in our development environment. I was almost sure that the problem could be because of the proxy servers used in the production and staging setup. The proxy server was blocking outbound traffic from the application servers.

After some trial and error attempts, I found the following solution to fix this issue.

I was using tomcat as the application server and I had to add the following lines in catalina.sh

JAVA_OPTS="$JAVA_OPTS -Dhttp.proxyHost=ProxyURL"
JAVA_OPTS="$JAVA_OPTS -Dhttp.proxyPort=ProxyPort"
JAVA_OPTS="$JAVA_OPTS -Dhttp.proxyUser=UserName" (Optional)
JAVA_OPTS="$JAVA_OPTS -Dhttp.proxyPassword=Password" (Optional)

Note: Please don’t copy and paste the above lines into Linux environment. The double quotes might give you issues on Linux if you copy double quotes from windows. I have faced this issue several times 😦

This problem can also be resolved by adding the following lines in your code.
System.getProperties().put("http.proxyHost", "ProxyURL");
System.getProperties().put("http.proxyPort", "ProxyPort");
System.getProperties().put("http.proxyUser", "UserName"); (Optional)
System.getProperties().put("http.proxyPassword", "Password"); (Optional)

Adding the settings at the server level seems to be a much better option though.

Shell script to compare two directories and copy missing files January 20, 2010

Posted by Anoop Somasundaran in Linux, Unix.
Tags: ,
4 comments

The following shell script compares two directories and copies missing files from the source directory to the destination directory. Please note that script doesn’t compare sub-directories. You can execute the scrip by supplying two parameters – source directory and destination directory.

/bin/bash
#####################################################################
# Created by Anoop Somasundaran
# This script takes two parameters- from_directory and to_directory.
# It finds the file differences and copy the files to the destination directory.
# Only missing files are copied. Sub directories are not considered
#####################################################################

# Check the number of input parameters. If two parameters are given go ahead, else exit
if [ $# -eq 2 ]
then
FROMDIR=$1
TODIR=$2
else
echo "Usage: script.sh fromdir todir"
exit
fi

# Validate from_directory
if [ ! -d "${FROMDIR}" ]
then
echo "Directory ${FROMDIR} does not exist!!"
exit
fi

# Validate to_directory
if [ ! -d "${TODIR}" ]
then
echo "Directory ${TODIR} does not exist!!"
exit
fi

cd ${FROMDIR}
for i in `find . -type f`
do
if [ ! -f ${TODIR}/$i ]
then
cp $i ${TODIR}/$i
fi

done

How to run the above script?

1) Copy the above script into a .sh file (script.sh)
2) You need to supply the complete path for source and destination directories while running the script.

$sh /home/script.sh /tmp/test1/ /tmp/test2/