ch.sportchef.business.event.boundary.EventImageResource.java Source code

Java tutorial

Introduction

Here is the source code for ch.sportchef.business.event.boundary.EventImageResource.java

Source

/*
 * SportChef  Sports Competition Management Software
 * Copyright (C) 2015, 2016 Marcus Fihlon
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package ch.sportchef.business.event.boundary;

import ch.sportchef.business.event.control.EventImageService;
import ch.sportchef.business.event.control.EventService;
import org.apache.commons.fileupload.MultipartStream;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotNull;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.NotFoundException;
import javax.ws.rs.PUT;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.StreamingOutput;
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

import static javax.ws.rs.core.Response.Status.BAD_REQUEST;

public class EventImageResource {

    private static final String IMAGE_PLACEHOLDER = "http://placehold.it/350x200"; //NON-NLS

    private final Long eventId;
    private final EventService eventService;
    private final EventImageService eventImageService;

    public EventImageResource(@NotNull final Long eventId, @NotNull final EventService eventService,
            @NotNull final EventImageService eventImageService) {
        this.eventId = eventId;
        this.eventService = eventService;
        this.eventImageService = eventImageService;
    }

    @GET
    @Produces({ "image/png" })
    public Response getImage() throws URISyntaxException, IOException {
        try {
            final byte[] image = eventImageService.getImage(eventId);
            return Response.ok().entity((StreamingOutput) stream -> {
                stream.write(image);
                stream.flush();
            }).build();
        } catch (final NotFoundException e) {
            // no image found, redirecting to placeholder image
            final URI location = new URI(IMAGE_PLACEHOLDER);
            return Response.temporaryRedirect(location).build();
        }
    }

    @PUT
    @Consumes(MediaType.MULTIPART_FORM_DATA)
    public Response uploadImage(@Context HttpServletRequest request) throws IOException, ServletException {
        final String contentType = request.getContentType();
        final byte[] boundary = contentType.substring(contentType.indexOf("boundary=") + 9).getBytes();

        try (final BufferedInputStream inputStream = new BufferedInputStream(request.getInputStream(), 8192)) {
            final MultipartStream multipartStream = new MultipartStream(inputStream, boundary, 8192, null);
            boolean nextPart = multipartStream.skipPreamble();
            while (nextPart) {
                multipartStream.readHeaders(); // don't remove, strips headers off
                try (final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(8192)) {
                    multipartStream.readBodyData(outputStream);
                    nextPart = multipartStream.readBoundary();
                    final byte[] image = outputStream.toByteArray();
                    eventImageService.uploadImage(eventId, image);
                    return Response.ok().build();
                }
            }
        }

        return Response.status(BAD_REQUEST).build();
    }

    @DELETE
    public Response deleteImage() {
        eventImageService.deleteImage(eventId);
        return Response.noContent().build();
    }
}