Skip to content

Commit

Permalink
added image-viewer servlet that can do:
Browse files Browse the repository at this point in the history
- each image that is requested is stored in the cache
- the image is taken from the cache if exists there
- the image can be scaled
The purpose of creation a scaled image is because of copyright problems
In a further stept the retrieval of not-shrinked images is restricted
to either access from localhost or with given authentication
This servlet can be used for image-preview purpose after an image search

git-svn-id: https://svn.berlios.de/svnroot/repos/yacy/trunk@1989 6c8d7289-2bf4-0310-a012-ef5d649a1542
  • Loading branch information
orbiter committed Apr 2, 2006
1 parent ddc6394 commit d8d0ac2
Show file tree
Hide file tree
Showing 4 changed files with 161 additions and 25 deletions.
89 changes: 89 additions & 0 deletions htroot/ViewImage.java
@@ -0,0 +1,89 @@
// ViewImage.java
// -----------------------
// part of YaCy
// (C) by Michael Peter Christen; mc@anomic.de
// first published on http://www.anomic.de
// Frankfurt, Germany, 2006
// created 03.04.2006
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
// Using this software in any meaning (reading, learning, copying, compiling,
// running) means that you agree that the Author(s) is (are) not responsible
// for cost, loss of data or any harm that may be caused directly or indirectly
// by usage of this softare or this documentation. The usage of this software
// is on your own risk. The installation and usage (starting/running) of this
// software may allow other people or application to access your computer and
// any attached devices and is highly dependent on the configuration of the
// software which must be done by the user of the software; the author(s) is
// (are) also not responsible for proper configuration and usage of the
// software, even if provoked by documentation provided together with
// the software.
//
// Any changes to this file according to the GPL as documented in the file
// gpl.txt aside this file in the shipment you received can be done to the
// lines that follows this copyright notice here, but changes must not be
// done inside the copyright notive above. A re-distribution must contain
// the intact and unchanged copyright notice.
// Contributions and changes to the program code must be marked as such.

import java.awt.Container;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.Toolkit;
import java.net.MalformedURLException;
import java.net.URL;

import de.anomic.http.httpHeader;
import de.anomic.plasma.plasmaSwitchboard;
import de.anomic.server.serverObjects;
import de.anomic.server.serverSwitch;

public class ViewImage {

public static Image respond(httpHeader header, serverObjects post, serverSwitch env) {

plasmaSwitchboard sb = (plasmaSwitchboard)env;

String urls = post.get("url", "");
URL url;
try {
url = new URL(urls);
} catch (MalformedURLException e1) {
return null;
}
int width = post.getInt("width", 0);
int height = post.getInt("height", 0);

// load image
byte[] imgb = sb.snippetCache.getResource(url, true);
if (imgb == null) return null;

// create image
MediaTracker mediaTracker = new MediaTracker(new Container());
Image original = Toolkit.getDefaultToolkit().createImage(imgb);
mediaTracker.addImage(original, 0);
try {mediaTracker.waitForID(0);} catch (InterruptedException e) {}
if ((width == 0) || (height == 0)) return original;

// scale image
Image scaled = original.getScaledInstance(width, height, Image.SCALE_AREA_AVERAGING);
mediaTracker.addImage(scaled, 0);
try {mediaTracker.waitForID(0);} catch (InterruptedException e) {}

return scaled;
}

}
82 changes: 61 additions & 21 deletions source/de/anomic/http/httpdFileHandler.java
Expand Up @@ -74,6 +74,8 @@ public static java.util.Hashtable respond(java.util.HashMap, serverSwitch)

package de.anomic.http;

