Search in sources :

Example 36 with MCRContent

use of org.mycore.common.content.MCRContent in project mycore by MyCoRe-Org.

the class MCRJerseyExceptionMapper method toResponse.

@Override
public Response toResponse(Exception exc) {
    LOGGER.warn(() -> "Error while processing request " + request.getRequestURI(), exc);
    Response response = getResponse(exc);
    if (headers.getAcceptableMediaTypes().contains(MediaType.TEXT_HTML_TYPE)) {
        // try to return a html error page
        if (!MCRSessionMgr.getCurrentSession().isTransactionActive()) {
            MCRSessionMgr.getCurrentSession().beginTransaction();
        }
        try {
            int status = response.getStatus();
            String source = exc.getStackTrace().length > 0 ? exc.getStackTrace()[0].toString() : null;
            Document errorPageDocument = MCRErrorServlet.buildErrorPage(exc.getMessage(), status, request.getRequestURI(), null, source, exc);
            MCRContent result = MCRJerseyUtil.transform(errorPageDocument, request);
            return Response.serverError().entity(result.getInputStream()).type(MediaType.valueOf(result.getMimeType())).status(status).build();
        } catch (Exception transformException) {
            return Response.status(Status.INTERNAL_SERVER_ERROR).entity(transformException).build();
        } finally {
            MCRSessionMgr.getCurrentSession().commitTransaction();
        }
    }
    return response;
}
Also used : Response(javax.ws.rs.core.Response) Document(org.jdom2.Document) MCRContent(org.mycore.common.content.MCRContent) WebApplicationException(javax.ws.rs.WebApplicationException)

Example 37 with MCRContent

use of org.mycore.common.content.MCRContent in project mycore by MyCoRe-Org.

the class MCRContainerLoginFormServlet method render.

/* (non-Javadoc)
     * @see org.mycore.frontend.servlets.MCRServlet#render(org.mycore.frontend.servlets.MCRServletJob, java.lang.Exception)
     */
@Override
protected void render(MCRServletJob job, Exception ex) throws Exception {
    if (ex != null) {
        throw ex;
    }
    MCRContent source = (MCRContent) job.getRequest().getAttribute(LOGIN_ATTR);
    getLayoutService().doLayout(job.getRequest(), job.getResponse(), source);
}
Also used : MCRContent(org.mycore.common.content.MCRContent)

Example 38 with MCRContent

use of org.mycore.common.content.MCRContent in project mycore by MyCoRe-Org.

the class MCRMETSServlet method doGetPost.

@Override
protected void doGetPost(MCRServletJob job) throws Exception {
    HttpServletRequest request = job.getRequest();
    HttpServletResponse response = job.getResponse();
    LOGGER.info(request.getPathInfo());
    String derivate = getOwnerID(request.getPathInfo());
    MCRPath rootPath = MCRPath.getPath(derivate, "/");
    if (!Files.isDirectory(rootPath)) {
        response.sendError(HttpServletResponse.SC_NOT_FOUND, MessageFormat.format("Derivate {0} does not exist.", derivate));
        return;
    }
    request.setAttribute("XSL.derivateID", derivate);
    Collection<String> linkList = MCRLinkTableManager.instance().getSourceOf(derivate);
    if (linkList.isEmpty()) {
        MCRDerivate derivate2 = MCRMetadataManager.retrieveMCRDerivate(MCRObjectID.getInstance(derivate));
        MCRObjectID ownerID = derivate2.getOwnerID();
        if (ownerID != null && ownerID.toString().length() != 0) {
            linkList.add(ownerID.toString());
        } else {
            response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, MessageFormat.format("Derivate {0} is not linked with a MCRObject. Please contact an administrator.", derivate));
            return;
        }
    }
    request.setAttribute("XSL.objectID", linkList.iterator().next());
    response.setContentType("text/xml");
    long lastModified = Files.getLastModifiedTime(rootPath).toMillis();
    MCRFrontendUtil.writeCacheHeaders(response, (long) CACHE_TIME, lastModified, useExpire);
    long start = System.currentTimeMillis();
    MCRContent metsContent = getMetsSource(job, useExistingMets(request), derivate);
    MCRLayoutService.instance().doLayout(request, response, metsContent);
    LOGGER.info("Generation of code by {} took {} ms", this.getClass().getSimpleName(), System.currentTimeMillis() - start);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) MCRDerivate(org.mycore.datamodel.metadata.MCRDerivate) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) MCRPath(org.mycore.datamodel.niofs.MCRPath) MCRContent(org.mycore.common.content.MCRContent)

