use of org.mycore.iiif.image.parser.MCRIIIFRegionParser in project mycore by MyCoRe-Org.
the class MCRIIIFImageResource method getImage.
@GET
@Path("{" + IDENTIFIER_PARAM + "}/{region}/{size}/{rotation}/{quality}.{format}")
public Response getImage(@PathParam(IMPL_PARAM) String implStr, @PathParam(IDENTIFIER_PARAM) String identifier, @PathParam("region") String region, @PathParam("size") String size, @PathParam("rotation") String rotation, @PathParam("quality") String quality, @PathParam("format") String format) {
try {
MCRIIIFImageImpl impl = getImpl(implStr);
MCRIIIFImageInformation information = impl.getInformation(identifier);
MCRIIIFRegionParser rp = new MCRIIIFRegionParser(region, information.width, information.height);
MCRIIIFImageSourceRegion sourceRegion = rp.parseImageRegion();
MCRIIIFScaleParser sp = new MCRIIIFScaleParser(size, sourceRegion.getX2() - sourceRegion.getX1(), sourceRegion.getY2() - sourceRegion.getY1());
MCRIIIFImageTargetSize targetSize = sp.parseTargetScale();
MCRIIIFRotationParser rotationParser = new MCRIIIFRotationParser(rotation);
MCRIIIFImageTargetRotation parsedRotation = rotationParser.parse();
MCRIIIFImageQuality imageQuality = MCRIIIFImageQuality.fromString(quality);
BufferedImage provide = impl.provide(identifier, sourceRegion, targetSize, parsedRotation, imageQuality, format);
Response.Status status = rp.isCompleteValid() ? Response.Status.OK : Response.Status.BAD_REQUEST;
Response.ResponseBuilder responseBuilder = Response.status(status);
return responseBuilder.header("Link", buildCanonicalURL(impl, identifier)).header("Profile", buildProfileURL()).type("image/" + format).entity((StreamingOutput) outputStream -> ImageIO.write(provide, format, outputStream)).build();
} catch (MCRIIIFImageNotFoundException e) {
return Response.status(Response.Status.NOT_FOUND).entity(e.getMessage()).build();
} catch (IllegalArgumentException | MCRIIIFUnsupportedFormatException e) {
return Response.status(Response.Status.BAD_REQUEST).entity(e.getMessage()).build();
} catch (MCRAccessException e) {
return Response.status(Response.Status.FORBIDDEN).entity(e.getMessage()).build();
} catch (Exception e) {
LOGGER.error(() -> "Error while getting Image " + identifier + " from " + implStr + " with region: " + region + ", size: " + size + ", rotation: " + rotation + ", quality: " + quality + ", format: " + format, e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
}
}
Aggregations