Search in sources :

Example 1 with QRCodeGenerator

use of gov.usgs.cida.utilities.QRCodeGenerator in project coastal-hazards by USGS-CIDA.

the class QRCodeResource method generateQRImageUsingItemID.

/**
 * Produces a QR code that directs to back of card for a given item ID
 *
 * @param id
 * @param width
 * @param height
 * @return
 */
@GET
@Path("/info/item/{id}")
@Produces("image/png")
public Response generateQRImageUsingItemID(@PathParam("id") String id, @QueryParam("width") int width, @QueryParam("height") int height) throws IOException {
    URL url = null;
    String urlString = "ui/info/item/" + id;
    Response response;
    QRCodeGenerator qrcr = new QRCodeGenerator();
    // Make sure the item exists in the database
    try (ItemManager itemManager = new ItemManager()) {
        Item item = itemManager.load(id);
        if (item == null) {
            throw new NotFoundException();
        }
    }
    // Check if the base URL doesn't contain a trailing slash. If not, attach one to the beginning of url string
    if (BASE_URL.charAt(BASE_URL.length() - 1) != '/') {
        urlString = "/" + urlString;
    }
    // Create the URL string
    urlString = BASE_URL + urlString;
    try {
        url = new URL(urlString);
        qrcr.setUrl(url);
    } catch (MalformedURLException | URISyntaxException ex) {
        throw new ParamException.QueryParamException(ex, "URL could not be formed", "URL " + url + " could not be formed.");
    }
    if (width > 0 && height > 0) {
        if (width > MAX_WIDTH) {
            qrcr.setWidth(MAX_WIDTH);
        } else {
            qrcr.setWidth(width);
        }
        if (height > MAX_HEIGHT) {
            qrcr.setHeight(MAX_HEIGHT);
        } else {
            qrcr.setHeight(height);
        }
    }
    try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
        qrcr.writeToOutputStream(baos);
        baos.flush();
        response = Response.ok(baos.toByteArray(), "image/png").build();
    }
    return response;
}
Also used : QRCodeGenerator(gov.usgs.cida.utilities.QRCodeGenerator) MalformedURLException(java.net.MalformedURLException) ItemManager(gov.usgs.cida.coastalhazards.jpa.ItemManager) NotFoundException(javax.ws.rs.NotFoundException) URISyntaxException(java.net.URISyntaxException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) URL(java.net.URL) Response(javax.ws.rs.core.Response) Item(gov.usgs.cida.coastalhazards.model.Item) ParamException(org.glassfish.jersey.server.ParamException) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Aggregations

ItemManager (gov.usgs.cida.coastalhazards.jpa.ItemManager)1 Item (gov.usgs.cida.coastalhazards.model.Item)1 QRCodeGenerator (gov.usgs.cida.utilities.QRCodeGenerator)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 MalformedURLException (java.net.MalformedURLException)1 URISyntaxException (java.net.URISyntaxException)1 URL (java.net.URL)1 GET (javax.ws.rs.GET)1 NotFoundException (javax.ws.rs.NotFoundException)1 Path (javax.ws.rs.Path)1 Produces (javax.ws.rs.Produces)1 Response (javax.ws.rs.core.Response)1 ParamException (org.glassfish.jersey.server.ParamException)1