Search in sources :

Example 1 with SecurityException

use of cz.incad.kramerius.security.SecurityException in project kramerius by ceskaexpedice.

the class PDFResource method selection.

/**
 * Generate pdf from selection
 * @param pidsParam List of pids
 * @param pageType First page type. Possible values TEXT, IMAGE
 * @param format Page format. Possible values : A0,...A5, B0,...B5, LETTER, POSTCARD
 * @return
 * @throws OutOfRangeException
 */
@GET
@Path("selection")
@Produces({ "application/pdf", "application/json" })
public Response selection(@QueryParam("pids") String pidsParam, @QueryParam("firstPageType") @DefaultValue("TEXT") String pageType, @QueryParam("format") String format) throws OutOfRangeException {
    boolean acquired = false;
    try {
        acquired = PDFExlusiveGenerateSupport.PDF_SEMAPHORE.tryAcquire();
        if (acquired) {
            try {
                AbstractPDFResource.FirstPage fp = pageType != null ? AbstractPDFResource.FirstPage.valueOf(pageType) : AbstractPDFResource.FirstPage.TEXT;
                if (StringUtils.isAnyString(pidsParam)) {
                    String[] pids = pidsParam.split(",");
                    // max number test
                    ConfigurationUtils.checkNumber(pids);
                    Rectangle formatRect = formatRect(format);
                    final File generatedPDF = super.selection(pids, formatRect, fp);
                    final InputStream fis = new FileInputStream(generatedPDF);
                    StreamingOutput stream = new StreamingOutput() {

                        public void write(OutputStream output) throws IOException, WebApplicationException {
                            try {
                                IOUtils.copyStreams(fis, output);
                            } catch (Exception e) {
                                throw new WebApplicationException(e);
                            } finally {
                                if (generatedPDF != null)
                                    generatedPDF.delete();
                            }
                        }
                    };
                    SimpleDateFormat sdate = new SimpleDateFormat("yyyyMMdd_mmhhss");
                    return Response.ok().header("Content-disposition", "attachment; filename=" + sdate.format(new Date()) + ".pdf").entity(stream).type("application/pdf").build();
                } else {
                    return Response.status(Response.Status.BAD_REQUEST).build();
                }
            } catch (MalformedURLException e) {
                LOGGER.log(Level.SEVERE, e.getMessage(), e);
                throw new GenericApplicationException(e.getMessage());
            } catch (IOException e) {
                LOGGER.log(Level.SEVERE, e.getMessage(), e);
                throw new GenericApplicationException(e.getMessage());
            } catch (ProcessSubtreeException e) {
                LOGGER.log(Level.SEVERE, e.getMessage(), e);
                throw new GenericApplicationException(e.getMessage());
            } catch (DocumentException e) {
                LOGGER.log(Level.SEVERE, e.getMessage(), e);
                throw new GenericApplicationException(e.getMessage());
            } catch (SecurityException e) {
                LOGGER.log(Level.INFO, e.getMessage());
                throw new ActionNotAllowed(e.getMessage());
            }
        } else {
            throw new PDFResourceNotReadyException("not ready");
        }
    } finally {
        if (acquired)
            PDFExlusiveGenerateSupport.PDF_SEMAPHORE.release();
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) WebApplicationException(javax.ws.rs.WebApplicationException) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) Rectangle(com.lowagie.text.Rectangle) StreamingOutput(javax.ws.rs.core.StreamingOutput) SecurityException(cz.incad.kramerius.security.SecurityException) IOException(java.io.IOException) GenericApplicationException(cz.incad.kramerius.rest.api.exceptions.GenericApplicationException) ActionNotAllowed(cz.incad.kramerius.rest.api.exceptions.ActionNotAllowed) FileInputStream(java.io.FileInputStream) XPathExpressionException(javax.xml.xpath.XPathExpressionException) ProcessSubtreeException(cz.incad.kramerius.ProcessSubtreeException) JSONException(org.json.JSONException) SecurityException(cz.incad.kramerius.security.SecurityException) GenericApplicationException(cz.incad.kramerius.rest.api.exceptions.GenericApplicationException) FileNotFoundException(java.io.FileNotFoundException) WebApplicationException(javax.ws.rs.WebApplicationException) MalformedURLException(java.net.MalformedURLException) BadRequestException(cz.incad.kramerius.rest.api.exceptions.BadRequestException) IOException(java.io.IOException) DocumentException(com.lowagie.text.DocumentException) OutOfRangeException(cz.incad.kramerius.pdf.OutOfRangeException) Date(java.util.Date) ProcessSubtreeException(cz.incad.kramerius.ProcessSubtreeException) DocumentException(com.lowagie.text.DocumentException) File(java.io.File) SimpleDateFormat(java.text.SimpleDateFormat) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 2 with SecurityException

