diff options
Diffstat (limited to 'pdf-as-web/src/main/java/at/gv/egiz/pdfas/web/servlets')
-rw-r--r-- | pdf-as-web/src/main/java/at/gv/egiz/pdfas/web/servlets/VisBlockServlet.java | 94 |
1 files changed, 94 insertions, 0 deletions
diff --git a/pdf-as-web/src/main/java/at/gv/egiz/pdfas/web/servlets/VisBlockServlet.java b/pdf-as-web/src/main/java/at/gv/egiz/pdfas/web/servlets/VisBlockServlet.java new file mode 100644 index 00000000..3008c25e --- /dev/null +++ b/pdf-as-web/src/main/java/at/gv/egiz/pdfas/web/servlets/VisBlockServlet.java @@ -0,0 +1,94 @@ +package at.gv.egiz.pdfas.web.servlets; + +import java.io.IOException; +import java.io.OutputStream; +import java.security.cert.CertificateException; + +import javax.servlet.ServletException; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import at.gv.egiz.pdfas.common.exceptions.PdfAsException; +import at.gv.egiz.pdfas.web.helper.PdfAsHelper; + +public class VisBlockServlet extends HttpServlet { + + private static final long serialVersionUID = 1L; + + private static final Logger logger = LoggerFactory + .getLogger(VisBlockServlet.class); + + private static String PARAM_PROFILE = "p"; + private static String PARAM_RESOLUTION = "r"; + + /** + * @see HttpServlet#HttpServlet() + */ + public VisBlockServlet() { + super(); + } + + /** + * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse + * response) + */ + protected void doGet(HttpServletRequest request, + HttpServletResponse response) throws ServletException, IOException { + this.process(request, response); + } + + /** + * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse + * response) + */ + protected void doPost(HttpServletRequest request, + HttpServletResponse response) throws ServletException, IOException { + this.process(request, response); + } + + protected void process(HttpServletRequest request, + HttpServletResponse response) throws ServletException, IOException { + + try { + + String profile = request.getParameter(PARAM_PROFILE); + String resolutionString = request.getParameter(PARAM_RESOLUTION); + + int resolution = Integer.parseInt(resolutionString); + + if (resolution < 16 || resolution > 512) { + throw new ServletException(PARAM_RESOLUTION + + " invalid value! Out of Range"); + } + + byte[] imageData = PdfAsHelper.generateVisualBlock(profile, resolution); + + if (imageData != null) { + + response.setHeader("Content-Disposition", "inline;filename=" + + profile + "_" + resolution + ".png"); + response.setContentType("image/png"); + OutputStream os = response.getOutputStream(); + os.write(imageData); + os.close(); + } else { + logger.warn("generateVisualBlock returned null!"); + throw new ServletException("Invalid profile id"); + } + } catch (NumberFormatException e) { + throw new ServletException(PARAM_RESOLUTION + + " invalid value! Not a Number"); + } catch (CertificateException e) { + logger.error("CERT Error", e); + throw new ServletException("Failed to find certificate"); + } catch (PdfAsException e) { + logger.error("PDF_AS Error", e); + throw new ServletException("Generic Error"); + } + } + +} |