use of org.opencastproject.serviceregistry.api.ServiceState 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;
}
use of org.opencastproject.serviceregistry.api.ServiceState 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);
}
Aggregations