You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
134 lines
4.6 KiB
134 lines
4.6 KiB
9 years ago
|
|
||
19 years ago
|
// ViewImage.java
|
||
|
// -----------------------
|
||
|
// part of YaCy
|
||
17 years ago
|
// (C) by Michael Peter Christen; mc@yacy.net
|
||
19 years ago
|
// 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
|
||
|
|
||
2 years ago
|
package net.yacy.htroot;
|
||
|
|
||
18 years ago
|
import java.io.IOException;
|
||
9 years ago
|
import java.io.InputStream;
|
||
19 years ago
|
|
||
9 years ago
|
import javax.imageio.ImageIO;
|
||
|
import javax.imageio.stream.ImageInputStream;
|
||
|
|
||
11 years ago
|
import net.yacy.cora.document.id.DigestURL;
|
||
10 years ago
|
import net.yacy.cora.document.id.MultiProtocolURL;
|
||
14 years ago
|
import net.yacy.cora.protocol.HeaderFramework;
|
||
|
import net.yacy.cora.protocol.RequestHeader;
|
||
9 years ago
|
import net.yacy.http.servlets.TemplateMissingParameterException;
|
||
10 years ago
|
import net.yacy.peers.graphics.EncodedImage;
|
||
13 years ago
|
import net.yacy.search.Switchboard;
|
||
12 years ago
|
import net.yacy.server.serverObjects;
|
||
|
import net.yacy.server.serverSwitch;
|
||
9 years ago
|
import net.yacy.visualization.ImageViewer;
|
||
19 years ago
|
|
||
|
public class ViewImage {
|
||
2 years ago
|
|
||
9 years ago
|
/** Single instance of ImageViewer */
|
||
|
private static final ImageViewer VIEWER = new ImageViewer();
|
||
9 years ago
|
|
||
9 years ago
|
/**
|
||
9 years ago
|
* Try parsing image from post "url" parameter (authenticated users) or from "code" parameter (non authenticated users).
|
||
9 years ago
|
* When image format is not supported, return directly image data. When
|
||
|
* image could be parsed, try encoding to target format specified by header
|
||
|
* "EXT".
|
||
2 years ago
|
*
|
||
9 years ago
|
* @param header
|
||
|
* request header
|
||
|
* @param post
|
||
|
* post parameters
|
||
|
* @param env
|
||
|
* environment
|
||
|
* @return an {@link EncodedImage} instance encoded in format specified in
|
||
2 years ago
|
* post, or an InputStream pointing to original image data.
|
||
|
* Return and EncodedImage with empty data when image format is not supported,
|
||
|
* a read/write or any other error occured while loading resource.
|
||
9 years ago
|
* @throws IOException
|
||
9 years ago
|
* when specified url is malformed.
|
||
9 years ago
|
* Sould end in a HTTP 500 error whose processing is more
|
||
9 years ago
|
* consistent across browsers than a response with zero content
|
||
|
* bytes.
|
||
9 years ago
|
* @throws TemplateMissingParameterException when one required parameter is missing
|
||
9 years ago
|
*/
|
||
|
public static Object respond(final RequestHeader header, final serverObjects post, final serverSwitch env)
|
||
|
throws IOException {
|
||
9 years ago
|
|
||
|
final Switchboard sb = (Switchboard) env;
|
||
|
|
||
9 years ago
|
if(post == null) {
|
||
|
throw new TemplateMissingParameterException("please fill at least url or code parameter");
|
||
|
}
|
||
9 years ago
|
|
||
2 years ago
|
final String ext = header.get(HeaderFramework.CONNECTION_PROP_EXT, null);
|
||
8 years ago
|
final boolean auth = ImageViewer.hasFullViewingRights(header, sb); // handle access rights
|
||
9 years ago
|
|
||
2 years ago
|
final DigestURL url = VIEWER.parseURL(post, auth);
|
||
9 years ago
|
|
||
|
// get the image as stream
|
||
9 years ago
|
EncodedImage encodedImage;
|
||
9 years ago
|
|
||
9 years ago
|
ImageInputStream imageInStream = null;
|
||
|
InputStream inStream = null;
|
||
|
try {
|
||
2 years ago
|
final String urlExt = MultiProtocolURL.getFileExtension(url.getFileName());
|
||
9 years ago
|
if (ext != null && ext.equalsIgnoreCase(urlExt) && ImageViewer.isBrowserRendered(urlExt)) {
|
||
|
return VIEWER.openInputStream(post, sb.loader, auth, url);
|
||
9 years ago
|
}
|
||
9 years ago
|
/*
|
||
9 years ago
|
* When opening a file, the most efficient is to open
|
||
|
* ImageInputStream directly on file
|
||
9 years ago
|
*/
|
||
9 years ago
|
if (url.isFile()) {
|
||
|
imageInStream = ImageIO.createImageInputStream(url.getFSFile());
|
||
|
} else {
|
||
|
inStream = VIEWER.openInputStream(post, sb.loader, auth, url);
|
||
|
imageInStream = ImageIO.createImageInputStream(inStream);
|
||
9 years ago
|
}
|
||
9 years ago
|
// read image
|
||
|
encodedImage = VIEWER.parseAndScale(post, auth, url, ext, imageInStream);
|
||
2 years ago
|
} catch (final Exception e) {
|
||
9 years ago
|
/*
|
||
9 years ago
|
* Exceptions are not propagated here : many error causes are
|
||
|
* possible, network errors, incorrect or unsupported format, bad
|
||
|
* ImageIO plugin... Instead return an empty EncodedImage. Caller is
|
||
|
* responsible for handling this correctly (500 status code
|
||
|
* response)
|
||
9 years ago
|
*/
|
||
9 years ago
|
encodedImage = new EncodedImage(new byte[0], ext, post.getBoolean("isStatic"));
|
||
9 years ago
|
} finally {
|
||
9 years ago
|
/*
|
||
9 years ago
|
* imageInStream.close() method doesn't close source input stream
|
||
9 years ago
|
*/
|
||
9 years ago
|
if (inStream != null) {
|
||
9 years ago
|
try {
|
||
9 years ago
|
inStream.close();
|
||
2 years ago
|
} catch (final IOException ignored) {
|
||
9 years ago
|
}
|
||
|
}
|
||
9 years ago
|
}
|
||
9 years ago
|
|
||
9 years ago
|
return encodedImage;
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
|
||
14 years ago
|
|
||
19 years ago
|
}
|