Search in sources :

Example 1 with PropertiesResponse

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);
        }
    }
}
Also used : JsonSyntaxException(com.google.gson.JsonSyntaxException) PropertiesResponse(org.opencastproject.util.PropertiesResponse) ByteArrayInputStream(java.io.ByteArrayInputStream) Gson(com.google.gson.Gson) IOException(java.io.IOException) Properties(java.util.Properties) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces) RestQuery(org.opencastproject.util.doc.rest.RestQuery)

Example 2 with PropertiesResponse

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();
    }
}
Also used : PropertiesResponse(org.opencastproject.util.PropertiesResponse) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) RestQuery(org.opencastproject.util.doc.rest.RestQuery)

Aggregations

Path (javax.ws.rs.Path)2 Produces (javax.ws.rs.Produces)2 PropertiesResponse (org.opencastproject.util.PropertiesResponse)2 RestQuery (org.opencastproject.util.doc.rest.RestQuery)2 Gson (com.google.gson.Gson)1 JsonSyntaxException (com.google.gson.JsonSyntaxException)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 Properties (java.util.Properties)1 GET (javax.ws.rs.GET)1 POST (javax.ws.rs.POST)1