use of org.opencastproject.util.PropertiesResponse in project opencast by opencast.
the class CaptureAgentStateRestService method setConfiguration.
@POST
@Produces({ MediaType.TEXT_XML, MediaType.APPLICATION_JSON })
@Path("agents/{name}/configuration")
@RestQuery(name = "setAgentStateConfiguration", description = "Set the configuration of a given capture agent, registering it if it does not exist", pathParameters = { @RestParameter(description = "Name of the capture agent", isRequired = true, name = "name", type = Type.STRING) }, restParameters = { @RestParameter(description = "An XML or JSON representation of the capabilities. XML as specified in " + "http://java.sun.com/dtd/properties.dtd (friendly names as keys, device locations as corresponding values)", type = Type.TEXT, isRequired = true, name = "configuration") }, reponses = { @RestResponse(description = "An XML or JSON representation of the agent configuration", responseCode = SC_OK), @RestResponse(description = "The configuration format is incorrect OR the agent name is blank or null", responseCode = SC_BAD_REQUEST) }, returnDescription = "")
public Response setConfiguration(@PathParam("name") String agentName, @FormParam("configuration") String configuration) {
if (service == null)
return Response.serverError().status(Response.Status.SERVICE_UNAVAILABLE).build();
if (StringUtils.isBlank(configuration)) {
logger.debug("The configuration data cannot be blank");
return Response.serverError().status(Response.Status.BAD_REQUEST).build();
}
Properties caps;
if (StringUtils.startsWith(configuration, "{")) {
// JSON
Gson gson = new Gson();
try {
caps = gson.fromJson(configuration, Properties.class);
if (!service.setAgentConfiguration(agentName, caps)) {
logger.debug("'{}''s configuration has not been updated because nothing has been changed", agentName);
}
return Response.ok(gson.toJson(caps)).type(MediaType.APPLICATION_JSON).build();
} catch (JsonSyntaxException e) {
logger.debug("Exception when deserializing capabilities: {}", e.getMessage());
return Response.status(javax.ws.rs.core.Response.Status.BAD_REQUEST).build();
}
} else {
// XML
caps = new Properties();
ByteArrayInputStream bais = null;
try {
bais = new ByteArrayInputStream(configuration.getBytes());
caps.loadFromXML(bais);
if (!service.setAgentConfiguration(agentName, caps)) {
logger.debug("'{}''s configuration has not been updated because nothing has been changed", agentName);
}
// Prepares the value to return
PropertiesResponse r = new PropertiesResponse(caps);
logger.debug("{}'s configuration updated", agentName);
return Response.ok(r).type(MediaType.TEXT_XML).build();
} catch (IOException e) {
logger.debug("Unexpected I/O Exception when unmarshalling the capabilities: {}", e.getMessage());
return Response.status(javax.ws.rs.core.Response.Status.BAD_REQUEST).build();
} finally {
IOUtils.closeQuietly(bais);
}
}
}
use of org.opencastproject.util.PropertiesResponse in project opencast by opencast.
the class CaptureAgentStateRestService method getConfiguration.
@GET
@Produces({ MediaType.TEXT_XML, MediaType.APPLICATION_JSON })
@Path("agents/{name}/configuration.{type:xml|json}")
@RestQuery(name = "getAgentConfiguration", description = "Return the configuration of a given capture agent", pathParameters = { @RestParameter(description = "Name of the capture agent", isRequired = true, name = "name", type = Type.STRING), @RestParameter(description = "The Document type", isRequired = true, name = "type", type = Type.STRING) }, restParameters = {}, reponses = { @RestResponse(description = "An XML or JSON representation of the agent configuration", responseCode = SC_OK), @RestResponse(description = "The agent {name} does not exist in the system", responseCode = SC_NOT_FOUND) }, returnDescription = "")
public Response getConfiguration(@PathParam("name") String agentName, @PathParam("type") String type) throws NotFoundException {
if (service == null)
return Response.serverError().status(Response.Status.SERVICE_UNAVAILABLE).build();
PropertiesResponse r = new PropertiesResponse(service.getAgentConfiguration(agentName));
logger.debug("Returning configuration for the agent {}", agentName);
if ("json".equals(type)) {
return Response.ok(r).type(MediaType.APPLICATION_JSON).build();
} else {
return Response.ok(r).type(MediaType.TEXT_XML).build();
}
}
Aggregations