From d8d0ac29c3c413027cffc64cdcdc544db1a38c92 Mon Sep 17 00:00:00 2001 From: orbiter Date: Sun, 2 Apr 2006 22:59:53 +0000 Subject: [PATCH] added image-viewer servlet that can do: - 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 --- htroot/ViewImage.java | 89 +++++++++++++++++++ source/de/anomic/http/httpdFileHandler.java | 82 ++++++++++++----- .../de/anomic/plasma/plasmaCrawlWorker.java | 14 ++- .../de/anomic/plasma/plasmaSnippetCache.java | 1 + 4 files changed, 161 insertions(+), 25 deletions(-) create mode 100644 htroot/ViewImage.java diff --git a/htroot/ViewImage.java b/htroot/ViewImage.java new file mode 100644 index 000000000..3bdbeaf0c --- /dev/null +++ b/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; + } + +} diff --git a/source/de/anomic/http/httpdFileHandler.java b/source/de/anomic/http/httpdFileHandler.java index 132411c10..4abb7b065 100644 --- a/source/de/anomic/http/httpdFileHandler.java +++ b/source/de/anomic/http/httpdFileHandler.java @@ -74,6 +74,8 @@ 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; @@ -456,12 +458,12 @@ public final class httpdFileHandler extends httpdAbstractHandler implements http 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() + @@ -471,28 +473,66 @@ public final class httpdFileHandler extends httpdAbstractHandler implements http "; 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"))) { diff --git a/source/de/anomic/plasma/plasmaCrawlWorker.java b/source/de/anomic/plasma/plasmaCrawlWorker.java index ae0c225b2..ee7a76cfb 100644 --- a/source/de/anomic/plasma/plasmaCrawlWorker.java +++ b/source/de/anomic/plasma/plasmaCrawlWorker.java @@ -215,6 +215,7 @@ public final class plasmaCrawlWorker extends Thread { this.socketTimeout, this.sb.remoteProxyConfig, this.cacheManager, + false, this.log ); @@ -256,6 +257,7 @@ public final class plasmaCrawlWorker extends Thread { int socketTimeout, httpRemoteProxyConfig theRemoteProxyConfig, plasmaHTCache cacheManager, + boolean acceptAllContent, serverLog log ) throws IOException { return load(url, @@ -269,7 +271,8 @@ public final class plasmaCrawlWorker extends Thread { cacheManager, log, DEFAULT_CRAWLING_RETRY_COUNT, - true + true, + acceptAllContent ); } @@ -285,7 +288,8 @@ public final class plasmaCrawlWorker extends Thread { plasmaHTCache cacheManager, serverLog log, int crawlingRetryCount, - boolean useContentEncodingGzip + boolean useContentEncodingGzip, + boolean acceptAllContent ) throws IOException { if (url == null) return null; @@ -378,7 +382,7 @@ public final class plasmaCrawlWorker extends Thread { // 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); } @@ -465,7 +469,8 @@ public final class plasmaCrawlWorker extends Thread { cacheManager, log, --crawlingRetryCount, - useContentEncodingGzip + useContentEncodingGzip, + acceptAllContent ); if (redirectedEntry != null) { @@ -574,6 +579,7 @@ public final class plasmaCrawlWorker extends Thread { cacheManager, log, --crawlingRetryCount, + false, false ); } diff --git a/source/de/anomic/plasma/plasmaSnippetCache.java b/source/de/anomic/plasma/plasmaSnippetCache.java index a62db6a3a..c7e470070 100644 --- a/source/de/anomic/plasma/plasmaSnippetCache.java +++ b/source/de/anomic/plasma/plasmaSnippetCache.java @@ -424,6 +424,7 @@ public class plasmaSnippetCache { socketTimeout, this.sb.remoteProxyConfig, this.cacheManager, + true, this.log); }