Search in sources :

Example 1 with DigilibServletRequest

use of digilib.conf.DigilibServletRequest in project digilib by robcast.

the class DigilibBean method setRequest.

/**
 * Sets the current DigilibRequest using a HttpServletRequest.
 *
 * @param request
 */
public void setRequest(HttpServletRequest request) throws Exception {
    // create dlRequest
    DigilibServletRequest dlRequest = new DigilibServletRequest(request, dlConfig);
    // use for initialisation
    setRequest(dlRequest);
}
Also used : DigilibServletRequest(digilib.conf.DigilibServletRequest)

Example 2 with DigilibServletRequest

use of digilib.conf.DigilibServletRequest in project digilib by robcast.

the class ScalerNoThread method getLastModified.

/**
 * Returns modification time relevant to the request for caching.
 *
 * @see javax.servlet.http.HttpServlet#getLastModified(javax.servlet.http.HttpServletRequest)
 */
public long getLastModified(HttpServletRequest request) {
    accountlog.debug("GetLastModified from " + request.getRemoteAddr() + " for " + request.getQueryString());
    long mtime = -1;
    // create new request
    DigilibServletRequest dlReq = new DigilibServletRequest(request, dlConfig);
    DocuDirectory dd = dirCache.getDirectory(dlReq.getFilePath());
    if (dd != null) {
        mtime = dd.getDirMTime() / 1000 * 1000;
    }
    logger.debug("  returns " + mtime);
    return mtime;
}
Also used : DigilibServletRequest(digilib.conf.DigilibServletRequest) DocuDirectory(digilib.io.DocuDirectory)

Example 3 with DigilibServletRequest

use of digilib.conf.DigilibServletRequest in project digilib by robcast.

the class IpAuthnOps method getUserRoles.

/* (non-Javadoc)
     * @see digilib.auth.AuthnOps#getUserRoles(digilib.conf.DigilibRequest)
     */
@Override
public List<String> getUserRoles(DigilibRequest dlRequest) throws AuthOpException {
    HttpServletRequest request = ((DigilibServletRequest) dlRequest).getServletRequest();
    String ip = request.getRemoteAddr();
    List<String> provided = null;
    if (ip.contains(":")) {
        // IPv6
        provided = authIP6s.match(ip);
    } else {
        // IPv4
        provided = authIP4s.match(ip);
    }
    logger.debug("Roles provided by ip " + ip + ": " + provided);
    return provided;
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) DigilibServletRequest(digilib.conf.DigilibServletRequest)

Example 4 with DigilibServletRequest

use of digilib.conf.DigilibServletRequest in project digilib by robcast.

the class OpenIdAuthnOps method getUserRoles.

/* (non-Javadoc)
     * @see digilib.auth.AuthnOps#getUserRoles(digilib.conf.DigilibRequest)
     */
@Override
public List<String> getUserRoles(DigilibRequest request) throws AuthOpException {
    /*
         * try token parameter first
         */
    String id_token = request.getAsString("id_token");
    if (id_token == null || id_token.isEmpty()) {
        /*
             * try token cookie next
             */
        HttpServletRequest srvReq = ((DigilibServletRequest) request).getServletRequest();
        Cookie[] cookies = srvReq.getCookies();
        if (cookies != null) {
            for (Cookie c : cookies) {
                if (c.getName().equals(tokenCookieName)) {
                    id_token = c.getValue();
                    break;
                }
            }
        }
        if (id_token == null || id_token.isEmpty()) {
            logger.error("Missing id token!");
            return null;
        }
    }
    // the first JwtConsumer is just used to parse the JWT into a JwtContext object.
    try {
        JwtContext jwtContext = firstPassJwtConsumer.process(id_token);
        // extract issuer
        String issuer = jwtContext.getJwtClaims().getIssuer();
        // get validating consumer for this issuer
        JwtConsumer secondPassJwtConsumer = idpJwtConsumers.get(issuer);
        if (secondPassJwtConsumer == null) {
            logger.error("Unknown id token issuer: " + issuer);
            return null;
        }
        // validate token
        secondPassJwtConsumer.processContext(jwtContext);
        JwtClaims claims = jwtContext.getJwtClaims();
        String sub = claims.getSubject();
        // get roles
        List<String> provided = idpRoles.get(issuer);
        logger.debug("Roles provided by id_token (sub='" + sub + "'): " + provided);
        return provided;
    } catch (InvalidJwtException | MalformedClaimException e) {
        logger.error("Error validating id token: " + e.getMessage());
        return null;
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) Cookie(javax.servlet.http.Cookie) InvalidJwtException(org.jose4j.jwt.consumer.InvalidJwtException) MalformedClaimException(org.jose4j.jwt.MalformedClaimException) DigilibServletRequest(digilib.conf.DigilibServletRequest) JwtClaims(org.jose4j.jwt.JwtClaims) JwtConsumer(org.jose4j.jwt.consumer.JwtConsumer) JwtContext(org.jose4j.jwt.consumer.JwtContext)

Example 5 with DigilibServletRequest

use of digilib.conf.DigilibServletRequest 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);
    }
}
Also used : ImageOpException(digilib.image.ImageOpException) DigilibServletRequest(digilib.conf.DigilibServletRequest) ImageJobDescription(digilib.image.ImageJobDescription) AsyncContext(javax.servlet.AsyncContext) IOException(java.io.IOException) ImageOpException(digilib.image.ImageOpException) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) AuthOpException(digilib.auth.AuthOpException) ServletException(javax.servlet.ServletException) AuthOpException(digilib.auth.AuthOpException) ImageInput(digilib.io.ImageInput)

Aggregations

DigilibServletRequest (digilib.conf.DigilibServletRequest)10 AuthOpException (digilib.auth.AuthOpException)5 ImageOpException (digilib.image.ImageOpException)5 IOException (java.io.IOException)5 DocuDirectory (digilib.io.DocuDirectory)4 ServletException (javax.servlet.ServletException)4 HttpServletRequest (javax.servlet.http.HttpServletRequest)3 ImageJobDescription (digilib.image.ImageJobDescription)2 ImageInput (digilib.io.ImageInput)2 List (java.util.List)2 DocuImage (digilib.image.DocuImage)1 ImageWorker (digilib.image.ImageWorker)1 File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1 JsonGenerator (javax.json.stream.JsonGenerator)1 AsyncContext (javax.servlet.AsyncContext)1 ServletOutputStream (javax.servlet.ServletOutputStream)1 Cookie (javax.servlet.http.Cookie)1 JwtClaims (org.jose4j.jwt.JwtClaims)1 MalformedClaimException (org.jose4j.jwt.MalformedClaimException)1