import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.File;
Expand Down Expand Up @@ -456,12 +458,12 @@ public void doResponse(Properties conProp, httpHeader requestHeader, OutputStrea

if ((targetClass != null) && (path.endsWith("png"))) {
// call an image-servlet to produce an on-the-fly - generated image
ymagePainter yp = null;
Object img = null;
try {
requestHeader.put("CLIENTIP", conProp.getProperty("CLIENTIP"));
requestHeader.put("PATH", path);
// in case that there are no args given, args = null or empty hashmap
yp = (ymagePainter) rewriteMethod(targetClass).invoke(null, new Object[] {requestHeader, args, switchboard});
img = rewriteMethod(targetClass).invoke(null, new Object[] {requestHeader, args, switchboard});
} catch (InvocationTargetException e) {
this.theLogger.logSevere("INTERNAL ERROR: " + e.toString() + ":" +
e.getMessage() +
Expand All @@ -471,28 +473,66 @@ public void doResponse(Properties conProp, httpHeader requestHeader, OutputStrea
"; java.awt.graphicsenv='" + System.getProperty("java.awt.graphicsenv","") + "'",e);
targetClass = null;
}
if (yp == null) {
if (img == null) {
// error with image generation; send file-not-found
httpd.sendRespondError(this.connectionProperties,out,3,404,"File not Found",null,null);
} else {
// send an image to client
targetDate = new Date(System.currentTimeMillis());
nocache = true;
String mimeType = mimeTable.getProperty(targetExt,"text/html");

// generate an byte array from the generated image
serverByteBuffer baos = new serverByteBuffer();
//ymagePNGEncoderJDE jde = new ymagePNGEncoderJDE((ymageMatrixPainter) yp, ymagePNGEncoderJDE.FILTER_NONE, 0);
//byte[] result = jde.pngEncode();
ImageIO.write(ymagePNGEncoderAWT.toImage((ymageMatrixPainter) yp, true), targetExt, baos);
byte[] result = baos.toByteArray();
baos.close(); baos = null;

// write the array to the client
httpd.sendRespondHeader(this.connectionProperties, out, httpVersion, 200, null, mimeType, result.length, targetDate, null, null, null, null, nocache);
if (! method.equals(httpHeader.METHOD_HEAD)) {
Thread.sleep(200); // see below
serverFileUtils.write(result, out);
if (img instanceof ymagePainter) {
ymagePainter yp = (ymagePainter) img;
// send an image to client
targetDate = new Date(System.currentTimeMillis());
nocache = true;
String mimeType = mimeTable.getProperty(targetExt, "text/html");

// generate an byte array from the generated image
serverByteBuffer baos = new serverByteBuffer();
// ymagePNGEncoderJDE jde = new
// ymagePNGEncoderJDE((ymageMatrixPainter) yp,
// ymagePNGEncoderJDE.FILTER_NONE, 0);
// byte[] result = jde.pngEncode();
ImageIO.write(ymagePNGEncoderAWT.toImage(
(ymageMatrixPainter) yp, true),
targetExt, baos);
byte[] result = baos.toByteArray();
baos.close();
baos = null;

// write the array to the client
httpd.sendRespondHeader(this.connectionProperties, out,
httpVersion, 200, null, mimeType,
result.length, targetDate, null, null, null,
null, nocache);
if (!method.equals(httpHeader.METHOD_HEAD)) {
Thread.sleep(200); // see below
serverFileUtils.write(result, out);
}
}
if (img instanceof Image) {
Image i = (Image) img;
// send an image to client
targetDate = new Date(System.currentTimeMillis());
nocache = true;
String mimeType = mimeTable.getProperty(targetExt, "text/html");

// generate an byte array from the generated image
serverByteBuffer baos = new serverByteBuffer();
BufferedImage bi = new BufferedImage(i.getWidth(null), i.getHeight(null), BufferedImage.TYPE_INT_RGB);
bi.createGraphics().drawImage(i, 0, 0, i.getWidth(null), i.getHeight(null), null);
ImageIO.write(bi, targetExt, baos);

byte[] result = baos.toByteArray();
baos.close();
baos = null;

// write the array to the client
httpd.sendRespondHeader(this.connectionProperties, out,
httpVersion, 200, null, mimeType,
result.length, targetDate, null, null, null,
null, nocache);
if (!method.equals(httpHeader.METHOD_HEAD)) {
Thread.sleep(200); // see below
serverFileUtils.write(result, out);
}
}
}
} else if ((targetClass != null) && (path.endsWith(".stream"))) {
Expand Down
14 changes: 10 additions & 4 deletions source/de/anomic/plasma/plasmaCrawlWorker.java
Expand Up @@ -215,6 +215,7 @@ public void execute() {
this.socketTimeout,
this.sb.remoteProxyConfig,
this.cacheManager,
false,
this.log
);

Expand Down Expand Up @@ -256,6 +257,7 @@ public static plasmaHTCache.Entry load(
int socketTimeout,
httpRemoteProxyConfig theRemoteProxyConfig,
plasmaHTCache cacheManager,
boolean acceptAllContent,
serverLog log
) throws IOException {
return load(url,
Expand All @@ -269,7 +271,8 @@ public static plasmaHTCache.Entry load(
cacheManager,
log,
DEFAULT_CRAWLING_RETRY_COUNT,
true
true,
acceptAllContent
);
}

Expand All @@ -285,7 +288,8 @@ private static plasmaHTCache.Entry load(
plasmaHTCache cacheManager,
serverLog log,
int crawlingRetryCount,
boolean useContentEncodingGzip
boolean useContentEncodingGzip,
boolean acceptAllContent
) throws IOException {
if (url == null) return null;

Expand Down Expand Up @@ -378,7 +382,7 @@ private static plasmaHTCache.Entry load(
// request has been placed and result has been returned. work off response
File cacheFile = cacheManager.getCachePath(url);
try {
if (plasmaParser.supportedContent(plasmaParser.PARSER_MODE_CRAWLER,url,res.responseHeader.mime())) {
if ((acceptAllContent) || (plasmaParser.supportedContent(plasmaParser.PARSER_MODE_CRAWLER,url,res.responseHeader.mime()))) {
if (cacheFile.isFile()) {
cacheManager.deleteFile(url);
}
Expand Down Expand Up @@ -465,7 +469,8 @@ private static plasmaHTCache.Entry load(
cacheManager,
log,
--crawlingRetryCount,
useContentEncodingGzip
useContentEncodingGzip,
acceptAllContent
);

if (redirectedEntry != null) {
Expand Down Expand Up @@ -574,6 +579,7 @@ private static plasmaHTCache.Entry load(
cacheManager,
log,
--crawlingRetryCount,
false,
false
);
}
Expand Down
1 change: 1 addition & 0 deletions source/de/anomic/plasma/plasmaSnippetCache.java
Expand Up @@ -424,6 +424,7 @@ public plasmaHTCache.Entry loadResourceFromWeb(URL url, int socketTimeout) throw
socketTimeout,
this.sb.remoteProxyConfig,
this.cacheManager,
true,
this.log);
}

Expand Down

0 comments on commit d8d0ac2

Please sign in to comment.