use of cz.incad.kramerius.security.SecurityException in project kramerius by ceskaexpedice.

the class PDFResource method parent.

/**
 * Generate whole document
 * @param pid PID of generating document
 * @param number Number of pages (whole document or maximum number of pages)
 * @param pageType Type of firt page. Possible values: TEXT,IMAGE
 * @param format Page format. Possible values : A0,...A5, B0,...B5, LETTER, POSTCARD
 * @return
 */
@GET
@Path("parent")
@Produces({ "application/pdf", "application/json" })
public Response parent(@QueryParam("pid") String pid, @QueryParam("number") String number, @QueryParam("firstPageType") @DefaultValue("TEXT") String pageType, @QueryParam("format") String format) {
    boolean acquired = false;
    try {
        acquired = PDFExlusiveGenerateSupport.PDF_SEMAPHORE.tryAcquire();
        if (acquired) {
            try {
                AbstractPDFResource.FirstPage fp = pageType != null ? AbstractPDFResource.FirstPage.valueOf(pageType) : AbstractPDFResource.FirstPage.TEXT;
                // max number test
                int n = ConfigurationUtils.checkNumber(number);
                Rectangle formatRect = formatRect(format);
                final File generatedPdf = super.parent(pid, n, formatRect, fp);
                final InputStream fis = new FileInputStream(generatedPdf);
                StreamingOutput stream = new StreamingOutput() {

                    public void write(OutputStream output) throws IOException, WebApplicationException {
                        try {
                            IOUtils.copyStreams(fis, output);
                        } catch (Exception e) {
                            throw new WebApplicationException(e);
                        } finally {
                            if (generatedPdf != null)
                                generatedPdf.delete();
                        }
                    }
                };
                SimpleDateFormat sdate = new SimpleDateFormat("yyyyMMdd_mmhhss");
                return Response.ok().header("Content-disposition", "attachment; filename=" + sdate.format(new Date()) + ".pdf").entity(stream).type("application/pdf").build();
            } catch (NumberFormatException e) {
                LOGGER.log(Level.SEVERE, e.getMessage(), e);
                throw new GenericApplicationException(e.getMessage());
            } catch (FileNotFoundException e) {
                LOGGER.log(Level.SEVERE, e.getMessage(), e);
                throw new GenericApplicationException(e.getMessage());
            } catch (DocumentException e) {
                LOGGER.log(Level.SEVERE, e.getMessage(), e);
                throw new GenericApplicationException(e.getMessage());
            } catch (IOException e) {
                LOGGER.log(Level.SEVERE, e.getMessage(), e);
                throw new GenericApplicationException(e.getMessage());
            } catch (ProcessSubtreeException e) {
                LOGGER.log(Level.SEVERE, e.getMessage(), e);
                throw new GenericApplicationException(e.getMessage());
            } catch (OutOfRangeException e1) {
                LOGGER.log(Level.WARNING, "too much pages for pdf generating - consider changing config attribute (generatePdfMaxRange)");
                throw new PDFResourceBadRequestException(e1.getMessage());
            } catch (SecurityException e1) {
                LOGGER.log(Level.INFO, e1.getMessage());
                throw new ActionNotAllowed(e1.getMessage());
            }
        } else {
            throw new PDFResourceNotReadyException("not ready");
        }
    } finally {
        if (acquired)
            PDFExlusiveGenerateSupport.PDF_SEMAPHORE.release();
    }
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) Rectangle(com.lowagie.text.Rectangle) FileNotFoundException(java.io.FileNotFoundException) StreamingOutput(javax.ws.rs.core.StreamingOutput) GenericApplicationException(cz.incad.kramerius.rest.api.exceptions.GenericApplicationException) DocumentException(com.lowagie.text.DocumentException) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) SecurityException(cz.incad.kramerius.security.SecurityException) IOException(java.io.IOException) ActionNotAllowed(cz.incad.kramerius.rest.api.exceptions.ActionNotAllowed) FileInputStream(java.io.FileInputStream) XPathExpressionException(javax.xml.xpath.XPathExpressionException) ProcessSubtreeException(cz.incad.kramerius.ProcessSubtreeException) JSONException(org.json.JSONException) SecurityException(cz.incad.kramerius.security.SecurityException) GenericApplicationException(cz.incad.kramerius.rest.api.exceptions.GenericApplicationException) FileNotFoundException(java.io.FileNotFoundException) WebApplicationException(javax.ws.rs.WebApplicationException) MalformedURLException(java.net.MalformedURLException) BadRequestException(cz.incad.kramerius.rest.api.exceptions.BadRequestException) IOException(java.io.IOException) DocumentException(com.lowagie.text.DocumentException) OutOfRangeException(cz.incad.kramerius.pdf.OutOfRangeException) Date(java.util.Date) ProcessSubtreeException(cz.incad.kramerius.ProcessSubtreeException) OutOfRangeException(cz.incad.kramerius.pdf.OutOfRangeException) File(java.io.File) SimpleDateFormat(java.text.SimpleDateFormat) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 3 with SecurityException

