All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.ge.snowizard.application.resources.IdResource Maven / Gradle / Ivy

There is a newer version: 1.8.1
Show newest version
package com.ge.snowizard.application.resources;

import static com.google.common.base.Preconditions.checkNotNull;
import io.dropwizard.jersey.caching.CacheControl;
import io.dropwizard.jersey.params.IntParam;
import java.util.List;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET;
import javax.ws.rs.HeaderParam;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response.Status;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.codahale.metrics.annotation.Timed;
import com.fasterxml.jackson.databind.util.JSONPObject;
import com.ge.snowizard.api.Id;
import com.ge.snowizard.api.protos.SnowizardProtos.SnowizardResponse;
import com.ge.snowizard.application.api.SnowizardError;
import com.ge.snowizard.application.core.MediaTypeAdditional;
import com.ge.snowizard.core.IdWorker;
import com.ge.snowizard.exceptions.InvalidSystemClock;
import com.ge.snowizard.exceptions.InvalidUserAgentError;
import com.google.common.base.Optional;
import com.google.common.collect.Lists;

@Path("/")
public class IdResource {

    private static final Logger LOGGER = LoggerFactory
            .getLogger(IdResource.class);
    private final IdWorker worker;

    /**
     * Constructor
     * 
     * @param worker
     */
    public IdResource(final IdWorker worker) {
        this.worker = checkNotNull(worker);
    }

    /**
     * Get a new ID and handle any thrown exceptions
     * 
     * @param agent
     *            User Agent
     * @return generated ID
     * @throws WebApplicationException
     */
    public long getId(final String agent) {
        try {
            return worker.getId(agent);
        } catch (final InvalidUserAgentError e) {
            LOGGER.error("Invalid user agent ({})", agent);
            throw new WebApplicationException(SnowizardError.newResponse(
                    Status.BAD_REQUEST, "Invalid User-Agent header"));
        } catch (final InvalidSystemClock e) {
            LOGGER.error("Invalid system clock", e);
            throw new WebApplicationException(SnowizardError.newResponse(
                    Status.INTERNAL_SERVER_ERROR, e.getMessage()));
        }
    }

    /**
     * Get a new ID as plain text
     * 
     * @param agent
     *            User Agent
     * @responseMessage 400 Invalid User-Agent header
     * @responseMessage 500 Invalid system clock
     * @return generated ID
     */
    @GET
    @Timed
    @Produces(MediaType.TEXT_PLAIN)
    @CacheControl(mustRevalidate = true, noCache = true, noStore = true)
    public String getIdAsString(@HeaderParam("User-Agent") final String agent) {
        return String.valueOf(getId(agent));
    }

    /**
     * Get a new ID as JSON
     * 
     * @param agent
     *            User Agent
     * @responseMessage 400 Invalid User-Agent header
     * @responseMessage 500 Invalid system clock
     * @return generated ID
     */
    @GET
    @Timed
    @Produces(MediaType.APPLICATION_JSON)
    @CacheControl(mustRevalidate = true, noCache = true, noStore = true)
    public Id getIdAsJSON(@HeaderParam("User-Agent") final String agent) {
        return new Id(getId(agent));
    }

    /**
     * Get a new ID as JSONP
     * 
     * @param agent
     *            User Agent
     * @responseMessage 400 Invalid User-Agent header
     * @responseMessage 500 Invalid system clock
     * @return generated ID
     */
    @GET
    @Timed
    @Produces(MediaTypeAdditional.APPLICATION_JAVASCRIPT)
    @CacheControl(mustRevalidate = true, noCache = true, noStore = true)
    public JSONPObject getIdAsJSONP(
            @HeaderParam("User-Agent") final String agent,
            @QueryParam("callback") @DefaultValue("callback") final String callback) {
        return new JSONPObject(callback, getIdAsJSON(agent));
    }

    /**
     * Get one or more IDs as a Google Protocol Buffer response
     * 
     * @param agent
     *            User Agent
     * @param count
     *            Number of IDs to return
     * @responseMessage 400 Invalid User-Agent header
     * @responseMessage 500 Invalid system clock
     * @return generated IDs
     */
    @GET
    @Timed
    @Produces(MediaTypeAdditional.APPLICATION_PROTOBUF)
    @CacheControl(mustRevalidate = true, noCache = true, noStore = true)
    public SnowizardResponse getIdAsProtobuf(
            @HeaderParam("User-Agent") final String agent,
            @QueryParam("count") final Optional count) {

        final List ids = Lists.newArrayList();
        if (count.isPresent()) {
            for (int i = 0; i < count.get().get(); i++) {
                ids.add(getId(agent));
            }
        } else {
            ids.add(getId(agent));
        }
        return SnowizardResponse.newBuilder().addAllId(ids).build();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy