Merge branch 'rest-api-improvements' of https://github.com/wso2/carbon-device-mgt into rest-api-improvements

revert-70aa11f8
hasuniea 9 years ago
commit dc72fd1553

@ -76,9 +76,13 @@ public interface ActivityInfoProviderService {
code = 400, code = 400,
message = "Bad Request. \n Invalid request or validation error.", message = "Bad Request. \n Invalid request or validation error.",
response = ErrorResponse.class), response = ErrorResponse.class),
@ApiResponse(
code = 401,
message = ". \n Invalid request or validation error."),
@ApiResponse( @ApiResponse(
code = 404, code = 404,
message = "Not Found. \n No activity is found under the provided id."), message = "Not Found. \n No activity is found under the provided id.",
response = ErrorResponse.class),
@ApiResponse( @ApiResponse(
code = 406, code = 406,
message = "Not Acceptable.\n The requested media type is not supported"), message = "Not Acceptable.\n The requested media type is not supported"),
@ -131,6 +135,10 @@ public interface ActivityInfoProviderService {
code = 304, code = 304,
message = "Not Modified. \n Empty body because the client has already the latest version of " + message = "Not Modified. \n Empty body because the client has already the latest version of " +
"the requested resource."), "the requested resource."),
@ApiResponse(
code = 404,
message = "Not Found. \n No activities found.",
response = ErrorResponse.class),
@ApiResponse( @ApiResponse(
code = 406, code = 406,
message = "Not Acceptable.\n The requested media type is not supported"), message = "Not Acceptable.\n The requested media type is not supported"),

@ -78,13 +78,24 @@ public interface DeviceManagementService {
}), }),
@ApiResponse( @ApiResponse(
code = 304, code = 304,
message = "Not Modified. \n Empty body because the client has already the latest version of the requested resource."), message = "Not Modified. \n Empty body because the client has already the latest version of " +
"the requested resource."),
@ApiResponse(
code = 400,
message = "The incoming request has more than one selection criteria defined through query" +
" parameters.",
response = ErrorResponse.class),
@ApiResponse(
code = 404,
message = "No device is currently enrolled with the server.",
response = ErrorResponse.class),
@ApiResponse( @ApiResponse(
code = 406, code = 406,
message = "Not Acceptable.\n The requested media type is not supported"), message = "Not Acceptable.\n The requested media type is not supported"),
@ApiResponse( @ApiResponse(
code = 500, code = 500,
message = "Internal Server ErrorResponse. \n Server error occurred while fetching the device list.") message = "Internal Server ErrorResponse. \n Server error occurred while fetching the device list.",
response = ErrorResponse.class)
}) })
@Permission(scope = "device-list", permissions = {"/permission/admin/device-mgt/admin/devices/list"}) @Permission(scope = "device-list", permissions = {"/permission/admin/device-mgt/admin/devices/list"})
Response getDevices( Response getDevices(
@ -170,10 +181,12 @@ public interface DeviceManagementService {
"Empty body because the client already has the latest version of the requested resource."), "Empty body because the client already has the latest version of the requested resource."),
@ApiResponse( @ApiResponse(
code = 400, code = 400,
message = "Bad Request. \n Invalid request or validation error."), message = "Bad Request. \n Invalid request or validation error.",
response = ErrorResponse.class),
@ApiResponse( @ApiResponse(
code = 404, code = 404,
message = "Not Found. \n No device is found under the provided type and id."), message = "Not Found. \n No device is found under the provided type and id.",
response = ErrorResponse.class),
@ApiResponse( @ApiResponse(
code = 406, code = 406,
message = "Not Acceptable. \n The requested media type is not supported."), message = "Not Acceptable. \n The requested media type is not supported."),
@ -243,14 +256,20 @@ public interface DeviceManagementService {
"Empty body because the client already has the latest version of the requested resource."), "Empty body because the client already has the latest version of the requested resource."),
@ApiResponse( @ApiResponse(
code = 400, code = 400,
message = "Bad Request. \n Invalid request or validation error."), message = "Bad Request. \n Invalid request or validation error.",
response = ErrorResponse.class),
@ApiResponse(
code = 404,
message = "Not Found. \n No device information is available for the device list submitted.",
response = ErrorResponse.class),
@ApiResponse( @ApiResponse(
code = 406, code = 406,
message = "Not Acceptable. \n The requested media type is not supported."), message = "Not Acceptable. \n The requested media type is not supported."),
@ApiResponse( @ApiResponse(
code = 500, code = 500,
message = "Internal Server ErrorResponse. \n " + message = "Internal Server ErrorResponse. Server error occurred while retrieving " +
"Server error occurred while retrieving information of the list of the devices submitted.") "information of the list of the devices submitted.",
response = ErrorResponse.class)
}) })
@Permission(scope = "device-info", permissions = {"/permission/admin/device-mgt/admin/devices/list"}) @Permission(scope = "device-info", permissions = {"/permission/admin/device-mgt/admin/devices/list"})
Response getDevicesInfo( Response getDevicesInfo(
@ -294,11 +313,16 @@ public interface DeviceManagementService {
}), }),
@ApiResponse( @ApiResponse(
code = 304, code = 304,
message = "Not Modified. \n " + message = "Not Modified. Empty body because the client already has the latest " +
"Empty body because the client already has the latest version of the requested resource."), "version of the requested resource."),
@ApiResponse(
code = 400,
message = "Bad Request. \n Invalid request or validation error.",
response = ErrorResponse.class),
@ApiResponse( @ApiResponse(
code = 404, code = 404,
message = "Not Found. \n No device is found under the provided type and id."), message = "Not Found. \n No device is found under the provided type and id.",
response = ErrorResponse.class),
@ApiResponse( @ApiResponse(
code = 500, code = 500,
message = "Internal Server ErrorResponse. \n " + message = "Internal Server ErrorResponse. \n " +
@ -345,9 +369,14 @@ public interface DeviceManagementService {
code = 304, code = 304,
message = "Not Modified. \n " + message = "Not Modified. \n " +
"Empty body because the client already has the latest version of the requested resource."), "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( @ApiResponse(
code = 404, code = 404,
message = "Not Found. \n No device is found under the provided type and id."), message = "Not Found. \n No device is found under the provided type and id.",
response = ErrorResponse.class),
@ApiResponse( @ApiResponse(
code = 500, code = 500,
message = "ErrorResponse occurred while getting the device location.", message = "ErrorResponse occurred while getting the device location.",
@ -393,9 +422,14 @@ public interface DeviceManagementService {
code = 304, code = 304,
message = "Not Modified. \n " + message = "Not Modified. \n " +
"Empty body because the client already has the latest version of the requested resource."), "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( @ApiResponse(
code = 404, code = 404,
message = "Location details are not available for the given devices."), message = "Location details are not available for the given devices.",
response = ErrorResponse.class),
@ApiResponse( @ApiResponse(
code = 500, code = 500,
message = "ErrorResponse occurred while getting the device location.", message = "ErrorResponse occurred while getting the device location.",
@ -461,7 +495,8 @@ public interface DeviceManagementService {
response = ErrorResponse.class), response = ErrorResponse.class),
@ApiResponse( @ApiResponse(
code = 404, code = 404,
message = "Not Found. \n Device of which the feature list is requested, is not found."), message = "Not Found. \n Device of which the feature list is requested, is not found.",
response = ErrorResponse.class),
@ApiResponse( @ApiResponse(
code = 406, code = 406,
message = "Not Acceptable. \n The requested media type is not supported."), message = "Not Acceptable. \n The requested media type is not supported."),
@ -527,6 +562,11 @@ public interface DeviceManagementService {
code = 400, code = 400,
message = "Bad Request. \n Invalid request or validation error.", message = "Bad Request. \n Invalid request or validation error.",
response = ErrorResponse.class), response = ErrorResponse.class),
@ApiResponse(
code = 404,
message = "Not Acceptable.\n TIt is likely that no device is found upon the " +
"provided filters",
response = ErrorResponse.class),
@ApiResponse( @ApiResponse(
code = 406, code = 406,
message = "Not Acceptable.\n The requested media type is not supported"), message = "Not Acceptable.\n The requested media type is not supported"),
@ -602,7 +642,8 @@ public interface DeviceManagementService {
response = ErrorResponse.class), response = ErrorResponse.class),
@ApiResponse( @ApiResponse(
code = 404, code = 404,
message = "Not Found. \n Device of which the application list is requested, is not found."), message = "Not Found. \n Device of which the application list is requested, is not found.",
response = ErrorResponse.class),
@ApiResponse( @ApiResponse(
code = 406, code = 406,
message = "Not Acceptable. \n The requested media type is not supported."), message = "Not Acceptable. \n The requested media type is not supported."),
@ -690,7 +731,8 @@ public interface DeviceManagementService {
response = ErrorResponse.class), response = ErrorResponse.class),
@ApiResponse( @ApiResponse(
code = 404, code = 404,
message = "Not Found. \n Device of which the operation list is requested, is not found."), message = "Not Found. \n Device of which the operation list is requested, is not found.",
response = ErrorResponse.class),
@ApiResponse( @ApiResponse(
code = 406, code = 406,
message = "Not Acceptable. \n The requested media type is not supported."), message = "Not Acceptable. \n The requested media type is not supported."),
@ -777,7 +819,8 @@ public interface DeviceManagementService {
response = ErrorResponse.class), response = ErrorResponse.class),
@ApiResponse( @ApiResponse(
code = 404, code = 404,
message = "Not Found. \n Device of which the effective policy is requested, is not found."), message = "Not Found. \n Device of which the effective policy is requested, is not found.",
response = ErrorResponse.class),
@ApiResponse( @ApiResponse(
code = 406, code = 406,
message = "Not Acceptable. \n The requested media type is not supported."), message = "Not Acceptable. \n The requested media type is not supported."),

