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

net.sf.ehcache.management.resource.services.QueryResourceServiceImpl Maven / Gradle / Ivy

package net.sf.ehcache.management.resource.services;

import java.util.Collection;
import java.util.List;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;

import net.sf.ehcache.management.resource.QueryResultsEntity;
import net.sf.ehcache.management.service.CacheManagerService;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.terracotta.management.ServiceExecutionException;
import org.terracotta.management.ServiceLocator;
import org.terracotta.management.resource.exceptions.ExceptionUtils;
import org.terracotta.management.resource.exceptions.ResourceRuntimeException;
import org.terracotta.management.resource.services.validator.RequestValidator;

/**
 * @author gkeim
 */
@Path("/agents/cacheManagers/query")
public final class QueryResourceServiceImpl {
  public final static String ATTR_QUERY_KEY = "text";

  private static final Logger LOG = LoggerFactory.getLogger(QueryResourceServiceImpl.class);

  private final RequestValidator validator;

  private final CacheManagerService cacheMgrSvc;

  public QueryResourceServiceImpl() {
    this.validator = ServiceLocator.locate(RequestValidator.class);
    this.cacheMgrSvc = ServiceLocator.locate(CacheManagerService.class);
  }

  @GET
  @Produces(MediaType.APPLICATION_JSON)
  public Collection executeQuery(@Context UriInfo info) {
    LOG.debug(String.format("Invoking executeQuery: %s", info.getRequestUri()));

    validator.validateSafe(info);

    String cacheManagerName = info.getPathSegments().get(1).getMatrixParameters().getFirst("names");

    MultivaluedMap qParams = info.getQueryParameters();
    List querys = qParams.get(ATTR_QUERY_KEY);
    String queryString = querys.size() > 0 ? querys.get(0) : null;

    try {
      return cacheMgrSvc.executeQuery(cacheManagerName, queryString);
    } catch (ServiceExecutionException e) {
      Throwable t = ExceptionUtils.getRootCause(e);
      throw new ResourceRuntimeException("Failed to execute query", t, Response.Status.BAD_REQUEST.getStatusCode());
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy