Skip to content

Distributed Peer-to-Peer Web Search Engine and Intranet Search Appliance

Render a relevant HTTP status code on snapshot image rendering error

luccioman746e0e788deb
diff --git a/htroot/api/snapshot.java b/htroot/api/snapshot.java
index c79c1eb97..7d17b2d3f 100644
--- a/htroot/api/snapshot.java
+++ b/htroot/api/snapshot.java
@@ -31,6 +31,7 @@ import java.util.Date;
import java.util.Map;
import java.util.TreeMap;
+import org.apache.http.HttpStatus;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrInputDocument;
@@ -46,9 +47,10 @@ import net.yacy.cora.util.Html2Image;
import net.yacy.cora.util.JSONException;
import net.yacy.cora.util.JSONObject;
import net.yacy.crawler.data.Snapshots;
-import net.yacy.crawler.data.Transactions;
import net.yacy.crawler.data.Snapshots.Revisions;
+import net.yacy.crawler.data.Transactions;
import net.yacy.document.ImageParser;
+import net.yacy.http.servlets.TemplateProcessingException;
import net.yacy.kelondro.util.FileUtils;
import net.yacy.peers.graphics.EncodedImage;
import net.yacy.search.Switchboard;
@@ -289,16 +291,24 @@ public class snapshot {
String imageFileStub = pdfFile.getAbsolutePath(); imageFileStub = imageFileStub.substring(0, imageFileStub.length() - 3); // cut off extension
File imageFile = new File(imageFileStub + DEFAULT_WIDTH + "." + DEFAULT_HEIGHT + "." + ext);
if (!imageFile.exists() && authenticated) {
- Html2Image.pdf2image(pdfFile, imageFile, DEFAULT_WIDTH, DEFAULT_HEIGHT, DEFAULT_DENSITY, DEFAULT_QUALITY);
+ if(!Html2Image.pdf2image(pdfFile, imageFile, DEFAULT_WIDTH, DEFAULT_HEIGHT, DEFAULT_DENSITY, DEFAULT_QUALITY)) {
+ throw new TemplateProcessingException(
+ "Could not generate the " + ext + " image snapshot file.");
+ }
+ }
+ if (!imageFile.exists()) {
+ throw new TemplateProcessingException(
+ "Could not find the " + ext
+ + " image snapshot file. You must be authenticated to generate one on the fly.",
+ HttpStatus.SC_NOT_FOUND);
}
- if (!imageFile.exists()) return null;
if (width == DEFAULT_WIDTH && height == DEFAULT_HEIGHT) {
try {
byte[] imageBinary = FileUtils.read(imageFile);
return new ByteArrayInputStream(imageBinary);
- } catch (IOException e) {
+ } catch (final IOException e) {
ConcurrentLog.logException(e);
- return null;
+ throw new TemplateProcessingException("Could not read the " + ext + " image snapshot file.");
}
}
// lets read the file and scale
@@ -306,8 +316,7 @@ public class snapshot {
try {
image = ImageParser.parse(imageFile.getAbsolutePath(), FileUtils.read(imageFile));
if(image == null) {
- /* Should not happen. If so, ImageParser.parse() should already have logged about the error */
- return null;
+ throw new TemplateProcessingException("Could not parse the " + ext + " image snapshot file.");
}
final Image scaled = image.getScaledInstance(width, height, Image.SCALE_AREA_AVERAGING);
final MediaTracker mediaTracker = new MediaTracker(new Container());
@@ -321,9 +330,9 @@ public class snapshot {
BufferedImage scaledBufferedImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
scaledBufferedImg.createGraphics().drawImage(scaled, 0, 0, width, height, null);
return new EncodedImage(scaledBufferedImg, ext, true);
- } catch (IOException e) {
+ } catch (final IOException e) {
ConcurrentLog.logException(e);
- return null;
+ throw new TemplateProcessingException("Could not scale the " + ext + " image snapshot file.");
}
}
diff --git a/source/net/yacy/http/servlets/TemplateMissingParameterException.java b/source/net/yacy/http/servlets/TemplateMissingParameterException.java
index 26aa6990a..93db3caf7 100644
--- a/source/net/yacy/http/servlets/TemplateMissingParameterException.java
+++ b/source/net/yacy/http/servlets/TemplateMissingParameterException.java
@@ -21,27 +21,31 @@
package net.yacy.http.servlets;
+import org.eclipse.jetty.http.HttpStatus;
+
/**
- * Use this to indicates a required parameter is missing for a template. Allows finer grained exception handling.
+ * Use this to indicates a required parameter is missing for a servlet template,
+ * and render a HTTP status 400 - bad Request. Allows finer grained exception
+ * handling.
+ *
* @author luc
*
*/
-public class TemplateMissingParameterException extends IllegalArgumentException {
-
- private static final long serialVersionUID = -3443324572847193267L;
+@SuppressWarnings("serial")
+public class TemplateMissingParameterException extends TemplateProcessingException {
/**
* Default constructor : use generic message.
*/
public TemplateMissingParameterException() {
- super("Missing required parameters");
+ super("Missing required parameters", HttpStatus.BAD_REQUEST_400);
}
/**
* @param message detail message
*/
- public TemplateMissingParameterException(String message) {
- super(message);
+ public TemplateMissingParameterException(final String message) {
+ super(message, HttpStatus.BAD_REQUEST_400);
}
}
diff --git a/source/net/yacy/http/servlets/TemplateProcessingException.java b/source/net/yacy/http/servlets/TemplateProcessingException.java
new file mode 100644
index 000000000..0cd49c4d9
--- /dev/null
+++ b/source/net/yacy/http/servlets/TemplateProcessingException.java
@@ -0,0 +1,71 @@
+// TemplateProcessingException.java
+// Copyright 2018 by luccioman; https://github.com/luccioman
+//
+// This is a part of YaCy, a peer-to-peer based web search engine
+//
+// LICENSE
+//
+// 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
+
+package net.yacy.http.servlets;
+
+import org.apache.http.HttpStatus;
+
+/**
+ * Use this to indicates that a servlet template processing error occurred, and
+ * which HTTP status should be rendered by the HTTP servlet.
+ *
+ */
+@SuppressWarnings("serial")
+public class TemplateProcessingException extends RuntimeException {
+
+ /** The HTTP status code that should be rendered. */
+ private final int status;
+
+ /**
+ * Default constructor : use a generic message and HTTP status 500 - Internal
+ * Server Error.
+ */
+ public TemplateProcessingException() {
+ this("An error occurred while processing the template.", HttpStatus.SC_INTERNAL_SERVER_ERROR);
+ }
+
+ /**
+ * Create an instance with a detail message, and the default HTTP status 500 -
+ * Internal Server Error.
+ *
+ * @param message the detail message
+ */
+ public TemplateProcessingException(final String message) {
+ this(message, HttpStatus.SC_INTERNAL_SERVER_ERROR);
+ }
+
+ /**
+ * @param message the detail message
+ * @param status the custom HTTP status code
+ */
+ public TemplateProcessingException(final String message, final int status) {
+ super(message);
+ this.status = status;
+ }
+
+ /**
+ * @return the HTTP status code that should be rendered.
+ */
+ public int getStatus() {
+ return this.status;
+ }
+
+}
diff --git a/source/net/yacy/http/servlets/YaCyDefaultServlet.java b/source/net/yacy/http/servlets/YaCyDefaultServlet.java
index f72f80598..f2a572ee6 100644
--- a/source/net/yacy/http/servlets/YaCyDefaultServlet.java
+++ b/source/net/yacy/http/servlets/YaCyDefaultServlet.java
@@ -895,11 +895,12 @@ public class YaCyDefaultServlet extends HttpServlet {
+ " If you sent this request with a web browser, please refresh the origin page.");
return;
}
- if(e.getCause() instanceof TemplateMissingParameterException) {
- /* A template is used but miss some required parameter */
- response.sendError(HttpServletResponse.SC_BAD_REQUEST, e.getCause().getMessage());
- return;
- }
+ if (e.getCause() instanceof TemplateProcessingException) {
+ /* A template processing error occurred, and the HTTP status and message have been set */
+ response.sendError(((TemplateProcessingException) e.getCause()).getStatus(),
+ e.getCause().getMessage());
+ return;
+ }
if(e.getCause() instanceof DisallowedMethodException) {
/* The request was sent using an disallowed HTTP method */
response.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, e.getCause().getMessage());