use of digilib.image.ImageJobDescription in project digilib by robcast.
the class PDFStreamWorker method renderPDF.
/**
* @throws DocumentException
* @throws InterruptedException
* @throws ExecutionException
* @throws IOException
* @throws ImageOpException
*/
protected OutputStream renderPDF() throws DocumentException, InterruptedException, ExecutionException, IOException, ImageOpException {
// create document object
doc = new Document(PageSize.A4, 0, 0, 0, 0);
PdfWriter docwriter = null;
long start_time = System.currentTimeMillis();
docwriter = PdfWriter.getInstance(doc, outstream);
setPDFProperties(doc);
doc.open();
addTitlePage(doc);
logger.debug("PDF: " + outstream + " doc.open()ed (" + (System.currentTimeMillis() - start_time) + "ms)");
NumRange pgs = job_info.getPages();
for (int p : pgs) {
logger.debug("PDF: adding Image " + p + " to " + outstream);
// set page number
job_info.setValue("pn", p);
// create ImageJobInformation
ImageJobDescription iji = ImageJobDescription.getInstance(job_info, job_info.getDlConfig());
addImage(doc, iji);
logger.debug("PDF: done adding Image " + p + " to " + outstream);
}
logger.debug("PDF: done adding all Images to " + outstream);
doc.close();
logger.debug("PDF: " + outstream + " doc.close() (" + (System.currentTimeMillis() - start_time) + "ms)");
docwriter.flush();
docwriter.close();
return outstream;
}
use of digilib.image.ImageJobDescription in project digilib by robcast.
the class Scaler method processRequest.
/**
* Service this request using the response.
*
* @param request
* @param response
* @throws ServletException
*/
public void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException {
if (dlConfig == null) {
logger.error("ERROR: No Configuration!");
throw new ServletException("NO VALID digilib CONFIGURATION!");
}
accountlog.debug("request: " + request.getQueryString());
logger.debug("request: " + request.getQueryString());
// logger.debug("processRequest response committed=" + response.isCommitted());
if (response.isCommitted()) {
logger.error("Crap: response committed before we got a chance!");
}
final long startTime = System.currentTimeMillis();
// parse request
DigilibServletRequest dlRequest = new DigilibServletRequest(request, dlConfig);
// type of error reporting
ErrMsg errMsgType = defaultErrMsgType;
if (dlRequest.hasOption(DigilibOption.errimg)) {
errMsgType = ErrMsg.IMAGE;
} else if (dlRequest.hasOption(DigilibOption.errtxt)) {
errMsgType = ErrMsg.TEXT;
} else if (dlRequest.hasOption(DigilibOption.errcode)) {
errMsgType = ErrMsg.CODE;
}
try {
// extract the job information
final ImageJobDescription jobTicket = ImageJobDescription.getInstance(dlRequest, dlConfig);
// handle the IIIF info-request
if (dlRequest.hasOption(DigilibOption.info)) {
ServletOps.sendIiifInfo(dlRequest, response, logger);
return;
}
if (dlRequest.hasOption(DigilibOption.redirect_info)) {
StringBuffer url = request.getRequestURL();
if (url.toString().endsWith("/")) {
url.append("info.json");
} else {
url.append("/info.json");
}
// TODO: the redirect should have code 303
response.sendRedirect(url.toString());
return;
}
// error out if request was bad
if (dlRequest.errorMessage != null) {
digilibError(errMsgType, Error.UNKNOWN, dlRequest.errorMessage, response);
return;
}
/*
* check permissions
*/
if (useAuthorization) {
// is the current request/user authorized?
if (!authzOp.isAuthorized(dlRequest)) {
// send deny answer and abort
throw new AuthOpException("Access denied!");
}
}
/*
* get the input file
*/
ImageInput fileToLoad = jobTicket.getInput();
/*
* if requested, send image as a file
*/
if (sendFileAllowed && jobTicket.getSendAsFile()) {
String mt = null;
if (jobTicket.hasOption(DigilibOption.rawfile)) {
// mo=rawfile sends as octet-stream
mt = "application/octet-stream";
}
logger.debug("Sending RAW File as is.");
ServletOps.sendFile(fileToLoad.getFile(), mt, null, response, logger);
logger.info("Done in " + (System.currentTimeMillis() - startTime) + "ms");
return;
}
/*
* send the image if it's possible without having to transform it
*/
if (!jobTicket.isTransformRequired()) {
logger.debug("Sending File as is.");
ServletOps.sendFile(fileToLoad.getFile(), null, null, response, logger);
logger.info("Done in " + (System.currentTimeMillis() - startTime) + "ms");
return;
}
/*
* check load of workers
*/
if (imageJobCenter.isBusy()) {
logger.error("Servlet overloaded!");
response.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE);
return;
}
/*
* dispatch worker job to be done asynchronously
*/
AsyncContext asyncCtx = request.startAsync();
// create job
AsyncServletWorker job = new AsyncServletWorker(dlConfig, jobTicket, asyncCtx, errMsgType, startTime);
// AsyncServletWorker is its own AsyncListener
asyncCtx.addListener(job);
// submit job
imageJobCenter.submit(job);
// we're done for now
} catch (ImageOpException e) {
logger.error(e.getClass() + ": " + e.getMessage());
digilibError(errMsgType, Error.IMAGE, null, response);
} catch (IOException e) {
logger.error(e.getClass() + ": " + e.getMessage());
digilibError(errMsgType, Error.FILE, null, response);
} catch (AuthOpException e) {
logger.error(e.getClass() + ": " + e.getMessage());
digilibError(errMsgType, Error.AUTH, null, response);
} catch (Exception e) {
logger.error("Other Exception: ", e);
// TODO: should we rethrow or swallow?
// throw new ServletException(e);
}
}
use of digilib.image.ImageJobDescription in project digilib by robcast.
the class ScalerNoThread method processRequest.
/**
* Service this request using the response.
*
* @param request
* @param response
* @throws ServletException
*/
public void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException {
if (dlConfig == null) {
logger.error("ERROR: No Configuration!");
throw new ServletException("NO VALID digilib CONFIGURATION!");
}
accountlog.debug("request: " + request.getQueryString());
logger.debug("request: " + request.getQueryString());
long startTime = System.currentTimeMillis();
// parse request
DigilibServletRequest dlRequest = new DigilibServletRequest(request, dlConfig);
// type of error reporting
ErrMsg errMsgType = ErrMsg.IMAGE;
if (dlRequest.hasOption(DigilibOption.errtxt)) {
errMsgType = ErrMsg.TEXT;
} else if (dlRequest.hasOption(DigilibOption.errcode)) {
errMsgType = ErrMsg.CODE;
}
try {
// extract the job information
ImageJobDescription jobTicket = ImageJobDescription.getInstance(dlRequest, dlConfig);
/*
* check if we can fast-track without scaling
*/
ImageInput fileToLoad = (ImageInput) jobTicket.getInput();
// check permissions
if (useAuthorization) {
// is the current request/user authorized?
if (!authzOp.isAuthorized(dlRequest)) {
// send deny answer and abort
throw new AuthOpException();
}
}
// if requested, send image as a file
if (sendFileAllowed && jobTicket.getSendAsFile()) {
String mt = null;
if (jobTicket.hasOption(DigilibOption.rawfile)) {
mt = "application/octet-stream";
}
logger.debug("Sending RAW File as is.");
ServletOps.sendFile(fileToLoad.getFile(), mt, null, response, logger);
logger.info("Done in " + (System.currentTimeMillis() - startTime) + "ms");
return;
}
// it
if (!jobTicket.isTransformRequired()) {
logger.debug("Sending File as is.");
ServletOps.sendFile(fileToLoad.getFile(), null, null, response, logger);
logger.info("Done in " + (System.currentTimeMillis() - startTime) + "ms");
return;
}
// create job
ImageWorker job = new ImageWorker(dlConfig, jobTicket);
// get result immediately
DocuImage img = job.call();
// forced destination image type
String mt = null;
if (jobTicket.hasOption(DigilibOption.jpg)) {
mt = "image/jpeg";
} else if (jobTicket.hasOption(DigilibOption.png)) {
mt = "image/png";
}
// send image
ServletOps.sendImage(img, mt, response, logger);
logger.debug("Job Processing Time: " + (System.currentTimeMillis() - startTime) + "ms");
} catch (ImageOpException e) {
logger.error(e.getClass() + ": " + e.getMessage());
digilibError(errMsgType, Error.IMAGE, null, response);
} catch (IOException e) {
logger.error(e.getClass() + ": " + e.getMessage());
digilibError(errMsgType, Error.FILE, null, response);
} catch (AuthOpException e) {
logger.error(e.getClass() + ": " + e.getMessage());
digilibError(errMsgType, Error.AUTH, null, response);
}
}
use of digilib.image.ImageJobDescription in project digilib by robcast.
the class PDFRequest method setWithRequest.
/**
* Read the request object.
*
* @param request
* @throws ImageOpException
* @throws IOException
*/
public void setWithRequest(HttpServletRequest request) throws IOException, ImageOpException {
// read matching request parameters for the parameters in this map
for (String k : params.keySet()) {
if (request.getParameterMap().containsKey(k)) {
setValueFromString(k, request.getParameter(k));
}
}
// process parameters
pages = new NumRange(getAsString("pgs"));
ImageJobDescription ij = ImageJobDescription.getInstance(this, dlConfig);
DocuDirectory dir = ij.getFileDirectory();
int dirsize = dir.size();
pages.setMaxnum(dirsize);
}
use of digilib.image.ImageJobDescription in project digilib by robcast.
the class MetaAccessAuthzOps method rolesForPath.
/**
* Return authorization roles needed for request.
*
* Returns the list of authorization roles that are needed to access the
* specified path. No list means the path is free.
*
* The location information of the request is also considered.
*
* @param request
* ServletRequest with address information.
* @throws AuthOpException
* Exception thrown on error.
* @return List of Strings with role names.
*/
@Override
public List<String> rolesForPath(DigilibServletRequest dlRequest) throws AuthOpException {
DocuDirent imgs;
try {
// try to get image file from JobDescription
ImageJobDescription ticket = dlRequest.getJobDescription();
if (ticket != null) {
imgs = (DocuDirent) ticket.getImageSet();
} else {
// try to get image file from DirCache
DigilibConfiguration config = dlRequest.getDigilibConfig();
DocuDirCache cache = (DocuDirCache) config.getValue(DigilibServletConfiguration.DIR_CACHE_KEY);
imgs = cache.getFile(dlRequest.getFilePath(), dlRequest.getAsInt("pn"));
}
} catch (FileOpException e) {
throw new AuthOpException("No file for auth check!");
}
/*
* get access restrictions from metadata
*/
String access = null;
try {
imgs.checkMeta();
MetadataMap meta = imgs.getMeta().getFileMeta();
if (meta != null) {
access = meta.get("access");
}
} catch (Exception e) {
logger.error("Error getting access meta for file!");
}
if (access == null) {
// no access tag - use default
logger.debug("Roles required for " + imgs.getName() + ": " + defaultRoles + "(default)");
return defaultRoles;
} else if (access.equalsIgnoreCase("free")) {
// access free
logger.debug("Roles required for " + imgs.getName() + ": (free)");
return null;
}
// get required roles
if (rolesMap.containsKey(access)) {
List<String> required = rolesMap.get(access);
logger.debug("Roles required for " + imgs.getName() + ": " + required);
return required;
} else {
// no mapping to role
logger.error("Error: no role for access type '" + access + "'");
// use default
logger.debug("Roles required for " + imgs.getName() + ": " + defaultRoles + "(substituted default)");
return defaultRoles;
}
}
Aggregations