Tuesday, June 22, 2010

How to install psycopg2 on Mac

1. download psycopg2 package from http://www.djangoproject.com/r/python-pgsql/(http://initd.org/pub/software/psycopg/),
2. set the POSTGRESQL_HOME environment variable in your .profile
export POSTGRESQL_HOME=/Library/PostgreSQL/8.4
export PATH=$POSTGRESQL_HOME/bin:$PATH
3. untar psycopg2-2.2.1.tar.gz file
tar xvfz psycopg2-2.2.1.tar.gz
4. then go to psycopg2-2.2.1 directory, run the following command:
sudo python setup.py install
5. it will come with the following log, which means pyscopg2 installation is successful:

...
copying build/lib.macosx-10.6-universal-2.6/psycopg2/tz.py -> /Library/Python/2.6/site-packages/psycopg2
byte-compiling /Library/Python/2.6/site-packages/psycopg2/__init__.py to __init__.pyc
byte-compiling /Library/Python/2.6/site-packages/psycopg2/errorcodes.py to errorcodes.pyc
byte-compiling /Library/Python/2.6/site-packages/psycopg2/extensions.py to extensions.pyc
byte-compiling /Library/Python/2.6/site-packages/psycopg2/extras.py to extras.pyc
byte-compiling /Library/Python/2.6/site-packages/psycopg2/pool.py to pool.pyc
byte-compiling /Library/Python/2.6/site-packages/psycopg2/psycopg1.py to psycopg1.pyc
byte-compiling /Library/Python/2.6/site-packages/psycopg2/tz.py to tz.pyc
running install_egg_info
Writing /Library/Python/2.6/site-packages/psycopg2-2.2.1-py2.6.egg-info

- End

Thursday, December 10, 2009

Tomcat SSL Installation

Reference Docs at http://tomcat.apache.org/tomcat-5.5-doc/ssl-howto.html#Edit%20the%20Tomcat%20Configuration%20File
Then follow the following instructions:

Installing the Certificates to the Keystore


1. Download your certificate files from your certificate authority and save them to the same directory as the keystore that you created during the CSR creation process. The certificate will only work with the same keystore that you initially created the CSR with. The certificates must be installed to your keystore in the correct order.
2. Install the Root Certificate file: Every time you install a certificate to the keystore you must enter the keystore password that you chose when you generated it. Enter the following command to install the Root certificate file:

keytool -import -trustcacerts -alias root -file RootCertFileName.crt -keystore keystore.key

If you receive a message that says "Certificate already exists in system-wide CA keystore under alias <...> Do you still want to add it to your own keystore? [no]:", select Yes. If successful, you will see "Certificate was added to keystore".
3.

Install the Intermediate Certificate file: If your certificate authority provided an intermediate certificate file, you will need to install it here by typing the following command:

keytool -import -trustcacerts -alias intermediate -file IntermediateCertFileName.crt -keystore keystore.key

If successful, you will see "Certificate was added to keystore".
4.

Install the Primary Certificate file: Type the following command to install the Primary certificate file (for your domain name):

keytool -import -trustcacerts -alias tomcat -file PrimaryCertFileName.crt -keystore keystore.key

If successful, you will see "Certificate reply was installed in keystore". You now have all the certificates installed to the keystore file. You just need to configure your server to use the keystore file.
Configuring your SSL Connector

Tomcat requires an SSL Connector to be configured before it can accept secure connections.

By default Tomcat looks for your Keystore with the file name .keystore in the home directory with the default password "changeit". The home directory is generally /home/user_name/ on Unix and Linux systems, and C:\Documents and Settings\user_name\ on Microsoft Windows systems. You will be able to change the password and file location.

Method 1 -- Add an SSL Connector using admintool:
1. Start Tomcat.
2. Enter 'http://localhost:8080/admin' in a local browser to start admintool.
3. Type a username and password with administrator rights.
4. On the left select service (Java Web Services Developer Pack).
5. Select Create New Connector from the drop-down list on the right.
6. Choose HTTPS in the Type field.
7. In the Port field, enter 443. This defines the TCP/IP port number on which Tomcat will listen for secure connections.
8. Enter the Keystore Name and Keystore Password if your keystore is named something other than .keystore, if .keystore is located in a directory other than the home directory of the machine on which Tomcat is running, or if the password is something other than the default value of changeit. If you have used the default values, you can leave these fields blank.
9. Select Save to save the new Connector.
10. Select Commit Changes to save the new Connector information to the server.xml file so that it is available the next time Tomcat is started.

Method 2 -- Configure the SSL Connector in server.xml :
1. Copy your keystore file (your_domain.key) to the home directory.
2. Open the file Home_Directory/conf/server.xml in a text editor.
3. Uncomment the SSL Connector Configuration.
4. Make sure that the Connector Port is 443.
5. Make sure the keystorePass matches the password for the keystore and the keystoreFile contains the path and filename of the keystore.
When you are done your connector should look something like this:




