it.tidalwave.bluebill.stats.rest.v1.PingResourceV1 Maven / Gradle / Ivy
The newest version!
/***********************************************************************************************************************
*
* blueBill Stats
* Copyright (C) 2011-2011 by Tidalwave s.a.s. (http://www.tidalwave.it)
*
***********************************************************************************************************************
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
***********************************************************************************************************************
*
* WWW: http://bluebill.tidalwave.it/mobile/
* SCM: http://java.net/hg/bluebill-server~stats-src
*
**********************************************************************************************************************/
package it.tidalwave.bluebill.stats.rest.v1;
import javax.ws.rs.core.Response.Status;
import javax.ws.rs.PUT;
import it.tidalwave.bluebill.stats.domain.PingV1;
import javax.annotation.Nonnull;
import javax.inject.Inject;
import java.util.ArrayList;
import java.util.List;
import java.io.IOException;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import org.springframework.beans.factory.annotation.Configurable;
import it.tidalwave.bluebill.stats.domain.Ping;
import it.tidalwave.bluebill.stats.domain.PingDao;
import javax.ws.rs.Consumes;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Response;
import lombok.extern.slf4j.Slf4j;
import static javax.ws.rs.core.MediaType.*;
import static it.tidalwave.util.Finder.SortDirection.*;
import static it.tidalwave.bluebill.stats.domain.PingFinder.*;
/***********************************************************************************************************************
*
* A REST interface for the {@link Ping} resource.
*
* @stereotype REST Service
*
* @author Fabrizio Giudici
* @version $Id$
*
**********************************************************************************************************************/
@Configurable(preConstruction=true) @Path("v1/ping") @Slf4j
public class PingResourceV1
{
private static final int DEFAULT_MAX_RESULTS = 10;
@Inject @Nonnull
private PingDao dao;
/*******************************************************************************************************************
*
* Exposes {@link Pings}.
*
******************************************************************************************************************/
@GET @Produces({APPLICATION_JSON, APPLICATION_XML})
public PingsV1 getPings (final @QueryParam("maxResults") @DefaultValue("" + DEFAULT_MAX_RESULTS) int maxResults)
throws IOException
{
log.info("getPings({})", maxResults);
final List pings = new ArrayList();
for (final Ping ping : dao.findPings().sort(BY_TIMESTAMP, DESCENDING).max(maxResults).results())
{
pings.add(exportPing(ping));
}
return new PingsV1(pings);
}
/*******************************************************************************************************************
*
* Puts a new {@link Ping}.
*
******************************************************************************************************************/
@PUT @Consumes({APPLICATION_JSON, APPLICATION_XML})
public Response putPing (final @Nonnull PingV1 ping)
{
log.info("putPing({})", ping);
dao.insert(ping.toPing());
return Response.status(Status.CREATED).build();
}
/*******************************************************************************************************************
*
* Exports a {@link Ping}, eventually removing some fields for enforcing the security policy.
*
******************************************************************************************************************/
@Nonnull
private PingV1 exportPing (final @Nonnull Ping ping)
{
final PingV1 pingV1 = new PingV1(ping);
// TODO: should be done by an aspect
pingV1.setInstallId(null);
pingV1.setIpAddress(null);
return pingV1;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy