From 4ccf018bb365f6293de3e1a710ec2ecb17b69e1a Mon Sep 17 00:00:00 2001 From: hasuniea Date: Fri, 8 Jul 2016 15:26:20 +0530 Subject: [PATCH] adding validations --- .../pom.xml | 8 + .../bean/AndroidPlatformConfiguration.java | 85 +++++ .../android/bean/ApplicationInstallation.java | 6 + .../bean/ApplicationUninstallation.java | 65 ++-- .../android/bean/ApplicationUpdate.java | 6 + .../android/bean/BlacklistApplications.java | 4 + .../services/android/bean/ErrorResponse.java | 3 - .../services/android/bean/PasscodePolicy.java | 7 + .../bean/wrapper/AndroidApplication.java | 206 +++++++++++ .../android/bean/wrapper/AndroidDevice.java | 221 ++++++++++++ .../bean/wrapper/AndroidDeviceInfo.java | 321 ++++++++++++++++++ .../ApplicationInstallationBeanWrapper.java | 1 - .../ApplicationUninstallationBeanWrapper.java | 34 +- .../BlacklistApplicationsBeanWrapper.java | 32 +- .../bean/wrapper/DeviceEnrollmentInfo.java | 126 +++++++ .../bean/wrapper/EventBeanWrapper.java | 6 + .../android/bean/wrapper/VpnBeanWrapper.java | 4 + .../bean/wrapper/WipeDataBeanWrapper.java | 30 +- .../exception/BadRequestException.java | 3 - .../services/android/exception/ErrorDTO.java | 86 +++++ .../android/exception/ForbiddenException.java | 51 +++ .../exception/GlobalThrowableMapper.java | 101 ++++++ .../android/exception/NotFoundException.java | 15 +- .../ParameterValidationException.java | 56 +++ .../UnexpectedServerErrorException.java | 17 +- .../exception/ValidationInterceptor.java | 118 +++++++ .../DeviceManagementAdminService.java | 14 +- .../services/DeviceManagementService.java | 25 +- .../DeviceTypeConfigurationService.java | 3 +- .../services/EventReceiverService.java | 9 + .../DeviceManagementAdminServiceImpl.java | 4 +- .../impl/DeviceManagementServiceImpl.java | 70 +++- .../DeviceTypeConfigurationServiceImpl.java | 6 +- .../impl/EventReceiverServiceImpl.java | 15 +- .../android/util/AndroidConstants.java | 9 + .../android/util/AndroidDeviceUtils.java | 52 +++ .../src/main/webapp/WEB-INF/cxf-servlet.xml | 20 +- pom.xml | 7 + 38 files changed, 1727 insertions(+), 119 deletions(-) create mode 100644 components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.api/src/main/java/org/wso2/carbon/mdm/services/android/bean/AndroidPlatformConfiguration.java create mode 100644 components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.api/src/main/java/org/wso2/carbon/mdm/services/android/bean/wrapper/AndroidApplication.java create mode 100644 components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.api/src/main/java/org/wso2/carbon/mdm/services/android/bean/wrapper/AndroidDevice.java create mode 100644 components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.api/src/main/java/org/wso2/carbon/mdm/services/android/bean/wrapper/AndroidDeviceInfo.java create mode 100644 components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.api/src/main/java/org/wso2/carbon/mdm/services/android/bean/wrapper/DeviceEnrollmentInfo.java create mode 100644 components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.api/src/main/java/org/wso2/carbon/mdm/services/android/exception/ErrorDTO.java create mode 100644 components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.api/src/main/java/org/wso2/carbon/mdm/services/android/exception/ForbiddenException.java create mode 100644 components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.api/src/main/java/org/wso2/carbon/mdm/services/android/exception/GlobalThrowableMapper.java create mode 100644 components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.api/src/main/java/org/wso2/carbon/mdm/services/android/exception/ParameterValidationException.java create mode 100644 components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.api/src/main/java/org/wso2/carbon/mdm/services/android/exception/ValidationInterceptor.java diff --git a/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.api/pom.xml b/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.api/pom.xml index 1258094d9..cf45b7153 100644 --- a/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.api/pom.xml +++ b/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.api/pom.xml @@ -205,5 +205,13 @@ servlet-api provided + + org.hibernate + hibernate-validator + + + javax.ws.rs + javax.ws.rs-api + 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/AndroidPlatformConfiguration.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/AndroidPlatformConfiguration.java new file mode 100644 index 000000000..19623877e --- /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/AndroidPlatformConfiguration.java @@ -0,0 +1,85 @@ +/* + * 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 io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; +import org.wso2.carbon.device.mgt.common.configuration.mgt.ConfigurationEntry; + +import javax.validation.constraints.NotNull; +import javax.validation.constraints.Pattern; +import javax.validation.constraints.Size; +import javax.xml.bind.annotation.XmlAccessType; +import javax.xml.bind.annotation.XmlAccessorType; +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlRootElement; +import java.io.Serializable; +import java.util.List; + +@XmlRootElement( + name = "PlatformConfiguration" +) +@XmlAccessorType(XmlAccessType.NONE) +@ApiModel( + value = "PlatformConfiguration", + description = "This class carries all information related to a Tenant configuration" +) +public class AndroidPlatformConfiguration implements Serializable { + public static final int INVALID_NOTIFIER_FREQUENCY = -1; + @XmlElement( + name = "type" + ) + @ApiModelProperty( + name = "type", + value = "type of device", + required = true + ) + @NotNull + @Size(min = 2, max = 10) + @Pattern(regexp = "^[A-Za-z0-9]*$") + private String type; + @ApiModelProperty( + name = "configuration", + value = "List of Configuration Entries", + required = true + ) + @XmlElement( + name = "configuration" + ) + private List configuration; + + public AndroidPlatformConfiguration() { + } + + public String getType() { + return this.type; + } + + public void setType(String type) { + this.type = type; + } + + public List getConfiguration() { + return this.configuration; + } + + public void setConfiguration(List configuration) { + this.configuration = configuration; + } +} 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/ApplicationInstallation.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/ApplicationInstallation.java index b0ed37377..454dc905a 100644 --- a/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.api/src/main/java/org/wso2/carbon/mdm/services/android/bean/ApplicationInstallation.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/ApplicationInstallation.java @@ -21,6 +21,8 @@ package org.wso2.carbon.mdm.services.android.bean; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; +import javax.validation.constraints.Pattern; +import javax.validation.constraints.Size; import java.io.Serializable; /** @@ -31,9 +33,13 @@ import java.io.Serializable; public class ApplicationInstallation extends AndroidOperation implements Serializable { @ApiModelProperty(name = "appIdentifier", value = "Application Identifier", required = true) + @Size(min = 2, max = 45) + @Pattern(regexp = "^[A-Za-z0-9]*$") private String appIdentifier; @ApiModelProperty(name = "type", value = "Application type(Enterprise/Web/public)", required = true) + @Size(min = 2, max = 12) + @Pattern(regexp = "^[A-Za-z]*$") private String type; @ApiModelProperty(name = "url", value = "Application URL", required = true) 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/ApplicationUninstallation.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/ApplicationUninstallation.java index a946eedc7..45b98b47d 100644 --- a/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.api/src/main/java/org/wso2/carbon/mdm/services/android/bean/ApplicationUninstallation.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/ApplicationUninstallation.java @@ -21,6 +21,8 @@ package org.wso2.carbon.mdm.services.android.bean; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; +import javax.validation.constraints.Pattern; +import javax.validation.constraints.Size; import java.io.Serializable; /** @@ -31,47 +33,52 @@ import java.io.Serializable; public class ApplicationUninstallation extends AndroidOperation implements Serializable { @ApiModelProperty(name = "appIdentifier", value = "The package name of the application to be uninstalled.", required = true) - private String appIdentifier; + @Size(min = 2, max = 45) + @Pattern(regexp = "^[A-Za-z0-9]*$") + private String appIdentifier; @ApiModelProperty(name = "type", value = "The type of the application. The following types of applications " + - "are supported: enterprise, public and webapp.", required = true) - private String type; + "are supported: enterprise, public and webapp.", required = true) + @Size(min = 2, max = 12) + @Pattern(regexp = "^[A-Za-z]*$") + private String type; @ApiModelProperty(name = "url", value = "The URL of the application.", required = true) - private String url; + private String url; @ApiModelProperty(name = "name", value = "The name of the application.", required = true) - private String name; + @Size(min = 2, max = 45) + private String name; - public String getAppIdentifier() { - return appIdentifier; - } + public String getAppIdentifier() { + return appIdentifier; + } - public void setAppIdentifier(String appIdentifier) { - this.appIdentifier = appIdentifier; - } + public void setAppIdentifier(String appIdentifier) { + this.appIdentifier = appIdentifier; + } - public String getType() { - return type; - } + public String getType() { + return type; + } - public void setType(String type) { - this.type = type; - } + public void setType(String type) { + this.type = type; + } - public String getUrl() { - return url; - } + public String getUrl() { + return url; + } - public void setUrl(String url) { - this.url = url; - } + public void setUrl(String url) { + this.url = url; + } - public String getName() { - return name; - } + public String getName() { + return name; + } - public void setName(String name) { - this.name = name; - } + public void setName(String name) { + this.name = name; + } } 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/ApplicationUpdate.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/ApplicationUpdate.java index f732f74a9..14f7c31db 100644 --- a/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.api/src/main/java/org/wso2/carbon/mdm/services/android/bean/ApplicationUpdate.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/ApplicationUpdate.java @@ -21,6 +21,8 @@ package org.wso2.carbon.mdm.services.android.bean; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; +import javax.validation.constraints.Pattern; +import javax.validation.constraints.Size; import java.io.Serializable; /** @@ -32,9 +34,13 @@ public class ApplicationUpdate extends AndroidOperation implements Serializable @ApiModelProperty(name = "appIdentifier", value = "The package name of the application " + "to be update.", required = true) + @Size(min = 2, max = 45) + @Pattern(regexp = "^[A-Za-z0-9]*$") private String appIdentifier; @ApiModelProperty(name = "type", value = "The type of the application. The following types of applications " + "are supported: enterprise, public and webapp.", required = true) + @Size(min = 2, max = 12) + @Pattern(regexp = "^[A-Za-z]*$") private String type; @ApiModelProperty(name = "url", value = "The URL of the application.", required = true) private String url; 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/BlacklistApplications.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/BlacklistApplications.java index 665735b51..97c1ae5de 100644 --- a/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.api/src/main/java/org/wso2/carbon/mdm/services/android/bean/BlacklistApplications.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/BlacklistApplications.java @@ -21,6 +21,8 @@ package org.wso2.carbon.mdm.services.android.bean; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; +import javax.validation.constraints.Pattern; +import javax.validation.constraints.Size; import java.io.Serializable; import java.util.List; @@ -33,6 +35,8 @@ public class BlacklistApplications extends AndroidOperation implements Serializa @ApiModelProperty(name = "appIdentifiers", value = "A list of application package names to be blacklisted.", required = true) + @Size(min = 2, max = 45) + @Pattern(regexp = "^[A-Za-z0-9]*$") private List appIdentifiers; public List getAppIdentifier() { 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 index 7680e3664..457782ade 100644 --- 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 @@ -34,9 +34,6 @@ public class ErrorResponse { private String moreInfo = null; private List errorItems = new ArrayList<>(); - private ErrorResponse() { - } - @JsonProperty(value = "code") @ApiModelProperty(required = true, value = "") public Long getCode() { 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/PasscodePolicy.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/PasscodePolicy.java index 0b1f93743..c2e48ce70 100644 --- a/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.api/src/main/java/org/wso2/carbon/mdm/services/android/bean/PasscodePolicy.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/PasscodePolicy.java @@ -21,6 +21,7 @@ package org.wso2.carbon.mdm.services.android.bean; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; +import javax.validation.constraints.Max; import java.io.Serializable; /** @@ -34,18 +35,24 @@ public class PasscodePolicy extends AndroidOperation implements Serializable { + " his/her device passcode incorrectly. EMM will take different courses of action based on the" + " OS when the failed attempts exceed the maximum failed attempts. Android devices will be " + "automatically reset to the original factory settings", required = true) + + @Max(10) private int maxFailedAttempts; @ApiModelProperty(name = "minLength", value = "The minimum number of alphanumerical values that the " + "end-user can enter as his/her passcode", required = true) + @Max(15) private int minLength; @ApiModelProperty(name = "pinHistory", value = "The end-user will not be allowed to reuse a passcode that" + " he/she previously entered until he/she exceeds the set pin history length", required = true) + @Max(50) private int pinHistory; @ApiModelProperty(name = "minComplexChars", value = "The minimum number of special characters that the " + "end-user will have to enter in his/her passcode", required = true) + @Max(5) private int minComplexChars; @ApiModelProperty(name = "maxPINAgeInDays", value = "The number of days after which the device owner has" + " to change his/her passcode", required = true) + @Max(730) private int maxPINAgeInDays; @ApiModelProperty(name = "requireAlphanumeric", value = "Whether or not it is mandatory for the end-user" + " to have a mix of digits and characters in his/her passcode", required = true) 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/wrapper/AndroidApplication.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/wrapper/AndroidApplication.java new file mode 100644 index 000000000..8dee1a60d --- /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/wrapper/AndroidApplication.java @@ -0,0 +1,206 @@ +package org.wso2.carbon.mdm.services.android.bean.wrapper; + +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; + +import java.io.Serializable; +import java.util.Properties; + +@ApiModel( + value = "Application", + description = "This class carries all information related application" +) +public class AndroidApplication implements Serializable{ + private static final long serialVersionUID = 1998101711L; + @ApiModelProperty( + name = "id", + value = "The ID given to the application when it is stored in the EMM database", + required = true + ) + private int id; + @ApiModelProperty( + name = "platform", + value = "The mobile device platform. It can be android, ios or windows", + required = true + ) + private String platform; + @ApiModelProperty( + name = "category", + value = "The application category", + required = true + ) + private String category; + @ApiModelProperty( + name = "name", + value = "The application\'s name", + required = true + ) + private String name; + private String locationUrl; + @ApiModelProperty( + name = "imageUrl", + value = "The icon url of the application", + required = true + ) + private String imageUrl; + @ApiModelProperty( + name = "version", + value = "The application\'s version", + required = true + ) + private String version; + @ApiModelProperty( + name = "type", + value = "The application type", + required = true + ) + private String type; + @ApiModelProperty( + name = "appProperties", + value = "The properties of the application", + required = true + ) + private Properties appProperties; + @ApiModelProperty( + name = "applicationIdentifier", + value = "The application identifier", + required = true + ) + private String applicationIdentifier; + @ApiModelProperty( + name = "memoryUsage", + value = "Amount of memory used by the application", + required = true + ) + private int memoryUsage; + @ApiModelProperty( + name = "isActive", + value = "Is the application actively running", + required = true + ) + private boolean isActive; + + public AndroidApplication() { + } + + public String getType() { + return this.type; + } + + public void setType(String type) { + this.type = type; + } + + public String getName() { + return this.name; + } + + public void setName(String name) { + this.name = name; + } + + public int getId() { + return this.id; + } + + public void setId(int id) { + this.id = id; + } + + public String getLocationUrl() { + return this.locationUrl; + } + + public void setLocationUrl(String locationUrl) { + this.locationUrl = locationUrl; + } + + public String getImageUrl() { + return this.imageUrl; + } + + public void setImageUrl(String imageUrl) { + this.imageUrl = imageUrl; + } + + public String getVersion() { + return this.version; + } + + public void setVersion(String version) { + this.version = version; + } + + public String getPlatform() { + return this.platform; + } + + public void setPlatform(String platform) { + this.platform = platform; + } + + public String getCategory() { + return this.category; + } + + public void setCategory(String category) { + this.category = category; + } + + public String getApplicationIdentifier() { + return this.applicationIdentifier; + } + + public void setApplicationIdentifier(String applicationIdentifier) { + this.applicationIdentifier = applicationIdentifier; + } + + public int getMemoryUsage() { + return this.memoryUsage; + } + + public void setMemoryUsage(int memoryUsage) { + this.memoryUsage = memoryUsage; + } + + public boolean equals(Object o) { + if(this == o) { + return true; + } else if(o != null && this.getClass() == o.getClass()) { + AndroidApplication that = (AndroidApplication)o; + if(this.applicationIdentifier != null) { + if(!this.applicationIdentifier.equals(that.applicationIdentifier)) { + return false; + } + } else if(that.applicationIdentifier != null) { + return false; + } + + return true; + } else { + return false; + } + } + + public int hashCode() { + int result = this.id; + result = 31 * result + (this.applicationIdentifier != null?this.applicationIdentifier.hashCode():0); + return result; + } + + public Properties getAppProperties() { + return this.appProperties; + } + + public void setAppProperties(Properties appProperties) { + this.appProperties = appProperties; + } + + public boolean isActive() { + return this.isActive; + } + + public void setActive(boolean active) { + this.isActive = active; + } +} 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/wrapper/AndroidDevice.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/wrapper/AndroidDevice.java new file mode 100644 index 000000000..cbd0f0339 --- /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/wrapper/AndroidDevice.java @@ -0,0 +1,221 @@ +/* + * 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.wrapper; + +import io.swagger.annotations.ApiModelProperty; +import org.wso2.carbon.device.mgt.common.Device; +import org.wso2.carbon.device.mgt.common.EnrolmentInfo; +import org.wso2.carbon.device.mgt.common.Feature; +import org.wso2.carbon.device.mgt.common.app.mgt.Application; +import org.wso2.carbon.device.mgt.common.device.details.DeviceInfo; + +import javax.validation.constraints.NotNull; +import javax.validation.constraints.Pattern; +import javax.validation.constraints.Size; +import java.io.Serializable; +import java.util.List; + +public class AndroidDevice implements Serializable { + private static final long serialVersionUID = 1998101711L; + + @ApiModelProperty( + name = "name", + value = "The device name that can be set on the device by the device user.", + required = true + ) + @Size(min = 2, max = 45) + @Pattern(regexp = "^[A-Za-z0-9]*$") + private String name; + @ApiModelProperty( + name = "type", + value = "The OS type of the device.", + required = true + ) + @NotNull + @Size(min = 2, max = 45) + @Pattern(regexp = "^[A-Za-z]*$") + private String type; + @ApiModelProperty( + name = "description", + value = "Additional information on the device.", + required = true + ) + private String description; + @ApiModelProperty( + name = "deviceIdentifier", + value = "This is a 64-bit number (as a hex string) that is randomly generated when the user first sets up the device and should remain constant for the lifetime of the user\'s device. The value may change if a factory reset is performed on the device.", + required = true + ) + @NotNull + @Size(min = 2, max = 45) + @Pattern(regexp = "^[A-Za-z0-9]*$") + private String deviceIdentifier; + @ApiModelProperty( + name = "enrolmentInfo", + value = "This defines the device registration related information. It is mandatory to define this information.", + required = true + ) + private EnrolmentInfo enrolmentInfo; + @ApiModelProperty( + name = "features", + value = "List of features.", + required = true + ) + private List features; + private List properties; + @ApiModelProperty( + name = "advanceInfo", + value = "This defines the device registration related information. It is mandatory to define this information.", + required = false + ) + private DeviceInfo deviceInfo; + @ApiModelProperty( + name = "applications", + value = "This represents the application list installed into the device", + required = false + ) + private List applications; + + public AndroidDevice() { + } + + public AndroidDevice(String name, String type, String description, String deviceId, EnrolmentInfo enrolmentInfo, List features, List properties) { + this.name = name; + this.type = type; + this.description = description; + this.deviceIdentifier = deviceId; + this.enrolmentInfo = enrolmentInfo; + this.features = features; + this.properties = properties; + } + + public String getName() { + return this.name; + } + + public void setName(String name) { + this.name = name; + } + + public String getType() { + return this.type; + } + + public void setType(String type) { + this.type = type; + } + + public String getDescription() { + return this.description; + } + + public void setDescription(String description) { + this.description = description; + } + + public String getDeviceIdentifier() { + return this.deviceIdentifier; + } + + public void setDeviceIdentifier(String deviceIdentifier) { + this.deviceIdentifier = deviceIdentifier; + } + + public EnrolmentInfo getEnrolmentInfo() { + return this.enrolmentInfo; + } + + public void setEnrolmentInfo(EnrolmentInfo enrolmentInfo) { + this.enrolmentInfo = enrolmentInfo; + } + + public List getFeatures() { + return this.features; + } + + public void setFeatures(List features) { + this.features = features; + } + + public List getProperties() { + return this.properties; + } + + public void setProperties(List properties) { + this.properties = properties; + } + + public DeviceInfo getDeviceInfo() { + return this.deviceInfo; + } + + public void setDeviceInfo(DeviceInfo deviceInfo) { + this.deviceInfo = deviceInfo; + } + + public List getApplications() { + return this.applications; + } + + public void setApplications(List applications) { + this.applications = applications; + } + + public String toString() { + return "device [name=" + this.name + ";" + "type=" + this.type + ";" + "description=" + this.description + ";" + "identifier=" + this.deviceIdentifier + ";" + "]"; + } + + public boolean equals(Object o) { + if (this == o) { + return true; + } else if (!(o instanceof AndroidDevice)) { + return false; + } else { + AndroidDevice device = (AndroidDevice) o; + return this.getDeviceIdentifier().equals(device.getDeviceIdentifier()); + } + } + + public int hashCode() { + return this.getDeviceIdentifier().hashCode(); + } + + public static class Property { + private String name; + private String value; + + public Property() { + } + + public String getName() { + return this.name; + } + + public void setName(String name) { + this.name = name; + } + + public String getValue() { + return this.value; + } + + public void setValue(String value) { + this.value = value; + } + } +} 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/wrapper/AndroidDeviceInfo.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/wrapper/AndroidDeviceInfo.java new file mode 100644 index 000000000..bf775edfe --- /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/wrapper/AndroidDeviceInfo.java @@ -0,0 +1,321 @@ +package org.wso2.carbon.mdm.services.android.bean.wrapper; + + +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; +import org.wso2.carbon.device.mgt.common.device.details.DeviceInfo; +import org.wso2.carbon.device.mgt.common.device.details.DeviceLocation; + +import java.io.Serializable; +import java.util.Date; +import java.util.HashMap; +import java.util.Map; + +@ApiModel( + value = "DeviceInfo", + description = "This class carries all information related to the device information provided by a device." +) +public class AndroidDeviceInfo extends DeviceInfo implements Serializable { + private static final long serialVersionUID = 1998101733L; + @ApiModelProperty( + name = "IMEI", + value = "IMEI number of the device.", + required = true + ) + private String IMEI; + @ApiModelProperty( + name = "IMSI", + value = "IMSI number of the device.", + required = true + ) + private String IMSI; + @ApiModelProperty( + name = "deviceModel", + value = "Model of the device.", + required = true + ) + private String deviceModel; + @ApiModelProperty( + name = "vendor", + value = "Vendor of the device.", + required = true + ) + private String vendor; + @ApiModelProperty( + name = "osVersion", + value = "Operating system version.", + required = true + ) + private String osVersion; + @ApiModelProperty( + name = "batteryLevel", + value = "Battery level of the device.", + required = true + ) + private Double batteryLevel; + @ApiModelProperty( + name = "internalTotalMemory", + value = "Total internal memory of the device.", + required = true + ) + private Double internalTotalMemory; + @ApiModelProperty( + name = "internalAvailableMemory", + value = "Total available memory of the device.", + required = true + ) + private Double internalAvailableMemory; + @ApiModelProperty( + name = "externalTotalMemory", + value = "Total external memory of the device.", + required = true + ) + private Double externalTotalMemory; + @ApiModelProperty( + name = "externalAvailableMemory", + value = "Total external memory avilable of the device.", + required = true + ) + private Double externalAvailableMemory; + @ApiModelProperty( + name = "operator", + value = "Mobile operator of the device.", + required = true + ) + private String operator; + @ApiModelProperty( + name = "connectionType", + value = "How the device is connected to the network.", + required = true + ) + private String connectionType; + @ApiModelProperty( + name = "mobileSignalStrength", + value = "Current mobile signal strength.", + required = true + ) + private Double mobileSignalStrength; + @ApiModelProperty( + name = "ssid", + value = "ssid of the connected WiFi.", + required = true + ) + private String ssid; + @ApiModelProperty( + name = "cpuUsage", + value = "Current total cpu usage.", + required = true + ) + private Double cpuUsage; + @ApiModelProperty( + name = "totalRAMMemory", + value = "Total Ram memory size.", + required = true + ) + private Double totalRAMMemory; + @ApiModelProperty( + name = "availableRAMMemory", + value = "Available total memory of RAM.", + required = true + ) + private Double availableRAMMemory; + @ApiModelProperty( + name = "pluggedIn", + value = "Whether the device is plugged into power or not.", + required = true + ) + private boolean pluggedIn; + @ApiModelProperty( + name = "updatedTime", + value = "Device updated time.", + required = true + ) + private Date updatedTime; + @ApiModelProperty( + name = "location", + value = "Last updated location of the device", + required = false + ) + private DeviceLocation location; + @ApiModelProperty( + name = "deviceDetailsMap", + value = ".", + required = true + ) + private Map deviceDetailsMap = new HashMap(); + + public AndroidDeviceInfo() { + } + + public DeviceLocation getLocation() { + return this.location; + } + + public void setLocation(DeviceLocation location) { + this.location = location; + } + + public String getIMEI() { + return this.IMEI != null?this.IMEI:""; + } + + public void setIMEI(String IMEI) { + this.IMEI = IMEI; + } + + public String getIMSI() { + return this.IMSI != null?this.IMSI:""; + } + + public void setIMSI(String IMSI) { + this.IMSI = IMSI; + } + + public String getDeviceModel() { + return this.deviceModel != null?this.deviceModel:""; + } + + public void setDeviceModel(String deviceModel) { + this.deviceModel = deviceModel; + } + + public String getVendor() { + return this.vendor != null?this.vendor:""; + } + + public void setVendor(String vendor) { + this.vendor = vendor; + } + + public String getOsVersion() { + return this.osVersion != null?this.osVersion:""; + } + + public void setOsVersion(String osVersion) { + this.osVersion = osVersion; + } + + public Double getBatteryLevel() { + return this.batteryLevel != null?this.batteryLevel:Double.valueOf(0.0D); + } + + public void setBatteryLevel(Double batteryLevel) { + this.batteryLevel = batteryLevel; + } + + public Double getInternalTotalMemory() { + return this.internalTotalMemory != null?this.internalTotalMemory:Double.valueOf(0.0D); + } + + public void setInternalTotalMemory(Double internalTotalMemory) { + this.internalTotalMemory = internalTotalMemory; + } + + public Double getInternalAvailableMemory() { + return this.internalAvailableMemory != null?this.internalAvailableMemory:Double.valueOf(0.0D); + } + + public void setInternalAvailableMemory(Double internalAvailableMemory) { + this.internalAvailableMemory = internalAvailableMemory; + } + + public Double getExternalTotalMemory() { + return this.externalTotalMemory != null?this.externalTotalMemory:Double.valueOf(0.0D); + } + + public void setExternalTotalMemory(Double externalTotalMemory) { + this.externalTotalMemory = externalTotalMemory; + } + + public Double getExternalAvailableMemory() { + return this.externalAvailableMemory != null?this.externalAvailableMemory:Double.valueOf(0.0D); + } + + public void setExternalAvailableMemory(Double externalAvailableMemory) { + this.externalAvailableMemory = externalAvailableMemory; + } + + public String getOperator() { + return this.operator != null?this.operator:""; + } + + public void setOperator(String operator) { + this.operator = operator; + } + + public String getConnectionType() { + return this.connectionType != null?this.connectionType:""; + } + + public void setConnectionType(String connectionType) { + this.connectionType = connectionType; + } + + public Double getMobileSignalStrength() { + return this.mobileSignalStrength != null?this.mobileSignalStrength:Double.valueOf(0.0D); + } + + public void setMobileSignalStrength(Double mobileSignalStrength) { + this.mobileSignalStrength = mobileSignalStrength; + } + + public String getSsid() { + return this.ssid != null?this.ssid:""; + } + + public void setSsid(String ssid) { + this.ssid = ssid; + } + + public Double getCpuUsage() { + return this.cpuUsage != null?this.cpuUsage:Double.valueOf(0.0D); + } + + public void setCpuUsage(Double cpuUsage) { + this.cpuUsage = cpuUsage; + } + + public Double getTotalRAMMemory() { + return this.totalRAMMemory != null?this.totalRAMMemory:Double.valueOf(0.0D); + } + + public void setTotalRAMMemory(Double totalRAMMemory) { + this.totalRAMMemory = totalRAMMemory; + } + + public Double getAvailableRAMMemory() { + return this.availableRAMMemory != null?this.availableRAMMemory:Double.valueOf(0.0D); + } + + public void setAvailableRAMMemory(Double availableRAMMemory) { + this.availableRAMMemory = availableRAMMemory; + } + + public boolean isPluggedIn() { + return this.pluggedIn; + } + + public void setPluggedIn(boolean pluggedIn) { + this.pluggedIn = pluggedIn; + } + + public Date getUpdatedTime() { + if(this.updatedTime.equals((Object)null)) { + this.updatedTime = new Date(); + } + + return this.updatedTime; + } + + public void setUpdatedTime(Date updatedTime) { + this.updatedTime = updatedTime; + } + + public void setDeviceDetailsMap(Map deviceDetailsMap) { + this.deviceDetailsMap = deviceDetailsMap; + } + + public Map getDeviceDetailsMap() { + return this.deviceDetailsMap; + } +} 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/wrapper/ApplicationInstallationBeanWrapper.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/wrapper/ApplicationInstallationBeanWrapper.java index 2d4ea4d00..b0bd54cf3 100644 --- a/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.api/src/main/java/org/wso2/carbon/mdm/services/android/bean/wrapper/ApplicationInstallationBeanWrapper.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/wrapper/ApplicationInstallationBeanWrapper.java @@ -33,7 +33,6 @@ public class ApplicationInstallationBeanWrapper { @ApiModelProperty(name = "deviceIDs", value = "List of Devices", required = true) private List deviceIDs; - @ApiModelProperty(name = "operation", value = "App Installation property", required = true) private ApplicationInstallation operation; 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/wrapper/ApplicationUninstallationBeanWrapper.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/wrapper/ApplicationUninstallationBeanWrapper.java index 568b85f0c..28c72c6cd 100644 --- a/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.api/src/main/java/org/wso2/carbon/mdm/services/android/bean/wrapper/ApplicationUninstallationBeanWrapper.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/wrapper/ApplicationUninstallationBeanWrapper.java @@ -27,28 +27,28 @@ import java.util.List; * This class is used to wrap the UninstallApplication bean with devices. */ @ApiModel(value = "ApplicationUninstallationBeanWrapper", - description = "This class carries all information related to Uninstall Application") + description = "This class carries all information related to Uninstall Application") public class ApplicationUninstallationBeanWrapper { - @ApiModelProperty(name = "deviceIDs", value = "List of device Ids", required = true) - private List deviceIDs; + @ApiModelProperty(name = "deviceIDs", value = "List of device Ids", required = true) + private List deviceIDs; - @ApiModelProperty(name = "operation", value = "Name of the device", required = true) - private ApplicationUninstallation operation; + @ApiModelProperty(name = "operation", value = "Name of the device", required = true) + private ApplicationUninstallation operation; - public List getDeviceIDs() { - return deviceIDs; - } + public List getDeviceIDs() { + return deviceIDs; + } - public void setDeviceIDs(List deviceIDs) { - this.deviceIDs = deviceIDs; - } + public void setDeviceIDs(List deviceIDs) { + this.deviceIDs = deviceIDs; + } - public ApplicationUninstallation getOperation() { - return operation; - } + public ApplicationUninstallation getOperation() { + return operation; + } - public void setOperation(ApplicationUninstallation operation) { - this.operation = operation; - } + public void setOperation(ApplicationUninstallation operation) { + this.operation = operation; + } } 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/wrapper/BlacklistApplicationsBeanWrapper.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/wrapper/BlacklistApplicationsBeanWrapper.java index 1f824661d..3abf32c22 100644 --- a/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.api/src/main/java/org/wso2/carbon/mdm/services/android/bean/wrapper/BlacklistApplicationsBeanWrapper.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/wrapper/BlacklistApplicationsBeanWrapper.java @@ -21,33 +21,35 @@ import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; import org.wso2.carbon.mdm.services.android.bean.BlacklistApplications; +import javax.validation.Valid; import java.util.List; /** * This class is used to wrap the BlacklistApplications bean with devices. */ @ApiModel(value = "BlacklistApplicationsBeanWrapper", - description = "Mapping between blacklist application and the device ids.") + description = "Mapping between blacklist application and the device ids.") public class BlacklistApplicationsBeanWrapper { @ApiModelProperty(name = "operation", value = "Blacklist applications information", required = true) - private BlacklistApplications operation; + @Valid + private BlacklistApplications operation; @ApiModelProperty(name = "deviceIDs", value = "List of device Ids", required = true) - private List deviceIDs; + private List deviceIDs; - public BlacklistApplications getOperation() { - return operation; - } + public BlacklistApplications getOperation() { + return operation; + } - public void setOperation(BlacklistApplications operation) { - this.operation = operation; - } + public void setOperation(BlacklistApplications operation) { + this.operation = operation; + } - public List getDeviceIDs() { - return deviceIDs; - } + public List getDeviceIDs() { + return deviceIDs; + } - public void setDeviceIDs(List deviceIDs) { - this.deviceIDs = deviceIDs; - } + public void setDeviceIDs(List deviceIDs) { + this.deviceIDs = deviceIDs; + } } 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/wrapper/DeviceEnrollmentInfo.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/wrapper/DeviceEnrollmentInfo.java new file mode 100644 index 000000000..b2f77cc31 --- /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/wrapper/DeviceEnrollmentInfo.java @@ -0,0 +1,126 @@ +/* + * 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.wrapper; + +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; +import org.wso2.carbon.device.mgt.common.Device; +import org.wso2.carbon.device.mgt.common.EnrolmentInfo; + +import java.io.Serializable; + +@ApiModel(value = "EnrolmentInfo", description = "This class carries all information related to a devices enrollment" + + " status.") +public class DeviceEnrollmentInfo extends EnrolmentInfo implements Serializable { + private static final long serialVersionUID = 1998101712L; + + @ApiModelProperty(name = "device", value = "Enrolled device.", required = true) + private Device device; + @ApiModelProperty(name = "dateOfEnrolment", value = "Date of the device enrollment.", required = true) + private Long dateOfEnrolment; + @ApiModelProperty(name = "dateOfLastUpdate", value = "Date of the device's last update.", required = true) + private Long dateOfLastUpdate; + @ApiModelProperty(name = "ownership", value = "Defines the ownership details. The ownership type can be any of the" + + " following values.\n" + + "BYOD - Bring your own device (BYOD).\n" + + "COPE - Corporate owned personally enabled (COPE).", required = true) + private OwnerShip ownership; + @ApiModelProperty(name = "status", value = "Current status of the device, such as whether the device " + + "is active, removed etc.", required = true) + private Status status; + @ApiModelProperty(name = "owner", value = "The device owner's name.", required = true) + private String owner; + + public DeviceEnrollmentInfo() { + } + + public DeviceEnrollmentInfo(Device device, String owner, OwnerShip ownership, Status status) { + this.device = device; + this.owner = owner; + this.ownership = ownership; + this.status = status; + } + + public Long getDateOfEnrolment() { + return dateOfEnrolment; + } + + public void setDateOfEnrolment(Long dateOfEnrolment) { + this.dateOfEnrolment = dateOfEnrolment; + } + + public Long getDateOfLastUpdate() { + return dateOfLastUpdate; + } + + public void setDateOfLastUpdate(Long dateOfLastUpdate) { + this.dateOfLastUpdate = dateOfLastUpdate; + } + + public OwnerShip getOwnership() { + return ownership; + } + + public void setOwnership(OwnerShip ownership) { + this.ownership = ownership; + } + + public Status getStatus() { + return status; + } + + public void setStatus(Status status) { + this.status = status; + } + + public String getOwner() { + return owner; + } + + public void setOwner(String owner) { + this.owner = owner; + } + + public Device getDevice() { + return device; + } + + public void setDevice(Device device) { + this.device = device; + } + + @Override + public boolean equals(Object obj) { + if (obj instanceof DeviceEnrollmentInfo) { + DeviceEnrollmentInfo tempInfo = (DeviceEnrollmentInfo) obj; + if (this.owner != null && this.ownership != null) { + if (this.owner.equals(tempInfo.getOwner()) && this.ownership.equals(tempInfo.getOwnership())) { + return true; + } + } + } + return false; + } + + @Override + public int hashCode() { + return owner.hashCode() ^ ownership.hashCode(); + } + + +} \ No newline at end of file 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/wrapper/EventBeanWrapper.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/wrapper/EventBeanWrapper.java index d05cb58f8..c6efc2131 100644 --- a/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.api/src/main/java/org/wso2/carbon/mdm/services/android/bean/wrapper/EventBeanWrapper.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/wrapper/EventBeanWrapper.java @@ -21,6 +21,9 @@ package org.wso2.carbon.mdm.services.android.bean.wrapper; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; +import javax.validation.constraints.Pattern; +import javax.validation.constraints.Size; + /** * This class is used to wrap the events which receive from the agent application. */ @@ -29,10 +32,13 @@ import io.swagger.annotations.ApiModelProperty; public class EventBeanWrapper { @ApiModelProperty(name = "deviceIdentifier", value = "DeviceIdentifier to be need to retrieve/publish Event.", required = true) + @Size(min = 2, max = 45) + @Pattern(regexp = "^[A-Za-z0-9]*$") private String deviceIdentifier; @ApiModelProperty(name = "payload", value = "Event payload.", required = true) private String payload; @ApiModelProperty(name = "type", value = "Type of the event.", required = true) + @Size(min = 2, max = 20) private String type; public String getPayload() { 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/wrapper/VpnBeanWrapper.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/wrapper/VpnBeanWrapper.java index bfdbd43da..e0d1d6bc1 100644 --- a/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.api/src/main/java/org/wso2/carbon/mdm/services/android/bean/wrapper/VpnBeanWrapper.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/wrapper/VpnBeanWrapper.java @@ -21,6 +21,8 @@ import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; import org.wso2.carbon.mdm.services.android.bean.Vpn; +import javax.validation.constraints.Pattern; +import javax.validation.constraints.Size; import java.util.List; /** @@ -34,6 +36,8 @@ public class VpnBeanWrapper { private Vpn operation; @ApiModelProperty(name = "deviceIDs", value = "List of device Ids to be need to execute VPN operation.", required = true) + @Size(min = 2, max = 45) + @Pattern(regexp = "^[A-Za-z0-9]*$") private List deviceIDs; public Vpn getOperation() { 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/wrapper/WipeDataBeanWrapper.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/wrapper/WipeDataBeanWrapper.java index 1c72af45b..b7ebb2903 100644 --- a/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.api/src/main/java/org/wso2/carbon/mdm/services/android/bean/wrapper/WipeDataBeanWrapper.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/wrapper/WipeDataBeanWrapper.java @@ -27,27 +27,27 @@ import java.util.List; * This class is used to wrap the WipeData bean with devices. */ @ApiModel(value = "WipeDataBeanWrapper", - description = "Mapping between wipe operation and device list to be applied.") + description = "Mapping between wipe operation and device list to be applied.") public class WipeDataBeanWrapper { @ApiModelProperty(name = "operation", value = "The information of wipedata operation", required = true) - private WipeData operation; + private WipeData operation; @ApiModelProperty(name = "deviceIDs", value = "List of device Ids", required = true) - private List deviceIDs; + private List deviceIDs; - public WipeData getOperation() { - return operation; - } + public WipeData getOperation() { + return operation; + } - public void setOperation(WipeData operation) { - this.operation = operation; - } + public void setOperation(WipeData operation) { + this.operation = operation; + } - public List getDeviceIDs() { - return deviceIDs; - } + public List getDeviceIDs() { + return deviceIDs; + } - public void setDeviceIDs(List deviceIDs) { - this.deviceIDs = deviceIDs; - } + public void setDeviceIDs(List deviceIDs) { + this.deviceIDs = deviceIDs; + } } 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/BadRequestException.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/BadRequestException.java index 741f24b31..5fa5ededc 100644 --- a/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.api/src/main/java/org/wso2/carbon/mdm/services/android/exception/BadRequestException.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/BadRequestException.java @@ -19,10 +19,8 @@ package org.wso2.carbon.mdm.services.android.exception; import org.wso2.carbon.mdm.services.android.bean.ErrorResponse; -import org.wso2.carbon.mdm.services.android.util.Message; import javax.ws.rs.WebApplicationException; -import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; /** @@ -33,5 +31,4 @@ public class BadRequestException extends WebApplicationException { public BadRequestException(ErrorResponse error) { super(Response.status(Response.Status.BAD_REQUEST).entity(error).build()); } - } \ No newline at end of file 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/ErrorDTO.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/ErrorDTO.java new file mode 100644 index 000000000..ebd5ec8fc --- /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/ErrorDTO.java @@ -0,0 +1,86 @@ +/* + * 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 java.util.ArrayList; +import java.util.List; + +public class ErrorDTO { + + private Long code = null; + private String message = null; + private String description = null; + + public void setMoreInfo(String moreInfo) { + this.moreInfo = moreInfo; + } + + public void setCode(Long code) { + this.code = code; + } + + public void setMessage(String message) { + this.message = message; + } + + public void setDescription(String description) { + this.description = description; + } + + public void setError(List error) { + this.error = error; + } + + private String moreInfo = null; + + public String getMessage() { + return message; + } + + public Long getCode() { + return code; + } + + public String getDescription() { + return description; + } + + public String getMoreInfo() { + return moreInfo; + } + + public List getError() { + return error; + } + + public String toString() { + StringBuilder stringBuilder = new StringBuilder(); + stringBuilder.append("class ErrorDTO {\n"); + stringBuilder.append(" code: ").append(code).append("\n"); + stringBuilder.append(" message: ").append(message).append("\n"); + stringBuilder.append(" description: ").append(description).append("\n"); + stringBuilder.append(" moreInfo: ").append(moreInfo).append("\n"); + stringBuilder.append(" error: ").append(error).append("\n"); + stringBuilder.append("}\n"); + return stringBuilder.toString(); + } + + private List error = new ArrayList<>(); + +} 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/ForbiddenException.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/ForbiddenException.java new file mode 100644 index 000000000..75113f850 --- /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/ForbiddenException.java @@ -0,0 +1,51 @@ +/* + * 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.util.AndroidConstants; + +import javax.ws.rs.WebApplicationException; +import javax.ws.rs.core.Response; + +/** + * Exception class that is corresponding to 401 Forbidden response + */ + +public class ForbiddenException extends WebApplicationException { + + private String message; + + public ForbiddenException() { + super(Response.status(Response.Status.FORBIDDEN) + .build()); + } + + public ForbiddenException(ErrorDTO errorDTO) { + super(Response.status(Response.Status.FORBIDDEN) + .entity(errorDTO) + .header(AndroidConstants.HEADER_CONTENT_TYPE, AndroidConstants.APPLICATION_JSON) + .build()); + message = errorDTO.getDescription(); + } + + @Override + public String getMessage() { + return message; + } +} 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/GlobalThrowableMapper.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/GlobalThrowableMapper.java new file mode 100644 index 000000000..f324a2d0f --- /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/GlobalThrowableMapper.java @@ -0,0 +1,101 @@ +/* + * 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 com.google.gson.JsonParseException; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.mdm.services.android.util.AndroidDeviceUtils; + +import javax.naming.AuthenticationException; +import javax.validation.ConstraintViolationException; +import javax.ws.rs.ClientErrorException; +import javax.ws.rs.core.Response; +import javax.ws.rs.ext.ExceptionMapper; + +/** + * Handle the cxf level exceptions. + */ +public class GlobalThrowableMapper implements ExceptionMapper { + private static final Log log = LogFactory.getLog(GlobalThrowableMapper.class); + + private ErrorDTO e500 = new ErrorDTO(); + + GlobalThrowableMapper() { + e500.setCode((long) 500); + e500.setMessage("Internal server error."); + e500.setMoreInfo(""); + e500.setDescription("The server encountered an internal error. Please contact administrator."); + + } + + @Override + public Response toResponse(Throwable e) { + + if (e instanceof JsonParseException) { + String errorMessage = "Malformed request body."; + log.error(errorMessage); + return AndroidDeviceUtils.buildBadRequestException(errorMessage).getResponse(); + + } + if (e instanceof NotFoundException) { + return ((NotFoundException) e).getResponse(); + } + if (e instanceof ConstraintViolationException) { + log.error("Constraint violation", e); + return Response.status(Response.Status.BAD_REQUEST).header("Content-Type", "application/json") + .entity(400l).build(); + } + if (e instanceof UnexpectedServerErrorException) { + log.error("Unexpected server error", e); + return ((UnexpectedServerErrorException) e).getResponse(); + } + if (e instanceof ConstraintViolationException) { + return ((ParameterValidationException) e).getResponse(); + } + if (e instanceof IllegalArgumentException) { + log.error("Illegal exception.", e); + return Response.status(Response.Status.BAD_REQUEST).header("Content-Type", "application/json") + .entity(400l).build(); + } + if (e instanceof ClientErrorException) { + log.error("Client error", e); + return ((ClientErrorException) e).getResponse(); + } + if (e instanceof AuthenticationException) { + ErrorDTO errorDetail = new ErrorDTO(); + errorDetail.setCode((long) 401); + errorDetail.setMoreInfo(""); + errorDetail.setMessage(""); + errorDetail.setDescription(e.getMessage()); + return Response + .status(Response.Status.UNAUTHORIZED) + .entity(errorDetail) + .build(); + } + if (e instanceof ForbiddenException) { + log.error("Resource forbidden", e); + return ((ForbiddenException) e).getResponse(); + } + //unknown exception log and return + log.error("An Unknown exception has been captured by global exception mapper.", e); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).header("Content-Type", "application/json") + .entity(e500).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/exception/NotFoundException.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/NotFoundException.java index bda2219bf..53863c3ac 100644 --- a/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.api/src/main/java/org/wso2/carbon/mdm/services/android/exception/NotFoundException.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/NotFoundException.java @@ -19,15 +19,28 @@ package org.wso2.carbon.mdm.services.android.exception; import org.wso2.carbon.mdm.services.android.bean.ErrorResponse; +import org.wso2.carbon.mdm.services.android.util.AndroidConstants; + import javax.ws.rs.WebApplicationException; import javax.ws.rs.core.Response; public class NotFoundException extends WebApplicationException { - + private String message; private static final long serialVersionUID = 147943572342342340L; public NotFoundException(ErrorResponse error) { super(Response.status(Response.Status.NOT_FOUND).entity(error).build()); } + public NotFoundException(ErrorDTO errorDTO) { + super(Response.status(Response.Status.NOT_FOUND) + .entity(errorDTO) + .header(AndroidConstants.HEADER_CONTENT_TYPE, AndroidConstants.APPLICATION_JSON) + .build()); + message = errorDTO.getDescription(); + } + @Override + public String getMessage() { + return message; + } } 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/ParameterValidationException.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/ParameterValidationException.java new file mode 100644 index 000000000..efb38a3d7 --- /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/ParameterValidationException.java @@ -0,0 +1,56 @@ +/* + * 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.util.AndroidConstants; +import org.wso2.carbon.mdm.services.android.util.AndroidDeviceUtils; + +import javax.validation.ConstraintViolation; +import javax.ws.rs.WebApplicationException; +import javax.ws.rs.core.Response; +import java.util.Set; + +public class ParameterValidationException extends WebApplicationException { + + private String message; + public ParameterValidationException(Set> violations) { + super(Response.status(Response.Status.BAD_REQUEST) + .entity(AndroidDeviceUtils.getConstraintViolationErrorDTO(violations)) + .header(AndroidConstants.HEADER_CONTENT_TYPE, AndroidConstants.APPLICATION_JSON) + .build()); + + //Set the error message + StringBuilder stringBuilder = new StringBuilder(); + for (ConstraintViolation violation : violations) { + stringBuilder.append(violation.getRootBeanClass().getSimpleName()); + stringBuilder.append("."); + stringBuilder.append(violation.getPropertyPath()); + stringBuilder.append(": "); + stringBuilder.append(violation.getMessage()); + stringBuilder.append(", "); + } + message = stringBuilder.toString(); + } + + @Override + public String getMessage() { + return message; + } + +} 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 index 6db5b0e69..a66c2f506 100644 --- 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 @@ -18,18 +18,31 @@ */ package org.wso2.carbon.mdm.services.android.exception; - import org.wso2.carbon.mdm.services.android.bean.ErrorResponse; +import org.wso2.carbon.mdm.services.android.util.AndroidConstants; import javax.ws.rs.WebApplicationException; import javax.ws.rs.core.Response; public class UnexpectedServerErrorException extends WebApplicationException { - + private String message; private static final long serialVersionUID = 147943579458906890L; public UnexpectedServerErrorException(ErrorResponse error) { super(Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(error).build()); } + public UnexpectedServerErrorException(ErrorDTO errorDTO) { + super(Response.status(Response.Status.INTERNAL_SERVER_ERROR) + .entity(errorDTO) + .header(AndroidConstants.HEADER_CONTENT_TYPE, AndroidConstants.APPLICATION_JSON) + .build()); + message = errorDTO.getDescription(); + } + + @Override + public String getMessage() { + return message; + } + } 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/ValidationInterceptor.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/ValidationInterceptor.java new file mode 100644 index 000000000..172a1e5ff --- /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/ValidationInterceptor.java @@ -0,0 +1,118 @@ +/* + * 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.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.apache.cxf.interceptor.Fault; +import org.apache.cxf.jaxrs.lifecycle.ResourceProvider; +import org.apache.cxf.jaxrs.model.ClassResourceInfo; +import org.apache.cxf.jaxrs.model.OperationResourceInfo; +import org.apache.cxf.message.Message; +import org.apache.cxf.message.MessageContentsList; +import org.apache.cxf.phase.AbstractPhaseInterceptor; +import org.apache.cxf.phase.Phase; + +import javax.validation.*; +import javax.validation.executable.ExecutableValidator; +import java.lang.reflect.Method; +import java.util.List; +import java.util.Set; + +public class ValidationInterceptor extends AbstractPhaseInterceptor { + + private Log log = LogFactory.getLog(getClass()); + private Validator validator = null; //validator interface is thread-safe + + public ValidationInterceptor() { + super(Phase.PRE_INVOKE); + ValidatorFactory defaultFactory = Validation.buildDefaultValidatorFactory(); + validator = defaultFactory.getValidator(); + if (validator == null) { + log.warn("Bean Validation provider could not be found, no validation will be performed"); + } else { + log.debug("Validation In-Interceptor initialized successfully"); + } + } + @Override + public void handleMessage(Message message) throws Fault { + final OperationResourceInfo operationResource = message.getExchange().get(OperationResourceInfo.class); + if (operationResource == null) { + log.info("OperationResourceInfo is not available, skipping validation"); + return; + } + + final ClassResourceInfo classResource = operationResource.getClassResourceInfo(); + if (classResource == null) { + log.info("ClassResourceInfo is not available, skipping validation"); + return; + } + + final ResourceProvider resourceProvider = classResource.getResourceProvider(); + if (resourceProvider == null) { + log.info("ResourceProvider is not available, skipping validation"); + return; + } + + final List arguments = MessageContentsList.getContentsList(message); + final Method method = operationResource.getAnnotatedMethod(); + final Object instance = resourceProvider.getInstance(message); + if (method != null && arguments != null) { + //validate the parameters(arguments) over the invoked method + validate(method, arguments.toArray(), instance); + + //validate the fields of each argument + for (Object arg : arguments) { + if (arg != null) + validate(arg); + } + } + + } + public void validate(final Method method, final Object[] arguments, final T instance) { + if (validator == null) { + log.warn("Bean Validation provider could not be found, no validation will be performed"); + return; + } + + ExecutableValidator methodValidator = validator.forExecutables(); + Set> violations = methodValidator.validateParameters(instance, + method, arguments); + + if (!violations.isEmpty()) { + throw new ConstraintViolationException(violations); + } + } + + public void validate(final T object) { + if (validator == null) { + log.warn("Bean Validation provider could be found, no validation will be performed"); + return; + } + + Set> violations = validator.validate(object); + + if (!violations.isEmpty()) { + throw new ConstraintViolationException(violations); + } + } + + public void handleFault(Message messageParam) { + } +} 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/DeviceManagementAdminService.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/DeviceManagementAdminService.java index 3e504e52c..4b57d5fe4 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/DeviceManagementAdminService.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/DeviceManagementAdminService.java @@ -22,6 +22,7 @@ import io.swagger.annotations.*; import org.wso2.carbon.device.mgt.common.operation.mgt.Activity; import org.wso2.carbon.mdm.services.android.bean.wrapper.*; +import javax.validation.Valid; import javax.ws.rs.Consumes; import javax.ws.rs.POST; import javax.ws.rs.Path; @@ -244,7 +245,7 @@ public interface DeviceManagementAdminService { }) Response removePassword( @ApiParam(name = "deviceIDs", - value = "DeviceIds to be requested to remove password") List deviceIDs); + value = "DeviceIds to be requested to remove password") List deviceIDs); @POST @Path("/control-camera") @@ -562,7 +563,8 @@ public interface DeviceManagementAdminService { "Server error occurred while adding a new device ring operation.") }) Response ringDevice( - @ApiParam(name = "deviceIDs", value = "Device Ids needed for ring") List deviceIDs); + @ApiParam(name = "deviceIDs", value = "Device Ids needed for ring") + List deviceIDs); @POST @Path("/reboot") @@ -613,7 +615,8 @@ public interface DeviceManagementAdminService { "Server error occurred while adding a new device reboot operation.") }) Response rebootDevice( - @ApiParam(name = "deviceIDs", value = "Device Ids needed for reboot.") List deviceIDs); + @ApiParam(name = "deviceIDs", value = "Device Ids needed for reboot.") + List deviceIDs); @POST @ApiOperation( @@ -664,7 +667,8 @@ public interface DeviceManagementAdminService { }) @Path("/mute") Response muteDevice( - @ApiParam(name = "deviceIDs", value = "DeviceIDs need to be muted") List deviceIDs); + @ApiParam(name = "deviceIDs", value = "DeviceIDs need to be muted") + List deviceIDs); @POST @Path("/install-application") @@ -1094,7 +1098,7 @@ public interface DeviceManagementAdminService { }) Response configureWifi( @ApiParam(name = "wifiBeanWrapper", - value = "WifiConfigurations and Device Ids") WifiBeanWrapper wifiBeanWrapper); + value = "WifiConfigurations and Device Ids") @Valid WifiBeanWrapper wifiBeanWrapper); @POST @Path("/encrypt-storage") 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/DeviceManagementService.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/DeviceManagementService.java index 4464d6c73..b97f1843a 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/DeviceManagementService.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/DeviceManagementService.java @@ -19,10 +19,14 @@ package org.wso2.carbon.mdm.services.android.services; import io.swagger.annotations.*; -import org.wso2.carbon.device.mgt.common.Device; -import org.wso2.carbon.device.mgt.common.app.mgt.Application; import org.wso2.carbon.device.mgt.common.operation.mgt.Operation; +import org.wso2.carbon.mdm.services.android.bean.wrapper.AndroidApplication; +import org.wso2.carbon.mdm.services.android.bean.wrapper.AndroidDevice; +import javax.validation.Valid; +import javax.validation.constraints.NotNull; +import javax.validation.constraints.Pattern; +import javax.validation.constraints.Size; import javax.ws.rs.*; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; @@ -81,11 +85,15 @@ public interface DeviceManagementService { @ApiParam( name = "id", value = "Device Identifier") + @NotNull + @Size(min = 2 , max = 45) + @Pattern(regexp = "^[A-Za-z0-9]*$") @PathParam("id") String id, @ApiParam( name = "applications", value = "List of applications that need to be persisted against the device") - List applications); + @Valid + List androidApplications); @PUT @Path("/{id}/pending-operations") @@ -191,7 +199,8 @@ public interface DeviceManagementService { message = "Internal Server Error. \n " + "Server error occurred while adding a new policy.") }) - Response enrollDevice(@ApiParam(name = "device", value = "Device Information to be enroll") Device device); + Response enrollDevice(@ApiParam(name = "device", value = "Device Information to be enroll") + @Valid AndroidDevice device); @GET @Path("/{id}/status") @@ -285,10 +294,13 @@ public interface DeviceManagementService { @ApiParam( name = "id", value = "Device Identifier") + @NotNull + @Size(min = 2 , max = 45) + @Pattern(regexp = "^[A-Za-z0-9]*$") @PathParam("id") String id, @ApiParam( name = "device", - value = "Device information to be modify") Device device); + value = "Device information to be modify") @Valid AndroidDevice androidDevice); @DELETE @Path("/{id}") @@ -314,6 +326,9 @@ public interface DeviceManagementService { @ApiParam( name = "id", value = "Device Identifier") + @NotNull + @Size(min = 2 , max = 45) + @Pattern(regexp = "^[A-Za-z0-9]*$") @PathParam("id") String id); } 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/DeviceTypeConfigurationService.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/DeviceTypeConfigurationService.java index 7d74b700d..4aac40c53 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/DeviceTypeConfigurationService.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/DeviceTypeConfigurationService.java @@ -20,6 +20,7 @@ package org.wso2.carbon.mdm.services.android.services; import io.swagger.annotations.*; import org.wso2.carbon.device.mgt.common.configuration.mgt.PlatformConfiguration; +import org.wso2.carbon.mdm.services.android.bean.AndroidPlatformConfiguration; import org.wso2.carbon.mdm.services.android.exception.AndroidAgentException; import javax.ws.rs.*; @@ -125,7 +126,7 @@ public interface DeviceTypeConfigurationService { Response updateConfiguration( @ApiParam(name = "configuration", value = "AndroidPlatformConfiguration") - PlatformConfiguration configuration); + AndroidPlatformConfiguration androidPlatformConfiguration); @GET @Path("license") 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/EventReceiverService.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/EventReceiverService.java index c48844090..cc77bb42f 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/EventReceiverService.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/EventReceiverService.java @@ -22,6 +22,9 @@ import io.swagger.annotations.*; import org.wso2.carbon.mdm.services.android.bean.DeviceState; import org.wso2.carbon.mdm.services.android.bean.wrapper.EventBeanWrapper; +import javax.validation.Valid; +import javax.validation.constraints.Pattern; +import javax.validation.constraints.Size; import javax.ws.rs.*; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; @@ -86,6 +89,7 @@ public interface EventReceiverService { @ApiParam( name = "eventBeanWrapper", value = "Information of the agent event to be published on DAS.") + @Valid EventBeanWrapper eventBeanWrapper); @GET @@ -130,11 +134,14 @@ public interface EventReceiverService { code = 500, message = "Error occurred while getting published events for specific device.") }) + Response retrieveAlerts( @ApiParam( name = "id", value = "Device Identifier to be need to retrieve events.", required = true) + @Size(min = 2, max = 45) + @Pattern(regexp = "^[A-Za-z0-9]*$") @QueryParam("id") String deviceId, @ApiParam( name = "from", @@ -147,6 +154,8 @@ public interface EventReceiverService { @ApiParam( name = "type", value = "Type of the Alert to be need to retrieve events.") + @Size(min = 2, max = 45) + @Pattern(regexp = "^[A-Za-z0-9]*$") @QueryParam("type") String type, @ApiParam( name = "If-Modified-Since", 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 468882aa9..f1c2262b0 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 @@ -33,6 +33,7 @@ import org.wso2.carbon.mdm.services.android.services.DeviceManagementAdminServic import org.wso2.carbon.mdm.services.android.util.AndroidAPIUtils; import org.wso2.carbon.mdm.services.android.util.AndroidConstants; +import javax.validation.Valid; import javax.ws.rs.Consumes; import javax.ws.rs.POST; import javax.ws.rs.Path; @@ -568,7 +569,7 @@ public class DeviceManagementAdminServiceImpl implements DeviceManagementAdminSe @POST @Path("/configure-vpn") @Override - public Response configureVPN(VpnBeanWrapper vpnBeanWrapper) { + public Response configureVPN(@Valid VpnBeanWrapper vpnBeanWrapper) { if (log.isDebugEnabled()) { log.debug("Invoking Android VPN device operation"); } @@ -792,7 +793,6 @@ public class DeviceManagementAdminServiceImpl implements DeviceManagementAdminSe try { - if (webClipBeanWrapper == null || webClipBeanWrapper.getOperation() == null) { String errorMessage = "The payload of the add webclip operation is incorrect"; log.error(errorMessage); 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 f8c0980d7..c2a73600a 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 @@ -30,6 +30,8 @@ import org.wso2.carbon.device.mgt.common.notification.mgt.NotificationManagement 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.bean.wrapper.AndroidApplication; +import org.wso2.carbon.mdm.services.android.bean.wrapper.AndroidDevice; 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; @@ -40,9 +42,14 @@ import org.wso2.carbon.policy.mgt.common.PolicyManagementException; import org.wso2.carbon.policy.mgt.common.monitor.PolicyComplianceException; import org.wso2.carbon.policy.mgt.core.PolicyManagerService; +import javax.validation.Valid; +import javax.validation.constraints.NotNull; +import javax.validation.constraints.Pattern; +import javax.validation.constraints.Size; import javax.ws.rs.*; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; +import java.util.ArrayList; import java.util.List; @Path("/devices") @@ -56,7 +63,27 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { @PUT @Path("/{id}/applications") @Override - public Response updateApplicationList(@PathParam("id") String id, List applications) { + public Response updateApplicationList(@PathParam("id") + @NotNull + @Size(min = 2 , max = 45) + @Pattern(regexp = "^[A-Za-z0-9]*$") + String id,@Valid List androidApplications) { + Application application; + List applications = new ArrayList<>(); + for (AndroidApplication androidApplication : androidApplications) { + application = new Application(); + application.setPlatform(androidApplication.getPlatform()); + application.setCategory(androidApplication.getCategory()); + application.setName(androidApplication.getName()); + application.setLocationUrl(androidApplication.getLocationUrl()); + application.setImageUrl(androidApplication.getImageUrl()); + application.setVersion(androidApplication.getVersion()); + application.setType(androidApplication.getType()); + application.setAppProperties(androidApplication.getAppProperties()); + application.setApplicationIdentifier(androidApplication.getApplicationIdentifier()); + application.setMemoryUsage(androidApplication.getMemoryUsage()); + applications.add(application); + } Message responseMessage = new Message(); DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); deviceIdentifier.setId(id); @@ -164,32 +191,42 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { @POST @Override - public Response enrollDevice(Device device) { + public Response enrollDevice(@Valid AndroidDevice androidDevice) { try { + Device device = new Device(); device.setType(DeviceManagementConstants.MobileDeviceTypes.MOBILE_DEVICE_TYPE_ANDROID); + device.setEnrolmentInfo(androidDevice.getEnrolmentInfo()); device.getEnrolmentInfo().setOwner(AndroidAPIUtils.getAuthenticatedUser()); + device.setDeviceInfo(androidDevice.getDeviceInfo()); + device.setDeviceIdentifier(androidDevice.getDeviceIdentifier()); + device.setDescription(androidDevice.getDescription()); + device.setType(androidDevice.getType()); + device.setName(androidDevice.getName()); + device.setFeatures(androidDevice.getFeatures()); + device.setProperties(androidDevice.getProperties()); + boolean status = AndroidAPIUtils.getDeviceManagementService().enrollDevice(device); PolicyManagerService policyManagerService = AndroidAPIUtils.getPolicyManagerService(); - policyManagerService.getEffectivePolicy(new DeviceIdentifier(device.getDeviceIdentifier(), device.getType())); + policyManagerService.getEffectivePolicy(new DeviceIdentifier(androidDevice.getDeviceIdentifier(), androidDevice.getType())); if (status) { return Response.status(Response.Status.OK).entity("Android device, which carries the id '" + - device.getDeviceIdentifier() + "' has successfully been enrolled").build(); + androidDevice.getDeviceIdentifier() + "' has successfully been enrolled").build(); } else { return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity("Failed to enroll '" + - device.getType() + "' device, which carries the id '" + - device.getDeviceIdentifier() + "'").build(); + androidDevice.getType() + "' device, which carries the id '" + + androidDevice.getDeviceIdentifier() + "'").build(); } } catch (DeviceManagementException e) { - String msg = "Error occurred while enrolling the '" + device.getType() + "', which carries the id '" + - device.getDeviceIdentifier() + "'"; + String msg = "Error occurred while enrolling the '" + androidDevice.getType() + "', which carries the id '" + + androidDevice.getDeviceIdentifier() + "'"; log.error(msg, e); 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() + + String msg = "Error occurred while enforcing default enrollment policy upon '" + androidDevice.getType() + "', which carries the id '" + - device.getDeviceIdentifier() + "'"; + androidDevice.getDeviceIdentifier() + "'"; log.error(msg, e); throw new UnexpectedServerErrorException( new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build()); @@ -222,7 +259,18 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { @PUT @Path("/{id}") @Override - public Response modifyEnrollment(@PathParam("id") String id, Device device) { + public Response modifyEnrollment(@PathParam("id") String id, AndroidDevice androidDevice) { + Device device = new Device(); + device.setType(DeviceManagementConstants.MobileDeviceTypes.MOBILE_DEVICE_TYPE_ANDROID); + device.setEnrolmentInfo(androidDevice.getEnrolmentInfo()); + device.getEnrolmentInfo().setOwner(AndroidAPIUtils.getAuthenticatedUser()); + device.setDeviceInfo(androidDevice.getDeviceInfo()); + device.setDeviceIdentifier(androidDevice.getDeviceIdentifier()); + device.setDescription(androidDevice.getDescription()); + device.setType(androidDevice.getType()); + device.setName(androidDevice.getName()); + device.setFeatures(androidDevice.getFeatures()); + device.setProperties(androidDevice.getProperties()); boolean result; try { device.setType(DeviceManagementConstants.MobileDeviceTypes.MOBILE_DEVICE_TYPE_ANDROID); 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 6ec2c470b..0704ab188 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,6 +25,7 @@ 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.bean.AndroidPlatformConfiguration; 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; @@ -32,6 +33,7 @@ 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.validation.Valid; import javax.ws.rs.*; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; @@ -84,10 +86,12 @@ public class DeviceTypeConfigurationServiceImpl implements DeviceTypeConfigurati @PUT @Override - public Response updateConfiguration(PlatformConfiguration configuration) { + public Response updateConfiguration(@Valid AndroidPlatformConfiguration androidPlatformConfiguration) { String msg; Message responseMsg = new Message(); ConfigurationEntry licenseEntry = null; + PlatformConfiguration configuration = new PlatformConfiguration(); + configuration.setConfiguration(androidPlatformConfiguration.getConfiguration()); try { configuration.setType(DeviceManagementConstants.MobileDeviceTypes.MOBILE_DEVICE_TYPE_ANDROID); List configs = configuration.getConfiguration(); 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 cdbcab310..c933c4bb1 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 @@ -32,6 +32,9 @@ 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.validation.Valid; +import javax.validation.constraints.Pattern; +import javax.validation.constraints.Size; import javax.ws.rs.*; import javax.ws.rs.core.Response; import java.util.List; @@ -45,7 +48,7 @@ public class EventReceiverServiceImpl implements EventReceiverService { @POST @Path("/publish") @Override - public Response publishEvents(EventBeanWrapper eventBeanWrapper) { + public Response publishEvents(@Valid EventBeanWrapper eventBeanWrapper) { if (log.isDebugEnabled()) { log.debug("Invoking Android device even logging."); } @@ -73,8 +76,14 @@ public class EventReceiverServiceImpl implements EventReceiverService { @GET @Override - public Response retrieveAlerts(@QueryParam("id") String deviceId, @QueryParam("from") long from, - @QueryParam("to") long to, @QueryParam("type") String type, + public Response retrieveAlerts(@QueryParam("id") + @Size(min = 2, max = 45) + @Pattern(regexp = "^[A-Za-z0-9]*$") String deviceId, + @QueryParam("from") long from, + @QueryParam("to") long to, + @Size(min = 2, max = 45) + @Pattern(regexp = "^[A-Za-z0-9]*$") + @QueryParam("type") String type, @HeaderParam("If-Modified-Since") String ifModifiedSince) { if (from != 0l && to != 0l && deviceId != null) { 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/AndroidConstants.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/AndroidConstants.java index 7aad2f7b1..905783dcb 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/AndroidConstants.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/AndroidConstants.java @@ -24,6 +24,8 @@ package org.wso2.carbon.mdm.services.android.util; public final class AndroidConstants { public static final String DEVICE_TYPE_ANDROID = "android"; + public static final String HEADER_CONTENT_TYPE = "Content-Type"; + public static final String APPLICATION_JSON = "application/json"; public final class DeviceProperties { private DeviceProperties() { @@ -130,4 +132,11 @@ public final class AndroidConstants { public static final String ICON = "icon"; } + public final class ErrorMessages { + private ErrorMessages () { throw new AssertionError(); } + + public static final String STATUS_BAD_REQUEST_MESSAGE_DEFAULT = "Bad Request"; + + } + } 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 9c4c660a7..58d8d4895 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,8 +20,14 @@ 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.bean.ErrorListItem; +import org.wso2.carbon.mdm.services.android.bean.ErrorResponse; +import org.wso2.carbon.mdm.services.android.exception.BadRequestException; + +import javax.validation.ConstraintViolation; import java.util.ArrayList; import java.util.List; +import java.util.Set; /** * Util class for holding Android device related util methods. @@ -86,4 +92,50 @@ public class AndroidDeviceUtils { } return true; } + + /** + * Returns a new BadRequestException + * + * @param description description of the exception + * @return a new BadRequestException with the specified details as a response DTO + */ + public static BadRequestException buildBadRequestException(String description) { + ErrorResponse errorResponse = getErrorResponse(AndroidConstants. + ErrorMessages.STATUS_BAD_REQUEST_MESSAGE_DEFAULT,400l, description); + return new BadRequestException(errorResponse); + } + + /** + * Returns generic ErrorResponse. + * @param message specific error message + * @param code + * @param description + * @return generic Response with error specific details. + */ + public static ErrorResponse getErrorResponse(String message, Long code, String description) { + ErrorResponse errorResponse = new ErrorResponse(); + errorResponse.setCode(code); + errorResponse.setMoreInfo(""); + errorResponse.setMessage(message); + errorResponse.setDescription(description); + return errorResponse; + } + + public static ErrorResponse getConstraintViolationErrorDTO(Set> violations) { + ErrorResponse errorResponse = new ErrorResponse(); + errorResponse.setDescription("Validation Error"); + errorResponse.setMessage("Bad Request"); + errorResponse.setCode(400l); + errorResponse.setMoreInfo(""); + List errorListItems = new ArrayList<>(); + for (ConstraintViolation violation : violations) { + ErrorListItem errorListItemDTO = new ErrorListItem(); + errorListItemDTO.setCode(400 + "_" + violation.getPropertyPath()); + errorListItemDTO.setMessage(violation.getPropertyPath() + ": " + violation.getMessage()); + errorListItems.add(errorListItemDTO); + } + errorResponse.setErrorItems(errorListItems); + return errorResponse; + } + } diff --git a/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.api/src/main/webapp/WEB-INF/cxf-servlet.xml b/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.api/src/main/webapp/WEB-INF/cxf-servlet.xml index cc020be21..f6095bd02 100644 --- a/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.api/src/main/webapp/WEB-INF/cxf-servlet.xml +++ b/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.api/src/main/webapp/WEB-INF/cxf-servlet.xml @@ -18,11 +18,9 @@ --> - + xmlns:jaxrs="http://cxf.apache.org/jaxrs" xmlns:cxf="http://cxf.apache.org/core" + xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd + http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd"> @@ -35,11 +33,18 @@ + + + + + + + @@ -60,5 +65,10 @@ + + + + + diff --git a/pom.xml b/pom.xml index df3853bec..ce6d774dc 100644 --- a/pom.xml +++ b/pom.xml @@ -1062,6 +1062,11 @@ javax.ws.rs-api ${javax.version} + + org.hibernate + hibernate-validator + ${hibernate-validator.version} + @@ -1204,6 +1209,8 @@ github-scm 2.5 + + 5.0.2.Final