6. Save the changes to server.xml

Wednesday, October 21, 2009

Tuesday, May 26, 2009

Redirect all traffic from port 80 to port 443 https in Apache2

Force an SSL connection and redirect all traffic from port 80 (HTTP) to port 443 (HTTPS), use this instead:
(assuming: Apache2 Listen on 80 and 8080)


RewriteEngine on
RewriteCond %{SERVER_PORT} ^80$
RewriteRule ^(.*)$ https://%{SERVER_NAME}$1 [L,R]

RewriteCond %{SERVER_PORT} ^8080$
RewriteRule ^(.*)$ https://%{SERVER_NAME}$1 [L,R]

RewriteLog "logs/rewrite.log"
RewriteLogLevel 2

Wednesday, May 6, 2009

Struts2 Security Code Action

package com.ozview.struts2.action;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Random;

import javax.imageio.ImageIO;

import org.apache.log4j.Logger;

/**
*
* SecurityImageCodeAction.
*
*/
public class SecurityImageCodeAction extends BaseAction
{
/** The image font. * */
private static Font imageFont = new Font("Arial Bold", Font.BOLD, 24);

/** The InputStream imageStream. * */
protected InputStream imageStream;

/** The logger log. * */
private final Logger log = Logger.getLogger(this.getClass().getName());

/**
* Generate the security image code.
*
* @return SUCCESS if there is no error.
*/
public String genSecurityImageCode()
{
try
{
ByteArrayOutputStream output = new ByteArrayOutputStream();
String randomCode = generateImageCode(output);
// store the random number in session.
storeInSession(SECURITY_CODE, randomCode);
this.imageStream = new ByteArrayInputStream(output.toByteArray());
output.close();
} catch (Exception e)
{
removeFromSession(SECURITY_CODE);
log.error("generating the security code error");
return ERROR;
}
return SUCCESS;
}

/**
* Generate the Buffered image.
* @param output The ByteArrayOutputStream output.
* @return a SecurityImageCode which contians a random characters and an image.
*/
protected String generateImageCode(ByteArrayOutputStream output)
{
//image size
int width = 150, height = 30;
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);
Graphics g = image.getGraphics();
Random random = new Random();
//g.setColor(getRandColor(200, 250));
//set the background color
g.setColor(new Color(230,241,241));
g.fillRect(1, 1, width - 1, height - 1);
g.setColor(new Color(102, 102, 102));
g.drawRect(0, 0, width - 1, height - 1);
g.setFont(imageFont);
g.setColor(getRandColor(160, 200));
for (int i = 0; i < 155; i++)
{
int x = random.nextInt(width - 1);
int y = random.nextInt(height - 1);
int xl = random.nextInt(6) + 1;
int yl = random.nextInt(12) + 1;
g.drawLine(x, y, x + xl, y + yl);
}
for (int i = 0; i < 70; i++)
{
int x = random.nextInt(width - 1);
int y = random.nextInt(height - 1);
int xl = random.nextInt(12) + 1;
int yl = random.nextInt(6) + 1;
g.drawLine(x, y, x - xl, y - yl);
}

String sRand = "";
for (int i = 0; i < 8; i++)
{
String tmp = getRandomChars();
sRand += tmp;
// g.setColor(new Color(20 + random.nextInt(110), 20 + random.nextInt(110), 20 +
//random.nextInt(110)));
g.setColor(new Color(220, 220, 220));
g.drawString(tmp, 17 * i + 9, 22);
}
g.dispose();
try
{
ImageIO.write(image, "jpg", output);
} catch (IOException e)
{
log.error("Generating the security image code error, " + e.getMessage());
}
return sRand;
}

/**
* Get random color.
*
* @param fc
* a int fc.
* @param bc
* a int bc.
* @return a Random color.
*/
protected Color getRandColor(int fc, int bc)
{
Random random = new Random();
if (fc > 255)
fc = 255;
if (bc > 255)
bc = 255;
int r = fc + random.nextInt(bc - fc);
int g = fc + random.nextInt(bc - fc);
int b = fc + random.nextInt(bc - fc);
return new Color(r, g, b);
}

/**
* Get a random characters.
*
* @return a random characters.
*/
protected String getRandomChars()
{
int rand = (int) Math.round(Math.random() * 2);
long itmp = 0;
char ctmp = '\u0000';
switch (rand)
{
case 1:
itmp = Math.round(Math.random() * 25 + 65);
ctmp = (char) itmp;
return String.valueOf(ctmp);
case 2:
itmp = Math.round(Math.random() * 25 + 97);
ctmp = (char) itmp;
return String.valueOf(ctmp);
default:
itmp = Math.round(Math.random() * 9);
return String.valueOf(itmp);
}
}

