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();
}
}
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();
}
}
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();
}
}
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());
}
}
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());
}
}
Aggregations