Home  | My Account  |  Membership
WorldDeveloper.org
Forum Details
Topics
Posts
Categories
 
577 member(s)
718,905 hit(s)
14 categories
51 topics
26 messages
0 online
Forums: Code Post
HostJ2ME.com Image proxy servlet source code
hostj2me
43 topic(s)
23 post(s)

April 02, 2007 4:34 AM


package com.astrientlabs.biz.web;

import java.awt.image.renderable.ParameterBlock;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import javax.media.jai.Interpolation;
import javax.media.jai.JAI;
import javax.media.jai.RenderedOp;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.astrientlabs.files.AstrientFile;
import com.astrientlabs.logging.LogWriter;
import com.astrientlabs.lpa.CachingFileStore;
import com.astrientlabs.misc.ApiKey;
import com.astrientlabs.util.Strings;
import com.astrientlabs.web.WebServlet;
import com.astrientlabs.webstats.StatRecorder;
import com.sun.media.jai.codec.FileSeekableStream;
import com.sun.media.jai.codec.JPEGEncodeParam;

public class ImageProxyServlet extends WebServlet
{
/**
*
*/
private static final long serialVersionUID = -7462909049956679674L;

private static final CachingFileStore fileStore = new CachingFileStore("/tmp/j2me/imageproxy");
private static final long maxFileSize = 1024L * 1024L * 10;

public void _doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{

URL u = null;
HttpURLConnection con = null;

try
{
StatRecorder.record(request);

String heightString = request.getParameter("h");
String widthString = request.getParameter("w");
String url = request.getParameter("u");
String format = Strings.ifNull(request.getParameter("f"), "JPEG");
//String auth = Strings.ifNull(request.getParameter("auth"), "public");

String apiKey = request.getParameter("key");
ApiKey.testValid(apiKey);

LogWriter.system.log(ImageProxyServlet.class, apiKey + " Requested file " + url);

u = new URL(url);

con = (HttpURLConnection) u.openConnection();

String id = "F" + u.getHost().hashCode() + "." + u.getPath().hashCode();

String path = fileStore.getLocation(id, "orig");
File f = new File(path);

if (con.getContentLength() != -1 && con.getContentLength() > maxFileSize)
{
response.sendError(HttpURLConnection.HTTP_ENTITY_TOO_LARGE, "requested resource is too large");
return;
}

if ( /*!Preferences.system.getBoolean("imgproxy.caching",false) ||*/ (!f.exists() || f.lastModified() con.getLastModified() || f.length() == 0 || con.getContentLength() != f.length()) )
{
LogWriter.system.log(ImageProxyServlet.class, "downloading updated image");
fileStore.invalidate(id, "orig");
fileStore.add(id, "orig", new byte[0]);

InputStream is = u.openStream();
FileOutputStream fos = new FileOutputStream(f);
try
{
int r;
int read = 0;
byte[] buffer = new byte[4*1024];
while ((r = is.read(buffer)) != -1)
{
fos.write(buffer, 0, r);
read += r;
if (read > maxFileSize)
{
response.sendError(HttpURLConnection.HTTP_ENTITY_TOO_LARGE, "requested resource is too large");
return;
}
}
fos.flush();
}
finally
{
if (is != null)
is.close();
try
{
fos.close();
}
catch (Exception e)
{
}
}
}

String newFilePath = format.toUpperCase().hashCode() + "." + heightString + "." + widthString;

path = fileStore.getLocation(id, newFilePath);
File newFile = new File(path);

if (!newFile.exists() || newFile.lastModified() f.lastModified() || newFile.length() == 0)
{
fileStore.invalidate(id, newFilePath);

FileSeekableStream stream = new FileSeekableStream(f);
RenderedOp image = JAI.create("stream", stream);

scale(image, format, path, widthString, heightString);
image.dispose();
}

try
{
response.setContentType(AstrientFile.fileType(new FileSeekableStream(newFile)));
}
catch (Exception e)
{
LogWriter.system.log(ImageProxyServlet.class, e);
}

fileStore.writeTo(response.getOutputStream(), id, newFilePath);
}
catch (Exception e)
{
LogWriter.system.log(ImageProxyServlet.class, e);
response.sendError(HttpURLConnection.HTTP_NO_CONTENT, e.getMessage());
}
finally
{
if ( con != null )
{
try
{
con.disconnect();
}
catch (Exception ignored)
{

}
}
}
}

public void scale(RenderedOp image, String format, String location, String widthString, String heightString)
{
int width = (widthString == null) ? image.getWidth() : Integer.parseInt(widthString);
int height = (heightString == null) ? image.getHeight() : Integer.parseInt(heightString);

try
{
/*boolean landscape = (image.getWidth() > image.getHeight());
if (landscape && (height > width))
{
int tmpi = height;
height = width;
width = tmpi;
}*/

double cw = width / (1.0 * image.getWidth());
double ch = height / (1.0 * image.getHeight());

double scale;
if (cw ch)
{
scale = cw;
}
else
{
scale = ch;
}

if (scale > 1.0)
scale = 1.0;

Interpolation interp = Interpolation.getInstance(Interpolation.INTERP_BICUBIC);
ParameterBlock params = new ParameterBlock();
params.addSource(image);
params.add(new Float(scale));
params.add(new Float(scale));
params.add(0.0F);
params.add(0.0F);
params.add(interp);

RenderedOp image1 = JAI.create("scale", params);

if (format.equalsIgnoreCase("JPEG"))
{
JPEGEncodeParam param = new JPEGEncodeParam();
param.setQuality(1.2F);
RenderedOp op = JAI.create("filestore", image1, location, format, param);
op.dispose();
}
else
{
RenderedOp op = JAI.create("filestore", image1, location, format);
op.dispose();
}

}
finally
{
}
}
}


©2008 WorldDeveloper.org - PO Box 310575 Miami, FL 33231

WorldDeveloper.org is a managed social network for application developers around the world.