use of cz.incad.kramerius.security.SecurityException in project kramerius by ceskaexpedice.

the class ItemResource method stream.

@GET
@Path("{pid}/streams/{dsid}")
public Response stream(@PathParam("pid") String pid, @PathParam("dsid") String dsid) {
    try {
        checkPid(pid);
        if (!FedoraUtils.FEDORA_INTERNAL_STREAMS.contains(dsid)) {
            if (!PIDSupport.isComposedPID(pid)) {
                // audio streams - bacause of support rage in headers
                if (FedoraUtils.AUDIO_STREAMS.contains(dsid)) {
                    String mimeTypeForStream = this.fedoraAccess.getMimeTypeForStream(pid, dsid);
                    ResponseBuilder responseBuilder = Response.ok();
                    responseBuilder = responseBuilder.type(mimeTypeForStream);
                    HttpServletRequest request = this.requestProvider.get();
                    User user = this.userProvider.get();
                    AudioStreamId audioStreamId = new AudioStreamId(pid, AudioFormat.valueOf(dsid));
                    ResponseBuilder builder = AudioStreamForwardUtils.GET(audioStreamId, request, responseBuilder, solrAccess, user, this.rightsResolver, urlManager);
                    return builder.build();
                } else {
                    final InputStream is = this.fedoraAccess.getDataStream(pid, dsid);
                    String mimeTypeForStream = this.fedoraAccess.getMimeTypeForStream(pid, dsid);
                    StreamingOutput stream = new StreamingOutput() {

                        public void write(OutputStream output) throws IOException, WebApplicationException {
                            try {
                                IOUtils.copyStreams(is, output);
                            } catch (Exception e) {
                                throw new WebApplicationException(e);
                            }
                        }
                    };
                    return Response.ok().entity(stream).type(mimeTypeForStream).build();
                }
            } else
                throw new PIDNotFound("cannot find stream " + dsid);
        } else {
            throw new PIDNotFound("cannot find stream " + dsid);
        }
    } catch (IOException e) {
        throw new PIDNotFound(e.getMessage());
    } catch (SecurityException e) {
        throw new ActionNotAllowed(e.getMessage());
    }
}
Also used : AudioStreamId(cz.incad.kramerius.audio.AudioStreamId) User(cz.incad.kramerius.security.User) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) SecurityException(cz.incad.kramerius.security.SecurityException) IOException(java.io.IOException) ActionNotAllowed(cz.incad.kramerius.rest.api.exceptions.ActionNotAllowed) URISyntaxException(java.net.URISyntaxException) LexerException(cz.incad.kramerius.utils.pid.LexerException) JSONException(org.json.JSONException) SecurityException(cz.incad.kramerius.security.SecurityException) GenericApplicationException(cz.incad.kramerius.rest.api.exceptions.GenericApplicationException) ResourceIndexException(cz.incad.kramerius.resourceindex.ResourceIndexException) ReplicateException(cz.incad.kramerius.service.ReplicateException) IOException(java.io.IOException) HttpServletRequest(javax.servlet.http.HttpServletRequest) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) PIDNotFound(cz.incad.kramerius.rest.api.k5.client.item.exceptions.PIDNotFound)

