forked from community/device-mgt-core
parent
56faa75861
commit
c3f23fa564
@ -0,0 +1,44 @@
|
||||
/*
|
||||
* Copyright (c) 2016, WSO2 Inc. (http:www.wso2.org) All Rights Reserved.
|
||||
*
|
||||
* WSO2 Inc. licenses this file to you under the Apache License,
|
||||
* Version 2.0 (the "License"); you may not use this file except
|
||||
* in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
|
||||
package org.wso2.carbon.device.mgt.jaxrs.beans;
|
||||
|
||||
import io.swagger.annotations.ApiModel;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
|
||||
import javax.validation.constraints.Size;
|
||||
|
||||
/**
|
||||
* This class is used to wrap the events which receive from the agent application.
|
||||
*/
|
||||
@ApiModel(value = "EventBeanWrapper",
|
||||
description = "agent's event related Information.")
|
||||
public class EventBeanWrapper {
|
||||
|
||||
@ApiModelProperty(name = "payloadData", value = "Event payload payload.", required = true)
|
||||
Object[] payloadData;
|
||||
|
||||
public Object[] getPayloadData() {
|
||||
return payloadData;
|
||||
}
|
||||
|
||||
public void setPayloadData(Object[] payloadData) {
|
||||
this.payloadData = payloadData;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,543 @@
|
||||
/*
|
||||
* Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
|
||||
*
|
||||
* WSO2 Inc. licenses this file to you under the Apache License,
|
||||
* Version 2.0 (the "License"); you may not use this file except
|
||||
* in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*
|
||||
*/
|
||||
package org.wso2.carbon.device.mgt.jaxrs.service.api;
|
||||
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import io.swagger.annotations.ApiParam;
|
||||
import io.swagger.annotations.ApiResponse;
|
||||
import io.swagger.annotations.ApiResponses;
|
||||
import io.swagger.annotations.Extension;
|
||||
import io.swagger.annotations.ExtensionProperty;
|
||||
import io.swagger.annotations.Info;
|
||||
import io.swagger.annotations.ResponseHeader;
|
||||
import io.swagger.annotations.SwaggerDefinition;
|
||||
import io.swagger.annotations.Tag;
|
||||
import org.wso2.carbon.apimgt.annotations.api.Scope;
|
||||
import org.wso2.carbon.apimgt.annotations.api.Scopes;
|
||||
import org.wso2.carbon.device.mgt.common.Device;
|
||||
import org.wso2.carbon.device.mgt.common.operation.mgt.Operation;
|
||||
import org.wso2.carbon.device.mgt.jaxrs.beans.ErrorResponse;
|
||||
import org.wso2.carbon.device.mgt.jaxrs.beans.EventBeanWrapper;
|
||||
import org.wso2.carbon.device.mgt.jaxrs.beans.OperationList;
|
||||
import org.wso2.carbon.device.mgt.jaxrs.util.Constants;
|
||||
|
||||
import javax.validation.Valid;
|
||||
import javax.ws.rs.Consumes;
|
||||
import javax.ws.rs.DELETE;
|
||||
import javax.ws.rs.GET;
|
||||
import javax.ws.rs.POST;
|
||||
import javax.ws.rs.PUT;
|
||||
import javax.ws.rs.Path;
|
||||
import javax.ws.rs.PathParam;
|
||||
import javax.ws.rs.Produces;
|
||||
import javax.ws.rs.QueryParam;
|
||||
import javax.ws.rs.core.MediaType;
|
||||
import javax.ws.rs.core.Response;
|
||||
|
||||
@SwaggerDefinition(
|
||||
info = @Info(
|
||||
version = "1.0.0",
|
||||
title = "",
|
||||
extensions = {
|
||||
@Extension(properties = {
|
||||
@ExtensionProperty(name = "name", value = "DeviceAgent Service"),
|
||||
@ExtensionProperty(name = "context", value = "/api/device-mgt/v1.0/device/agent"),
|
||||
})
|
||||
}
|
||||
),
|
||||
tags = {
|
||||
@Tag(name = "device_agent, device_management", description = "")
|
||||
}
|
||||
)
|
||||
@Api(value = "Device Agent", description = "Device Agent Service")
|
||||
@Path("/device/agent")
|
||||
@Produces(MediaType.APPLICATION_JSON)
|
||||
@Consumes(MediaType.APPLICATION_JSON)
|
||||
@Scopes(
|
||||
scopes = {
|
||||
@Scope(
|
||||
name = "Enroll Device",
|
||||
description = "Register a device",
|
||||
key = "perm:device:enroll",
|
||||
permissions = {"/device-mgt/devices/owning-device/add"}
|
||||
),
|
||||
@Scope(
|
||||
name = "Modify Device",
|
||||
description = "Modify a device",
|
||||
key = "perm:device:modify",
|
||||
permissions = {"/device-mgt/devices/owning-device/modify"}
|
||||
),
|
||||
@Scope(
|
||||
name = "Getting Device Operation Details",
|
||||
description = "Getting Device Operation Details",
|
||||
key = "perm:devices:operations",
|
||||
permissions = {"/device-mgt/devices/owning-device/view"}
|
||||
),
|
||||
@Scope(
|
||||
name = "Disenroll Device",
|
||||
description = "Disenroll a device",
|
||||
key = "perm:device:disenroll",
|
||||
permissions = {"/device-mgt/devices/owning-device/remove"}
|
||||
)
|
||||
}
|
||||
)
|
||||
public interface DeviceAgentService {
|
||||
|
||||
@POST
|
||||
@Path("/enroll")
|
||||
@ApiOperation(
|
||||
produces = MediaType.APPLICATION_JSON,
|
||||
httpMethod = "POST",
|
||||
value = "Create a device instance",
|
||||
notes = "Create a device Instance",
|
||||
tags = "Device Management",
|
||||
extensions = {
|
||||
@Extension(properties = {
|
||||
@ExtensionProperty(name = Constants.SCOPE, value = "perm:device:enroll")
|
||||
})
|
||||
}
|
||||
)
|
||||
@ApiResponses(
|
||||
value = {
|
||||
@ApiResponse(
|
||||
code = 200,
|
||||
message = "OK. \n Successfully created a device instance.",
|
||||
responseHeaders = {
|
||||
@ResponseHeader(
|
||||
name = "Content-Type",
|
||||
description = "The content type of the body"),
|
||||
@ResponseHeader(
|
||||
name = "ETag",
|
||||
description = "Entity Tag of the response resource.\n" +
|
||||
"Used by caches, or in conditional requests."),
|
||||
@ResponseHeader(
|
||||
name = "Last-Modified",
|
||||
description = "Date and time the resource was last modified.\n" +
|
||||
"Used by caches, or in conditional requests."),
|
||||
}),
|
||||
@ApiResponse(
|
||||
code = 304,
|
||||
message = "Not Modified. Empty body because the client already has the latest version" +
|
||||
" of the requested resource.\n"),
|
||||
@ApiResponse(
|
||||
code = 400,
|
||||
message = "Bad Request. \n Invalid request or validation error.",
|
||||
response = ErrorResponse.class),
|
||||
@ApiResponse(
|
||||
code = 404,
|
||||
message = "Not Found. \n A deviceType with the specified device type was not found.",
|
||||
response = ErrorResponse.class),
|
||||
@ApiResponse(
|
||||
code = 500,
|
||||
message = "Internal Server Error. \n " +
|
||||
"Server error occurred while retrieving the device details.",
|
||||
response = ErrorResponse.class)
|
||||
})
|
||||
Response enrollDevice(@ApiParam(name = "device", value = "Device object with data.", required = true)
|
||||
@Valid Device device);
|
||||
|
||||
@DELETE
|
||||
@Path("/enroll/{type}/{id}")
|
||||
@ApiOperation(
|
||||
httpMethod = "DELETE",
|
||||
value = "Unregistering a Device",
|
||||
notes = "Use this REST API to unregister a device.",
|
||||
tags = "Device Management",
|
||||
extensions = {
|
||||
@Extension(properties = {
|
||||
@ExtensionProperty(name = Constants.SCOPE, value = "perm:device:disenroll")
|
||||
})
|
||||
}
|
||||
)
|
||||
@ApiResponses(value = {
|
||||
@ApiResponse(
|
||||
code = 200,
|
||||
message = "OK. \n Successfully disenrolled the device."),
|
||||
@ApiResponse(
|
||||
code = 404,
|
||||
message = "Not Found. \n The specified resource does not exist."),
|
||||
@ApiResponse(
|
||||
code = 500,
|
||||
message = "Internal Server Error. \n " +
|
||||
"Server error occurred while dis-enrolling the device.")
|
||||
})
|
||||
Response disEnrollDevice(
|
||||
@ApiParam(name = "type", value = "The unique device identifier.") @PathParam("type") String type,
|
||||
@ApiParam(name = "id", value = "The unique device identifier.") @PathParam("id") String id);
|
||||
|
||||
@PUT
|
||||
@Path("/enroll/{type}/{id}")
|
||||
@ApiOperation(
|
||||
produces = MediaType.APPLICATION_JSON,
|
||||
httpMethod = "PUT",
|
||||
value = "modify device",
|
||||
notes = "modify device",
|
||||
tags = "Device Agent Management",
|
||||
extensions = {
|
||||
@Extension(properties = {
|
||||
@ExtensionProperty(name = Constants.SCOPE, value = "perm:device:modify")
|
||||
})
|
||||
}
|
||||
)
|
||||
@ApiResponses(
|
||||
value = {
|
||||
@ApiResponse(
|
||||
code = 200,
|
||||
message = "OK. \n Successfully updated device instance.",
|
||||
responseHeaders = {
|
||||
@ResponseHeader(
|
||||
name = "Content-Type",
|
||||
description = "The content type of the body"),
|
||||
@ResponseHeader(
|
||||
name = "ETag",
|
||||
description = "Entity Tag of the response resource.\n" +
|
||||
"Used by caches, or in conditional requests."),
|
||||
@ResponseHeader(
|
||||
name = "Last-Modified",
|
||||
description = "Date and time the resource was last modified.\n" +
|
||||
"Used by caches, or in conditional requests."),
|
||||
}),
|
||||
@ApiResponse(
|
||||
code = 304,
|
||||
message = "Not Modified. Empty body because the client already has the latest version" +
|
||||
" of the requested resource.\n"),
|
||||
@ApiResponse(
|
||||
code = 400,
|
||||
message = "Bad Request. \n Invalid request or validation error.",
|
||||
response = ErrorResponse.class),
|
||||
@ApiResponse(
|
||||
code = 404,
|
||||
message = "Not Found. \n A deviceType with the specified device type was not found.",
|
||||
response = ErrorResponse.class),
|
||||
@ApiResponse(
|
||||
code = 500,
|
||||
message = "Internal Server Error. \n " +
|
||||
"Server error occurred while retrieving the device details.",
|
||||
response = ErrorResponse.class)
|
||||
})
|
||||
Response updateDevice(@ApiParam(name = "type", value = "The device type, such as ios, android or windows....etc", required = true)
|
||||
@PathParam("type") String type,
|
||||
@ApiParam(name = "id", value = "The device id.", required = true)
|
||||
@PathParam("id") String deviceId,
|
||||
@ApiParam(name = "device", value = "Device object with data.", required = true)
|
||||
@Valid Device updateDevice);
|
||||
|
||||
@POST
|
||||
@Path("/events/publish/{type}/{deviceId}")
|
||||
@ApiOperation(
|
||||
produces = MediaType.APPLICATION_JSON,
|
||||
consumes = MediaType.APPLICATION_JSON,
|
||||
httpMethod = "POST",
|
||||
value = "Publishing Events",
|
||||
notes = "Publish events received by the device client to the WSO2 Data Analytics Server (DAS) using this API.",
|
||||
tags = "Device Agent Management",
|
||||
extensions = {
|
||||
@Extension(properties = {
|
||||
@ExtensionProperty(name = Constants.SCOPE, value = "perm:device:enroll")
|
||||
})
|
||||
}
|
||||
)
|
||||
@ApiResponses(
|
||||
value = {
|
||||
@ApiResponse(code = 201, message = "Created. \n Successfully published the event. Location header " +
|
||||
"contains URL of newly enrolled device",
|
||||
responseHeaders = {
|
||||
@ResponseHeader(
|
||||
name = "Content-Location",
|
||||
description = "The URL of the added policy."),
|
||||
@ResponseHeader(
|
||||
name = "Content-Type",
|
||||
description = "The content type of the body"),
|
||||
@ResponseHeader(
|
||||
name = "ETag",
|
||||
description = "Entity Tag of the response resource.\n" +
|
||||
"Used by caches, or in conditional requests."),
|
||||
@ResponseHeader(
|
||||
name = "Last-Modified",
|
||||
description = "Date and time the resource was last modified.\n" +
|
||||
"Used by caches, or in conditional requests.")
|
||||
}),
|
||||
@ApiResponse(
|
||||
code = 303,
|
||||
message = "See Other. \n The source can be retrieved from the URL specified in the location header.",
|
||||
responseHeaders = {
|
||||
@ResponseHeader(
|
||||
name = "Content-Location",
|
||||
description = "The Source URL of the document.")}),
|
||||
@ApiResponse(
|
||||
code = 400,
|
||||
message = "Bad Request. \n Invalid request or validation error."),
|
||||
@ApiResponse(
|
||||
code = 415,
|
||||
message = "Unsupported media type. \n The format of the requested entity was not supported."),
|
||||
@ApiResponse(
|
||||
code = 500,
|
||||
message = "Internal Server Error. \n " +
|
||||
"Server error occurred while publishing events.")
|
||||
})
|
||||
Response publishEvents(
|
||||
@ApiParam(
|
||||
name = "eventBeanWrapper",
|
||||
value = "Information of the agent event to be published on DAS.")
|
||||
@Valid
|
||||
EventBeanWrapper eventBeanWrapper,
|
||||
@ApiParam(
|
||||
name = "type",
|
||||
value = "name of the device type")
|
||||
@PathParam("type") String type,
|
||||
@ApiParam(
|
||||
name = "deviceId",
|
||||
value = "deviceId of the device")
|
||||
@PathParam("deviceId") String deviceId);
|
||||
|
||||
@GET
|
||||
@Path("/pending/operations/{type}/{id}")
|
||||
@ApiOperation(
|
||||
produces = MediaType.APPLICATION_JSON,
|
||||
consumes = MediaType.APPLICATION_JSON,
|
||||
httpMethod = "GET",
|
||||
value = "Get pending operation of the given device",
|
||||
notes = "Returns the Operations.",
|
||||
tags = "Device Agent Management",
|
||||
extensions = {
|
||||
@Extension(properties = {
|
||||
@ExtensionProperty(name = Constants.SCOPE, value = "perm:devices:operations")
|
||||
})
|
||||
}
|
||||
)
|
||||
@ApiResponses(
|
||||
value = {
|
||||
@ApiResponse(
|
||||
code = 200,
|
||||
message = "OK. \n Successfully retrieved the operations.",
|
||||
response = OperationList.class,
|
||||
responseHeaders = {
|
||||
@ResponseHeader(
|
||||
name = "Content-Type",
|
||||
description = "The content type of the body"),
|
||||
@ResponseHeader(
|
||||
name = "ETag",
|
||||
description = "Entity Tag of the response resource.\n" +
|
||||
"Used by caches, or in conditional requests."),
|
||||
@ResponseHeader(
|
||||
name = "Last-Modified",
|
||||
description = "Date and time the resource has been modified the last time.\n" +
|
||||
"Used by caches, or in conditional requests."),
|
||||
}),
|
||||
@ApiResponse(
|
||||
code = 304,
|
||||
message = "Not Modified. Empty body because the client already has the latest " +
|
||||
"version of the requested resource."),
|
||||
@ApiResponse(
|
||||
code = 400,
|
||||
message = "Bad Request. \n Invalid request or validation error.",
|
||||
response = ErrorResponse.class),
|
||||
@ApiResponse(
|
||||
code = 404,
|
||||
message = "Not Found. \n No device is found under the provided type and id.",
|
||||
response = ErrorResponse.class),
|
||||
@ApiResponse(
|
||||
code = 500,
|
||||
message = "Internal Server Error. \n " +
|
||||
"Server error occurred while retrieving information requested device.",
|
||||
response = ErrorResponse.class)
|
||||
})
|
||||
Response getPendingOperations(@ApiParam(name = "type", value = "The device type, such as ios, android or windows.", required = true)
|
||||
@PathParam("type") String type,
|
||||
@ApiParam(name = "id", value = "The device id.", required = true)
|
||||
@PathParam("id") String deviceId);
|
||||
|
||||
@GET
|
||||
@Path("/last-pending/operation/{type}/{id}")
|
||||
@ApiOperation(
|
||||
produces = MediaType.APPLICATION_JSON,
|
||||
consumes = MediaType.APPLICATION_JSON,
|
||||
httpMethod = "GET",
|
||||
value = "Get pending operation of the given device",
|
||||
notes = "Returns the Operation.",
|
||||
tags = "Device Agent Management",
|
||||
extensions = {
|
||||
@Extension(properties = {
|
||||
@ExtensionProperty(name = Constants.SCOPE, value = "perm:devices:operations")
|
||||
})
|
||||
}
|
||||
)
|
||||
@ApiResponses(
|
||||
value = {
|
||||
@ApiResponse(
|
||||
code = 200,
|
||||
message = "OK. \n Successfully retrieved the operation.",
|
||||
response = Operation.class,
|
||||
responseHeaders = {
|
||||
@ResponseHeader(
|
||||
name = "Content-Type",
|
||||
description = "The content type of the body"),
|
||||
@ResponseHeader(
|
||||
name = "ETag",
|
||||
description = "Entity Tag of the response resource.\n" +
|
||||
"Used by caches, or in conditional requests."),
|
||||
@ResponseHeader(
|
||||
name = "Last-Modified",
|
||||
description = "Date and time the resource has been modified the last time.\n" +
|
||||
"Used by caches, or in conditional requests."),
|
||||
}),
|
||||
@ApiResponse(
|
||||
code = 304,
|
||||
message = "Not Modified. Empty body because the client already has the latest " +
|
||||
"version of the requested resource."),
|
||||
@ApiResponse(
|
||||
code = 400,
|
||||
message = "Bad Request. \n Invalid request or validation error.",
|
||||
response = ErrorResponse.class),
|
||||
@ApiResponse(
|
||||
code = 404,
|
||||
message = "Not Found. \n No device is found under the provided type and id.",
|
||||
response = ErrorResponse.class),
|
||||
@ApiResponse(
|
||||
code = 500,
|
||||
message = "Internal Server Error. \n " +
|
||||
"Server error occurred while retrieving information requested device.",
|
||||
response = ErrorResponse.class)
|
||||
})
|
||||
Response getNextPendingOperation(@ApiParam(name = "type", value = "The device type, such as ios, android or windows.", required = true)
|
||||
@PathParam("type") String type,
|
||||
@ApiParam(name = "id", value = "The device id.", required = true)
|
||||
@PathParam("id") String deviceId);
|
||||
|
||||
@PUT
|
||||
@Path("/operations/{type}/{id}")
|
||||
@ApiOperation(
|
||||
produces = MediaType.APPLICATION_JSON,
|
||||
consumes = MediaType.APPLICATION_JSON,
|
||||
httpMethod = "PUT",
|
||||
value = "Update Operation",
|
||||
notes = "Update the Operations.",
|
||||
tags = "Device Agent Management",
|
||||
extensions = {
|
||||
@Extension(properties = {
|
||||
@ExtensionProperty(name = Constants.SCOPE, value = "perm:devices:operations")
|
||||
})
|
||||
}
|
||||
)
|
||||
@ApiResponses(
|
||||
value = {
|
||||
@ApiResponse(
|
||||
code = 200,
|
||||
message = "OK. \n Successfully updated the operations.",
|
||||
responseHeaders = {
|
||||
@ResponseHeader(
|
||||
name = "Content-Type",
|
||||
description = "The content type of the body"),
|
||||
@ResponseHeader(
|
||||
name = "ETag",
|
||||
description = "Entity Tag of the response resource.\n" +
|
||||
"Used by caches, or in conditional requests."),
|
||||
@ResponseHeader(
|
||||
name = "Last-Modified",
|
||||
description = "Date and time the resource has been modified the last time.\n" +
|
||||
"Used by caches, or in conditional requests."),
|
||||
}),
|
||||
@ApiResponse(
|
||||
code = 304,
|
||||
message = "Not Modified. Empty body because the client already has the latest " +
|
||||
"version of the requested resource."),
|
||||
@ApiResponse(
|
||||
code = 400,
|
||||
message = "Bad Request. \n Invalid request or validation error.",
|
||||
response = ErrorResponse.class),
|
||||
@ApiResponse(
|
||||
code = 404,
|
||||
message = "Not Found. \n No device is found under the provided type and id.",
|
||||
response = ErrorResponse.class),
|
||||
@ApiResponse(
|
||||
code = 500,
|
||||
message = "Internal Server Error. \n " +
|
||||
"Server error occurred while retrieving information requested device.",
|
||||
response = ErrorResponse.class)
|
||||
})
|
||||
Response updateOperation(@ApiParam(name = "type", value = "The device type, such as ios, android or windows.", required = true)
|
||||
@PathParam("type") String type,
|
||||
@ApiParam(name = "id", value = "The device id.", required = true)
|
||||
@PathParam("id") String deviceId,
|
||||
@ApiParam(name = "operation", value = "Operation object with data.", required = true)
|
||||
@Valid Operation operation);
|
||||
|
||||
@GET
|
||||
@Path("/status/operations/{type}/{id}")
|
||||
@ApiOperation(
|
||||
produces = MediaType.APPLICATION_JSON,
|
||||
consumes = MediaType.APPLICATION_JSON,
|
||||
httpMethod = "GET",
|
||||
value = "Get pending operation of the given device",
|
||||
notes = "Returns the Operations.",
|
||||
tags = "Device Agent Management",
|
||||
extensions = {
|
||||
@Extension(properties = {
|
||||
@ExtensionProperty(name = Constants.SCOPE, value = "perm:devices:operations")
|
||||
})
|
||||
}
|
||||
)
|
||||
@ApiResponses(
|
||||
value = {
|
||||
@ApiResponse(
|
||||
code = 200,
|
||||
message = "OK. \n Successfully retrieved the operations.",
|
||||
response = OperationList.class,
|
||||
responseHeaders = {
|
||||
@ResponseHeader(
|
||||
name = "Content-Type",
|
||||
description = "The content type of the body"),
|
||||
@ResponseHeader(
|
||||
name = "ETag",
|
||||
description = "Entity Tag of the response resource.\n" +
|
||||
"Used by caches, or in conditional requests."),
|
||||
@ResponseHeader(
|
||||
name = "Last-Modified",
|
||||
description = "Date and time the resource has been modified the last time.\n" +
|
||||
"Used by caches, or in conditional requests."),
|
||||
}),
|
||||
@ApiResponse(
|
||||
code = 304,
|
||||
message = "Not Modified. Empty body because the client already has the latest " +
|
||||
"version of the requested resource."),
|
||||
@ApiResponse(
|
||||
code = 400,
|
||||
message = "Bad Request. \n Invalid request or validation error.",
|
||||
response = ErrorResponse.class),
|
||||
@ApiResponse(
|
||||
code = 404,
|
||||
message = "Not Found. \n No device is found under the provided type and id.",
|
||||
response = ErrorResponse.class),
|
||||
@ApiResponse(
|
||||
code = 500,
|
||||
message = "Internal Server Error. \n " +
|
||||
"Server error occurred while retrieving information requested device.",
|
||||
response = ErrorResponse.class)
|
||||
})
|
||||
Response getOperationsByDeviceAndStatus(@ApiParam(name = "type", value = "The device type, such as ios, android or windows.", required = true)
|
||||
@PathParam("type") String type,
|
||||
@ApiParam(name = "id", value = "The device id.", required = true)
|
||||
@PathParam("id") String deviceId,
|
||||
@ApiParam(name = "status", value = "status of the operation.", required = true)
|
||||
@QueryParam("status")Operation.Status status);
|
||||
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,441 @@
|
||||
/*
|
||||
* Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
|
||||
*
|
||||
* WSO2 Inc. licenses this file to you under the Apache License,
|
||||
* Version 2.0 (the "License"); you may not use this file except
|
||||
* in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*
|
||||
*/
|
||||
package org.wso2.carbon.device.mgt.jaxrs.service.impl;
|
||||
|
||||
import org.apache.axis2.AxisFault;
|
||||
import org.apache.axis2.client.Options;
|
||||
import org.apache.axis2.java.security.SSLProtocolSocketFactory;
|
||||
import org.apache.axis2.transport.http.HTTPConstants;
|
||||
import org.apache.commons.codec.binary.Base64;
|
||||
import org.apache.commons.httpclient.Header;
|
||||
import org.apache.commons.httpclient.protocol.Protocol;
|
||||
import org.apache.commons.httpclient.protocol.ProtocolSocketFactory;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.wso2.carbon.context.PrivilegedCarbonContext;
|
||||
import org.wso2.carbon.core.util.Utils;
|
||||
import org.wso2.carbon.device.mgt.analytics.data.publisher.exception.DataPublisherConfigurationException;
|
||||
import org.wso2.carbon.device.mgt.common.Device;
|
||||
import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
|
||||
import org.wso2.carbon.device.mgt.common.DeviceManagementException;
|
||||
import org.wso2.carbon.device.mgt.common.EnrolmentInfo;
|
||||
import org.wso2.carbon.device.mgt.common.authorization.DeviceAccessAuthorizationException;
|
||||
import org.wso2.carbon.device.mgt.common.authorization.DeviceAccessAuthorizationService;
|
||||
import org.wso2.carbon.device.mgt.common.operation.mgt.Operation;
|
||||
import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManagementException;
|
||||
import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService;
|
||||
import org.wso2.carbon.device.mgt.jaxrs.beans.ErrorResponse;
|
||||
import org.wso2.carbon.device.mgt.jaxrs.beans.EventBeanWrapper;
|
||||
import org.wso2.carbon.device.mgt.jaxrs.beans.OperationList;
|
||||
import org.wso2.carbon.device.mgt.jaxrs.beans.analytics.Attribute;
|
||||
import org.wso2.carbon.device.mgt.jaxrs.beans.analytics.AttributeType;
|
||||
import org.wso2.carbon.device.mgt.jaxrs.beans.analytics.EventAttributeList;
|
||||
import org.wso2.carbon.device.mgt.jaxrs.service.api.DeviceAgentService;
|
||||
import org.wso2.carbon.device.mgt.jaxrs.util.Constants;
|
||||
import org.wso2.carbon.device.mgt.jaxrs.util.DeviceMgtAPIUtils;
|
||||
import org.wso2.carbon.event.stream.stub.EventStreamAdminServiceStub;
|
||||
import org.wso2.carbon.event.stream.stub.types.EventStreamAttributeDto;
|
||||
import org.wso2.carbon.event.stream.stub.types.EventStreamDefinitionDto;
|
||||
import org.wso2.carbon.identity.jwt.client.extension.JWTClient;
|
||||
import org.wso2.carbon.identity.jwt.client.extension.exception.JWTClientException;
|
||||
import org.wso2.carbon.user.api.UserStoreException;
|
||||
|
||||
import javax.cache.Cache;
|
||||
import javax.cache.Caching;
|
||||
import javax.validation.Valid;
|
||||
import javax.ws.rs.DELETE;
|
||||
import javax.ws.rs.GET;
|
||||
import javax.ws.rs.POST;
|
||||
import javax.ws.rs.PUT;
|
||||
import javax.ws.rs.Path;
|
||||
import javax.ws.rs.PathParam;
|
||||
import javax.ws.rs.QueryParam;
|
||||
import javax.ws.rs.core.Response;
|
||||
import java.rmi.RemoteException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@Path("/device/agent")
|
||||
public class DeviceAgentServiceImpl implements DeviceAgentService {
|
||||
private static final Log log = LogFactory.getLog(DeviceAgentServiceImpl.class);
|
||||
|
||||
@POST
|
||||
@Path("/enroll")
|
||||
@Override
|
||||
public Response enrollDevice(@Valid Device device) {
|
||||
if (device == null) {
|
||||
String errorMessage = "The payload of the device enrollment is incorrect.";
|
||||
return Response.status(Response.Status.BAD_REQUEST).entity(errorMessage).build();
|
||||
}
|
||||
try {
|
||||
DeviceManagementProviderService dms = DeviceMgtAPIUtils.getDeviceManagementService();
|
||||
if (device.getType() == null || device.getDeviceIdentifier() == null) {
|
||||
String errorMessage = "The payload of the device enrollment is incorrect.";
|
||||
return Response.status(Response.Status.BAD_REQUEST).entity(errorMessage).build();
|
||||
}
|
||||
Device existingDevice = dms.getDevice(new DeviceIdentifier(device.getType(), device.getType()));
|
||||
if (existingDevice != null && existingDevice.getEnrolmentInfo() != null && existingDevice
|
||||
.getEnrolmentInfo().getStatus().equals(EnrolmentInfo.Status.ACTIVE)) {
|
||||
String errorMessage = "An active enrolment exists";
|
||||
return Response.status(Response.Status.BAD_REQUEST).entity(errorMessage).build();
|
||||
}
|
||||
device.getEnrolmentInfo().setOwner(DeviceMgtAPIUtils.getAuthenticatedUser());
|
||||
device.getEnrolmentInfo().setDateOfEnrolment(System.currentTimeMillis());
|
||||
device.getEnrolmentInfo().setDateOfLastUpdate(System.currentTimeMillis());
|
||||
boolean status = dms.enrollDevice(device);
|
||||
return Response.status(Response.Status.OK).entity(status).build();
|
||||
} catch (DeviceManagementException e) {
|
||||
String msg = "Error occurred while enrolling the device, which carries the id '" +
|
||||
device.getDeviceIdentifier() + "'";
|
||||
log.error(msg, e);
|
||||
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
|
||||
}
|
||||
}
|
||||
|
||||
@DELETE
|
||||
@Path("/enroll/{type}/{id}")
|
||||
@Override
|
||||
public Response disEnrollDevice(@PathParam("type") String type, @PathParam("id") String id) {
|
||||
boolean result;
|
||||
DeviceIdentifier deviceIdentifier = new DeviceIdentifier(id, type);
|
||||
try {
|
||||
result = DeviceMgtAPIUtils.getDeviceManagementService().disenrollDevice(deviceIdentifier);
|
||||
if (result) {
|
||||
return Response.status(Response.Status.OK).build();
|
||||
} else {
|
||||
return Response.status(Response.Status.NO_CONTENT).entity(type + " device that carries id '" + id +
|
||||
"' has not been dis-enrolled").build();
|
||||
}
|
||||
} catch (DeviceManagementException e) {
|
||||
String msg = "Error occurred while enrolling the device, which carries the id '" + id + "'";
|
||||
log.error(msg, e);
|
||||
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
|
||||
}
|
||||
}
|
||||
|
||||
@PUT
|
||||
@Path("/enroll/{type}/{id}")
|
||||
@Override
|
||||
public Response updateDevice(@PathParam("type") String type, @PathParam("id") String id, @Valid Device updateDevice) {
|
||||
Device device;
|
||||
DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
|
||||
deviceIdentifier.setId(id);
|
||||
deviceIdentifier.setType(type);
|
||||
try {
|
||||
device = DeviceMgtAPIUtils.getDeviceManagementService().getDevice(deviceIdentifier);
|
||||
} catch (DeviceManagementException e) {
|
||||
String msg = "Error occurred while getting enrollment details of the Android device that carries the id '" +
|
||||
id + "'";
|
||||
log.error(msg, e);
|
||||
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
|
||||
}
|
||||
|
||||
if (updateDevice == null) {
|
||||
String errorMessage = "The payload of the device enrollment is incorrect.";
|
||||
log.error(errorMessage);
|
||||
return Response.status(Response.Status.BAD_REQUEST).entity(errorMessage).build();
|
||||
}
|
||||
if (device == null) {
|
||||
String errorMessage = "The device to be modified doesn't exist.";
|
||||
log.error(errorMessage);
|
||||
return Response.status(Response.Status.NOT_FOUND).entity(errorMessage).build();
|
||||
}
|
||||
if (device.getEnrolmentInfo().getStatus() == EnrolmentInfo.Status.ACTIVE ) {
|
||||
DeviceAccessAuthorizationService deviceAccessAuthorizationService =
|
||||
DeviceMgtAPIUtils.getDeviceAccessAuthorizationService();
|
||||
boolean status;
|
||||
try {
|
||||
status = deviceAccessAuthorizationService.isUserAuthorized(new DeviceIdentifier(id, type));
|
||||
} catch (DeviceAccessAuthorizationException e) {
|
||||
String msg = "Error occurred while modifying enrollment of the Android device that carries the id '" +
|
||||
id + "'";
|
||||
log.error(msg, e);
|
||||
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
|
||||
}
|
||||
if (!status) {
|
||||
return Response.status(Response.Status.UNAUTHORIZED).build();
|
||||
}
|
||||
}
|
||||
if(updateDevice.getEnrolmentInfo() != null) {
|
||||
device.getEnrolmentInfo().setDateOfLastUpdate(System.currentTimeMillis());
|
||||
device.setEnrolmentInfo(device.getEnrolmentInfo());
|
||||
}
|
||||
device.getEnrolmentInfo().setOwner(DeviceMgtAPIUtils.getAuthenticatedUser());
|
||||
if(updateDevice.getDeviceInfo() != null) {
|
||||
device.setDeviceInfo(updateDevice.getDeviceInfo());
|
||||
}
|
||||
device.setDeviceIdentifier(id);
|
||||
if(updateDevice.getDescription() != null) {
|
||||
device.setDescription(updateDevice.getDescription());
|
||||
}
|
||||
if(updateDevice.getName() != null) {
|
||||
device.setName(updateDevice.getName());
|
||||
}
|
||||
if(updateDevice.getFeatures() != null) {
|
||||
device.setFeatures(updateDevice.getFeatures());
|
||||
}
|
||||
if(updateDevice.getProperties() != null) {
|
||||
device.setProperties(updateDevice.getProperties());
|
||||
}
|
||||
boolean result;
|
||||
try {
|
||||
device.setType(type);
|
||||
result = DeviceMgtAPIUtils.getDeviceManagementService().modifyEnrollment(device);
|
||||
if (result) {
|
||||
return Response.status(Response.Status.ACCEPTED).build();
|
||||
} else {
|
||||
return Response.status(Response.Status.NOT_MODIFIED).build();
|
||||
}
|
||||
} catch (DeviceManagementException e) {
|
||||
String msg = "Error occurred while modifying enrollment of the Android device that carries the id '" +
|
||||
id + "'";
|
||||
log.error(msg, e);
|
||||
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
|
||||
}
|
||||
}
|
||||
|
||||
@POST
|
||||
@Path("/events/publish/{type}/{deviceId}")
|
||||
@Override
|
||||
public Response publishEvents(@Valid EventBeanWrapper eventBeanWrapper, @PathParam("type") String type
|
||||
, @PathParam("deviceId") String deviceId) {
|
||||
|
||||
|
||||
String tenantDomain = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain();
|
||||
try {
|
||||
if (eventBeanWrapper == null) {
|
||||
String msg = "invalid payload structure";
|
||||
return Response.status(Response.Status.BAD_REQUEST).entity(msg).build();
|
||||
} else {
|
||||
boolean authorized = DeviceMgtAPIUtils.getDeviceAccessAuthorizationService().isUserAuthorized
|
||||
(new DeviceIdentifier(type, deviceId));
|
||||
if (!authorized) {
|
||||
String msg = "does not have permission to access the device.";
|
||||
return Response.status(Response.Status.UNAUTHORIZED).entity(msg).build();
|
||||
}
|
||||
}
|
||||
Object metaData[] = new Object[1];
|
||||
metaData[0] = deviceId;
|
||||
EventAttributeList eventAttributeList = DeviceMgtAPIUtils.getDynamicEventCache().get(type);
|
||||
if (eventAttributeList == null) {
|
||||
String streamName = DeviceMgtAPIUtils.getStreamDefinition(type, tenantDomain);
|
||||
EventStreamAdminServiceStub eventStreamAdminServiceStub =
|
||||
DeviceMgtAPIUtils.getEventStreamAdminServiceStub();
|
||||
EventStreamDefinitionDto eventStreamDefinitionDto = eventStreamAdminServiceStub.getStreamDefinitionDto(
|
||||
streamName + ":" + Constants.DEFAULT_STREAM_VERSION);
|
||||
if (eventStreamDefinitionDto == null) {
|
||||
return Response.status(Response.Status.BAD_REQUEST).build();
|
||||
} else {
|
||||
EventStreamAttributeDto[] eventStreamAttributeDtos = eventStreamDefinitionDto.getPayloadData();
|
||||
List<Attribute> attributes = new ArrayList<>();
|
||||
for (EventStreamAttributeDto eventStreamAttributeDto : eventStreamAttributeDtos) {
|
||||
attributes.add(new Attribute(eventStreamAttributeDto.getAttributeName()
|
||||
, AttributeType.valueOf(eventStreamAttributeDto.getAttributeType().toUpperCase())));
|
||||
|
||||
}
|
||||
if (eventBeanWrapper.getPayloadData().length != attributes.size()) {
|
||||
String msg = "payload does not match the stream definition";
|
||||
return Response.status(Response.Status.BAD_REQUEST).entity(msg).build();
|
||||
}
|
||||
eventAttributeList = new EventAttributeList();
|
||||
eventAttributeList.setList(attributes);
|
||||
DeviceMgtAPIUtils.getDynamicEventCache().put(type, eventAttributeList);
|
||||
}
|
||||
}
|
||||
Object[] payload = eventBeanWrapper.getPayloadData();
|
||||
int i = 0;
|
||||
for (Attribute attribute : eventAttributeList.getList()) {
|
||||
if (attribute.getType() == AttributeType.INT) {
|
||||
payload[i] = ((Double) payload[i]).intValue();
|
||||
} else if (attribute.getType() == AttributeType.LONG) {
|
||||
payload[i] = ((Double) payload[i]).longValue();
|
||||
}
|
||||
i++;
|
||||
}
|
||||
eventBeanWrapper.setPayloadData(payload);
|
||||
|
||||
if (DeviceMgtAPIUtils.getEventPublisherService().publishEvent(DeviceMgtAPIUtils.getStreamDefinition(type
|
||||
, PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain())
|
||||
, Constants.DEFAULT_STREAM_VERSION, metaData
|
||||
, null, eventBeanWrapper.getPayloadData())) {
|
||||
return Response.status(Response.Status.OK).build();
|
||||
} else {
|
||||
String msg = "Error occurred while publishing the event.";
|
||||
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
|
||||
}
|
||||
} catch (DataPublisherConfigurationException e) {
|
||||
String msg = "Error occurred while publishing the event.";
|
||||
log.error(msg, e);
|
||||
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
|
||||
} catch (DeviceAccessAuthorizationException e) {
|
||||
String msg = "Error occurred when checking for authorization";
|
||||
log.error(msg, e);
|
||||
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
|
||||
} catch (AxisFault e) {
|
||||
log.error("failed to retrieve event definitions for tenantDomain:" + tenantDomain, e);
|
||||
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
|
||||
} catch (RemoteException e) {
|
||||
log.error("Failed to connect with the remote services:" + tenantDomain, e);
|
||||
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
|
||||
} catch (JWTClientException e) {
|
||||
log.error("Failed to generate jwt token for tenantDomain:" + tenantDomain, e);
|
||||
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
|
||||
} catch (UserStoreException e) {
|
||||
log.error("Failed to connect with the user store, tenantDomain: " + tenantDomain, e);
|
||||
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@GET
|
||||
@Path("/pending/operations/{type}/{id}")
|
||||
public Response getPendingOperations(@PathParam("type") String type, @PathParam("id") String deviceId) {
|
||||
try {
|
||||
if (!DeviceMgtAPIUtils.getDeviceManagementService().getAvailableDeviceTypes().contains(type)) {
|
||||
String errorMessage = "Device identifier list is empty";
|
||||
log.error(errorMessage);
|
||||
return Response.status(Response.Status.BAD_REQUEST).build();
|
||||
}
|
||||
DeviceIdentifier deviceIdentifier = new DeviceIdentifier(deviceId, type);
|
||||
if (!DeviceMgtAPIUtils.isValidDeviceIdentifier(deviceIdentifier)) {
|
||||
String msg = "Device not found for identifier '" + deviceId + "'";
|
||||
log.error(msg);
|
||||
return Response.status(Response.Status.NO_CONTENT).entity(msg).build();
|
||||
}
|
||||
List<? extends Operation> operations = DeviceMgtAPIUtils.getDeviceManagementService().getPendingOperations(
|
||||
deviceIdentifier);
|
||||
OperationList operationsList = new OperationList();
|
||||
operationsList.setList(operations);
|
||||
operationsList.setCount(operations.size());
|
||||
return Response.status(Response.Status.OK).entity(operationsList).build();
|
||||
} catch (OperationManagementException e) {
|
||||
String errorMessage = "Issue in retrieving operation management service instance";
|
||||
log.error(errorMessage, e);
|
||||
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(
|
||||
new ErrorResponse.ErrorResponseBuilder().setMessage(errorMessage).build()).build();
|
||||
} catch (DeviceManagementException e) {
|
||||
String errorMessage = "Issue in retrieving deivce management service instance";
|
||||
log.error(errorMessage, e);
|
||||
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(
|
||||
new ErrorResponse.ErrorResponseBuilder().setMessage(errorMessage).build()).build();
|
||||
}
|
||||
}
|
||||
|
||||
@GET
|
||||
@Path("/last-pending/operation/{type}/{id}")
|
||||
public Response getNextPendingOperation(@PathParam("type") String type, @PathParam("id") String deviceId) {
|
||||
try {
|
||||
if (!DeviceMgtAPIUtils.getDeviceManagementService().getAvailableDeviceTypes().contains(type)) {
|
||||
String errorMessage = "Device identifier list is empty";
|
||||
log.error(errorMessage);
|
||||
return Response.status(Response.Status.BAD_REQUEST).build();
|
||||
}
|
||||
DeviceIdentifier deviceIdentifier = new DeviceIdentifier(deviceId, type);
|
||||
if (!DeviceMgtAPIUtils.isValidDeviceIdentifier(deviceIdentifier)) {
|
||||
String msg = "Device not found for identifier '" + deviceId + "'";
|
||||
log.error(msg);
|
||||
return Response.status(Response.Status.NO_CONTENT).entity(msg).build();
|
||||
}
|
||||
Operation operation = DeviceMgtAPIUtils.getDeviceManagementService().getNextPendingOperation(
|
||||
deviceIdentifier);
|
||||
return Response.status(Response.Status.OK).entity(operation).build();
|
||||
} catch (OperationManagementException e) {
|
||||
String errorMessage = "Issue in retrieving operation management service instance";
|
||||
log.error(errorMessage, e);
|
||||
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(
|
||||
new ErrorResponse.ErrorResponseBuilder().setMessage(errorMessage).build()).build();
|
||||
} catch (DeviceManagementException e) {
|
||||
String errorMessage = "Issue in retrieving deivce management service instance";
|
||||
log.error(errorMessage, e);
|
||||
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(
|
||||
new ErrorResponse.ErrorResponseBuilder().setMessage(errorMessage).build()).build();
|
||||
}
|
||||
}
|
||||
|
||||
@PUT
|
||||
@Path("/operations/{type}/{id}")
|
||||
public Response updateOperation(@PathParam("type") String type, @PathParam("id") String deviceId, @Valid Operation operation) {
|
||||
try {
|
||||
if (!DeviceMgtAPIUtils.getDeviceManagementService().getAvailableDeviceTypes().contains(type)) {
|
||||
String errorMessage = "Device identifier list is empty";
|
||||
log.error(errorMessage);
|
||||
return Response.status(Response.Status.BAD_REQUEST).build();
|
||||
}
|
||||
if (operation == null) {
|
||||
String errorMessage = "Device identifier list is empty";
|
||||
log.error(errorMessage);
|
||||
return Response.status(Response.Status.BAD_REQUEST).build();
|
||||
}
|
||||
DeviceIdentifier deviceIdentifier = new DeviceIdentifier(deviceId, type);
|
||||
if (!DeviceMgtAPIUtils.isValidDeviceIdentifier(deviceIdentifier)) {
|
||||
String msg = "Device not found for identifier '" + deviceId + "'";
|
||||
log.error(msg);
|
||||
return Response.status(Response.Status.NO_CONTENT).entity(msg).build();
|
||||
}
|
||||
DeviceMgtAPIUtils.getDeviceManagementService().updateOperation
|
||||
(deviceIdentifier, operation);
|
||||
return Response.status(Response.Status.ACCEPTED).build();
|
||||
} catch (OperationManagementException e) {
|
||||
String errorMessage = "Issue in retrieving operation management service instance";
|
||||
log.error(errorMessage, e);
|
||||
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(
|
||||
new ErrorResponse.ErrorResponseBuilder().setMessage(errorMessage).build()).build();
|
||||
} catch (DeviceManagementException e) {
|
||||
String errorMessage = "Issue in retrieving deivce management service instance";
|
||||
log.error(errorMessage, e);
|
||||
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(
|
||||
new ErrorResponse.ErrorResponseBuilder().setMessage(errorMessage).build()).build();
|
||||
}
|
||||
}
|
||||
|
||||
@GET
|
||||
@Path("/status/operations/{type}/{id}")
|
||||
public Response getOperationsByDeviceAndStatus(@PathParam("type") String type, @PathParam("id") String deviceId,
|
||||
@QueryParam("status")Operation.Status status) {
|
||||
if (status == null) {
|
||||
String errorMessage = "status is empty";
|
||||
log.error(errorMessage);
|
||||
return Response.status(Response.Status.BAD_REQUEST).build();
|
||||
}
|
||||
|
||||
try {
|
||||
if (!DeviceMgtAPIUtils.getDeviceManagementService().getAvailableDeviceTypes().contains(type)) {
|
||||
String errorMessage = "Device identifier list is empty";
|
||||
log.error(errorMessage);
|
||||
return Response.status(Response.Status.BAD_REQUEST).build();
|
||||
}
|
||||
List<? extends Operation> operations = DeviceMgtAPIUtils.getDeviceManagementService()
|
||||
.getOperationsByDeviceAndStatus(new DeviceIdentifier(deviceId, type), status);
|
||||
OperationList operationsList = new OperationList();
|
||||
operationsList.setList(operations);
|
||||
operationsList.setCount(operations.size());
|
||||
return Response.status(Response.Status.OK).entity(operationsList).build();
|
||||
} catch (OperationManagementException e) {
|
||||
String errorMessage = "Issue in retrieving operation management service instance";
|
||||
log.error(errorMessage, e);
|
||||
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(
|
||||
new ErrorResponse.ErrorResponseBuilder().setMessage(errorMessage).build()).build();
|
||||
} catch (DeviceManagementException e) {
|
||||
String errorMessage = "Issue in retrieving device management service";
|
||||
log.error(errorMessage, e);
|
||||
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(
|
||||
new ErrorResponse.ErrorResponseBuilder().setMessage(errorMessage).build()).build();
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in new issue