Search in sources :

Example 1 with ServicesListQuery

use of org.opencastproject.index.service.resources.list.query.ServicesListQuery in project opencast by opencast.

the class ServicesListProvider method getList.

@Override
public Map<String, String> getList(String listName, ResourceListQuery query, Organization organization) throws ListProviderException {
    ServicesListQuery servicesQuery;
    try {
        servicesQuery = (ServicesListQuery) query;
    } catch (ClassCastException ex) {
        servicesQuery = new ServicesListQuery(query);
    }
    Map<String, String> result = new HashMap<String, String>();
    if (LIST_STATUS.equals(listName)) {
        for (ServiceState s : ServiceState.values()) {
            result.put(s.toString(), SERVICE_STATUS_FILTER_PREFIX + s.toString());
        }
        return result;
    }
    List<ServiceRegistration> serviceRegistrations;
    try {
        serviceRegistrations = serviceRegistry.getServiceRegistrations();
    } catch (ServiceRegistryException ex) {
        throw new ListProviderException("Failed to get service registrations.", ex);
    }
    for (ServiceRegistration serviceRegistration : serviceRegistrations) {
        if (servicesQuery.getHostname().isSome() && !StringUtils.equals(servicesQuery.getHostname().get(), serviceRegistration.getHost()))
            continue;
        if (servicesQuery.getActions().isSome() && servicesQuery.getActions().get() && serviceRegistration.getServiceState() == ServiceState.NORMAL)
            continue;
        result.put(serviceRegistration.getServiceType(), serviceRegistration.getServiceType());
    }
    if (servicesQuery.getLimit().isSome() || servicesQuery.getLimit().isSome()) {
        int limit = servicesQuery.getLimit().getOrElse(0);
        int offset = servicesQuery.getOffset().getOrElse(0);
        result = new SmartIterator(limit, offset).applyLimitAndOffset(result);
    }
    return result;
}
Also used : ServiceState(org.opencastproject.serviceregistry.api.ServiceState) SmartIterator(org.opencastproject.util.SmartIterator) HashMap(java.util.HashMap) ListProviderException(org.opencastproject.index.service.exception.ListProviderException) ServicesListQuery(org.opencastproject.index.service.resources.list.query.ServicesListQuery) ServiceRegistryException(org.opencastproject.serviceregistry.api.ServiceRegistryException) ServiceRegistration(org.opencastproject.serviceregistry.api.ServiceRegistration)

Example 2 with ServicesListQuery

use of org.opencastproject.index.service.resources.list.query.ServicesListQuery in project opencast by opencast.

the class ServicesEndpoint method getServices.

@GET
@Path("services.json")
@Produces(MediaType.APPLICATION_JSON)
@RestQuery(description = "Returns the list of services", name = "services", restParameters = { @RestParameter(name = "limit", description = "The maximum number of items to return per page", isRequired = false, type = RestParameter.Type.INTEGER), @RestParameter(name = "offset", description = "The offset", isRequired = false, type = RestParameter.Type.INTEGER), @RestParameter(name = "filter", description = "Filter results by name, host, actions, status or free text query", isRequired = false, type = STRING), @RestParameter(name = "sort", description = "The sort order.  May include any " + "of the following: host, name, running, queued, completed,  meanRunTime, meanQueueTime, " + "status. The sort suffix must be :asc for ascending sort order and :desc for descending.", isRequired = false, type = STRING) }, reponses = { @RestResponse(description = "Returns the list of services from Opencast", responseCode = HttpServletResponse.SC_OK) }, returnDescription = "The list of services")
public Response getServices(@QueryParam("limit") final int limit, @QueryParam("offset") final int offset, @QueryParam("filter") String filter, @QueryParam("sort") String sort) throws Exception {
    Option<String> sortOpt = Option.option(StringUtils.trimToNull(sort));
    ServicesListQuery query = new ServicesListQuery();
    EndpointUtil.addRequestFiltersToQuery(filter, query);
    String fName = null;
    if (query.getName().isSome())
        fName = StringUtils.trimToNull(query.getName().get());
    String fHostname = null;
    if (query.getHostname().isSome())
        fHostname = StringUtils.trimToNull(query.getHostname().get());
    String fStatus = null;
    if (query.getStatus().isSome())
        fStatus = StringUtils.trimToNull(query.getStatus().get());
    String fFreeText = null;
    if (query.getFreeText().isSome())
        fFreeText = StringUtils.trimToNull(query.getFreeText().get());
    List<Service> services = new ArrayList<Service>();
    for (ServiceStatistics stats : serviceRegistry.getServiceStatistics()) {
        Service service = new Service(stats);
        if (fName != null && !StringUtils.equalsIgnoreCase(service.getName(), fName))
            continue;
        if (fHostname != null && !StringUtils.equalsIgnoreCase(service.getHost(), fHostname))
            continue;
        if (fStatus != null && !StringUtils.equalsIgnoreCase(service.getStatus().toString(), fStatus))
            continue;
        if (query.getActions().isSome()) {
            ServiceState serviceState = service.getStatus();
            if (query.getActions().get()) {
                if (ServiceState.NORMAL == serviceState)
                    continue;
            } else {
                if (ServiceState.NORMAL != serviceState)
                    continue;
            }
        }
        if (fFreeText != null && !StringUtils.containsIgnoreCase(service.getName(), fFreeText) && !StringUtils.containsIgnoreCase(service.getHost(), fFreeText) && !StringUtils.containsIgnoreCase(service.getStatus().toString(), fFreeText))
            continue;
        services.add(service);
    }
    int total = services.size();
    if (sortOpt.isSome()) {
        Set<SortCriterion> sortCriteria = RestUtils.parseSortQueryParameter(sortOpt.get());
        if (!sortCriteria.isEmpty()) {
            try {
                SortCriterion sortCriterion = sortCriteria.iterator().next();
                Collections.sort(services, new ServiceStatisticsComparator(sortCriterion.getFieldName(), sortCriterion.getOrder() == SearchQuery.Order.Ascending));
            } catch (Exception ex) {
                logger.warn("Failed to sort services collection.", ex);
            }
        }
    }
    List<JValue> jsonList = new ArrayList<JValue>();
    for (Service s : new SmartIterator<Service>(limit, offset).applyLimitAndOffset(services)) {
        jsonList.add(s.toJSON());
    }
    return RestUtils.okJsonList(jsonList, offset, limit, total);
}
Also used : ServiceState(org.opencastproject.serviceregistry.api.ServiceState) ArrayList(java.util.ArrayList) RestService(org.opencastproject.util.doc.rest.RestService) ServiceStatistics(org.opencastproject.serviceregistry.api.ServiceStatistics) SortCriterion(org.opencastproject.matterhorn.search.SortCriterion) JValue(com.entwinemedia.fn.data.json.JValue) ServicesListQuery(org.opencastproject.index.service.resources.list.query.ServicesListQuery) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) RestQuery(org.opencastproject.util.doc.rest.RestQuery)