@ -71,6 +71,15 @@ public interface NotificationManagementService {
@ApiResponse( @ApiResponse(
code = 304, code = 304,
message = "Not Modified. \n Empty body because the client has already the latest version of the requested resource."), message = "Not Modified. \n Empty body because the client has already the latest version of the requested resource."),
@ApiResponse(
code = 400,
message = "Bad Request. \n Invalid notification status type " +
"received. Valid status types are NEW | CHECKED",
response = ErrorResponse.class),
@ApiResponse(
code = 404,
message = "Not Found. \n No notification is available to be retrieved.",
response = ErrorResponse.class),
@ApiResponse( @ApiResponse(
code = 406, code = 406,
message = "Not Acceptable.\n The requested media type is not supported"), message = "Not Acceptable.\n The requested media type is not supported"),

@ -20,6 +20,7 @@ package org.wso2.carbon.device.mgt.jaxrs.service.api;
import io.swagger.annotations.*; import io.swagger.annotations.*;
import org.wso2.carbon.apimgt.annotations.api.Permission; import org.wso2.carbon.apimgt.annotations.api.Permission;
import org.wso2.carbon.device.mgt.jaxrs.beans.ErrorResponse;
import org.wso2.carbon.device.mgt.jaxrs.beans.PolicyWrapper; import org.wso2.carbon.device.mgt.jaxrs.beans.PolicyWrapper;
import org.wso2.carbon.policy.mgt.common.Policy; import org.wso2.carbon.policy.mgt.common.Policy;
@ -66,7 +67,8 @@ public interface PolicyManagementService {
@ResponseHeader( @ResponseHeader(
name = "Last-Modified", name = "Last-Modified",
description = "Date and time the resource has been modified the last time.\n" + description = "Date and time the resource has been modified the last time.\n" +
"Used by caches, or in conditional requests.")}), "Used by caches, or in conditional requests.")
}),
@ApiResponse( @ApiResponse(
code = 303, code = 303,
message = "See Other. \n Source can be retrieved from the URL specified at the Location header.", message = "See Other. \n Source can be retrieved from the URL specified at the Location header.",
@ -76,14 +78,21 @@ public interface PolicyManagementService {
description = "The Source URL of the document.")}), description = "The Source URL of the document.")}),
@ApiResponse( @ApiResponse(
code = 400, code = 400,
message = "Bad Request. \n Invalid request or validation error."), message = "Bad Request. \n Invalid request or validation error.",
response = ErrorResponse.class),
@ApiResponse(
code = 401,
message = "Not Found. \n Current logged in user is not authorized to add policies.",
response = ErrorResponse.class),
@ApiResponse( @ApiResponse(
code = 415, code = 415,
message = "Unsupported media type. \n The entity of the request was in a not supported format."), message = "Unsupported media type. \n The entity of the request was in a not supported format."),
@ApiResponse( @ApiResponse(
code = 500, code = 500,
message = "Internal Server ErrorResponse. \n " + message = "Internal Server ErrorResponse. \n " +
"Server error occurred while adding a new policy.")}) "Server error occurred while adding a new policy.",
response = ErrorResponse.class)
})
@Permission(scope = "policy-modify", permissions = {"/permission/admin/device-mgt/admin/policies/add"}) @Permission(scope = "policy-modify", permissions = {"/permission/admin/device-mgt/admin/policies/add"})
Response addPolicy( Response addPolicy(
@ApiParam( @ApiParam(
@ -123,12 +132,18 @@ public interface PolicyManagementService {
@ApiResponse( @ApiResponse(
code = 304, code = 304,
message = "Not Modified. \n Empty body because the client has already the latest version of the requested resource."), message = "Not Modified. \n Empty body because the client has already the latest version of the requested resource."),
@ApiResponse(
code = 400,
message = "Bad Request. \n Invalid request or validation error.",
response = ErrorResponse.class),
@ApiResponse( @ApiResponse(
code = 406, code = 406,
message = "Not Acceptable.\n The requested media type is not supported"), message = "Not Acceptable.\n The requested media type is not supported"),
@ApiResponse( @ApiResponse(
code = 500, code = 500,
message = "Internal Server ErrorResponse. \n Server error occurred while fetching policies.") message = ("Internal Server ErrorResponse. \n Server error occurred while fetching " +
"policies."),
response = ErrorResponse.class)
}) })
@Permission(scope = "policy-view", permissions = {"/permission/admin/device-mgt/admin/policies/list"}) @Permission(scope = "policy-view", permissions = {"/permission/admin/device-mgt/admin/policies/list"})
Response getPolicies( Response getPolicies(
@ -181,13 +196,17 @@ public interface PolicyManagementService {
message = "Not Modified. \n Empty body because the client has already the latest version of the requested resource."), message = "Not Modified. \n Empty body because the client has already the latest version of the requested resource."),
@ApiResponse( @ApiResponse(
code = 404, code = 404,
message = "Not Found. \n No policy is found with the given id."), message = "Not Found. \n No policy is found with the given id.",
response = ErrorResponse.class
),
@ApiResponse( @ApiResponse(
code = 406, code = 406,
message = "Not Acceptable.\n The requested media type is not supported"), message = "Not Acceptable.\n The requested media type is not supported"),
@ApiResponse( @ApiResponse(
code = 500, code = 500,
message = "Internal Server ErrorResponse. \n Server error occurred while fetching the policy.") message = "Internal Server ErrorResponse. \n Server error occurred while fetching the " +
"policy.",
response = ErrorResponse.class)
}) })
@Permission(scope = "policy-view", permissions = {"/permission/admin/device-mgt/admin/policies/list"}) @Permission(scope = "policy-view", permissions = {"/permission/admin/device-mgt/admin/policies/list"})
Response getPolicy( Response getPolicy(
@ -234,17 +253,20 @@ public interface PolicyManagementService {
"Used by caches, or in conditional requests.")}), "Used by caches, or in conditional requests.")}),
@ApiResponse( @ApiResponse(
code = 400, code = 400,
message = "Bad Request. \n Invalid request or validation error."), message = "Bad Request. \n Invalid request or validation error.",
response = ErrorResponse.class),
@ApiResponse( @ApiResponse(
code = 404, code = 404,
message = "Not Found. \n Resource to be deleted does not exist."), message = "Not Found. \n Resource to be deleted does not exist.",
response = ErrorResponse.class),
@ApiResponse( @ApiResponse(
code = 415, code = 415,
message = "Unsupported media type. \n The entity of the request was in a not supported format."), message = "Unsupported media type. \n The entity of the request was in a not supported format."),
@ApiResponse( @ApiResponse(
code = 500, code = 500,
message = "Internal Server ErrorResponse. \n " + message = "Internal Server ErrorResponse. \n " +
"Server error occurred while updating the policy.") "Server error occurred while updating the policy.",
response = ErrorResponse.class)
}) })
@Permission(scope = "policy-modify", permissions = {"/permission/admin/device-mgt/admin/policies/update"}) @Permission(scope = "policy-modify", permissions = {"/permission/admin/device-mgt/admin/policies/update"})
Response updatePolicy( Response updatePolicy(
@ -273,17 +295,20 @@ public interface PolicyManagementService {
message = "OK. \n Policies have successfully been removed"), message = "OK. \n Policies have successfully been removed"),
@ApiResponse( @ApiResponse(
code = 400, code = 400,
message = "Bad Request. \n Invalid request or validation error."), message = "Bad Request. \n Invalid request or validation error.",
response = ErrorResponse.class),
@ApiResponse( @ApiResponse(
code = 404, code = 404,
message = "Not Found. \n Resource to be deleted does not exist."), message = "Not Found. \n Resource to be deleted does not exist.",
response = ErrorResponse.class),
@ApiResponse( @ApiResponse(
code = 415, code = 415,
message = "Unsupported media type. \n The entity of the request was in a not supported format."), message = "Unsupported media type. \n The entity of the request was in a not supported format."),
@ApiResponse( @ApiResponse(
code = 500, code = 500,
message = "Internal Server ErrorResponse. \n " + message = "Internal Server ErrorResponse. \n " +
"Server error occurred while bulk removing policies.") "Server error occurred while bulk removing policies.",
response = ErrorResponse.class)
}) })
@Permission(scope = "policy-modify", permissions = {"/permission/admin/device-mgt/admin/policies/remove"}) @Permission(scope = "policy-modify", permissions = {"/permission/admin/device-mgt/admin/policies/remove"})
Response removePolicies( Response removePolicies(
@ -304,8 +329,21 @@ public interface PolicyManagementService {
tags = "Device Policy Management") tags = "Device Policy Management")
@ApiResponses( @ApiResponses(
value = { value = {
@ApiResponse(code = 200, message = "Policies have been successfully activated."), @ApiResponse(
@ApiResponse(code = 500, message = "ErrorResponse in activating policies.") code = 200,
message = "Policies have been successfully activated."),
@ApiResponse(
code = 400,
message = "Bad Request. \n Invalid request or validation error.",
response = ErrorResponse.class),
@ApiResponse(
code = 404,
message = "Not Found. \n Resource does not exist.",
response = ErrorResponse.class),
@ApiResponse(
code = 500,
message = "ErrorResponse in activating policies.",
response = ErrorResponse.class)
}) })
@Permission(scope = "policy-modify", permissions = { @Permission(scope = "policy-modify", permissions = {
"/permission/admin/device-mgt/admin/policies/update", "/permission/admin/device-mgt/admin/policies/update",
@ -325,8 +363,21 @@ public interface PolicyManagementService {
"is in the active state to the inactive state.", "is in the active state to the inactive state.",
tags = "Device Policy Management") tags = "Device Policy Management")
@ApiResponses(value = { @ApiResponses(value = {
@ApiResponse(code = 200, message = "Policies have been successfully deactivated."), @ApiResponse(
@ApiResponse(code = 500, message = "ErrorResponse in deactivating policies.") code = 200,
message = "Policies have been successfully deactivated."),
@ApiResponse(
code = 400,
message = "Bad Request. \n Invalid request or validation error.",
response = ErrorResponse.class),
@ApiResponse(
code = 404,
message = "Not Found. \n Resource does not exist.",
response = ErrorResponse.class),
@ApiResponse(
code = 500,
message = "ErrorResponse in deactivating policies.",
response = ErrorResponse.class)
}) })
@Permission(scope = "policy-modify", permissions = { @Permission(scope = "policy-modify", permissions = {
"/permission/admin/device-mgt/admin/policies/update", "/permission/admin/device-mgt/admin/policies/update",

@ -70,6 +70,10 @@ public interface RoleManagementService {
@ApiResponse( @ApiResponse(
code = 304, code = 304,
message = "Not Modified. \n Empty body because the client has already the latest version of the requested resource."), message = "Not Modified. \n Empty body because the client has already the latest version of the requested resource."),
@ApiResponse(
code = 404,
message = "Not Found. \n Resource does not exist.",
response = ErrorResponse.class),
@ApiResponse( @ApiResponse(
code = 406, code = 406,
message = "Not Acceptable.\n The requested media type is not supported"), message = "Not Acceptable.\n The requested media type is not supported"),
@ -147,9 +151,14 @@ public interface RoleManagementService {
@ApiResponse( @ApiResponse(
code = 304, code = 304,
message = "Not Modified. \n Empty body because the client has already the latest version of the requested resource."), message = "Not Modified. \n Empty body because the client has already the latest version of the requested resource."),
@ApiResponse(
code = 400,
message = "Bad Request. \n Invalid request or validation error.",
response = ErrorResponse.class),
@ApiResponse( @ApiResponse(
code = 404, code = 404,
message = "Not Found. \n Resource to be deleted does not exist."), message = "Not Found. \n Resource does not exist.",
response = ErrorResponse.class),
@ApiResponse( @ApiResponse(
code = 406, code = 406,
message = "Not Acceptable.\n The requested media type is not supported"), message = "Not Acceptable.\n The requested media type is not supported"),
@ -201,16 +210,23 @@ public interface RoleManagementService {
}), }),
@ApiResponse( @ApiResponse(
code = 304, code = 304,
message = "Not Modified. \n Empty body because the client has already the latest version of the requested resource."), message = "Not Modified. \n Empty body because the client has already the latest version of" +
" the requested resource."),
@ApiResponse(
code = 400,
message = "Bad Request. \n Invalid request or validation error.",
response = ErrorResponse.class),
@ApiResponse( @ApiResponse(
code = 404, code = 404,
message = "Not Found. \n Resource to be deleted does not exist."), message = "Not Found. \n Resource does not exist.",
response = ErrorResponse.class),
@ApiResponse( @ApiResponse(
code = 406, code = 406,
message = "Not Acceptable.\n The requested media type is not supported"), message = "Not Acceptable.\n The requested media type is not supported"),
@ApiResponse( @ApiResponse(
code = 500, code = 500,
message = "Internal Server ErrorResponse. \n Server error occurred while fetching the requested role.", message = "Internal Server ErrorResponse. \n Server error occurred while fetching the " +
"requested role.",
response = ErrorResponse.class) response = ErrorResponse.class)
}) })
@Permission(scope = "roles-view", permissions = {"/permission/admin/device-mgt/admin/roles/list"}) @Permission(scope = "roles-view", permissions = {"/permission/admin/device-mgt/admin/roles/list"})
@ -349,6 +365,10 @@ public interface RoleManagementService {
@ApiResponse( @ApiResponse(
code = 200, code = 200,
message = "OK. \n Role has successfully been removed"), message = "OK. \n Role has successfully been removed"),
@ApiResponse(
code = 400,
message = "Bad Request. \n Invalid request or validation error.",
response = ErrorResponse.class),
@ApiResponse( @ApiResponse(
code = 404, code = 404,
message = "Not Found. \n Resource to be deleted does not exist."), message = "Not Found. \n Resource to be deleted does not exist."),

@ -21,6 +21,7 @@ package org.wso2.carbon.device.mgt.jaxrs.service.api;
import io.swagger.annotations.*; import io.swagger.annotations.*;
import org.wso2.carbon.apimgt.annotations.api.API; import org.wso2.carbon.apimgt.annotations.api.API;
import org.wso2.carbon.apimgt.annotations.api.Permission; import org.wso2.carbon.apimgt.annotations.api.Permission;
import org.wso2.carbon.device.mgt.jaxrs.beans.ErrorResponse;
import org.wso2.carbon.device.mgt.jaxrs.beans.OldPasswordResetWrapper; import org.wso2.carbon.device.mgt.jaxrs.beans.OldPasswordResetWrapper;
import org.wso2.carbon.device.mgt.jaxrs.beans.UserList; import org.wso2.carbon.device.mgt.jaxrs.beans.UserList;
import org.wso2.carbon.device.mgt.jaxrs.beans.UserWrapper; import org.wso2.carbon.device.mgt.jaxrs.beans.UserWrapper;
@ -76,13 +77,18 @@ public interface UserManagementService {
@ApiResponse( @ApiResponse(
code = 400, code = 400,
message = "Bad Request. \n Invalid request or validation error."), message = "Bad Request. \n Invalid request or validation error."),
@ApiResponse(
code = 409,
message = "Conflict. \n User already exist.",
response = ErrorResponse.class),
@ApiResponse( @ApiResponse(
code = 415, code = 415,
message = "Unsupported media type. \n The entity of the request was in a not supported format."), message = "Unsupported media type. \n The entity of the request was in a not supported format."),
@ApiResponse( @ApiResponse(
code = 500, code = 500,
message = "Internal Server ErrorResponse. \n " + message = "Internal Server ErrorResponse. \n " +
"Server error occurred while adding a new user.") "Server error occurred while adding a new user.",
response = ErrorResponse.class)
}) })
@Permission(scope = "user-modify", permissions = {"/permission/admin/device-mgt/admin/user/add"}) @Permission(scope = "user-modify", permissions = {"/permission/admin/device-mgt/admin/user/add"})
Response addUser( Response addUser(
@ -124,13 +130,16 @@ public interface UserManagementService {
message = "Not Modified. \n Empty body because the client has already the latest version of the requested resource."), message = "Not Modified. \n Empty body because the client has already the latest version of the requested resource."),
@ApiResponse( @ApiResponse(
code = 404, code = 404,
message = "Not Found. \n Resource to be deleted does not exist."), message = "Not Found. \n Resource does not exist.",
response = ErrorResponse.class),
@ApiResponse( @ApiResponse(
code = 406, code = 406,
message = "Not Acceptable.\n The requested media type is not supported"), message = "Not Acceptable.\n The requested media type is not supported"),
@ApiResponse( @ApiResponse(
code = 500, code = 500,
message = "Internal Server ErrorResponse. \n Server error occurred while fetching the requested user.") message = "Internal Server ErrorResponse. \n Server error occurred while" +
" fetching the requested user.",
response = ErrorResponse.class)
}) })
@Permission(scope = "user-view", permissions = {"/permission/admin/device-mgt/admin/user/view"}) @Permission(scope = "user-view", permissions = {"/permission/admin/device-mgt/admin/user/view"})
Response getUser( Response getUser(
@ -179,14 +188,16 @@ public interface UserManagementService {
message = "Bad Request. \n Invalid request or validation error."), message = "Bad Request. \n Invalid request or validation error."),
@ApiResponse( @ApiResponse(
code = 404, code = 404,
message = "Not Found. \n Resource to be deleted does not exist."), message = "Not Found. \n Resource does not exist.",
response = ErrorResponse.class),
@ApiResponse( @ApiResponse(
code = 415, code = 415,
message = "Unsupported media type. \n The entity of the request was in a not supported format."), message = "Unsupported media type. \n The entity of the request was in a not supported format."),
@ApiResponse( @ApiResponse(
code = 500, code = 500,
message = "Internal Server ErrorResponse. \n " + message = "Internal Server ErrorResponse. \n " +
"Server error occurred while updating the user.") "Server error occurred while updating the user.",
response = ErrorResponse.class)
}) })
@Permission(scope = "user-modify", permissions = {"/permission/admin/device-mgt/admin/user/update"}) @Permission(scope = "user-modify", permissions = {"/permission/admin/device-mgt/admin/user/update"})
Response updateUser( Response updateUser(
@ -214,11 +225,14 @@ public interface UserManagementService {
message = "OK. \n User has successfully been removed"), message = "OK. \n User has successfully been removed"),
@ApiResponse( @ApiResponse(
code = 404, code = 404,
message = "Not Found. \n Resource to be deleted does not exist."), message = "Not Found. \n Resource to be deleted does not exist.",
response = ErrorResponse.class),
@ApiResponse( @ApiResponse(
code = 500, code = 500,
message = "Internal Server ErrorResponse. \n " + message = "Internal Server ErrorResponse. \n " +
"Server error occurred while removing the user.") "Server error occurred while removing the user.",
response = ErrorResponse.class
)
}) })
@Permission(scope = "user-modify", permissions = {"/permission/admin/device-mgt/admin/user/remove"}) @Permission(scope = "user-modify", permissions = {"/permission/admin/device-mgt/admin/user/remove"})
Response removeUser( Response removeUser(
@ -260,13 +274,16 @@ public interface UserManagementService {
message = "Not Modified. \n Empty body because the client has already the latest version of the requested resource."), message = "Not Modified. \n Empty body because the client has already the latest version of the requested resource."),
@ApiResponse( @ApiResponse(
code = 404, code = 404,
message = "Not Found. \n Resource to be deleted does not exist."), message = "Not Found. \n Resource to be deleted does not exist.",
response = ErrorResponse.class),
@ApiResponse( @ApiResponse(
code = 406, code = 406,
message = "Not Acceptable.\n The requested media type is not supported"), message = "Not Acceptable.\n The requested media type is not supported"),
@ApiResponse( @ApiResponse(
code = 500, code = 500,
message = "Internal Server ErrorResponse. \n Server error occurred while fetching the role list assigned to the user.") message = "Internal Server ErrorResponse. \n Server error occurred while fetching the role list" +
" assigned to the user.",
response = ErrorResponse.class)
}) })
@Permission(scope = "user-view", permissions = {"/permission/admin/device-mgt/admin/user/view"}) @Permission(scope = "user-view", permissions = {"/permission/admin/device-mgt/admin/user/view"})
Response getRolesOfUser( Response getRolesOfUser(
@ -310,7 +327,8 @@ public interface UserManagementService {
message = "Not Acceptable.\n The requested media type is not supported"), message = "Not Acceptable.\n The requested media type is not supported"),
@ApiResponse( @ApiResponse(
code = 500, code = 500,
message = "Internal Server ErrorResponse. \n Server error occurred while fetching the user list.") message = "Internal Server ErrorResponse. \n Server error occurred while fetching the user list.",
response = ErrorResponse.class)
}) })
@Permission(scope = "user-view", permissions = {"/permission/admin/device-mgt/admin/user/list"}) @Permission(scope = "user-view", permissions = {"/permission/admin/device-mgt/admin/user/list"})
Response getUsers( Response getUsers(
@ -376,7 +394,9 @@ public interface UserManagementService {
message = "Not Acceptable.\n The requested media type is not supported"), message = "Not Acceptable.\n The requested media type is not supported"),
@ApiResponse( @ApiResponse(
code = 500, code = 500,
message = "Internal Server ErrorResponse. \n Server error occurred while fetching the username list that matches the given filter.") message = "Internal Server ErrorResponse. \n Server error occurred while fetching the username " +
"list that matches the given filter.",
response = ErrorResponse.class)
}) })
@Permission(scope = "user-view", permissions = {"/permission/admin/device-mgt/admin/user/list"}) @Permission(scope = "user-view", permissions = {"/permission/admin/device-mgt/admin/user/list"})
Response getUserNames( Response getUserNames(
@ -416,7 +436,8 @@ public interface UserManagementService {
message = "OK. \n Credentials of the user have been updated successfully"), message = "OK. \n Credentials of the user have been updated successfully"),
@ApiResponse( @ApiResponse(
code = 400, code = 400,
message = "Bad Request. \n Invalid request or validation error."), message = "Bad Request. \n Invalid request or validation error.",
response = ErrorResponse.class),
@ApiResponse( @ApiResponse(
code = 404, code = 404,
message = "Not Found. \n Resource to be deleted does not exist."), message = "Not Found. \n Resource to be deleted does not exist."),
@ -426,7 +447,8 @@ public interface UserManagementService {
@ApiResponse( @ApiResponse(
code = 500, code = 500,
message = "Internal Server ErrorResponse. \n " + message = "Internal Server ErrorResponse. \n " +
"Server error occurred while updating credentials of the user.") "Server error occurred while updating credentials of the user.",
response = ErrorResponse.class)
}) })
@Permission(scope = "user-modify", permissions = {"/permission/admin/login"}) @Permission(scope = "user-modify", permissions = {"/permission/admin/login"})
Response resetPassword( Response resetPassword(

@ -21,6 +21,7 @@ package org.wso2.carbon.device.mgt.jaxrs.service.api.admin;
import io.swagger.annotations.*; import io.swagger.annotations.*;
import org.wso2.carbon.apimgt.annotations.api.API; import org.wso2.carbon.apimgt.annotations.api.API;
import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.Device;
import org.wso2.carbon.device.mgt.jaxrs.beans.ErrorResponse;
import javax.ws.rs.*; import javax.ws.rs.*;
import javax.ws.rs.core.MediaType; import javax.ws.rs.core.MediaType;
@ -64,16 +65,23 @@ public interface DeviceManagementAdminService {
}), }),
@ApiResponse( @ApiResponse(
code = 304, code = 304,
message = "Not Modified. \n Empty body because the client has already the latest version of the requested resource."), message = "Not Modified. \n Empty body because the client has already the latest version of " +
"the requested resource."),
@ApiResponse( @ApiResponse(
code = 401, code = 401,
message = "Unauthorized.\n The requested resource access is unauthorized"), message = "Unauthorized.\n The requested resource access is unauthorized",
response = ErrorResponse.class),
@ApiResponse(
code = 404,
message = "Not Found.\n No device found that matches the given name.",
response = ErrorResponse.class),
@ApiResponse( @ApiResponse(
code = 406, code = 406,
message = "Not Acceptable.\n The requested media type is not supported"), message = "Not Acceptable.\n The requested media type is not supported"),
@ApiResponse( @ApiResponse(
code = 500, code = 500,
message = "Internal Server ErrorResponse. \n Server error occurred while fetching the device list.") message = "Internal Server ErrorResponse. \n Server error occurred while fetching the device list.",
response = ErrorResponse.class)
}) })
Response getDevicesByName( Response getDevicesByName(
@ApiParam( @ApiParam(

@ -20,6 +20,7 @@ package org.wso2.carbon.device.mgt.jaxrs.service.api.admin;
import io.swagger.annotations.*; import io.swagger.annotations.*;
import org.wso2.carbon.apimgt.annotations.api.Permission; import org.wso2.carbon.apimgt.annotations.api.Permission;
import org.wso2.carbon.device.mgt.jaxrs.beans.ErrorResponse;
import org.wso2.carbon.device.mgt.jaxrs.beans.PasswordResetWrapper; import org.wso2.carbon.device.mgt.jaxrs.beans.PasswordResetWrapper;
import javax.ws.rs.*; import javax.ws.rs.*;
@ -49,7 +50,8 @@ public interface UserManagementAdminService {
message = "OK. \n Credentials of the user have been updated successfully"), message = "OK. \n Credentials of the user have been updated successfully"),
@ApiResponse( @ApiResponse(
code = 400, code = 400,
message = "Bad Request. \n Invalid request or validation error."), message = "Bad Request. \n Invalid request or validation error.",
response = ErrorResponse.class),
@ApiResponse( @ApiResponse(
code = 404, code = 404,
message = "Not Found. \n Resource to be deleted does not exist."), message = "Not Found. \n Resource to be deleted does not exist."),
@ -59,7 +61,8 @@ public interface UserManagementAdminService {
@ApiResponse( @ApiResponse(
code = 500, code = 500,
message = "Internal Server ErrorResponse. \n " + message = "Internal Server ErrorResponse. \n " +
"Server error occurred while updating credentials of the user.") "Server error occurred while updating credentials of the user.",
response = ErrorResponse.class)
}) })
@Permission(scope = "user-modify", permissions = {"/permission/admin/login"}) @Permission(scope = "user-modify", permissions = {"/permission/admin/login"})
Response resetUserPassword( Response resetUserPassword(

@ -25,8 +25,8 @@ import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManagementExcept
import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; 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.ErrorResponse;
import org.wso2.carbon.device.mgt.jaxrs.service.api.ActivityInfoProviderService; import org.wso2.carbon.device.mgt.jaxrs.service.api.ActivityInfoProviderService;
import org.wso2.carbon.device.mgt.jaxrs.service.impl.util.RequestValidationUtil; import org.wso2.carbon.device.mgt.jaxrs.service.impl.util.*;
import org.wso2.carbon.device.mgt.jaxrs.service.impl.util.UnexpectedServerErrorException; import org.wso2.carbon.device.mgt.jaxrs.service.impl.util.NotFoundException;
import org.wso2.carbon.device.mgt.jaxrs.util.DeviceMgtAPIUtils; import org.wso2.carbon.device.mgt.jaxrs.util.DeviceMgtAPIUtils;
import javax.ws.rs.*; import javax.ws.rs.*;
@ -55,8 +55,9 @@ public class ActivityProviderServiceImpl implements ActivityInfoProviderService
dmService = DeviceMgtAPIUtils.getDeviceManagementService(); dmService = DeviceMgtAPIUtils.getDeviceManagementService();
activity = dmService.getOperationByActivityId(id); activity = dmService.getOperationByActivityId(id);
if (activity == null) { if (activity == null) {
return Response.status(Response.Status.NOT_FOUND).entity("No activity can be found upon the provided " + throw new NotFoundException(
"activity id '" + id + "'").build(); new ErrorResponse.ErrorResponseBuilder().setCode(404l).setMessage("No activity can be " +
"found upon the provided activity id '" + id + "'").build());
} }
} catch (OperationManagementException e) { } catch (OperationManagementException e) {
String msg = "ErrorResponse occurred while fetching the activity for the supplied id."; String msg = "ErrorResponse occurred while fetching the activity for the supplied id.";
@ -78,6 +79,11 @@ public class ActivityProviderServiceImpl implements ActivityInfoProviderService
try { try {
dmService = DeviceMgtAPIUtils.getDeviceManagementService(); dmService = DeviceMgtAPIUtils.getDeviceManagementService();
activities = dmService.getActivitiesUpdatedAfter(timestamp); activities = dmService.getActivitiesUpdatedAfter(timestamp);
if (activities == null || activities.size() == 0) {
throw new NotFoundException(
new ErrorResponse.ErrorResponseBuilder().setCode(404l).setMessage("No activities " +
"found.").build());
}
} catch (OperationManagementException e) { } catch (OperationManagementException e) {
String msg = "ErrorResponse occurred while fetching the activities updated after given time stamp."; String msg = "ErrorResponse occurred while fetching the activities updated after given time stamp.";

@ -25,6 +25,7 @@ import org.wso2.carbon.device.mgt.common.configuration.mgt.ConfigurationManageme
import org.wso2.carbon.device.mgt.common.configuration.mgt.PlatformConfiguration; import org.wso2.carbon.device.mgt.common.configuration.mgt.PlatformConfiguration;
import org.wso2.carbon.device.mgt.jaxrs.beans.ErrorResponse; import org.wso2.carbon.device.mgt.jaxrs.beans.ErrorResponse;
import org.wso2.carbon.device.mgt.jaxrs.service.api.ConfigurationManagementService; import org.wso2.carbon.device.mgt.jaxrs.service.api.ConfigurationManagementService;
import org.wso2.carbon.device.mgt.jaxrs.service.impl.util.RequestValidationUtil;
import org.wso2.carbon.device.mgt.jaxrs.service.impl.util.UnexpectedServerErrorException; import org.wso2.carbon.device.mgt.jaxrs.service.impl.util.UnexpectedServerErrorException;
import org.wso2.carbon.device.mgt.jaxrs.util.DeviceMgtAPIUtils; import org.wso2.carbon.device.mgt.jaxrs.util.DeviceMgtAPIUtils;
import org.wso2.carbon.device.mgt.jaxrs.util.MDMAppConstants; import org.wso2.carbon.device.mgt.jaxrs.util.MDMAppConstants;
@ -74,6 +75,7 @@ public class ConfigurationServiceImpl implements ConfigurationManagementService
@Override @Override
public Response updateConfiguration(PlatformConfiguration config) { public Response updateConfiguration(PlatformConfiguration config) {
try { try {
RequestValidationUtil.validateUpdateConfiguration(config);
DeviceMgtAPIUtils.getPlatformConfigurationManagementService().saveConfiguration(config, DeviceMgtAPIUtils.getPlatformConfigurationManagementService().saveConfiguration(config,
MDMAppConstants.RegistryConstants.GENERAL_CONFIG_RESOURCE_PATH); MDMAppConstants.RegistryConstants.GENERAL_CONFIG_RESOURCE_PATH);
//Schedule the task service //Schedule the task service

@ -38,8 +38,8 @@ import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService;
import org.wso2.carbon.device.mgt.jaxrs.beans.DeviceList; import org.wso2.carbon.device.mgt.jaxrs.beans.DeviceList;
import org.wso2.carbon.device.mgt.jaxrs.beans.ErrorResponse; import org.wso2.carbon.device.mgt.jaxrs.beans.ErrorResponse;
import org.wso2.carbon.device.mgt.jaxrs.service.api.DeviceManagementService; import org.wso2.carbon.device.mgt.jaxrs.service.api.DeviceManagementService;
import org.wso2.carbon.device.mgt.jaxrs.service.impl.util.RequestValidationUtil; import org.wso2.carbon.device.mgt.jaxrs.service.impl.util.*;
import org.wso2.carbon.device.mgt.jaxrs.service.impl.util.UnexpectedServerErrorException; import org.wso2.carbon.device.mgt.jaxrs.service.impl.util.NotFoundException;
import org.wso2.carbon.device.mgt.jaxrs.util.DeviceMgtAPIUtils; import org.wso2.carbon.device.mgt.jaxrs.util.DeviceMgtAPIUtils;
import org.wso2.carbon.policy.mgt.common.Policy; import org.wso2.carbon.policy.mgt.common.Policy;
import org.wso2.carbon.policy.mgt.common.PolicyManagementException; import org.wso2.carbon.policy.mgt.common.PolicyManagementException;
@ -94,8 +94,9 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
result = dms.getAllDevices(request); result = dms.getAllDevices(request);
} }
if (result == null) { if (result == null) {
return Response.status(Response.Status.NOT_FOUND).entity("No device is currently enrolled " + throw new NotFoundException(
"with the server").build(); new ErrorResponse.ErrorResponseBuilder().setCode(404l).setMessage("No device is currently" +
" enrolled with the server").build());
} }
DeviceList devices = new DeviceList(); DeviceList devices = new DeviceList();
devices.setList((List<Device>) result.getData()); devices.setList((List<Device>) result.getData());
@ -124,8 +125,9 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
informationManager = DeviceMgtAPIUtils.getDeviceInformationManagerService(); informationManager = DeviceMgtAPIUtils.getDeviceInformationManagerService();
deviceInfo = informationManager.getDeviceInfo(deviceIdentifier); deviceInfo = informationManager.getDeviceInfo(deviceIdentifier);
if (deviceInfo == null) { if (deviceInfo == null) {
return Response.status(Response.Status.NOT_FOUND).entity("It is likely that no device is " + throw new NotFoundException(
"found upon the give type '" + type + "' and id '" + id + "'").build(); new ErrorResponse.ErrorResponseBuilder().setCode(404l).setMessage("It is likely that no " +
"device is found upon the give type '" + type + "' and id '" + id + "'").build());
} }
} catch (DeviceDetailsMgtException e) { } catch (DeviceDetailsMgtException e) {
String msg = "Error occurred while getting the device information."; String msg = "Error occurred while getting the device information.";
@ -148,8 +150,9 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
informationManager = DeviceMgtAPIUtils.getDeviceInformationManagerService(); informationManager = DeviceMgtAPIUtils.getDeviceInformationManagerService();
deviceInfo = informationManager.getDevicesInfo(deviceIds); deviceInfo = informationManager.getDevicesInfo(deviceIds);
if (deviceInfo == null) { if (deviceInfo == null) {
return Response.status(Response.Status.NOT_FOUND).entity("No device information is available for the " + throw new NotFoundException(
"device list submitted").build(); new ErrorResponse.ErrorResponseBuilder().setCode(404l).setMessage("No device information " +
"is available for the device list submitted").build());
} }
} catch (DeviceDetailsMgtException e) { } catch (DeviceDetailsMgtException e) {
String msg = "Error occurred while getting the device information."; String msg = "Error occurred while getting the device information.";
@ -179,8 +182,9 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build()); new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build());
} }
if (device == null) { if (device == null) {
return Response.status(Response.Status.NOT_FOUND).entity("Requested device of type '" + type + throw new NotFoundException(
"', which carries id '" + id + "' does not exist").build(); new ErrorResponse.ErrorResponseBuilder().setCode(404l).setMessage("Requested device of type '" +
type + "', which carries id '" + id + "' does not exist").build());
} }
return Response.status(Response.Status.OK).entity(device).build(); return Response.status(Response.Status.OK).entity(device).build();
} }
@ -201,8 +205,9 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
deviceLocation = informationManager.getDeviceLocation(new DeviceIdentifier(id, type)); deviceLocation = informationManager.getDeviceLocation(new DeviceIdentifier(id, type));
if (deviceLocation == null || deviceLocation.getLatitude() == null || if (deviceLocation == null || deviceLocation.getLatitude() == null ||
deviceLocation.getLongitude() == null) { deviceLocation.getLongitude() == null) {
return Response.status(Response.Status.NOT_FOUND).entity("Location details are not available for the " + throw new NotFoundException(
"given device id '" + id + "'").build(); new ErrorResponse.ErrorResponseBuilder().setCode(404l).setMessage("Location details " +
"are not available for the given device id '" + id + "'").build());
} }
} catch (DeviceDetailsMgtException e) { } catch (DeviceDetailsMgtException e) {
String msg = "Error occurred while getting the last updated location of the '" + type + "' device, " + String msg = "Error occurred while getting the last updated location of the '" + type + "' device, " +
@ -220,9 +225,15 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
@HeaderParam("If-Modified-Since") String ifModifiedSince) { @HeaderParam("If-Modified-Since") String ifModifiedSince) {
DeviceInformationManager informationManager; DeviceInformationManager informationManager;
List<DeviceLocation> deviceLocations; List<DeviceLocation> deviceLocations;
RequestValidationUtil.validateDeviceIdentifiers(deviceIdentifiers);
try { try {
informationManager = DeviceMgtAPIUtils.getDeviceInformationManagerService(); informationManager = DeviceMgtAPIUtils.getDeviceInformationManagerService();
deviceLocations = informationManager.getDeviceLocations(deviceIdentifiers); deviceLocations = informationManager.getDeviceLocations(deviceIdentifiers);
if (deviceLocations == null || deviceLocations.size() == 0) {
throw new NotFoundException(
new ErrorResponse.ErrorResponseBuilder().setCode(404l).setMessage("No location related " +
"details were found.").build());
}
} catch (DeviceDetailsMgtException e) { } catch (DeviceDetailsMgtException e) {
String msg = "Error occurred while getting the device location."; String msg = "Error occurred while getting the device location.";
log.error(msg, e); log.error(msg, e);
@ -247,8 +258,9 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
dms = DeviceMgtAPIUtils.getDeviceManagementService(); dms = DeviceMgtAPIUtils.getDeviceManagementService();
FeatureManager fm = dms.getFeatureManager(type); FeatureManager fm = dms.getFeatureManager(type);
if (fm == null) { if (fm == null) {
return Response.status(Response.Status.NOT_FOUND).entity("No feature manager is registered with " + throw new NotFoundException(
"the given type '" + type + "'").build(); new ErrorResponse.ErrorResponseBuilder().setCode(404l).setMessage("No feature manager is " +
"registered with the given type '" + type + "'").build());
} }
features = fm.getFeatures(); features = fm.getFeatures();
} catch (DeviceManagementException e) { } catch (DeviceManagementException e) {
@ -278,10 +290,10 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build()); new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build());
} }
if (devices == null || devices.size() == 0) { if (devices == null || devices.size() == 0) {
return Response.status(Response.Status.NOT_FOUND).entity("It is likely that no device is found upon " + throw new NotFoundException(
"the provided search filters").build(); new ErrorResponse.ErrorResponseBuilder().setCode(404l).setMessage("It is likely that no device is found upon " +
"the provided search filters").build());
} }
return Response.status(Response.Status.OK).entity(devices).build(); return Response.status(Response.Status.OK).entity(devices).build();
} }
@ -302,8 +314,9 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
amc = DeviceMgtAPIUtils.getAppManagementService(); amc = DeviceMgtAPIUtils.getAppManagementService();
applications = amc.getApplicationListForDevice(new DeviceIdentifier(id, type)); applications = amc.getApplicationListForDevice(new DeviceIdentifier(id, type));
if (applications == null) { if (applications == null) {
return Response.status(Response.Status.NOT_FOUND).entity("It is likely that no device is found upon" + throw new NotFoundException(
" the provided type and id").build(); new ErrorResponse.ErrorResponseBuilder().setCode(404l).setMessage("It is likely that " +
"no applications is found upon the provided type and id").build());
} }
} catch (ApplicationManagementException e) { } catch (ApplicationManagementException e) {
String msg = "Error occurred while fetching the apps of the '" + type + "' device, which carries " + String msg = "Error occurred while fetching the apps of the '" + type + "' device, which carries " +
@ -332,8 +345,9 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
dms = DeviceMgtAPIUtils.getDeviceManagementService(); dms = DeviceMgtAPIUtils.getDeviceManagementService();
operations = dms.getOperations(new DeviceIdentifier(id, type)); operations = dms.getOperations(new DeviceIdentifier(id, type));
if (operations == null) { if (operations == null) {
return Response.status(Response.Status.NOT_FOUND).entity("It is likely that no device is found upon " + throw new NotFoundException(
"the provided type and id").build(); new ErrorResponse.ErrorResponseBuilder().setCode(404l).setMessage("It is likely that" +
" no operation is found upon the provided type and id").build());
} }
} catch (OperationManagementException e) { } catch (OperationManagementException e) {
String msg = "Error occurred while fetching the operations for the '" + type + "' device, which " + String msg = "Error occurred while fetching the operations for the '" + type + "' device, which " +
@ -357,8 +371,9 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
PolicyManagerService policyManagementService = DeviceMgtAPIUtils.getPolicyManagementService(); PolicyManagerService policyManagementService = DeviceMgtAPIUtils.getPolicyManagementService();
Policy policy = policyManagementService.getAppliedPolicyToDevice(new DeviceIdentifier(id, type)); Policy policy = policyManagementService.getAppliedPolicyToDevice(new DeviceIdentifier(id, type));
if (policy == null) { if (policy == null) {
return Response.status(Response.Status.NOT_FOUND).entity("No policy has been found for the '" + throw new NotFoundException(
type + "' device, which carries the id '" + id + "'").build(); new ErrorResponse.ErrorResponseBuilder().setCode(404l).setMessage("No policy has " +
"been found for the '" + type + "' device, which carries the id '" + id + "'").build());
} }
return Response.status(Response.Status.OK).entity(policy).build(); return Response.status(Response.Status.OK).entity(policy).build();
} catch (PolicyManagementException e) { } catch (PolicyManagementException e) {

@ -25,8 +25,8 @@ import org.wso2.carbon.device.mgt.common.notification.mgt.NotificationManagement
import org.wso2.carbon.device.mgt.jaxrs.NotificationContext; import org.wso2.carbon.device.mgt.jaxrs.NotificationContext;
import org.wso2.carbon.device.mgt.jaxrs.beans.ErrorResponse; import org.wso2.carbon.device.mgt.jaxrs.beans.ErrorResponse;
import org.wso2.carbon.device.mgt.jaxrs.service.api.NotificationManagementService; import org.wso2.carbon.device.mgt.jaxrs.service.api.NotificationManagementService;
import org.wso2.carbon.device.mgt.jaxrs.service.impl.util.RequestValidationUtil; import org.wso2.carbon.device.mgt.jaxrs.service.impl.util.*;
import org.wso2.carbon.device.mgt.jaxrs.service.impl.util.UnexpectedServerErrorException; import org.wso2.carbon.device.mgt.jaxrs.service.impl.util.NotFoundException;
import org.wso2.carbon.device.mgt.jaxrs.util.DeviceMgtAPIUtils; import org.wso2.carbon.device.mgt.jaxrs.util.DeviceMgtAPIUtils;
import javax.ws.rs.*; import javax.ws.rs.*;
@ -59,9 +59,10 @@ public class NotificationManagementServiceImpl implements NotificationManagement
notifications = DeviceMgtAPIUtils.getNotificationManagementService().getAllNotifications(); notifications = DeviceMgtAPIUtils.getNotificationManagementService().getAllNotifications();
} }
if (notifications == null) { if (notifications == null || notifications.size() == 0) {
return Response.status(Response.Status.NOT_FOUND).entity("No notification is available to be " + throw new NotFoundException(
"retrieved").build(); new ErrorResponse.ErrorResponseBuilder().setCode(404l).setMessage("No notification is " +
"available to be retrieved.").build());
} }
return Response.status(Response.Status.OK).entity(notifications).build(); return Response.status(Response.Status.OK).entity(notifications).build();
} catch (NotificationManagementException e) { } catch (NotificationManagementException e) {

@ -27,9 +27,12 @@ import org.wso2.carbon.device.mgt.common.DeviceManagementException;
import org.wso2.carbon.device.mgt.common.authorization.DeviceAccessAuthorizationException; 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.authorization.DeviceAccessAuthorizationService;
import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder; import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder;
import org.wso2.carbon.device.mgt.jaxrs.beans.PolicyList; import org.wso2.carbon.device.mgt.jaxrs.beans.ErrorResponse;
import org.wso2.carbon.device.mgt.jaxrs.beans.PolicyWrapper; import org.wso2.carbon.device.mgt.jaxrs.beans.PolicyWrapper;
import org.wso2.carbon.device.mgt.jaxrs.service.api.PolicyManagementService; import org.wso2.carbon.device.mgt.jaxrs.service.api.PolicyManagementService;
import org.wso2.carbon.device.mgt.jaxrs.service.impl.util.*;
import org.wso2.carbon.device.mgt.jaxrs.service.impl.util.NotFoundException;
import org.wso2.carbon.device.mgt.jaxrs.beans.PolicyList;
import org.wso2.carbon.device.mgt.jaxrs.service.impl.util.FilteringUtil; import org.wso2.carbon.device.mgt.jaxrs.service.impl.util.FilteringUtil;
import org.wso2.carbon.device.mgt.jaxrs.util.DeviceMgtAPIUtils; import org.wso2.carbon.device.mgt.jaxrs.util.DeviceMgtAPIUtils;
import org.wso2.carbon.device.mgt.jaxrs.util.DeviceMgtUtil; import org.wso2.carbon.device.mgt.jaxrs.util.DeviceMgtUtil;
@ -53,7 +56,9 @@ public class PolicyManagementServiceImpl implements PolicyManagementService {
@POST @POST
@Override @Override
public Response addPolicy(PolicyWrapper policyWrapper) { public Response addPolicy(PolicyWrapper policyWrapper) {
RequestValidationUtil.validatePolicyDetails(policyWrapper);
PolicyManagerService policyManagementService = DeviceMgtAPIUtils.getPolicyManagementService(); PolicyManagerService policyManagementService = DeviceMgtAPIUtils.getPolicyManagementService();
try { try {
Policy policy = this.getPolicyFromWrapper(policyWrapper); Policy policy = this.getPolicyFromWrapper(policyWrapper);
@ -67,13 +72,15 @@ public class PolicyManagementServiceImpl implements PolicyManagementService {
String username = threadLocalCarbonContext.getUsername(); String username = threadLocalCarbonContext.getUsername();
try { try {
if (!deviceAccessAuthorizationService.isUserAuthorized(deviceIdentifier, username)) { if (!deviceAccessAuthorizationService.isUserAuthorized(deviceIdentifier, username)) {
return Response.status(Response.Status.UNAUTHORIZED).entity("Current logged in user is " + throw new UnauthorizedAccessException(
"not authorized to add policies").build(); new ErrorResponse.ErrorResponseBuilder().setCode(401l).setMessage
("Current logged in user is not authorized to add policies").build());
} }
} catch (DeviceAccessAuthorizationException e) { } catch (DeviceAccessAuthorizationException e) {
String msg = "ErrorResponse occurred while checking if the current user is authorized to add a policy"; String msg = "ErrorResponse occurred while checking if the current user is authorized to add a policy";
log.error(msg, e); log.error(msg, e);
return javax.ws.rs.core.Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); throw new UnexpectedServerErrorException(
new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build());
} }
} }
@ -83,11 +90,13 @@ public class PolicyManagementServiceImpl implements PolicyManagementService {
} catch (PolicyManagementException e) { } catch (PolicyManagementException e) {
String msg = "ErrorResponse occurred while adding policy"; String msg = "ErrorResponse occurred while adding policy";
log.error(msg, e); log.error(msg, e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); throw new UnexpectedServerErrorException(
new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build());
} catch (DeviceManagementException e) { } catch (DeviceManagementException e) {
String msg = "ErrorResponse occurred while retrieving device list."; String msg = "ErrorResponse occurred while retrieving device list.";
log.error(msg, e); log.error(msg, e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); throw new UnexpectedServerErrorException(
new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build());
} }
} }
@ -127,7 +136,8 @@ public class PolicyManagementServiceImpl implements PolicyManagementService {
PolicyAdministratorPoint policyAdministratorPoint = policyManagementService.getPAP(); PolicyAdministratorPoint policyAdministratorPoint = policyManagementService.getPAP();
policies = policyAdministratorPoint.getPolicies(); policies = policyAdministratorPoint.getPolicies();
if (policies == null || policies.size() == 0) { if (policies == null || policies.size() == 0) {
return Response.status(Response.Status.NOT_FOUND).entity("No policies found.").build(); throw new NotFoundException(
new ErrorResponse.ErrorResponseBuilder().setCode(404l).setMessage("No policies found.").build());
} }
targetPolicies.setCount(policies.size()); targetPolicies.setCount(policies.size());
filteredPolicies = FilteringUtil.getFilteredList(policies, offset, limit); filteredPolicies = FilteringUtil.getFilteredList(policies, offset, limit);
@ -138,7 +148,8 @@ public class PolicyManagementServiceImpl implements PolicyManagementService {
} catch (PolicyManagementException e) { } catch (PolicyManagementException e) {
String msg = "ErrorResponse occurred while retrieving all available policies"; String msg = "ErrorResponse occurred while retrieving all available policies";
log.error(msg, e); log.error(msg, e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); throw new UnexpectedServerErrorException(
new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build());
} }
return Response.status(Response.Status.OK).entity(targetPolicies).build(); return Response.status(Response.Status.OK).entity(targetPolicies).build();
@ -154,12 +165,14 @@ public class PolicyManagementServiceImpl implements PolicyManagementService {
PolicyAdministratorPoint policyAdministratorPoint = policyManagementService.getPAP(); PolicyAdministratorPoint policyAdministratorPoint = policyManagementService.getPAP();
policy = policyAdministratorPoint.getPolicy(id); policy = policyAdministratorPoint.getPolicy(id);
if (policy == null) { if (policy == null) {
return Response.status(Response.Status.NOT_FOUND).entity("Policy not found.").build(); throw new NotFoundException(
new ErrorResponse.ErrorResponseBuilder().setCode(404l).setMessage("No policy found.").build());
} }
} catch (PolicyManagementException e) { } catch (PolicyManagementException e) {
String msg = "ErrorResponse occurred while retrieving policy corresponding to the id '" + id + "'"; String msg = "ErrorResponse occurred while retrieving policy corresponding to the id '" + id + "'";
log.error(msg, e); log.error(msg, e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); throw new UnexpectedServerErrorException(
new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build());
} }
return Response.status(Response.Status.OK).entity(policy).build(); return Response.status(Response.Status.OK).entity(policy).build();
} }
@ -168,6 +181,7 @@ public class PolicyManagementServiceImpl implements PolicyManagementService {
@Path("/{id}") @Path("/{id}")
@Override @Override
public Response updatePolicy(@PathParam("id") int id, PolicyWrapper policyWrapper) { public Response updatePolicy(@PathParam("id") int id, PolicyWrapper policyWrapper) {
RequestValidationUtil.validatePolicyDetails(policyWrapper);
PolicyManagerService policyManagementService = DeviceMgtAPIUtils.getPolicyManagementService(); PolicyManagerService policyManagementService = DeviceMgtAPIUtils.getPolicyManagementService();
try { try {
Policy policy = this.getPolicyFromWrapper(policyWrapper); Policy policy = this.getPolicyFromWrapper(policyWrapper);
@ -178,15 +192,17 @@ public class PolicyManagementServiceImpl implements PolicyManagementService {
return Response.status(Response.Status.NOT_FOUND).entity("Policy not found.").build(); return Response.status(Response.Status.NOT_FOUND).entity("Policy not found.").build();
} }
pap.updatePolicy(policy); pap.updatePolicy(policy);
return Response.status(Response.Status.OK).entity("Policy has successfully been updated").build(); return Response.status(Response.Status.OK).entity("Policy has successfully been updated.").build();
} catch (PolicyManagementException e) { } catch (PolicyManagementException e) {
String msg = "ErrorResponse occurred while updating the policy"; String msg = "ErrorResponse occurred while updating the policy";
log.error(msg, e); log.error(msg, e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); throw new UnexpectedServerErrorException(
new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build());
} catch (DeviceManagementException e) { } catch (DeviceManagementException e) {
String msg = "ErrorResponse occurred while retrieving the device list."; String msg = "ErrorResponse occurred while retrieving the device list.";
log.error(msg, e); log.error(msg, e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); throw new UnexpectedServerErrorException(
new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build());
} }
} }
@ -194,6 +210,7 @@ public class PolicyManagementServiceImpl implements PolicyManagementService {
@Path("/remove-policy") @Path("/remove-policy")
@Override @Override
public Response removePolicies(List<Integer> policyIds) { public Response removePolicies(List<Integer> policyIds) {
RequestValidationUtil.validatePolicyIds(policyIds);
PolicyManagerService policyManagementService = DeviceMgtAPIUtils.getPolicyManagementService(); PolicyManagerService policyManagementService = DeviceMgtAPIUtils.getPolicyManagementService();
boolean policyDeleted = true; boolean policyDeleted = true;
try { try {
@ -207,13 +224,15 @@ public class PolicyManagementServiceImpl implements PolicyManagementService {
} catch (PolicyManagementException e) { } catch (PolicyManagementException e) {
String msg = "ErrorResponse occurred while removing policies"; String msg = "ErrorResponse occurred while removing policies";
log.error(msg, e); log.error(msg, e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); throw new UnexpectedServerErrorException(
new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build());
} }
if (policyDeleted) { if (policyDeleted) {
return Response.status(Response.Status.OK).entity("Policies have been successfully deleted").build(); return Response.status(Response.Status.OK).entity("Policies have been successfully deleted").build();
} else { } else {
//TODO:Check of this logic is correct //TODO:Check of this logic is correct
return Response.status(Response.Status.NOT_FOUND).entity("Policy doesn't exist").build(); throw new NotFoundException(
new ErrorResponse.ErrorResponseBuilder().setCode(404l).setMessage("Policy doesn't exist").build());
} }
} }
@ -221,6 +240,7 @@ public class PolicyManagementServiceImpl implements PolicyManagementService {
@Path("/activate-policy") @Path("/activate-policy")
@Override @Override
public Response activatePolicies(List<Integer> policyIds) { public Response activatePolicies(List<Integer> policyIds) {
RequestValidationUtil.validatePolicyIds(policyIds);
boolean isPolicyActivated = false; boolean isPolicyActivated = false;
try { try {
PolicyManagerService policyManagementService = DeviceMgtAPIUtils.getPolicyManagementService(); PolicyManagerService policyManagementService = DeviceMgtAPIUtils.getPolicyManagementService();
@ -235,14 +255,16 @@ public class PolicyManagementServiceImpl implements PolicyManagementService {
} catch (PolicyManagementException e) { } catch (PolicyManagementException e) {
String msg = "ErrorResponse occurred while activating policies"; String msg = "ErrorResponse occurred while activating policies";
log.error(msg, e); log.error(msg, e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); throw new UnexpectedServerErrorException(
new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build());
} }
if (isPolicyActivated) { if (isPolicyActivated) {
return Response.status(Response.Status.OK).entity("Selected policies have been successfully activated") return Response.status(Response.Status.OK).entity("Selected policies have been successfully activated")
.build(); .build();
} else { } else {
return Response.status(Response.Status.NOT_FOUND).entity("Selected policies have not been activated") throw new NotFoundException(
.build(); new ErrorResponse.ErrorResponseBuilder().setCode(404l).setMessage("Selected policies have " +
"not been activated").build());
} }
} }
@ -250,6 +272,7 @@ public class PolicyManagementServiceImpl implements PolicyManagementService {
@Path("/deactivate-policy") @Path("/deactivate-policy")
@Override @Override
public Response deactivatePolicies(List<Integer> policyIds) { public Response deactivatePolicies(List<Integer> policyIds) {
RequestValidationUtil.validatePolicyIds(policyIds);
boolean isPolicyDeActivated = false; boolean isPolicyDeActivated = false;
try { try {
PolicyManagerService policyManagementService = DeviceMgtAPIUtils.getPolicyManagementService(); PolicyManagerService policyManagementService = DeviceMgtAPIUtils.getPolicyManagementService();
@ -264,14 +287,16 @@ public class PolicyManagementServiceImpl implements PolicyManagementService {
} catch (PolicyManagementException e) { } catch (PolicyManagementException e) {
String msg = "Exception in inactivating policies."; String msg = "Exception in inactivating policies.";
log.error(msg, e); log.error(msg, e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); throw new UnexpectedServerErrorException(
new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build());
} }
if (isPolicyDeActivated) { if (isPolicyDeActivated) {
return Response.status(Response.Status.OK).entity("Selected policies have been successfully " + return Response.status(Response.Status.OK).entity("Selected policies have been successfully " +
"deactivated").build(); "deactivated").build();
} else { } else {
return Response.status(Response.Status.NOT_FOUND).entity("Selected policies have not been deactivated") throw new NotFoundException(
.build(); new ErrorResponse.ErrorResponseBuilder().setCode(404l).setMessage("Selected policies have " +
"not been deactivated").build());
} }
} }

@ -23,15 +23,16 @@ import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.CarbonConstants; import org.wso2.carbon.CarbonConstants;
import org.wso2.carbon.base.MultitenantConstants; import org.wso2.carbon.base.MultitenantConstants;
import org.wso2.carbon.device.mgt.jaxrs.beans.ErrorResponse; import org.wso2.carbon.device.mgt.jaxrs.beans.ErrorResponse;
import org.wso2.carbon.device.mgt.jaxrs.beans.RoleList;
import org.wso2.carbon.device.mgt.jaxrs.service.api.RoleManagementService; import org.wso2.carbon.device.mgt.jaxrs.service.api.RoleManagementService;
import org.wso2.carbon.device.mgt.jaxrs.service.impl.util.*;
import org.wso2.carbon.device.mgt.jaxrs.service.impl.util.NotFoundException;
import org.wso2.carbon.device.mgt.jaxrs.beans.RoleList;
import org.wso2.carbon.device.mgt.jaxrs.service.impl.util.FilteringUtil; import org.wso2.carbon.device.mgt.jaxrs.service.impl.util.FilteringUtil;
import org.wso2.carbon.device.mgt.jaxrs.service.impl.util.UnexpectedServerErrorException; import org.wso2.carbon.device.mgt.jaxrs.service.impl.util.UnexpectedServerErrorException;
import org.wso2.carbon.device.mgt.jaxrs.util.DeviceMgtAPIUtils; import org.wso2.carbon.device.mgt.jaxrs.util.DeviceMgtAPIUtils;
import org.wso2.carbon.device.mgt.jaxrs.beans.RoleWrapper; import org.wso2.carbon.device.mgt.jaxrs.beans.RoleWrapper;
import org.wso2.carbon.device.mgt.jaxrs.util.SetReferenceTransformer; import org.wso2.carbon.device.mgt.jaxrs.util.SetReferenceTransformer;
import org.wso2.carbon.user.api.*; import org.wso2.carbon.user.api.*;
import org.wso2.carbon.user.core.common.AbstractUserStoreManager;
import org.wso2.carbon.user.mgt.UserRealmProxy; import org.wso2.carbon.user.mgt.UserRealmProxy;
import org.wso2.carbon.user.mgt.common.UIPermissionNode; import org.wso2.carbon.user.mgt.common.UIPermissionNode;
import org.wso2.carbon.user.mgt.common.UserAdminException; import org.wso2.carbon.user.mgt.common.UserAdminException;
@ -62,12 +63,14 @@ public class RoleManagementServiceImpl implements RoleManagementService {
try { try {
filteredRoles = getRolesFromUserStore(); filteredRoles = getRolesFromUserStore();
if (filteredRoles == null || filteredRoles.size() == 0) { if (filteredRoles == null || filteredRoles.size() == 0) {
return Response.status(Response.Status.NOT_FOUND).entity("No roles found.").build(); throw new NotFoundException(
new ErrorResponse.ErrorResponseBuilder().setCode(404l).setMessage("No roles found.").build());
} }
targetRoles.setCount(filteredRoles.size()); targetRoles.setCount(filteredRoles.size());
filteredRoles = FilteringUtil.getFilteredList(getRolesFromUserStore(), offset, limit); filteredRoles = FilteringUtil.getFilteredList(getRolesFromUserStore(), offset, limit);
if (filteredRoles.size() == 0) { if (filteredRoles.size() == 0) {
return Response.status(Response.Status.NOT_FOUND).entity("No roles found.").build(); throw new NotFoundException(
new ErrorResponse.ErrorResponseBuilder().setCode(404l).setMessage("No roles found").build());
} }
targetRoles.setList(filteredRoles); targetRoles.setList(filteredRoles);
} catch (UserStoreException e) { } catch (UserStoreException e) {
@ -85,6 +88,7 @@ public class RoleManagementServiceImpl implements RoleManagementService {
public Response getPermissionsOfRole( public Response getPermissionsOfRole(
@PathParam("roleName") String roleName, @PathParam("roleName") String roleName,
@HeaderParam("If-Modified-Since") String ifModifiedSince) { @HeaderParam("If-Modified-Since") String ifModifiedSince) {
RequestValidationUtil.validateRoleName(roleName);
try { try {
final UserRealm userRealm = DeviceMgtAPIUtils.getUserRealm(); final UserRealm userRealm = DeviceMgtAPIUtils.getUserRealm();
org.wso2.carbon.user.core.UserRealm userRealmCore = null; org.wso2.carbon.user.core.UserRealm userRealmCore = null;
@ -95,8 +99,9 @@ public class RoleManagementServiceImpl implements RoleManagementService {
final UserRealmProxy userRealmProxy = new UserRealmProxy(userRealmCore); final UserRealmProxy userRealmProxy = new UserRealmProxy(userRealmCore);
rolePermissions = this.getUIPermissionNode(roleName, userRealmProxy); rolePermissions = this.getUIPermissionNode(roleName, userRealmProxy);
if (rolePermissions == null) { if (rolePermissions == null) {
return Response.status(Response.Status.NOT_FOUND).entity("No permissions found for the role '" + throw new NotFoundException(
roleName + "'").build(); new ErrorResponse.ErrorResponseBuilder().setCode(404l).setMessage("No permissions found" +
" for the role '" + roleName + "'").build());
} }
return Response.status(Response.Status.OK).entity(rolePermissions).build(); return Response.status(Response.Status.OK).entity(rolePermissions).build();
} catch (UserAdminException e) { } catch (UserAdminException e) {
@ -139,6 +144,7 @@ public class RoleManagementServiceImpl implements RoleManagementService {
@Override @Override
public Response getRole(@PathParam("roleName") String roleName, public Response getRole(@PathParam("roleName") String roleName,
@HeaderParam("If-Modified-Since") String ifModifiedSince) { @HeaderParam("If-Modified-Since") String ifModifiedSince) {
RequestValidationUtil.validateRoleName(roleName);
RoleWrapper roleWrapper = new RoleWrapper(); RoleWrapper roleWrapper = new RoleWrapper();
try { try {
final UserStoreManager userStoreManager = DeviceMgtAPIUtils.getUserStoreManager(); final UserStoreManager userStoreManager = DeviceMgtAPIUtils.getUserStoreManager();
@ -162,6 +168,10 @@ public class RoleManagementServiceImpl implements RoleManagementService {
roleWrapper.setPermissionList(rolePermissions); roleWrapper.setPermissionList(rolePermissions);
String[] permListAr = new String[permList.size()]; String[] permListAr = new String[permList.size()];
roleWrapper.setPermissions(permList.toArray(permListAr)); roleWrapper.setPermissions(permList.toArray(permListAr));
} else {
throw new NotFoundException(
new ErrorResponse.ErrorResponseBuilder().setCode(404l).setMessage("Role name doesn't exist.")
.build());
} }
} catch (UserStoreException | UserAdminException e) { } catch (UserStoreException | UserAdminException e) {
String msg = "Error occurred while retrieving the user role '" + roleName + "'"; String msg = "Error occurred while retrieving the user role '" + roleName + "'";
@ -185,10 +195,8 @@ public class RoleManagementServiceImpl implements RoleManagementService {
@POST @POST
@Override @Override
public Response addRole(RoleWrapper roleWrapper) { public Response addRole(RoleWrapper roleWrapper) {
if (roleWrapper == null) { RequestValidationUtil.validateRoleDetails(roleWrapper);
log.error("Request body is incorrect or empty"); RequestValidationUtil.validateRoleName(roleWrapper.getRoleName());
return Response.status(Response.Status.BAD_REQUEST).build();
}
try { try {
UserStoreManager userStoreManager = DeviceMgtAPIUtils.getUserStoreManager(); UserStoreManager userStoreManager = DeviceMgtAPIUtils.getUserStoreManager();
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
@ -218,9 +226,8 @@ public class RoleManagementServiceImpl implements RoleManagementService {
@Path("/{roleName}") @Path("/{roleName}")
@Override @Override
public Response updateRole(@PathParam("roleName") String roleName, RoleWrapper roleWrapper) { public Response updateRole(@PathParam("roleName") String roleName, RoleWrapper roleWrapper) {
if (roleWrapper == null) { RequestValidationUtil.validateRoleName(roleName);
return Response.status(Response.Status.BAD_REQUEST).entity("Request body is incorrect or empty").build(); RequestValidationUtil.validateRoleDetails(roleWrapper);
}
String newRoleName = roleWrapper.getRoleName(); String newRoleName = roleWrapper.getRoleName();
try { try {
final UserStoreManager userStoreManager = DeviceMgtAPIUtils.getUserStoreManager(); final UserStoreManager userStoreManager = DeviceMgtAPIUtils.getUserStoreManager();
@ -265,6 +272,7 @@ public class RoleManagementServiceImpl implements RoleManagementService {
@Path("/{roleName}") @Path("/{roleName}")
@Override @Override
public Response deleteRole(@PathParam("roleName") String roleName) { public Response deleteRole(@PathParam("roleName") String roleName) {
RequestValidationUtil.validateRoleName(roleName);
try { try {
final UserStoreManager userStoreManager = DeviceMgtAPIUtils.getUserStoreManager(); final UserStoreManager userStoreManager = DeviceMgtAPIUtils.getUserStoreManager();
final AuthorizationManager authorizationManager = DeviceMgtAPIUtils.getAuthorizationManager(); final AuthorizationManager authorizationManager = DeviceMgtAPIUtils.getAuthorizationManager();
@ -288,9 +296,8 @@ public class RoleManagementServiceImpl implements RoleManagementService {
@Path("/{roleName}/users") @Path("/{roleName}/users")
@Override @Override
public Response updateUsersOfRole(@PathParam("roleName") String roleName, List<String> users) { public Response updateUsersOfRole(@PathParam("roleName") String roleName, List<String> users) {
if (users == null || users.size() == 0) { RequestValidationUtil.validateRoleName(roleName);
return Response.status(Response.Status.BAD_REQUEST).entity("No users are found in the request").build(); RequestValidationUtil.validateUsers(users);
}
try { try {
final UserStoreManager userStoreManager = DeviceMgtAPIUtils.getUserStoreManager(); final UserStoreManager userStoreManager = DeviceMgtAPIUtils.getUserStoreManager();
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {

@ -27,11 +27,12 @@ import org.wso2.carbon.device.mgt.common.DeviceManagementException;
import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService;
import org.wso2.carbon.device.mgt.core.service.EmailMetaInfo; import org.wso2.carbon.device.mgt.core.service.EmailMetaInfo;
import org.wso2.carbon.device.mgt.jaxrs.beans.ErrorResponse; import org.wso2.carbon.device.mgt.jaxrs.beans.ErrorResponse;
import org.wso2.carbon.device.mgt.jaxrs.beans.OldPasswordResetWrapper;
import org.wso2.carbon.device.mgt.jaxrs.beans.UserList;
import org.wso2.carbon.device.mgt.jaxrs.beans.UserWrapper; import org.wso2.carbon.device.mgt.jaxrs.beans.UserWrapper;
import org.wso2.carbon.device.mgt.jaxrs.service.api.UserManagementService; import org.wso2.carbon.device.mgt.jaxrs.service.api.UserManagementService;
import org.wso2.carbon.device.mgt.jaxrs.service.impl.util.UnexpectedServerErrorException; import org.wso2.carbon.device.mgt.jaxrs.service.impl.util.*;
import org.wso2.carbon.device.mgt.jaxrs.beans.OldPasswordResetWrapper;
import org.wso2.carbon.device.mgt.jaxrs.beans.UserList;
import org.wso2.carbon.device.mgt.jaxrs.service.impl.util.NotFoundException;
import org.wso2.carbon.device.mgt.jaxrs.util.Constants; import org.wso2.carbon.device.mgt.jaxrs.util.Constants;
import org.wso2.carbon.device.mgt.jaxrs.util.CredentialManagementResponseBuilder; import org.wso2.carbon.device.mgt.jaxrs.util.CredentialManagementResponseBuilder;
import org.wso2.carbon.device.mgt.jaxrs.util.DeviceMgtAPIUtils; import org.wso2.carbon.device.mgt.jaxrs.util.DeviceMgtAPIUtils;
@ -42,7 +43,6 @@ import org.wso2.carbon.utils.multitenancy.MultitenantConstants;
import javax.ws.rs.*; import javax.ws.rs.*;
import javax.ws.rs.core.MediaType; import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response; import javax.ws.rs.core.Response;
import java.io.UnsupportedEncodingException;
import java.util.*; import java.util.*;
@Path("/users") @Path("/users")
@ -65,8 +65,10 @@ public class UserManagementServiceImpl implements UserManagementService {
" already exists. Therefore, request made to add user was refused."); " already exists. Therefore, request made to add user was refused.");
} }
// returning response with bad request state // returning response with bad request state
return Response.status(Response.Status.CONFLICT).entity("User by username: " + userWrapper.getUsername() + throw new ConflictException(
" already exists. Therefore, request made to add user was refused.").build(); new ErrorResponse.ErrorResponseBuilder().setCode(409l).setMessage("User by username: " +
userWrapper.getUsername() + " already exists. Therefore, request made to add user " +
"was refused.").build());
} else { } else {
String initialUserPassword = this.generateInitialUserPassword(); String initialUserPassword = this.generateInitialUserPassword();
Map<String, String> defaultUserClaims = Map<String, String> defaultUserClaims =
@ -186,8 +188,9 @@ public class UserManagementServiceImpl implements UserManagementService {
log.debug("User by username: " + username + " does not exist."); log.debug("User by username: " + username + " does not exist.");
} }
// returning response with bad request state // returning response with bad request state
return Response.status(Response.Status.NOT_FOUND).entity( throw new NotFoundException(
"User by username: " + username + " does not exist").build(); new ErrorResponse.ErrorResponseBuilder().setCode(404l).setMessage("User doesn't exist.")
.build());
} }
} catch (UserStoreException e) { } catch (UserStoreException e) {
String msg = "ErrorResponse occurred while retrieving information of the user '" + username + "'"; String msg = "ErrorResponse occurred while retrieving information of the user '" + username + "'";
@ -243,9 +246,10 @@ public class UserManagementServiceImpl implements UserManagementService {
log.debug("User by username: " + userWrapper.getUsername() + log.debug("User by username: " + userWrapper.getUsername() +
" doesn't exists. Therefore, request made to update user was refused."); " doesn't exists. Therefore, request made to update user was refused.");
} }
return Response.status(Response.Status.CONFLICT).entity("User by username: " + throw new NotFoundException(
userWrapper.getUsername() + " doesn't exists. Therefore, request made to update user was " + new ErrorResponse.ErrorResponseBuilder().setCode(404l).setMessage("User by username: " +
"refused.").build(); userWrapper.getUsername() + " doesn't exists. Therefore, request made to update user" +
" was refused.").build());
} }
} catch (UserStoreException e) { } catch (UserStoreException e) {
String msg = "Exception in trying to update user by username: " + userWrapper.getUsername(); String msg = "Exception in trying to update user by username: " + userWrapper.getUsername();
@ -293,8 +297,9 @@ public class UserManagementServiceImpl implements UserManagementService {
log.debug("User by username: " + username + " does not exist for removal."); log.debug("User by username: " + username + " does not exist for removal.");
} }
// returning response with bad request state // returning response with bad request state
return Response.status(Response.Status.NOT_FOUND).entity("User by username: " + username + throw new NotFoundException(
" does not exist for removal.").build(); new ErrorResponse.ErrorResponseBuilder().setCode(404l).setMessage("User by username: " +
username + " does not exist for removal.").build());
} }
} catch (UserStoreException e) { } catch (UserStoreException e) {
String msg = "Exception in trying to remove user by username: " + username; String msg = "Exception in trying to remove user by username: " + username;
@ -318,8 +323,9 @@ public class UserManagementServiceImpl implements UserManagementService {
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("User by username: " + username + " does not exist for role retrieval."); log.debug("User by username: " + username + " does not exist for role retrieval.");
} }
return Response.status(Response.Status.NOT_FOUND).entity("User by username: " + username + throw new NotFoundException(
" does not exist for role retrieval.").build(); new ErrorResponse.ErrorResponseBuilder().setCode(404l).setMessage("User by username: " + username +
" does not exist for role retrieval.").build());
} }
} catch (UserStoreException e) { } catch (UserStoreException e) {
String msg = "Exception in trying to retrieve roles for user by username: " + username; String msg = "Exception in trying to retrieve roles for user by username: " + username;

@ -28,8 +28,8 @@ import org.wso2.carbon.device.mgt.common.DeviceManagementException;
import org.wso2.carbon.device.mgt.jaxrs.beans.DeviceList; import org.wso2.carbon.device.mgt.jaxrs.beans.DeviceList;
import org.wso2.carbon.device.mgt.jaxrs.beans.ErrorResponse; import org.wso2.carbon.device.mgt.jaxrs.beans.ErrorResponse;
import org.wso2.carbon.device.mgt.jaxrs.service.api.admin.DeviceManagementAdminService; import org.wso2.carbon.device.mgt.jaxrs.service.api.admin.DeviceManagementAdminService;
import org.wso2.carbon.device.mgt.jaxrs.service.impl.util.UnauthorizedAccessException; import org.wso2.carbon.device.mgt.jaxrs.service.impl.util.*;
import org.wso2.carbon.device.mgt.jaxrs.service.impl.util.UnexpectedServerErrorException; import org.wso2.carbon.device.mgt.jaxrs.service.impl.util.NotFoundException;
import org.wso2.carbon.device.mgt.jaxrs.util.DeviceMgtAPIUtils; import org.wso2.carbon.device.mgt.jaxrs.util.DeviceMgtAPIUtils;
import javax.ws.rs.*; import javax.ws.rs.*;
@ -66,8 +66,9 @@ public class DeviceManagementAdminServiceImpl implements DeviceManagementAdminSe
List<Device> devices = DeviceMgtAPIUtils.getDeviceManagementService(). List<Device> devices = DeviceMgtAPIUtils.getDeviceManagementService().
getDevicesByNameAndType(name, type, offset, limit); getDevicesByNameAndType(name, type, offset, limit);
if (devices == null || devices.size() == 0) { if (devices == null || devices.size() == 0) {
return Response.status(Response.Status.NOT_FOUND).entity("No device, which carries the name '" + throw new NotFoundException(
name + "', is currently enrolled in the system").build(); new ErrorResponse.ErrorResponseBuilder().setCode(404l).setMessage("No device, which carries" +
" the name '" + name + "', is currently enrolled in the system").build());
} }
// setting up paginated result // setting up paginated result

@ -0,0 +1,34 @@
/*
* 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.util;
import org.wso2.carbon.device.mgt.jaxrs.beans.ErrorResponse;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Response;
public class ConflictException extends WebApplicationException {
private static final long serialVersionUID = 14234272342342340L;
public ConflictException(ErrorResponse error) {
super(Response.status(Response.Status.CONFLICT).entity(error).build());
}
}

@ -0,0 +1,34 @@
/*
* 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.util;
import org.wso2.carbon.device.mgt.jaxrs.beans.ErrorResponse;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Response;
public class NotFoundException extends WebApplicationException {
private static final long serialVersionUID = 147943572342342340L;
public NotFoundException(ErrorResponse error) {
super(Response.status(Response.Status.NOT_FOUND).entity(error).build());
}
}

@ -18,12 +18,10 @@
*/ */
package org.wso2.carbon.device.mgt.jaxrs.service.impl.util; package org.wso2.carbon.device.mgt.jaxrs.service.impl.util;
import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
import org.wso2.carbon.device.mgt.common.configuration.mgt.PlatformConfiguration;
import org.wso2.carbon.device.mgt.common.notification.mgt.Notification; import org.wso2.carbon.device.mgt.common.notification.mgt.Notification;
import org.wso2.carbon.device.mgt.jaxrs.beans.ApplicationWrapper; import org.wso2.carbon.device.mgt.jaxrs.beans.*;
import org.wso2.carbon.device.mgt.jaxrs.beans.ErrorResponse;
import org.wso2.carbon.device.mgt.jaxrs.beans.OldPasswordResetWrapper;
import javax.ws.rs.core.Response;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -247,6 +245,57 @@ public class RequestValidationUtil {
} }
} }
public static void validateUpdateConfiguration(PlatformConfiguration config) {
if (config == null) {
throw new InputValidationException(
new ErrorResponse.ErrorResponseBuilder().setCode(400l).setMessage("Configurations are not defined.")
.build());
} else if (config.getConfiguration() == null || config.getConfiguration().size() == 0) {
throw new InputValidationException(
new ErrorResponse.ErrorResponseBuilder().setCode(400l).setMessage("Does not contain any " +
"configuration entries.").build());
}
}
public static void validateDeviceIdentifiers(List<DeviceIdentifier> deviceIdentifiers) {
if (deviceIdentifiers == null || deviceIdentifiers.size() == 0) {
throw new InputValidationException(
new ErrorResponse.ErrorResponseBuilder().setCode(400l).setMessage("Device identifier list is " +
"empty.").build());
}
}
public static void validatePolicyDetails(PolicyWrapper policyWrapper) {
if (policyWrapper == null) {
throw new InputValidationException(
new ErrorResponse.ErrorResponseBuilder().setCode(400l).setMessage("Policy is empty.").build());
}
}
public static void validatePolicyIds(List<Integer> policyIds) {
if (policyIds == null || policyIds.size() == 0) {
throw new InputValidationException(
new ErrorResponse.ErrorResponseBuilder().setCode(400l).setMessage("Policy Id list is empty.").build
());
}
}
public static void validateRoleName(String roleName) {
if (roleName == null || roleName.isEmpty()) {
throw new InputValidationException(
new ErrorResponse.ErrorResponseBuilder().setCode(400l).setMessage("Role name isn't valid.").build
());
}
}
public static void validateUsers(List<String> users) {
if (users == null || users.size() == 0) {
throw new InputValidationException(
new ErrorResponse.ErrorResponseBuilder().setCode(400l).setMessage("User list isn't valid.").build
());
}
}
public static void validateCredentials(OldPasswordResetWrapper credentials) { public static void validateCredentials(OldPasswordResetWrapper credentials) {
if (credentials == null || credentials.getNewPassword() == null || credentials.getOldPassword() == null) { if (credentials == null || credentials.getNewPassword() == null || credentials.getOldPassword() == null) {
throw new InputValidationException( throw new InputValidationException(
@ -255,4 +304,12 @@ public class RequestValidationUtil {
} }
} }
public static void validateRoleDetails(RoleWrapper roleWrapper) {
if (roleWrapper == null) {
throw new InputValidationException(
new ErrorResponse.ErrorResponseBuilder().setCode(400l).setMessage("Request body is incorrect or" +
" empty").build());
}
}
} }

Loading…
Cancel
Save