JavaTechie

Its all about Technology

Tomcat Configuration for Removing webapps name from URL January 5, 2008

Filed under: Java — javatechie @ 12:32 pm
Tags:

To change Host Name Locally modify hosts file which is located under below specified path and add new hostname whatever you like for testing locally.

C:\WINDOWS\system32\drivers\etc\hosts

To modify URL and remove webapps name from url you have to do below steps.

Create below specified file under specified path and insert that code. This for default localhost application.

conf/Catalina/localhost/ROOT.xml

<?xml version='1.0' encoding='utf-8'?>
<Context displayName="localhost" docBase="" path=""
workDir="work/Catalina/localhost/_">
</Context>

For your custom application you have to add code as specified below.

Where $host is your custom web application.

Add a configuration file for the host

mkdir conf/Catalina/$host
cat >conf/Catalina/$host/ROOT.xml
<?xml version='1.0' encoding='utf-8'?>
<Context displayName="$host" docBase="" path=""
workDir="work/Catalina/$host/_">
</Context>

Main step is to add ROOT folder under your webapps/$host folder like specified below

$tomcatdir/webapps/$host/ROOT/all the files
Next You have to modify server.xml file to add context paths and host
Example:
<Server port="8005" shutdown="SHUTDOWN">
  <!-- Comment these entries out to disable JMX MBeans support used for the
       administration web application -->
  <Listener className="org.apache.catalina.core.AprLifecycleListener" />
  <Listener className="org.apache.catalina.mbeans.ServerLifecycleListener" />
  <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
  <Listener className="org.apache.catalina.storeconfig.StoreConfigLifecycleListener"/>
  <!-- Global JNDI resources -->
  <GlobalNamingResources>
    <!-- Test entry for demonstration purposes -->
    <Environment name="simpleValue" type="java.lang.Integer" value="30"/>
    <!-- Editable user database that can also be used by
         UserDatabaseRealm to authenticate users -->
    <Resource name="UserDatabase" auth="Container"
              type="org.apache.catalina.UserDatabase"
       description="User database that can be updated and saved"
           factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
          pathname="conf/tomcat-users.xml" />
  </GlobalNamingResources>
  <!-- Define the Tomcat Stand-Alone Service -->
  <Service name="Catalina">
   <!-- Define a non-SSL HTTP/1.1 Connector on port 8080 -->
    <Connector port="8080" maxHttpHeaderSize="8192"
               maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
               enableLookups="false" redirectPort="8443" acceptCount="100"
               connectionTimeout="20000" disableUploadTimeout="true" />
    <!-- Note : To disable connection timeouts, set connectionTimeout value
     to 0 -->
     <!-- Define an AJP 1.3 Connector on port 8009 -->
    <Connector port="8009"
               enableLookups="false" redirectPort="8443" protocol="AJP/1.3" />
    <!-- Define the top level container in our container hierarchy -->
    <Engine name="Catalina" defaultHost="localhost">
      <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
             resourceName="UserDatabase"/>
        <Context path="" docBase="webapps/ca"
        debug="5" reloadable="true" crossContext="true">
 <Resource name="jdbc/TestDB" auth="Container" type="javax.sql.DataSource"
               maxActive="100" maxIdle="30" maxWait="10000"
               username="username" password="usrpwd" driverClassName="com.mysql.jdbc.Driver"
               url="jdbc:mysql://localhost:3306/beta2?autoReconnect=true"/>
</Context>
<Context path="" docBase="webapps/cl" debug="5" reloadable="true" crossContext="true"></Context>
      <Host name="localhost" appBase="webapps/ca"
       unpackWARs="true" autoDeploy="true"
       xmlValidation="false" xmlNamespaceAware="false">
       </Host>
          <Host name="consumer" debug="0" appBase="C:/tomcat-5.5/webapps/cl" unpackWARs="true"                                    autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false">
           <Logger className="org.apache.catalina.logger.FileLogger"
               directory="logs"  prefix="consumer_log." suffix=".txt" timestamp="true"/>
                 <Alias>consumer.com</Alias>
        </Host>
    </Engine>
  </Service>
</Server>
For as many applications you have to apply
 

Installing mod_proxy_html January 5, 2008

Filed under: Uncategorized — javatechie @ 5:35 am
Tags:

Download mod_proxy_html.c from here (please remember to register if you use this software).
Compile it like so

apxs -a -c -I/usr/include/libxml2 -i mod_proxy_html.c

Create a file /etc/httpd/conf.d/mod_proxy_html.conf

LoadFile /usr/lib/libxml2.soLoadModule proxy_html_module  /usr/lib/httpd/modules/mod_proxy_html.soProxyHTMLExtended On

 

Resizes jpeg image files January 1, 2008

Filed under: Java — javatechie @ 5:50 am
Tags:

import java.awt.*;
import java.awt.image.*;
import java.io.*;
import com.sun.image.codec.jpeg.*;

/**
* Resizes jpeg image files on your file system.
* Uses the com.sun.image.codec.jpeg package shipped
* by Sun with Java 2 Standard Edition.
*
* @author Randy Belknap
*/
public class Resize extends Panel {

/**
* @param originalImage the file name of the image to resize
* @param newImage the new file name for the resized image
* @param factor the new image’s width will be  width * factor.
* The height will be proportionally scaled.
*/
public void doResize(String originalImage, String newImage, double factor) {
Image img = getToolkit().getImage(originalImage);
loadImage(img);
int iw = img.getWidth(this);
int ih = img.getHeight(this);

//Reduce the image
int w = (int)(iw * factor);
Image i2 = img.getScaledInstance(w, -1, 0);
loadImage(i2);

//Load it into a BufferedImage
int i2w = i2.getWidth(this);
int i2h = i2.getHeight(this);
BufferedImage bi = new BufferedImage(i2w, i2h, BufferedImage.TYPE_INT_RGB);
Graphics2D big = bi.createGraphics();
big.drawImage(i2,0,0,this);

//Use JPEGImageEncoder to write the BufferedImage to a file
try{
OutputStream os = new FileOutputStream(newImage);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(os);
encoder.encode(bi);
} catch(IOException ioe){
ioe.printStackTrace();
}
}

public void doResize(String originalImage, String newImage, int area) {
Image img = getToolkit().getImage(originalImage);
loadImage(img);
int iw = img.getWidth(this);
int ih = img.getHeight(this);

double factor = (double)area / (iw * ih);

//Reduce the image
int w = (int)(iw * factor);
Image i2 = img.getScaledInstance(w, -1, 0);
loadImage(i2);

//Load it into a BufferedImage
int i2w = i2.getWidth(this);
int i2h = i2.getHeight(this);
BufferedImage bi = new BufferedImage(i2w, i2h, BufferedImage.TYPE_INT_RGB);
Graphics2D big = bi.createGraphics();
big.drawImage(i2,0,0,this);

//Use JPEGImageEncoder to write the BufferedImage to a file
try{
OutputStream os = new FileOutputStream(newImage);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(os);
encoder.encode(bi);
} catch(IOException ioe){
ioe.printStackTrace();
}
}

/**
* Cause the image to be loaded into the Image object
*/
private void loadImage(Image img){
try {
MediaTracker tracker = new MediaTracker(this);
tracker.addImage(img, 0);
tracker.waitForID(0);
} catch (Exception e) {}
}

public static void main(String args[]) {
if(args.length != 3){usage();}
//double factor = Double.parseDouble(args[2]);
int area = Integer.parseInt(args[2]);
Resize resizer = new Resize();
//resizer.doResize(args[0], args[1], factor);
resizer.doResize(args[0], args[1], area);
System.exit(0);
}

public static void usage(){
System.out.println(“usage: java Resize original_file new_filename resize_factor”);
System.exit(1);
}

}

 

Thumbnail.java – Load an image, scale it to thumbnail size and save it as JPEG January 1, 2008

Filed under: Java — javatechie @ 5:39 am
import com.sun.image.codec.jpeg.*;
import java.awt.*;
import java.awt.image.*;
import java.io.*;

/**
 * Thumbnail.java (requires Java 1.2+)
 * Load an image, scale it down and save it as a JPEG file.
 * @author Marco Schmidt
 */
