To Infinity and Beyond …

Install another version of python

Posted by: jango on: October 6, 2009

download the latest from http://www.python.org/download/

Unzip it, change to that directory and run ./configure –prefix=< Your custom python install path>

then make,make install

You have new version of python at <Your custom python install path>/bin (ie <Your custom python install path>/bin/python will be the new python) . without disturbing the existing version

:)

SSL Key, Certifcate CSR – Java

Posted by: jango on: August 14, 2009

Create a server key: openssl genrsa -des3 -out server.key.pass 1024

Remove the pass: openssl rsa -in server.key.pass -out server.key

Generate CSR: openssl req -new -key server.key -out server.csr

Haproxy and ssl

Posted by: jango on: June 6, 2009

Haproxy SSL

JS CSS minify filter in java

Posted by: jango on: April 27, 2009

This filter is based on YUI Compressor. So for performance you must use any caching filter. The code is

package com.vinu.web.performance;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StringReader;
import java.io.StringWriter;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;

import org.mozilla.javascript.ErrorReporter;
import org.mozilla.javascript.EvaluatorException;

import com.yahoo.platform.yui.compressor.CssCompressor;
import com.yahoo.platform.yui.compressor.JavaScriptCompressor;

/**
* @author Vinu Varghese (Email: vinu@x-minds.org) Minifies JS and CSS on the fly, based on the YUI Compressor The minification is a heavy process so use this
* filter along with a caching filter for performance.
*/
public class JsCssMinifyFilter implements Filter
{

private FilterConfig config;

/*
* (non-Javadoc)
*
* @see javax.servlet.Filter#init(javax.servlet.FilterConfig)
*/
public void init(FilterConfig filterConfig) throws ServletException
{
this.config = filterConfig;
}

/*
* (non-Javadoc)
*
* @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain)
*/
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException
{
HttpServletRequest originalRequest = (HttpServletRequest) request;

HttpServletResponse originalResponse = (HttpServletResponse) response;

// Create the response wrapper
MinifiedResponse minifiedResponse = new MinifiedResponse(originalResponse);

// Continue the chain - get the contents
chain.doFilter(originalRequest, minifiedResponse);

// The data
String data = new String(minifiedResponse.getData());

// Create the reader
StringReader reader = new StringReader(data);
//Create the writer
StringWriter writer = new StringWriter();

String requestURI = originalRequest.getRequestURI();

try
{
//Do JS minification
if (requestURI.endsWith(".js"))
{
minifyJS(reader, writer);
}
// Do Css minification
else if (requestURI.endsWith(".css"))
{
minifyCSS(reader, writer);
}
//Else - we can't do any other minification ;)
else
{
writer.write(data);

}

}
catch (Exception ex)
{
// We do a generic exception handling

//Log error
config.getServletContext().log("Error in minifying : " + requestURI, ex);
// Write the data as it is
writer.write(data);
}

//Write the data to the original response

// Get the data from the writer (Compressed data)
String compressed = writer.toString();

//Set the content length correctly
originalResponse.setContentLength(compressed.length());

//Write the data
originalResponse.getOutputStream().write(compressed.getBytes());

}

/**
* @param reader
* @param writer
* @throws IOException
*/
private void minifyCSS(StringReader reader, StringWriter writer) throws IOException
{
CssCompressor cssCompressor = new CssCompressor(reader);
cssCompressor.compress(writer, -1);
}

/**
* @param reader
* @param writer
*/
private void minifyJS(StringReader reader, StringWriter writer) throws IOException
{
JavaScriptCompressor compressor = new JavaScriptCompressor(reader, new ErrorReporter() {

public void warning(String message, String sourceName, int line, String lineSource, int lineOffset)
{
if (line < 0)
{
System.err.println("\n[WARNING] " + message);
}
else
{
System.err.println("\n[WARNING] " + line + ':' + lineOffset + ':' + message);
}
}

public void error(String message, String sourceName, int line, String lineSource, int lineOffset)
{
if (line < 0)
{
System.err.println("\n[ERROR] " + message);
}
else
{
System.err.println("\n[ERROR] " + line + ':' + lineOffset + ':' + message);
}
}

public EvaluatorException runtimeError(String message, String sourceName, int line, String lineSource, int lineOffset)
{
error(message, sourceName, line, lineSource, lineOffset);
return new EvaluatorException(message);
}
});

// Does the compression
compressor.compress(writer, -1, true, false, true, false);

}

/*
* (non-Javadoc)
*
* @see javax.servlet.Filter#destroy()
*/
public void destroy()
{

}

/**
* A response wrapper, the content is saved to the output buffer, the stream returned is ServletOutputStreamWrapper
*
* @author Vinu Varghese (Email: vinu@x-minds.org)
*
*/
class MinifiedResponse extends HttpServletResponseWrapper
{

private ByteArrayOutputStream output = new ByteArrayOutputStream();

/**
* @param response
*/
public MinifiedResponse(HttpServletResponse response)
{
super(response);
}

/*
* (non-Javadoc)
*
* @see javax.servlet.ServletResponseWrapper#getOutputStream()
*/
@Override
public ServletOutputStream getOutputStream() throws IOException
{
return new ServletOutputStreamWrapper(this.output);
}

/*
* (non-Javadoc)
*
* @see javax.servlet.ServletResponseWrapper#getWriter()
*/
@Override
public PrintWriter getWriter() throws IOException
{
PrintWriter localPrintWriter;
try
{
localPrintWriter = new PrintWriter(new OutputStreamWriter(getOutputStream(), "UTF-8"), true);
}
catch (Exception localException)
{
localPrintWriter = new PrintWriter(getOutputStream(), true);
}

return localPrintWriter;
}

/**
* Returns the data
*
* @return
*/
public byte[] getData()
{
try
{
output.flush();
}
catch (IOException e)
{
//ignore
}

return output.toByteArray();
}

}

/**
* Output stream wrapper I have overriden the write method, is that enough?
*
* @author Vinu Varghese (Email: vinu@x-minds.org)
*
*/
class ServletOutputStreamWrapper extends ServletOutputStream
{

private ByteArrayOutputStream output;

/**
*
*/
public ServletOutputStreamWrapper(ByteArrayOutputStream output)
{
this.output = output;
}

/*
* (non-Javadoc)
*
* @see java.io.OutputStream#write(int)
*/
public void write(int b) throws IOException
{
output.write(b);
}

}

}

