diff --git a/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.api/src/main/java/org/wso2/carbon/mdm/services/android/bean/ErrorListItem.java b/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.api/src/main/java/org/wso2/carbon/mdm/services/android/bean/ErrorListItem.java new file mode 100644 index 0000000000..1e48b01833 --- /dev/null +++ b/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.api/src/main/java/org/wso2/carbon/mdm/services/android/bean/ErrorListItem.java @@ -0,0 +1,77 @@ +/* + * 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.mdm.services.android.bean; + +import com.fasterxml.jackson.annotation.JsonProperty; +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; + +import javax.validation.constraints.NotNull; + +@ApiModel(description = "") +public class ErrorListItem { + + @NotNull + private String code = null; + @NotNull + private String message = null; + + @ApiModelProperty(required = true, value = "") + @JsonProperty("code") + public String getCode() { + return code; + } + public void setCode(String code) { + this.code = code; + } + + public ErrorListItem() {} + + public ErrorListItem(String code, String msg) { + this.code = code; + this.message = msg; + } + + + /** + * Description about individual errors occurred + **/ + @ApiModelProperty(required = true, value = "Description about individual errors occurred") + @JsonProperty("message") + public String getMessage() { + return message; + } + public void setMessage(String message) { + this.message = message; + } + + + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append("errorItem {\n"); + + sb.append(" code: ").append(code).append("\n"); + sb.append(" message: ").append(message).append("\n"); + sb.append("}\n"); + return sb.toString(); + } + +} diff --git a/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.api/src/main/java/org/wso2/carbon/mdm/services/android/bean/ErrorResponse.java b/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.api/src/main/java/org/wso2/carbon/mdm/services/android/bean/ErrorResponse.java new file mode 100644 index 0000000000..7680e36641 --- /dev/null +++ b/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.api/src/main/java/org/wso2/carbon/mdm/services/android/bean/ErrorResponse.java @@ -0,0 +1,193 @@ +/* + * 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.mdm.services.android.bean; + +import com.fasterxml.jackson.annotation.JsonProperty; +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; + +import java.util.ArrayList; +import java.util.List; + +@ApiModel(description = "") +public class ErrorResponse { + + private Long code = null; + private String message = null; + private String description = null; + private String moreInfo = null; + private List errorItems = new ArrayList<>(); + + private ErrorResponse() { + } + + @JsonProperty(value = "code") + @ApiModelProperty(required = true, value = "") + public Long getCode() { + return code; + } + + public void setCode(Long code) { + this.code = code; + } + + @JsonProperty(value = "message") + @ApiModelProperty(required = true, value = "ErrorResponse message.") + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + + @JsonProperty(value = "description") + @ApiModelProperty(value = "A detail description about the error message.") + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + @JsonProperty(value = "moreInfo") + @ApiModelProperty(value = "Preferably an url with more details about the error.") + public String getMoreInfo() { + return moreInfo; + } + + public void setMoreInfo(String moreInfo) { + this.moreInfo = moreInfo; + } + + public void addErrorListItem(ErrorListItem item) { + this.errorItems.add(item); + } + + /** + * If there are more than one error list them out. \nFor example, list out validation errors by each field. + */ + @JsonProperty(value = "errorItems") + @ApiModelProperty(value = "If there are more than one error list them out. \n" + + "For example, list out validation errors by each field.") + public List getErrorItems() { + return errorItems; + } + + public void setErrorItems(List error) { + this.errorItems = error; + } + + @Override + public String toString() { +// StringBuilder sb = new StringBuilder(); +// sb.append("{"); +// boolean cont = false; +// if (code != null) { +// cont = true; +// sb.append(" \"code\": ").append(code); +// } +// if (message != null) { +// if (cont) { +// sb.append(","); +// } +// cont = true; +// sb.append(" \"message\": \"").append(message).append("\""); +// } +// if (description != null) { +// if (cont) { +// sb.append(","); +// } +// cont = true; +// sb.append(" \"description\": ").append(description).append("\""); +// } +// if (moreInfo != null) { +// if (cont) { +// sb.append(","); +// } +// cont = true; +// sb.append(" \"moreInfo\": \"").append(moreInfo).append("\""); +// } +// if (error != null && error.size() > 0) { +// if (cont) { +// sb.append(","); +// } +// sb.append(" \"errorItems\": ").append(error); +// } +// sb.append("}"); +// return sb.toString(); + return null; + } + + public static class ErrorResponseBuilder { + + private Long code = null; + private String message = null; + private String description = null; + private String moreInfo = null; + private List error; + + + public ErrorResponseBuilder() { + this.error = new ArrayList<>(); + } + + public ErrorResponseBuilder setCode(long code) { + this.code = code; + return this; + } + + public ErrorResponseBuilder setMessage(String message) { + this.message = message; + return this; + } + + public ErrorResponseBuilder setDescription(String description) { + this.description = description; + return this; + } + + public ErrorResponseBuilder setMoreInfo(String moreInfo) { + this.moreInfo = moreInfo; + return this; + } + + public ErrorResponseBuilder addErrorItem(String code, String msg) { + ErrorListItem item = new ErrorListItem(); + item.setCode(code); + item.setMessage(msg); + this.error.add(item); + return this; + } + + public ErrorResponse build() { + ErrorResponse errorResponse = new ErrorResponse(); + errorResponse.setCode(code); + errorResponse.setMessage(message); + errorResponse.setErrorItems(error); + errorResponse.setDescription(description); + errorResponse.setMoreInfo(moreInfo); + return errorResponse; + } + } + +} + + diff --git a/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.api/src/main/java/org/wso2/carbon/mdm/services/android/exception/UnexpectedServerErrorException.java b/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.api/src/main/java/org/wso2/carbon/mdm/services/android/exception/UnexpectedServerErrorException.java new file mode 100644 index 0000000000..6db5b0e69c --- /dev/null +++ b/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.api/src/main/java/org/wso2/carbon/mdm/services/android/exception/UnexpectedServerErrorException.java @@ -0,0 +1,35 @@ +/* + * 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.mdm.services.android.exception; + + +import org.wso2.carbon.mdm.services.android.bean.ErrorResponse; + +import javax.ws.rs.WebApplicationException; +import javax.ws.rs.core.Response; + +public class UnexpectedServerErrorException extends WebApplicationException { + + private static final long serialVersionUID = 147943579458906890L; + + public UnexpectedServerErrorException(ErrorResponse error) { + super(Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(error).build()); + } + +} diff --git a/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.api/src/main/java/org/wso2/carbon/mdm/services/android/services/impl/DeviceManagementAdminServiceImpl.java b/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.api/src/main/java/org/wso2/carbon/mdm/services/android/services/impl/DeviceManagementAdminServiceImpl.java index f4fb1ed25e..2c24cbc024 100644 --- a/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.api/src/main/java/org/wso2/carbon/mdm/services/android/services/impl/DeviceManagementAdminServiceImpl.java +++ b/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.api/src/main/java/org/wso2/carbon/mdm/services/android/services/impl/DeviceManagementAdminServiceImpl.java @@ -27,10 +27,10 @@ import org.wso2.carbon.device.mgt.core.operation.mgt.CommandOperation; import org.wso2.carbon.device.mgt.core.operation.mgt.ProfileOperation; import org.wso2.carbon.mdm.services.android.bean.*; import org.wso2.carbon.mdm.services.android.bean.wrapper.*; +import org.wso2.carbon.mdm.services.android.exception.UnexpectedServerErrorException; import org.wso2.carbon.mdm.services.android.services.DeviceManagementAdminService; import org.wso2.carbon.mdm.services.android.util.AndroidAPIUtils; import org.wso2.carbon.mdm.services.android.util.AndroidConstants; -import org.wso2.carbon.mdm.services.android.util.Message; import javax.ws.rs.Consumes; import javax.ws.rs.POST; @@ -72,11 +72,13 @@ public class DeviceManagementAdminServiceImpl implements DeviceManagementAdminSe } catch (OperationManagementException e) { String errorMessage = "Issue in retrieving operation management service instance"; log.error(errorMessage, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorMessage).build(); + throw new UnexpectedServerErrorException( + new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(errorMessage).build()); } catch (DeviceManagementException e) { String errorMessage = "Issue in retrieving device management service instance"; log.error(errorMessage, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorMessage).build(); + throw new UnexpectedServerErrorException( + new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(errorMessage).build()); } } @@ -97,11 +99,13 @@ public class DeviceManagementAdminServiceImpl implements DeviceManagementAdminSe } catch (OperationManagementException e) { String errorMessage = "Issue in retrieving operation management service instance"; log.error(errorMessage, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorMessage).build(); + throw new UnexpectedServerErrorException( + new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(errorMessage).build()); } catch (DeviceManagementException e) { String errorMessage = "Issue in retrieving device management service instance"; log.error(errorMessage, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorMessage).build(); + throw new UnexpectedServerErrorException( + new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(errorMessage).build()); } } @@ -121,11 +125,13 @@ public class DeviceManagementAdminServiceImpl implements DeviceManagementAdminSe } catch (OperationManagementException e) { String errorMessage = "Issue in retrieving operation management service instance"; log.error(errorMessage, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorMessage).build(); + throw new UnexpectedServerErrorException( + new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(errorMessage).build()); } catch (DeviceManagementException e) { String errorMessage = "Issue in retrieving device management service instance"; log.error(errorMessage, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorMessage).build(); + throw new UnexpectedServerErrorException( + new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(errorMessage).build()); } } @@ -145,11 +151,13 @@ public class DeviceManagementAdminServiceImpl implements DeviceManagementAdminSe } catch (OperationManagementException e) { String errorMessage = "Issue in retrieving operation management service instance."; log.error(errorMessage, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorMessage).build(); + throw new UnexpectedServerErrorException( + new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(errorMessage).build()); } catch (DeviceManagementException e) { String errorMessage = "Issue in retrieving device management service instance"; log.error(errorMessage, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorMessage).build(); + throw new UnexpectedServerErrorException( + new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(errorMessage).build()); } } @@ -176,11 +184,13 @@ public class DeviceManagementAdminServiceImpl implements DeviceManagementAdminSe } catch (OperationManagementException e) { String errorMessage = "Issue in retrieving operation management service instance"; log.error(errorMessage, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorMessage).build(); + throw new UnexpectedServerErrorException( + new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(errorMessage).build()); } catch (DeviceManagementException e) { String errorMessage = "Issue in retrieving device management service instance"; log.error(errorMessage, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorMessage).build(); + throw new UnexpectedServerErrorException( + new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(errorMessage).build()); } } @@ -203,10 +213,9 @@ public class DeviceManagementAdminServiceImpl implements DeviceManagementAdminSe return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorMessage).build(); } catch (DeviceManagementException e) { String errorMessage = "Issue in retrieving device management service instance"; - - log.error(errorMessage, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorMessage).build(); + throw new UnexpectedServerErrorException( + new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(errorMessage).build()); } } @@ -226,11 +235,13 @@ public class DeviceManagementAdminServiceImpl implements DeviceManagementAdminSe } catch (OperationManagementException e) { String errorMessage = "Issue in retrieving operation management service instance"; log.error(errorMessage, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorMessage).build(); + throw new UnexpectedServerErrorException( + new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(errorMessage).build()); } catch (DeviceManagementException e) { String errorMessage = "Issue in retrieving device management service instance"; log.error(errorMessage, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorMessage).build(); + throw new UnexpectedServerErrorException( + new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(errorMessage).build()); } } @@ -259,11 +270,13 @@ public class DeviceManagementAdminServiceImpl implements DeviceManagementAdminSe } catch (OperationManagementException e) { String errorMessage = "Issue in retrieving operation management service instance"; log.error(errorMessage, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorMessage).build(); + throw new UnexpectedServerErrorException( + new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(errorMessage).build()); } catch (DeviceManagementException e) { String errorMessage = "Issue in retrieving device management service instance"; log.error(errorMessage, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorMessage).build(); + throw new UnexpectedServerErrorException( + new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(errorMessage).build()); } } @@ -283,11 +296,13 @@ public class DeviceManagementAdminServiceImpl implements DeviceManagementAdminSe } catch (OperationManagementException e) { String errorMessage = "Issue in retrieving operation management service instance"; log.error(errorMessage, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorMessage).build(); + throw new UnexpectedServerErrorException( + new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(errorMessage).build()); } catch (DeviceManagementException e) { String errorMessage = "Issue in retrieving device management service instance"; log.error(errorMessage, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorMessage).build(); + throw new UnexpectedServerErrorException( + new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(errorMessage).build()); } } @@ -307,11 +322,13 @@ public class DeviceManagementAdminServiceImpl implements DeviceManagementAdminSe } catch (OperationManagementException e) { String errorMessage = "Issue in retrieving operation management service instance"; log.error(errorMessage, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorMessage).build(); + throw new UnexpectedServerErrorException( + new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(errorMessage).build()); } catch (DeviceManagementException e) { String errorMessage = "Issue in retrieving device management service instance"; log.error(errorMessage, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorMessage).build(); + throw new UnexpectedServerErrorException( + new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(errorMessage).build()); } } @@ -331,11 +348,13 @@ public class DeviceManagementAdminServiceImpl implements DeviceManagementAdminSe } catch (OperationManagementException e) { String errorMessage = "Issue in retrieving operation management service instance"; log.error(errorMessage, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorMessage).build(); + throw new UnexpectedServerErrorException( + new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(errorMessage).build()); } catch (DeviceManagementException e) { String errorMessage = "Issue in retrieving device management service instance"; log.error(errorMessage, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorMessage).build(); + throw new UnexpectedServerErrorException( + new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(errorMessage).build()); } } @@ -356,11 +375,13 @@ public class DeviceManagementAdminServiceImpl implements DeviceManagementAdminSe } catch (OperationManagementException e) { String errorMessage = "Issue in retrieving operation management service instance"; log.error(errorMessage, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorMessage).build(); + throw new UnexpectedServerErrorException( + new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(errorMessage).build()); } catch (DeviceManagementException e) { String errorMessage = "Issue in retrieving device management service instance"; log.error(errorMessage, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorMessage).build(); + throw new UnexpectedServerErrorException( + new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(errorMessage).build()); } } @@ -390,11 +411,13 @@ public class DeviceManagementAdminServiceImpl implements DeviceManagementAdminSe } catch (OperationManagementException e) { String errorMessage = "Issue in retrieving operation management service instance"; log.error(errorMessage, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorMessage).build(); + throw new UnexpectedServerErrorException( + new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(errorMessage).build()); } catch (DeviceManagementException e) { String errorMessage = "Issue in retrieving device management service instance"; log.error(errorMessage, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorMessage).build(); + throw new UnexpectedServerErrorException( + new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(errorMessage).build()); } } @@ -425,11 +448,13 @@ public class DeviceManagementAdminServiceImpl implements DeviceManagementAdminSe } catch (OperationManagementException e) { String errorMessage = "Issue in retrieving operation management service instance"; log.error(errorMessage, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorMessage).build(); + throw new UnexpectedServerErrorException( + new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(errorMessage).build()); } catch (DeviceManagementException e) { String errorMessage = "Issue in retrieving device management service instance"; log.error(errorMessage, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorMessage).build(); + throw new UnexpectedServerErrorException( + new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(errorMessage).build()); } } @@ -460,11 +485,13 @@ public class DeviceManagementAdminServiceImpl implements DeviceManagementAdminSe } catch (OperationManagementException e) { String errorMessage = "Issue in retrieving operation management service instance"; log.error(errorMessage, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorMessage).build(); + throw new UnexpectedServerErrorException( + new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(errorMessage).build()); } catch (DeviceManagementException e) { String errorMessage = "Issue in retrieving device management service instance"; log.error(errorMessage, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorMessage).build(); + throw new UnexpectedServerErrorException( + new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(errorMessage).build()); } } @@ -496,11 +523,13 @@ public class DeviceManagementAdminServiceImpl implements DeviceManagementAdminSe } catch (OperationManagementException e) { String errorMessage = "Issue in retrieving operation management service instance"; log.error(errorMessage, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorMessage).build(); + throw new UnexpectedServerErrorException( + new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(errorMessage).build()); } catch (DeviceManagementException e) { String errorMessage = "Issue in retrieving device management service instance"; log.error(errorMessage, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorMessage).build(); + throw new UnexpectedServerErrorException( + new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(errorMessage).build()); } } @@ -530,11 +559,13 @@ public class DeviceManagementAdminServiceImpl implements DeviceManagementAdminSe } catch (OperationManagementException e) { String errorMessage = "Issue in retrieving operation management service instance"; log.error(errorMessage, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorMessage).build(); + throw new UnexpectedServerErrorException( + new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(errorMessage).build()); } catch (DeviceManagementException e) { String errorMessage = "Issue in retrieving device management service instance"; log.error(errorMessage, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorMessage).build(); + throw new UnexpectedServerErrorException( + new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(errorMessage).build()); } } @@ -564,11 +595,13 @@ public class DeviceManagementAdminServiceImpl implements DeviceManagementAdminSe } catch (OperationManagementException e) { String errorMessage = "Issue in retrieving operation management service instance"; log.error(errorMessage, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorMessage).build(); + throw new UnexpectedServerErrorException( + new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(errorMessage).build()); } catch (DeviceManagementException e) { String errorMessage = "Issue in retrieving device management service instance"; log.error(errorMessage, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorMessage).build(); + throw new UnexpectedServerErrorException( + new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(errorMessage).build()); } } @@ -599,11 +632,13 @@ public class DeviceManagementAdminServiceImpl implements DeviceManagementAdminSe } catch (OperationManagementException e) { String errorMessage = "Issue in retrieving operation management service instance"; log.error(errorMessage, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorMessage).build(); + throw new UnexpectedServerErrorException( + new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(errorMessage).build()); } catch (DeviceManagementException e) { String errorMessage = "Issue in retrieving device management service instance"; log.error(errorMessage, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorMessage).build(); + throw new UnexpectedServerErrorException( + new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(errorMessage).build()); } } @@ -635,11 +670,13 @@ public class DeviceManagementAdminServiceImpl implements DeviceManagementAdminSe } catch (OperationManagementException e) { String errorMessage = "Issue in retrieving operation management service instance"; log.error(errorMessage, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorMessage).build(); + throw new UnexpectedServerErrorException( + new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(errorMessage).build()); } catch (DeviceManagementException e) { String errorMessage = "Issue in retrieving device management service instance"; log.error(errorMessage, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorMessage).build(); + throw new UnexpectedServerErrorException( + new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(errorMessage).build()); } } @@ -670,11 +707,13 @@ public class DeviceManagementAdminServiceImpl implements DeviceManagementAdminSe } catch (OperationManagementException e) { String errorMessage = "Issue in retrieving operation management service instance"; log.error(errorMessage, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorMessage).build(); + throw new UnexpectedServerErrorException( + new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(errorMessage).build()); } catch (DeviceManagementException e) { String errorMessage = "Issue in retrieving device management service instance"; log.error(errorMessage, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorMessage).build(); + throw new UnexpectedServerErrorException( + new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(errorMessage).build()); } } @@ -705,11 +744,13 @@ public class DeviceManagementAdminServiceImpl implements DeviceManagementAdminSe } catch (OperationManagementException e) { String errorMessage = "Issue in retrieving operation management service instance"; log.error(errorMessage, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorMessage).build(); + throw new UnexpectedServerErrorException( + new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(errorMessage).build()); } catch (DeviceManagementException e) { String errorMessage = "Issue in retrieving device management service instance"; log.error(errorMessage, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorMessage).build(); + throw new UnexpectedServerErrorException( + new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(errorMessage).build()); } } @@ -740,11 +781,13 @@ public class DeviceManagementAdminServiceImpl implements DeviceManagementAdminSe } catch (OperationManagementException e) { String errorMessage = "Issue in retrieving operation management service instance"; log.error(errorMessage, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorMessage).build(); + throw new UnexpectedServerErrorException( + new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(errorMessage).build()); } catch (DeviceManagementException e) { String errorMessage = "Issue in retrieving device management service instance"; log.error(errorMessage, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorMessage).build(); + throw new UnexpectedServerErrorException( + new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(errorMessage).build()); } } @@ -774,11 +817,13 @@ public class DeviceManagementAdminServiceImpl implements DeviceManagementAdminSe } catch (OperationManagementException e) { String errorMessage = "Issue in retrieving operation management service instance"; log.error(errorMessage, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorMessage).build(); + throw new UnexpectedServerErrorException( + new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(errorMessage).build()); } catch (DeviceManagementException e) { String errorMessage = "Issue in retrieving device management service instance"; log.error(errorMessage, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorMessage).build(); + throw new UnexpectedServerErrorException( + new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(errorMessage).build()); } } diff --git a/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.api/src/main/java/org/wso2/carbon/mdm/services/android/services/impl/DeviceManagementServiceImpl.java b/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.api/src/main/java/org/wso2/carbon/mdm/services/android/services/impl/DeviceManagementServiceImpl.java index 987d14f6dd..746e61d778 100644 --- a/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.api/src/main/java/org/wso2/carbon/mdm/services/android/services/impl/DeviceManagementServiceImpl.java +++ b/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.api/src/main/java/org/wso2/carbon/mdm/services/android/services/impl/DeviceManagementServiceImpl.java @@ -29,6 +29,8 @@ import org.wso2.carbon.device.mgt.common.app.mgt.ApplicationManagementException; import org.wso2.carbon.device.mgt.common.notification.mgt.NotificationManagementException; import org.wso2.carbon.device.mgt.common.operation.mgt.Operation; import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManagementException; +import org.wso2.carbon.mdm.services.android.bean.ErrorResponse; +import org.wso2.carbon.mdm.services.android.exception.UnexpectedServerErrorException; import org.wso2.carbon.mdm.services.android.services.DeviceManagementService; import org.wso2.carbon.mdm.services.android.util.AndroidAPIUtils; import org.wso2.carbon.mdm.services.android.util.AndroidConstants; @@ -67,7 +69,8 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { } catch (ApplicationManagementException e) { String msg = "Error occurred while modifying the application list."; 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()); } } @@ -100,23 +103,28 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { } catch (OperationManagementException e) { String msg = "Issue in retrieving operation management service instance"; 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 (PolicyComplianceException e) { String msg = "Issue in updating Monitoring operation"; 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) { String msg = "Issue in retrieving device management service instance"; 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 (ApplicationManagementException e) { String msg = "Issue in retrieving application management service instance"; 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 (NotificationManagementException e) { String msg = "Issue in retrieving Notification management service instance"; 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()); } List pendingOperations; @@ -177,13 +185,15 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { String msg = "Error occurred while enrolling the '" + device.getType() + "', which carries the id '" + device.getDeviceIdentifier() + "'"; 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 (PolicyManagementException e) { String msg = "Error occurred while enforcing default enrollment policy upon '" + device.getType() + "', which carries the id '" + device.getDeviceIdentifier() + "'"; 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()); } } @@ -205,7 +215,8 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { } catch (DeviceManagementException e) { String msg = "Error occurred while checking enrollment status of the device."; 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()); } } @@ -228,7 +239,8 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { String msg = "Error occurred while modifying enrollment of the Android device that carries the id '" + id + "'"; log.error(msg, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); + throw new UnexpectedServerErrorException( + new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build()); } } @@ -250,7 +262,8 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { } catch (DeviceManagementException e) { String msg = "Error occurred while dis-enrolling the Android device that carries the id '" + id + "'"; 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()); } } diff --git a/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.api/src/main/java/org/wso2/carbon/mdm/services/android/services/impl/DeviceTypeConfigurationServiceImpl.java b/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.api/src/main/java/org/wso2/carbon/mdm/services/android/services/impl/DeviceTypeConfigurationServiceImpl.java index e52f007648..6ec2c470b7 100644 --- a/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.api/src/main/java/org/wso2/carbon/mdm/services/android/services/impl/DeviceTypeConfigurationServiceImpl.java +++ b/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.api/src/main/java/org/wso2/carbon/mdm/services/android/services/impl/DeviceTypeConfigurationServiceImpl.java @@ -25,7 +25,8 @@ import org.wso2.carbon.device.mgt.common.DeviceManagementException; import org.wso2.carbon.device.mgt.common.configuration.mgt.ConfigurationEntry; import org.wso2.carbon.device.mgt.common.configuration.mgt.PlatformConfiguration; import org.wso2.carbon.device.mgt.common.license.mgt.License; -import org.wso2.carbon.mdm.services.android.exception.AndroidAgentException; +import org.wso2.carbon.mdm.services.android.bean.ErrorResponse; +import org.wso2.carbon.mdm.services.android.exception.UnexpectedServerErrorException; import org.wso2.carbon.mdm.services.android.services.DeviceTypeConfigurationService; import org.wso2.carbon.mdm.services.android.util.AndroidAPIUtils; import org.wso2.carbon.mdm.services.android.util.AndroidConstants; @@ -75,7 +76,8 @@ public class DeviceTypeConfigurationServiceImpl implements DeviceTypeConfigurati } catch (DeviceManagementException e) { msg = "Error occurred while retrieving the Android tenant configuration"; 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(PlatformConfiguration).build(); } @@ -114,7 +116,8 @@ public class DeviceTypeConfigurationServiceImpl implements DeviceTypeConfigurati } catch (DeviceManagementException e) { msg = "Error occurred while modifying configuration settings of Android platform"; 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.CREATED).entity(responseMsg).build(); } @@ -134,7 +137,8 @@ public class DeviceTypeConfigurationServiceImpl implements DeviceTypeConfigurati } catch (DeviceManagementException e) { String msg = "Error occurred while retrieving the license configured for Android device enrolment"; 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((license == null) ? null : license.getText()).build(); } diff --git a/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.api/src/main/java/org/wso2/carbon/mdm/services/android/services/impl/EventReceiverServiceImpl.java b/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.api/src/main/java/org/wso2/carbon/mdm/services/android/services/impl/EventReceiverServiceImpl.java index 5b9d0c592c..b7bb1e5dc0 100644 --- a/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.api/src/main/java/org/wso2/carbon/mdm/services/android/services/impl/EventReceiverServiceImpl.java +++ b/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.api/src/main/java/org/wso2/carbon/mdm/services/android/services/impl/EventReceiverServiceImpl.java @@ -23,19 +23,18 @@ import org.apache.commons.logging.LogFactory; import org.wso2.carbon.analytics.datasource.commons.exception.AnalyticsException; import org.wso2.carbon.device.mgt.analytics.data.publisher.exception.DataPublisherConfigurationException; import org.wso2.carbon.mdm.services.android.bean.DeviceState; +import org.wso2.carbon.mdm.services.android.bean.ErrorResponse; import org.wso2.carbon.mdm.services.android.bean.wrapper.EventBeanWrapper; +import org.wso2.carbon.mdm.services.android.exception.UnexpectedServerErrorException; import org.wso2.carbon.mdm.services.android.services.EventReceiverService; import org.wso2.carbon.mdm.services.android.util.AndroidAPIUtils; import org.wso2.carbon.mdm.services.android.util.Message; import javax.ws.rs.*; -import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; import java.util.List; @Path("/events") -@Produces(MediaType.APPLICATION_JSON) -@Consumes(MediaType.APPLICATION_JSON) public class EventReceiverServiceImpl implements EventReceiverService { private static final String EVENT_STREAM_DEFINITION = "android_agent"; @@ -44,7 +43,7 @@ public class EventReceiverServiceImpl implements EventReceiverService { @POST @Path("/publish") @Override - public Response publishEvents(EventBeanWrapper eventBeanWrapper) { + public Response publishEvents(EventBeanWrapper eventBeanWrapper) { if (log.isDebugEnabled()) { log.debug("Invoking Android device even logging."); } @@ -65,7 +64,8 @@ public class EventReceiverServiceImpl implements EventReceiverService { } catch (DataPublisherConfigurationException e) { String msg = "Error occurred while getting the Data publisher Service."; 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()); } } @@ -105,7 +105,8 @@ public class EventReceiverServiceImpl implements EventReceiverService { } catch (AnalyticsException e) { String msg = "Error occurred while getting published events for specific device: " + deviceId + "."; 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()); } } @@ -133,7 +134,8 @@ public class EventReceiverServiceImpl implements EventReceiverService { String msg = "Error occurred while getting published events for specific " + "Device: " + deviceId + " on given Date."; 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()); } } @@ -158,7 +160,8 @@ public class EventReceiverServiceImpl implements EventReceiverService { String msg = "Error occurred while getting published events for specific " + "Device: " + deviceId + "and given device Type."; 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()); } } diff --git a/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.api/src/main/java/org/wso2/carbon/mdm/services/android/util/AndroidAPIUtils.java b/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.api/src/main/java/org/wso2/carbon/mdm/services/android/util/AndroidAPIUtils.java index 6cbb5645ca..6b55b844fb 100644 --- a/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.api/src/main/java/org/wso2/carbon/mdm/services/android/util/AndroidAPIUtils.java +++ b/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.api/src/main/java/org/wso2/carbon/mdm/services/android/util/AndroidAPIUtils.java @@ -47,7 +47,6 @@ import org.wso2.carbon.device.mgt.core.device.details.mgt.DeviceInformationManag import org.wso2.carbon.device.mgt.core.search.mgt.impl.Utils; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; import org.wso2.carbon.mdm.services.android.bean.DeviceState; -import org.wso2.carbon.mdm.services.android.exception.BadRequestException; import org.wso2.carbon.policy.mgt.common.monitor.PolicyComplianceException; import org.wso2.carbon.policy.mgt.core.PolicyManagerService; @@ -140,8 +139,7 @@ public class AndroidAPIUtils { // } // } if (!deviceIDHolder.getErrorDeviceIdList().isEmpty()) { - return javax.ws.rs.core.Response.status(AndroidConstants.StatusCodes. - MULTI_STATUS_HTTP_CODE).entity(deviceUtils. + return javax.ws.rs.core.Response.status(Response.Status.BAD_REQUEST).entity(deviceUtils. convertErrorMapIntoErrorMessage(deviceIDHolder.getErrorDeviceIdList())).build(); } return Response.status(Response.Status.CREATED).entity(activity).build(); diff --git a/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.api/src/main/java/org/wso2/carbon/mdm/services/android/util/AndroidDeviceUtils.java b/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.api/src/main/java/org/wso2/carbon/mdm/services/android/util/AndroidDeviceUtils.java index acfdf83a58..f2ed96cdd4 100644 --- a/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.api/src/main/java/org/wso2/carbon/mdm/services/android/util/AndroidDeviceUtils.java +++ b/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.api/src/main/java/org/wso2/carbon/mdm/services/android/util/AndroidDeviceUtils.java @@ -20,9 +20,6 @@ package org.wso2.carbon.mdm.services.android.util; import org.apache.commons.lang.StringUtils; import org.wso2.carbon.device.mgt.common.*; -import org.wso2.carbon.mdm.services.android.exception.BadRequestException; - -import javax.ws.rs.core.MediaType; import java.util.ArrayList; import java.util.List;