Example 4 with SecurityException

use of cz.incad.kramerius.security.SecurityException in project kramerius by ceskaexpedice.

the class PDFResource method selection.

/**
 * Generate pdf from selection
 *
 * @param pidsParam     List of pids
 * @param firstPageType First page type. Possible values TEXT, IMAGE
 * @param format        Page format. Possible values : A0,...A5, B0,...B5, LETTER, POSTCARD
 * @return
 * @throws OutOfRangeException
 */
@GET
@Path("selection")
@Produces({ "application/pdf", "application/json" })
public Response selection(@QueryParam("pids") String pidsParam, @QueryParam("firstPageType") @DefaultValue("TEXT") String firstPageType, @QueryParam("format") String format) throws OutOfRangeException {
    if (PDF_ENDPOINTS_DISABLED) {
        return Response.status(Response.Status.SERVICE_UNAVAILABLE).build();
    }
    boolean acquired = false;
    try {
        acquired = PDFExlusiveGenerateSupport.PDF_SEMAPHORE.tryAcquire();
        if (acquired) {
            try {
                FirstPageType fistPageTypeEn = extractFirstPageType(firstPageType);
                if (StringUtils.isAnyString(pidsParam)) {
                    String[] pids = pidsParam.split(",");
                    // max number test
                    int numberOfPages = extractNumberOfPages("" + pids.length);
                    // ConfigurationUtils.checkNumber(pids);
                    // TODO: remove for production
                    LOGGER.info("number of pages: " + numberOfPages);
                    Rectangle formatRect = formatRect(format);
                    final File generatedPDF = super.selection(pids, formatRect, fistPageTypeEn);
                    final InputStream fis = new FileInputStream(generatedPDF);
                    StreamingOutput stream = new StreamingOutput() {

                        public void write(OutputStream output) throws IOException, WebApplicationException {
                            try {
                                IOUtils.copyStreams(fis, output);
                            } catch (Exception e) {
                                throw new WebApplicationException(e);
                            } finally {
                                if (generatedPDF != null)
                                    generatedPDF.delete();
                            }
                        }
                    };
                    SimpleDateFormat sdate = new SimpleDateFormat("yyyyMMdd_mmhhss");
                    return Response.ok().header("Content-disposition", "attachment; filename=" + sdate.format(new Date()) + ".pdf").entity(stream).type("application/pdf").build();
                } else {
                    return Response.status(Response.Status.BAD_REQUEST).build();
                }
            } catch (MalformedURLException e) {
                LOGGER.log(Level.SEVERE, e.getMessage(), e);
                throw new GenericApplicationException(e.getMessage());
            } catch (IOException e) {
                LOGGER.log(Level.SEVERE, e.getMessage(), e);
                throw new GenericApplicationException(e.getMessage());
            } catch (ProcessSubtreeException e) {
                LOGGER.log(Level.SEVERE, e.getMessage(), e);
                throw new GenericApplicationException(e.getMessage());
            } catch (DocumentException e) {
                LOGGER.log(Level.SEVERE, e.getMessage(), e);
                throw new GenericApplicationException(e.getMessage());
            } catch (SecurityException e) {
                LOGGER.log(Level.INFO, e.getMessage());
                throw new ActionNotAllowed(e.getMessage());
            }
        } else {
            throw new PDFResourceNotReadyException("not ready");
        }
    } finally {
        if (acquired)
            PDFExlusiveGenerateSupport.PDF_SEMAPHORE.release();
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) StreamingOutput(javax.ws.rs.core.StreamingOutput) SecurityException(cz.incad.kramerius.security.SecurityException) GenericApplicationException(cz.incad.kramerius.rest.api.exceptions.GenericApplicationException) ActionNotAllowed(cz.incad.kramerius.rest.api.exceptions.ActionNotAllowed) PDFResourceNotReadyException(cz.incad.kramerius.rest.api.k5.client.pdf.PDFResourceNotReadyException) XPathExpressionException(javax.xml.xpath.XPathExpressionException) ProcessSubtreeException(cz.incad.kramerius.ProcessSubtreeException) JSONException(org.json.JSONException) PDFResourceBadRequestException(cz.incad.kramerius.rest.api.k5.client.pdf.PDFResourceBadRequestException) SecurityException(cz.incad.kramerius.security.SecurityException) GenericApplicationException(cz.incad.kramerius.rest.api.exceptions.GenericApplicationException) PDFResourceNotReadyException(cz.incad.kramerius.rest.api.k5.client.pdf.PDFResourceNotReadyException) MalformedURLException(java.net.MalformedURLException) BadRequestException(cz.incad.kramerius.rest.api.exceptions.BadRequestException) OutOfRangeException(cz.incad.kramerius.pdf.OutOfRangeException) Date(java.util.Date) ProcessSubtreeException(cz.incad.kramerius.ProcessSubtreeException) SimpleDateFormat(java.text.SimpleDateFormat)

Example 5 with SecurityException

use of cz.incad.kramerius.security.SecurityException in project kramerius by ceskaexpedice.

the class SmallThumbnailImageServlet method doGet.

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    OutputFormats outputFormat = null;
    String pid = req.getParameter(UUID_PARAMETER);
    // TODO: Change it !!
    pid = fedoraAccess.findFirstViewablePid(pid);
    String outputFormatParam = req.getParameter(OUTPUT_FORMAT_PARAMETER);
    if (outputFormatParam != null) {
        outputFormat = OutputFormats.valueOf(outputFormatParam);
    }
    try {
        if (outputFormat == null) {
            BufferedImage image = rawThumbnailImage(pid, 0);
            Rectangle rectangle = new Rectangle(image.getWidth(null), image.getHeight(null));
            BufferedImage scale = scale(image, rectangle, req, getScalingMethod());
            if (scale != null) {
                setDateHaders(pid, FedoraUtils.IMG_THUMB_STREAM, resp);
                setResponseCode(pid, FedoraUtils.IMG_THUMB_STREAM, req, resp);
                writeImage(req, resp, scale, OutputFormats.JPEG);
            } else
                resp.setStatus(HttpServletResponse.SC_BAD_REQUEST);
        } else {
            InputStream is = this.fedoraAccess.getSmallThumbnail(pid);
            if (outputFormat.equals(OutputFormats.RAW)) {
                rawContent(req, resp, pid, is);
            } else {
                BufferedImage rawImage = rawThumbnailImage(pid, 0);
                setDateHaders(pid, FedoraUtils.IMG_THUMB_STREAM, resp);
                setResponseCode(pid, FedoraUtils.IMG_THUMB_STREAM, req, resp);
                writeImage(req, resp, rawImage, outputFormat);
            }
        }
    } catch (SecurityException e) {
        LOGGER.log(Level.INFO, e.getMessage());
        resp.setStatus(HttpServletResponse.SC_FORBIDDEN);
    } catch (Exception e) {
        LOGGER.log(Level.SEVERE, e.getMessage(), e);
        resp.setStatus(HttpServletResponse.SC_NOT_FOUND);
    }
}
Also used : InputStream(java.io.InputStream) SecurityException(cz.incad.kramerius.security.SecurityException) BufferedImage(java.awt.image.BufferedImage) XPathExpressionException(javax.xml.xpath.XPathExpressionException) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) SQLException(java.sql.SQLException) SecurityException(cz.incad.kramerius.security.SecurityException)

Aggregations

SecurityException (cz.incad.kramerius.security.SecurityException)17 XPathExpressionException (javax.xml.xpath.XPathExpressionException)8 ActionNotAllowed (cz.incad.kramerius.rest.api.exceptions.ActionNotAllowed)7 IOException (java.io.IOException)7 JSONException (org.json.JSONException)6 GenericApplicationException (cz.incad.kramerius.rest.api.exceptions.GenericApplicationException)5 User (cz.incad.kramerius.security.User)5 InputStream (java.io.InputStream)5 MalformedURLException (java.net.MalformedURLException)5 URISyntaxException (java.net.URISyntaxException)5 ProcessSubtreeException (cz.incad.kramerius.ProcessSubtreeException)4 OutOfRangeException (cz.incad.kramerius.pdf.OutOfRangeException)4 BadRequestException (cz.incad.kramerius.rest.api.exceptions.BadRequestException)4 URL (java.net.URL)4 SimpleDateFormat (java.text.SimpleDateFormat)4 ArrayList (java.util.ArrayList)4 Date (java.util.Date)4 ServletException (javax.servlet.ServletException)4 StreamingOutput (javax.ws.rs.core.StreamingOutput)4 BufferedImage (java.awt.image.BufferedImage)3