Import private key to Java key store

Posted by: jango on: August 13, 2008

See this link: http://www.agentbob.info/agentbob/79-AB.html

Free java decompiler

Posted by: jango on: July 29, 2008

Check this: http://java.decompiler.free.fr

A free java decompiler

Simple Tar reader in Java

Posted by: jango on: July 22, 2008


/**
* A simple tar reader
* Just reads through tar file, parses the headers and writes file.
*/
package com.jango.tarreader;

import java.io.IOException;
import java.io.InputStream;

/**
* @author vinu
*
*/
public abstract class AbstractTarReader {

public void read(InputStream in) throws IOException {

while (true) {

Header head = parseHeader(in);

if (head == null)
break;

if (head.isFile && head.filesize > 0) {
int remainder = head.filesize % 512;
int skiplen = 0;
if (remainder != 0)
skiplen = 512 – remainder;

byte[] fileContents = new byte[head.filesize];

in.read(fileContents);
in.skip(skiplen);

createFile(head.filename, fileContents);

} else if (!head.isFile) {
createDir(head.filename);
}

}
}

/**
* Creates the file – dependent on implementation
*
* @param filename
* @param fileContents
* @throws IOException
*/
public abstract void createFile(String filename, byte[] fileContents)
throws IOException;

/**
* Creates the directory as in the tar
*
* @param dirname
*/
public abstract void createDir(String dirname);

// Parses the header
private Header parseHeader(InputStream in) throws IOException {

Header head = new Header();

byte buf[] = new byte[512];

int len = in.read(buf);

// EOF or not fully read
if (len == -1 || len != 512)
return null;

StringBuffer sBufFilename = new StringBuffer();

byte linkIndicator = buf[156];

for (int i = 0; i < 100; i++) {
if (buf[i] != 0) {
sBufFilename.append((char) buf[i]);
}
}

StringBuffer sBufFilesize = new StringBuffer();
for (int i = 124; i < 136; i++) {
sBufFilesize.append((char) buf[i]);
}

head.filename = sBufFilename.toString();
head.filesize = Integer.parseInt(“0″ + sBufFilesize.toString().trim(),8);

if(head.filesize==0 && head.filename.length()==0)
return null;

if ((char) linkIndicator == ‘5′) {
head.isFile = false;
}

return head;
}

// To store the header info – Simple for now
// Only the filename
class Header {
String filename;
int filesize;
boolean isFile = true;
}

}

Install a new version of postgres via YUM

Posted by: jango on: May 16, 2008

getLevenshteinDistance in javascript

Posted by: jango on: May 5, 2008

/*

Java script implementation of LD:

Author: Vinu

Adapted from URL:http://www.merriampark.com/ldjava.htm

*/

function

getLevenshteinDistance(s,t) {

if (s == null || t == null) {

retrun -1;

}

var n = s.length; // length of s

var m = t.length; // length of t

if (n == 0) {

return m;

} else if (m == 0) {

return n;

}

var p = new Array(n+1); //’previous’ cost array, horizontally

var d = new Array(n+1); // cost array, horizontally

var _d; //placeholder to assist in swapping p and d

// indexes into strings s and t

var i; // iterates through s

var j; // iterates through t

var t_j; // jth character of t

var cost; // cost

for (i = 0; i<=n; i++) {

p[i] = i;

}

for (j = 1; j<=m; j++) {

t_j = t.charAt(j-1);

d[0] = j;

for (i=1; i<=n; i++) {

cost = s.charAt(i-1)==t_j ? 0 : 1;

// minimum of cell to the left+1, to the top+1, diagonally left and up +cost

d[i] = Math.min(Math.min(d[i-1]+1, p[i]+1), p[i-1]+cost);

}

// copy current distance counts to ‘previous row’ distance counts

_d = p;

p = d;

d = _d;

}

// our last action in the above loop was to switch d and p, so p now

// actually has the most recent cost counts

return p[n];

}

Happy New year 2008

Posted by: jango on: December 31, 2007

Happy New Year 2008

To

ALL

 

ENJOY

Quote of the moment

Quote

Jango Twitts