Example 3 with ServicesListQuery

use of org.opencastproject.index.service.resources.list.query.ServicesListQuery in project opencast by opencast.

the class ServicesListProviderTest method setUp.

@Before
public void setUp() throws Exception {
    serviceRegistry = EasyMock.createNiceMock(ServiceRegistry.class);
    ServiceRegistration sr1 = new ServiceRegistrationInMemoryImpl(SERVICE_TYPE_1, "host1", "service-path-1", true);
    ServiceRegistration sr2 = new ServiceRegistrationInMemoryImpl(SERVICE_TYPE_2, "host1", "service-path-2", false);
    ServiceRegistration sr3 = new ServiceRegistrationInMemoryImpl(SERVICE_TYPE_3, "host1", "service-path-3", true);
    EasyMock.expect(serviceRegistry.getServiceRegistrations()).andReturn(Arrays.asList(sr1, sr2, sr3)).anyTimes();
    servicesListProvider = new ServicesListProvider();
    servicesListProvider.setServiceRegistry(serviceRegistry);
    servicesListProvider.activate(null);
    servicesQuery = new ServicesListQuery();
    EasyMock.replay(serviceRegistry);
}
Also used : ServiceRegistrationInMemoryImpl(org.opencastproject.serviceregistry.api.ServiceRegistrationInMemoryImpl) ServiceRegistry(org.opencastproject.serviceregistry.api.ServiceRegistry) ServicesListQuery(org.opencastproject.index.service.resources.list.query.ServicesListQuery) ServiceRegistration(org.opencastproject.serviceregistry.api.ServiceRegistration) Before(org.junit.Before)

Aggregations

ServicesListQuery (org.opencastproject.index.service.resources.list.query.ServicesListQuery)3 ServiceRegistration (org.opencastproject.serviceregistry.api.ServiceRegistration)2 ServiceState (org.opencastproject.serviceregistry.api.ServiceState)2 JValue (com.entwinemedia.fn.data.json.JValue)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 GET (javax.ws.rs.GET)1 Path (javax.ws.rs.Path)1 Produces (javax.ws.rs.Produces)1 Before (org.junit.Before)1 ListProviderException (org.opencastproject.index.service.exception.ListProviderException)1 SortCriterion (org.opencastproject.matterhorn.search.SortCriterion)1 ServiceRegistrationInMemoryImpl (org.opencastproject.serviceregistry.api.ServiceRegistrationInMemoryImpl)1 ServiceRegistry (org.opencastproject.serviceregistry.api.ServiceRegistry)1 ServiceRegistryException (org.opencastproject.serviceregistry.api.ServiceRegistryException)1 ServiceStatistics (org.opencastproject.serviceregistry.api.ServiceStatistics)1 SmartIterator (org.opencastproject.util.SmartIterator)1 RestQuery (org.opencastproject.util.doc.rest.RestQuery)1 RestService (org.opencastproject.util.doc.rest.RestService)1