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