Example 39 with MCRContent

use of org.mycore.common.content.MCRContent in project mycore by MyCoRe-Org.

the class MCRGoobiMetsPostUploadProcessor method processFile.

@Override
public Path processFile(String path, Path tempFile, Supplier<Path> tempFileSupplier) throws IOException {
    try (InputStream in = Files.newInputStream(tempFile)) {
        MCRStreamContent streamContent = new MCRStreamContent(in);
        MCRContent transform = goobiMetsTransformer.transform(streamContent);
        Path result = tempFileSupplier.get();
        try (OutputStream out = Files.newOutputStream(result)) {
            out.write(transform.asByteArray());
            return result;
        }
    }
}
Also used : Path(java.nio.file.Path) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) MCRStreamContent(org.mycore.common.content.MCRStreamContent) MCRContent(org.mycore.common.content.MCRContent)

Example 40 with MCRContent

use of org.mycore.common.content.MCRContent in project mycore by MyCoRe-Org.

the class MCRPDFThumbnailServlet method getContent.

/* (non-Javadoc)
     * @see org.mycore.frontend.servlets.MCRContentServlet#getContent(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
     */
@Override
public MCRContent getContent(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    try {
        ThumnailInfo thumbnailInfo = getThumbnailInfo(req.getPathInfo());
        MCRPath pdfFile = MCRPath.getPath(thumbnailInfo.derivate, thumbnailInfo.filePath);
        LOGGER.info("PDF file: {}", pdfFile);
        if (Files.notExists(pdfFile)) {
            resp.sendError(HttpServletResponse.SC_NOT_FOUND, MessageFormat.format("Could not find pdf file for {0}{1}", thumbnailInfo.derivate, thumbnailInfo.filePath));
            return null;
        }
        String centerThumb = req.getParameter("centerThumb");
        String imgSize = req.getParameter("ts");
        // defaults to "yes"
        boolean centered = !"no".equals(centerThumb);
        int thumbnailSize = imgSize == null ? this.thumbnailSize : Integer.parseInt(imgSize);
        BasicFileAttributes attrs = Files.readAttributes(pdfFile, BasicFileAttributes.class);
        MCRContent imageContent = pdfTools.getThumnail(pdfFile, attrs, thumbnailSize, centered);
        if (imageContent != null) {
            resp.setHeader("Cache-Control", "max-age=" + MAX_AGE);
            Date expires = new Date(System.currentTimeMillis() + MAX_AGE * 1000);
            LOGGER.debug("Last-Modified: {}, expire on: {}", new Date(attrs.lastModifiedTime().toMillis()), expires);
            resp.setDateHeader("Expires", expires.getTime());
        }
        return imageContent;
    } finally {
        LOGGER.debug("Finished sending {}", req.getPathInfo());
    }
}
Also used : MCRPath(org.mycore.datamodel.niofs.MCRPath) MCRContent(org.mycore.common.content.MCRContent) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes) Date(java.util.Date)

Aggregations

MCRContent (org.mycore.common.content.MCRContent)63 Document (org.jdom2.Document)21 MCRJDOMContent (org.mycore.common.content.MCRJDOMContent)20 IOException (java.io.IOException)16 Element (org.jdom2.Element)13 MCRObjectID (org.mycore.datamodel.metadata.MCRObjectID)11 MCRPath (org.mycore.datamodel.niofs.MCRPath)10 Test (org.junit.Test)8 MCRPathContent (org.mycore.common.content.MCRPathContent)7 MCRParameterCollector (org.mycore.common.xsl.MCRParameterCollector)6 File (java.io.File)5 HttpServletRequest (javax.servlet.http.HttpServletRequest)5 JDOMException (org.jdom2.JDOMException)5 InputStream (java.io.InputStream)4 HashMap (java.util.HashMap)4 HttpServletResponse (javax.servlet.http.HttpServletResponse)4 MCRException (org.mycore.common.MCRException)4 MCRDerivate (org.mycore.datamodel.metadata.MCRDerivate)4 URL (java.net.URL)3 BasicFileAttributes (java.nio.file.attribute.BasicFileAttributes)3