Search in sources :

Example 1 with GenericApplicationException

use of cz.incad.kramerius.rest.api.exceptions.GenericApplicationException 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 GenericApplicationException

use of cz.incad.kramerius.rest.api.exceptions.GenericApplicationException in project kramerius by ceskaexpedice.

the class PDFResource method part.

/**
 * Print only part of image
 * @param pid PID
 * @param xpos Determine x-position. Value is defined by percentage of total width.
 * @param ypos Determine y-position. Value is defined by percentage of total height.
 * @param width Deterime width. Value is defiend by percentage of total width.
 * @param height Determine height. Value is defined by percentage of total height
 * @param format Page format. Possible values : A0,...A5, B0,...B5, LETTER, POSTCARD
 * @return
 * @throws OutOfRangeException
 */
@GET
@Path("part")
@Produces({ "application/pdf", "application/json" })
public Response part(@QueryParam("pid") String pid, @QueryParam("xpos") String xpos, @QueryParam("ypos") String ypos, @QueryParam("width") String width, @QueryParam("height") String height, @QueryParam("format") String format) throws OutOfRangeException {
    boolean acquired = false;
    try {
        acquired = PDFExlusiveGenerateSupport.PDF_SEMAPHORE.tryAcquire();
        if (acquired) {
            if (pid != null) {
                File fileToDelete = null;
                try {
                    pid = this.fedoraAccess.findFirstViewablePid(pid);
                    BufferedImage bufImage = KrameriusImageSupport.readImage(pid, FedoraUtils.IMG_FULL_STREAM, this.fedoraAccess, 0);
                    double xPerctDouble = Double.parseDouble(xpos);
                    double yPerctDouble = Double.parseDouble(ypos);
                    double widthPerctDouble = Double.parseDouble(width);
                    double heightPerctDouble = Double.parseDouble(height);
                    BufferedImage subImage = KrameriusImageSupport.partOfImage(bufImage, xPerctDouble, yPerctDouble, widthPerctDouble, heightPerctDouble);
                    fileToDelete = File.createTempFile("subimage", ".png");
                    FileOutputStream fos = new FileOutputStream(fileToDelete);
                    KrameriusImageSupport.writeImageToStream(subImage, ImageMimeType.PNG.getDefaultFileExtension(), fos);
                    fos.close();
                    reportAccess(pid);
                    StreamingOutput stream = streamingOutput(fileToDelete, format);
                    return Response.ok().entity(stream).type("application/pdf").build();
                } catch (NumberFormatException e) {
                    LOGGER.log(Level.SEVERE, e.getMessage(), e);
                    throw new GenericApplicationException(e.getMessage());
                } catch (XPathExpressionException 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 (IOException e) {
                    LOGGER.log(Level.SEVERE, e.getMessage(), e);
                    throw new GenericApplicationException(e.getMessage());
                }
            } else {
                LOGGER.log(Level.SEVERE, "No pid defined");
                throw new BadRequestException("No pid defined");
            }
        } else {
            throw new PDFResourceNotReadyException("not ready");
        }
    } finally {
        if (acquired)
            PDFExlusiveGenerateSupport.PDF_SEMAPHORE.release();
    }
}
Also used : XPathExpressionException(javax.xml.xpath.XPathExpressionException) FileNotFoundException(java.io.FileNotFoundException) StreamingOutput(javax.ws.rs.core.StreamingOutput) IOException(java.io.IOException) GenericApplicationException(cz.incad.kramerius.rest.api.exceptions.GenericApplicationException) BufferedImage(java.awt.image.BufferedImage) FileOutputStream(java.io.FileOutputStream) BadRequestException(cz.incad.kramerius.rest.api.exceptions.BadRequestException) File(java.io.File) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 3 with GenericApplicationException

use of cz.incad.kramerius.rest.api.exceptions.GenericApplicationException 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 4 with GenericApplicationException

use of cz.incad.kramerius.rest.api.exceptions.GenericApplicationException in project kramerius by ceskaexpedice.

the class SearchResource method termsJSON.

@GET
@Path("terms")
@Produces({ MediaType.APPLICATION_JSON + ";charset=utf-8" })
public Response termsJSON(@Context UriInfo uriInfo) {
    try {
        MultivaluedMap<String, String> queryParameters = uriInfo.getQueryParameters();
        StringBuilder builder = new StringBuilder();
        Set<String> keys = queryParameters.keySet();
        for (String k : keys) {
            for (String v : queryParameters.get(k)) {
                builder.append(k + "=" + URLEncoder.encode(v, "UTF-8"));
                builder.append("&");
            }
        }
        InputStream istream = this.solrAccess.requestWithTerms(builder.toString(), "json");
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        IOUtils.copyStreams(istream, bos);
        String rawString = new String(bos.toByteArray(), "UTF-8");
        return Response.ok().entity(rawString).build();
    } catch (IOException e) {
        LOGGER.log(Level.SEVERE, e.getMessage());
        throw new GenericApplicationException(e.getMessage());
    }
}
Also used : InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) GenericApplicationException(cz.incad.kramerius.rest.api.exceptions.GenericApplicationException) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 5 with GenericApplicationException

use of cz.incad.kramerius.rest.api.exceptions.GenericApplicationException in project kramerius by ceskaexpedice.

the class SearchResource method termsXML.

@GET
@Path("terms")
@Produces({ MediaType.APPLICATION_XML + ";charset=utf-8" })
public Response termsXML(@Context UriInfo uriInfo) {
    try {
        MultivaluedMap<String, String> queryParameters = uriInfo.getQueryParameters();
        StringBuilder builder = new StringBuilder();
        Set<String> keys = queryParameters.keySet();
        for (String k : keys) {
            for (String v : queryParameters.get(k)) {
                builder.append(k + "=" + URLEncoder.encode(v, "UTF-8"));
                builder.append("&");
            }
        }
        InputStream istream = this.solrAccess.requestWithTerms(builder.toString(), "xml");
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        IOUtils.copyStreams(istream, bos);
        String rawString = new String(bos.toByteArray(), "UTF-8");
        String uri = UriBuilder.fromResource(SearchResource.class).path("terms").build().toString();
        return Response.ok().entity(rawString).build();
    } catch (IOException e) {
        LOGGER.log(Level.SEVERE, e.getMessage());
        throw new GenericApplicationException(e.getMessage());
    }
}
Also used : InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) GenericApplicationException(cz.incad.kramerius.rest.api.exceptions.GenericApplicationException) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Aggregations

GenericApplicationException (cz.incad.kramerius.rest.api.exceptions.GenericApplicationException)88 JSONException (org.json.JSONException)77 IOException (java.io.IOException)51 Produces (javax.ws.rs.Produces)40 JSONObject (org.json.JSONObject)38 ActionNotAllowed (cz.incad.kramerius.rest.api.exceptions.ActionNotAllowed)35 Path (javax.ws.rs.Path)27 Element (org.w3c.dom.Element)26 ObjectPidsPath (cz.incad.kramerius.ObjectPidsPath)25 GET (javax.ws.rs.GET)25 JSONArray (org.json.JSONArray)23 BadRequestException (cz.incad.kramerius.rest.api.exceptions.BadRequestException)14 ObjectNotFound (cz.incad.kramerius.rest.api.replication.exceptions.ObjectNotFound)14 User (cz.incad.kramerius.security.User)13 SQLException (java.sql.SQLException)11 Consumes (javax.ws.rs.Consumes)10 Collection (cz.incad.kramerius.virtualcollections.Collection)7 StreamingOutput (javax.ws.rs.core.StreamingOutput)7 Document (org.w3c.dom.Document)7 PUT (javax.ws.rs.PUT)6