public class Thumbnail {
  public static void main(String[] args) throws Exception {
    if (args.length != 5) {
      System.err.println("Usage: java Thumbnail INFILE " +
        "OUTFILE WIDTH HEIGHT QUALITY");
      System.exit(1);
    }
    // load image from INFILE
    Image image = Toolkit.getDefaultToolkit().getImage(args[0]);
    MediaTracker mediaTracker = new MediaTracker(new Container());
    mediaTracker.addImage(image, 0);
    mediaTracker.waitForID(0);
    // determine thumbnail size from WIDTH and HEIGHT
    int thumbWidth = Integer.parseInt(args[2]);
    int thumbHeight = Integer.parseInt(args[3]);
    double thumbRatio = (double)thumbWidth / (double)thumbHeight;
    int imageWidth = image.getWidth(null);
    int imageHeight = image.getHeight(null);
    double imageRatio = (double)imageWidth / (double)imageHeight;
    if (thumbRatio < imageRatio) {
      thumbHeight = (int)(thumbWidth / imageRatio);
    } else {
      thumbWidth = (int)(thumbHeight * imageRatio);
    }
    // draw original image to thumbnail image object and
    // scale it to the new size on-the-fly
    BufferedImage thumbImage = new BufferedImage(thumbWidth,
      thumbHeight, BufferedImage.TYPE_INT_RGB);
    Graphics2D graphics2D = thumbImage.createGraphics();
    graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
      RenderingHints.VALUE_INTERPOLATION_BILINEAR);
    graphics2D.drawImage(image, 0, 0, thumbWidth, thumbHeight, null);
    // save thumbnail image to OUTFILE
    BufferedOutputStream out = new BufferedOutputStream(new
      FileOutputStream(args[1]));
    JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
    JPEGEncodeParam param = encoder.
      getDefaultJPEGEncodeParam(thumbImage);
    int quality = Integer.parseInt(args[4]);
    quality = Math.max(0, Math.min(quality, 100));
    param.setQuality((float)quality / 100.0f, false);
    encoder.setJPEGEncodeParam(param);
    encoder.encode(thumbImage);
    out.close();
    System.out.println("Done.");
    System.exit(0);
  }
}
 

Basic JPEG image resizing in Java January 1, 2008

Filed under: Java — javatechie @ 5:31 am

/* JpegResizerDemo.java */

import java.io.FileInputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.awt.image.RescaleOp;
import java.awt.image.AffineTransformOp;
import java.awt.geom.AffineTransform;

import java.util.Map;
import java.util.HashMap;

import com.sun.image.codec.jpeg.JPEGImageDecoder;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.ImageFormatException;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

/**
* @author CyranoVR
*/
public class ImageResizer {

public static void main(String args[]) {

if (args.length != 3) {
System.out.println(“Usage: ImageResizer <input> <output>
<scale>”);
System.exit(0);
}

String inFile = args[0];
String outFile = args[1];

FileInputStream fs = null;

try {

float scale = Float.parseFloat(args[2]);

fs = new FileInputStream(inFile);
JPEGImageDecoder decoder = JPEGCodec.createJPEGDecoder(fs);
BufferedImage srcImg = decoder.decodeAsBufferedImage();
fs.close();

AffineTransform af =
AffineTransform.getScaleInstance(scale, scale );

Map hints = new HashMap();
hints.put(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
RenderingHints rh = new RenderingHints(hints);

AffineTransformOp transform = new AffineTransformOp(af,rh);

BufferedImage destImg =
transform.createCompatibleDestImage(srcImg, srcImg.getColorModel());
transform.filter(srcImg, destImg);

FileOutputStream out = new FileOutputStream(outFile);

JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out,
JPEGCodec.getDefaultJPEGEncodeParam(destImg));

encoder.encode(destImg);
out.close();
System.out.println(“Saved file ” + outFile);

} catch (FileNotFoundException fnfe ) {
System.out.println(“File ” + inFile + ” does not exist!”);

} catch (NumberFormatException nfe){
System.out.println(“You entered ” + args[2] + “. Please
enter a decimal expression.”);

} catch (IOException ioe) {
System.out.println(“IO Exception: ” + ioe.getMessage());

} catch (ImageFormatException ife) {
System.out.println(“Image Format Excpetion: Could not
decode ” + inFile);

} catch (Exception e) {
System.out.println(e.getMessage());

} finally {
try {
if(fs != null)
fs.close();
} catch (Exception e) {
System.err.println(e.getMessage());
}
}
}
}