/**
* Set the InputSteam imageStream.
*
* @param imageStream
* The InputSteam imageStream to be set.
*/
public void setImageStream(InputStream imageStream)
{
this.imageStream = imageStream;
}

/**
* Get the InputSteam imageStream.
*
* @return An InputSteam imageStream.
*/
public InputStream getImageStream()
{
return imageStream;
}
}

Add the following lines into struts.xml file:



Using the Security Code in the JSP file:

How to ceate a Security Code

package com.ozview;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Random;

import javax.imageio.ImageIO;

public class Test
{
/** The image font. * */
private static Font imageFont = new Font("Arial Bold", Font.BOLD, 24);



protected String generateImageCode(ByteArrayOutputStream output)
{
int width = 150, height = 30;
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);
Graphics g = image.getGraphics();
Random random = new Random();
//g.setColor(getRandColor(200, 250));
g.setColor(new Color(230, 241, 241));
g.fillRect(1, 1, width - 1, height - 1);
g.setColor(new Color(102, 102, 102));
g.drawRect(0, 0, width - 1, height - 1);
g.setFont(imageFont);
g.setColor(getRandColor(160, 200));
for (int i = 0; i < 155; i++)
{
int x = random.nextInt(width - 1);
int y = random.nextInt(height - 1);
int xl = random.nextInt(6) + 1;
int yl = random.nextInt(12) + 1;
g.drawLine(x, y, x + xl, y + yl);
}
for (int i = 0; i < 70; i++)
{
int x = random.nextInt(width - 1);
int y = random.nextInt(height - 1);
int xl = random.nextInt(12) + 1;
int yl = random.nextInt(6) + 1;
g.drawLine(x, y, x - xl, y - yl);
}

String sRand = "";
for (int i = 0; i < 8; i++)
{
String tmp = getRandomChars();
sRand += tmp;
//g.setColor(new Color(20 + random.nextInt(110), 20 + random.nextInt(110), 20 + random.nextInt(110)));
g.setColor(new Color(0, 0, 255));
g.drawString(tmp, 17 * i + 9, 22);
}
g.dispose();
try
{
File imagefile = new File("securitycode.jpg");
ImageIO.write(image, "jpg", imagefile);
} catch (IOException e)
{
System.err.print("Generating the security image code error, " + e.getMessage());
}
return sRand;
}

/**
* Get random color.
*
* @param fc
* a int fc.
* @param bc
* a int bc.
* @return a Random color.
*/
protected Color getRandColor(int fc, int bc)
{
Random random = new Random();
if (fc > 255)
fc = 255;
if (bc > 255)
bc = 255;
int r = fc + random.nextInt(bc - fc);
int g = fc + random.nextInt(bc - fc);
int b = fc + random.nextInt(bc - fc);
return new Color(r, g, b);
}

/**
* Get a random characters.
*
* @return a random characters.
*/
protected String getRandomChars()
{
int rand = (int) Math.round(Math.random() * 2);
long itmp = 0;
char ctmp = '\u0000';
switch (rand)
{
case 1:
itmp = Math.round(Math.random() * 25 + 65);
ctmp = (char) itmp;
return String.valueOf(ctmp);
case 2:
itmp = Math.round(Math.random() * 25 + 97);
ctmp = (char) itmp;
return String.valueOf(ctmp);
default:
itmp = Math.round(Math.random() * 9);
return String.valueOf(itmp);
}
}

public static void main(String [] args) {
Test test = new Test();
ByteArrayOutputStream output = new ByteArrayOutputStream();
String randomCode = test.generateImageCode(output);

System.out.println(randomCode);


}

}

Saturday, April 18, 2009

UsingMod_proxy With Apache2 and Tomcat

1. yum install apache2
2. Setup Apache to use mod_proxy (AJP)
Make sure that at least following modules are loaded (uncomment this in httpd.conf)
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_balancer_module modules/mod_proxy_balancer.so
LoadModule proxy_ajp_module modules/mod_proxy_ajp.so
Add those lines in /etc/httpd/conf/httpd.conf :
<Proxy balancer://mycluster>
Order deny,allow
Allow from all
BalancerMember ajp://localhost:8009/yourapp
</Proxy>

<VirtualHost *:80>
ServerAdmin admin@yourhost.com
ServerName www.yourhost.com
ServerAlias yourhost.com

ProxyPass /yourapp balancer://mycluster
ProxyPassReverse /yourapp balancer://mycluster/

ErrorLog logs/yourhost_error_log
CustomLog logs/yourhost_access_log combined

</VirtualHost>
3. The yourapp application will be deployed in tomcat, and make sure the tomcat 8009 connector is opened.

4. In a browser, type: http://www.yourhost.com/yourapp, it will go to yourapp that deployed in the tomcat, you don't need to type the port number.

Reference: usintMod_ProxyWithJBoss