1 package com.srv4pos.server.api.activation; 2 3 import com.srv4pos.server.api.infrastructure.EmptyConnectionWriter; 4 import com.srv4pos.server.api.infrastructure.HttpMethod; 5 import com.srv4pos.server.api.infrastructure.StringConnectionWriter; 6 import com.srv4pos.server.api.infrastructure.credentials.Credentials; 7 import com.srv4pos.server.api.infrastructure.exceptions.enums.ForbiddenErrorType; 8 import com.srv4pos.server.api.infrastructure.exceptions.enums.InconsistentDataErrorType; 9 import com.srv4pos.server.api.infrastructure.exceptions.enums.UnprocessableEntityErrorType; 10 import com.srv4pos.server.api.infrastructure.http.HttpHelper; 11 import com.srv4pos.server.api.infrastructure.http.Transport; 12 13 import java.net.URL; 14 import java.util.Collection; 15 import java.util.Date; 16 import java.util.HashMap; 17 import java.util.Map; 18 import java.util.Set; 19 20 import static com.srv4pos.server.api.infrastructure.request.RequestHelper.requestString; 21 22 /** 23 * This service is responsible for activating registartions. 24 * <p>User: Kirill, Date: 01.10.13 11:21</p>. 25 */ 26 public class ActivationService { 27 28 private URL url; 29 private Credentials credentials; 30 private Transport transport; 31 32 /** 33 * Constructs the service. 34 * 35 * @param url of the server 36 * @param credentials to work with the server 37 * @param transport to know the method to reach the server 38 */ 39 public ActivationService(URL url, Credentials credentials, Transport transport) { 40 this.url = url; 41 this.credentials = credentials; 42 this.transport = transport; 43 } 44 45 /** 46 * Checks that cash register name is unique for particular seller. 47 * 48 * @param cashRegisterNameExistsInfo see javadoc of members of this type for more info 49 * @return true if exists 50 */ 51 public boolean cashRegisterNameExists(CashRegisterNameExistsInfo cashRegisterNameExistsInfo) { 52 return CashRegisterNameExistsResultInfo.fromJsonToCashRegisterNameExistsResultInfo( 53 requestString( 54 "noauth/activations/cash-register-name/existance", 55 HttpMethod.POST, 56 url, 57 null, 58 null, 59 null, 60 new StringConnectionWriter(cashRegisterNameExistsInfo.toJson()), 61 HttpHelper.CONTENT_TYPE_APPLICATION_JSON, 62 transport).getContent()).isExists(); 63 } 64 65 /** 66 * This method requires credentials and returns {@link ActivationCodeInfo} for provided credentials. 67 * It must be authenticated by credentials initialized by {@link Credentials#(String, String, String, String)} 68 * 69 * @param credentials to authenticate to pass activationSecurityToken as {@link Credentials#(String, String, String, String)} 70 * @return {@link ActivationDescriptorInfo}, it's signature and it's version (to correctly check digital signature) 71 * @throws com.srv4pos.server.api.exceptions.ForbiddenJsonException see: 72 * {@link ForbiddenErrorType#ACTIVATION_STILL_ISSUED}, 73 * {@link ForbiddenErrorType#WRONG_USERNAME_OR_PASSWORD} 74 */ 75 public ActivationCodeInfo getActivationCode(Credentials credentials) { 76 return ActivationCodeInfo.fromJsonToActivationCodeInfo( 77 requestString( 78 "activations/activation-code", 79 HttpMethod.POST, 80 url, 81 credentials.toAuthentication(), 82 null, 83 null, 84 EmptyConnectionWriter.EMPTY_CONNECTION_WRITER, 85 HttpHelper.CONTENT_TYPE_APPLICATION_JSON, 86 transport).getContent()); 87 } 88 89 /** 90 * Creates an activation. 91 * 92 * @param activationConnectInfo information about Activation connection needed 93 * @return the ActivationConnectReplyInfo 94 * @throws com.srv4pos.server.api.exceptions.ReferenceNotFoundJsonException if Activation not found by code 95 * @throws com.srv4pos.server.api.exceptions.InconsistentDataJsonException see: 96 * {@link InconsistentDataErrorType#ACTIVATION_CODE_WRONG_LENGTH}, 97 * {@link InconsistentDataErrorType#PRODUCTION_NUMBER_AND_CONNECTION_MISMATCH}, 98 * {@link InconsistentDataErrorType#PRODUCTION_NUMBER_AND_DEVICE_ID_MISMATCH} 99 * @throws com.srv4pos.server.api.exceptions.UnprocessableEntityJsonException see: 100 * {@link UnprocessableEntityErrorType#APPLICATION_PACKAGES_MISMATCH} 101 */ 102 public ActivationSecurityInfo connect(ActivationConnectInfo activationConnectInfo) { 103 return ActivationSecurityInfo.fromJsonToActivationSecurityInfo(requestString( 104 "/activations/connection", 105 HttpMethod.POST, 106 url, 107 null, 108 null, 109 null, 110 new StringConnectionWriter(activationConnectInfo.toJson()), 111 HttpHelper.CONTENT_TYPE_APPLICATION_JSON, 112 transport).getContent()); 113 } 114 115 /** 116 * Returns list of activations with tokens and descriptor info. It's needed for replication activations from a cloud server to a local server. 117 * 118 * @return list of entities 119 */ 120 public Collection<FullActivationAdvancedInfo> listFull() { 121 return FullActivationAdvancedInfo.fromJsonArrayToFullActivationAdvancedInfoes(requestString( 122 "/activations-full", 123 HttpMethod.GET, 124 url, 125 credentials.toAuthentication(), 126 null, 127 new HashMap<String, String>(), 128 EmptyConnectionWriter.EMPTY_CONNECTION_WRITER, 129 HttpHelper.CONTENT_TYPE_APPLICATION_JSON, 130 transport).getContent()); 131 } 132 133 /** 134 * Creates activation. 135 * Seller and/or installation will be created, if it's needed. 136 * 137 * @param activationCreationInfo information about activation 138 * @return ActivationSecurity - entity contains unique secure random identifier which represents activation ordered, production number and activation id. 139 * @throws com.srv4pos.server.api.exceptions.ReferenceNotFoundJsonException if installation, application package or control unit are not found 140 * @throws com.srv4pos.server.api.exceptions.NonUniqueJsonException if cash register name is not unique within a seller 141 * @throws com.srv4pos.server.api.exceptions.InconsistentDataJsonException see: 142 * {@link InconsistentDataErrorType#ACTIVATION_CODE_ALREADY_EXISTS}, 143 * {@link InconsistentDataErrorType#VALID_FROM_AND_VALID_TO_INCONSISTENCY}, 144 * {@link InconsistentDataErrorType#SV_MISMATCH_IN_APP_DESC_AND_CU} 145 * {@link InconsistentDataErrorType#REGISTRATION_EXISTS_FOR_SELLER_AND_INSTALLATION} 146 * {@link InconsistentDataErrorType#ACTIVATION_APPLICATION_PACKAGE_MUST_BE_NULL_OR_EQUAL} 147 * {@link InconsistentDataErrorType#ACTIVATION_PRODUCTION_NUMBER_SHOULD_BE_NULL} 148 * {@link InconsistentDataErrorType#ACTIVATION_PRODUCTION_NUMBER_SHOULD_BE_NULL_OR_EQUAL} 149 * @throws com.srv4pos.server.api.exceptions.UnprocessableEntityJsonException see: 150 * {@link UnprocessableEntityErrorType#CU_LOCATION_MUST_BE_SET} 151 * {@link UnprocessableEntityErrorType#CU_IS_MISSING_FOR_ACTIVATION} 152 * {@link UnprocessableEntityErrorType#PRODUCTION_NUMBER_IS_OUT_OF_DATE} 153 */ 154 public ActivationSecurityInfo create(ActivationCreationInfo activationCreationInfo) { 155 final String content = requestString( 156 "/activations-advanced", 157 HttpMethod.POST, 158 url, 159 credentials != null ? credentials.toAuthentication() : null, 160 null, 161 null, 162 new StringConnectionWriter(activationCreationInfo.toJson()), 163 HttpHelper.CONTENT_TYPE_APPLICATION_JSON, 164 transport).getContent(); 165 return ActivationSecurityInfo.fromJsonToActivationSecurityInfo(content); 166 } 167 168 /** 169 * If current application does not have Activation the app uses this method to request (order) new one. 170 * Seller and/or installation will be created, if it's needed. 171 * 172 * @param country where seller is located 173 * @param corporateId of the seller 174 * @param cashRegisterName the name for the cash register 175 * @param applicationPackage identifies the application description 176 * @param features the features 177 * @param validFrom the start date for activation interval 178 * @param validTo the end date for activation interval 179 * @param contactInfo contact information, can be null 180 * @param comment the comment for the activation, can be null 181 * @param controlUnitSerial the control unit serial, can be null 182 * @param controlUnitLocation the control unit location, can be null 183 * @param controlUnitGeolocation the control unit geolocation, can be null 184 * @param deviceId id of device 185 * @param buildInfo information about android build 186 * @param gcmRegistrationId the gcm registration id, can be null 187 * @return ActivationSecurity - entity contains unique secure random identifier which represents activation ordered, production number and activation id. 188 * @throws com.srv4pos.server.api.exceptions.ReferenceNotFoundJsonException if application package or control unit are not found 189 * @throws com.srv4pos.server.api.exceptions.NonUniqueJsonException if cash register name is not unique within a seller 190 * @throws com.srv4pos.server.api.exceptions.InconsistentDataJsonException see: 191 * {@link InconsistentDataErrorType#VALID_FROM_AND_VALID_TO_INCONSISTENCY}, 192 * {@link InconsistentDataErrorType#REGISTRATION_EXISTS_FOR_SELLER_AND_INSTALLATION} 193 * {@link InconsistentDataErrorType#SV_MISMATCH_IN_APP_DESC_AND_CU} 194 * @throws com.srv4pos.server.api.exceptions.UnprocessableEntityJsonException see: 195 * {@link UnprocessableEntityErrorType#CU_LOCATION_MUST_BE_SET} 196 * {@link UnprocessableEntityErrorType#CU_IS_MISSING_FOR_ACTIVATION} 197 */ 198 //CHECKSTYLE:OFF:ParameterNumber 199 public ActivationSecurityInfo claim(String country, String corporateId, String cashRegisterName, String applicationPackage, 200 Set<Feature> features, Date validFrom, Date validTo, ContactInfo contactInfo, String comment, 201 String controlUnitSerial, ControlUnitLocation controlUnitLocation, GeolocationInfo controlUnitGeolocation, 202 String deviceId, Map<String, String> buildInfo, String gcmRegistrationId) { 203 //CHECKSTYLE:ON:ParameterNumber 204 ActivationCreationInfo activationCreationInfo = 205 newActivationCreationInfo(country, corporateId, cashRegisterName, applicationPackage, features, validFrom, validTo, contactInfo, 206 comment, controlUnitSerial, controlUnitLocation, controlUnitGeolocation, deviceId, buildInfo, gcmRegistrationId, controlUnitGeolocation); 207 208 return create(activationCreationInfo); 209 } 210 211 /** 212 * If current application does not have Activation, the app uses this method to request (order) new one. 213 * Seller and/or installation will be created, if it's needed. 214 * 215 * @param country where seller is located 216 * @param corporateId of the seller 217 * @param cashRegisterName the name for the cash register 218 * @param applicationPackage identifies the application description 219 * @param features the features 220 * @param validFrom the start date for activation interval 221 * @param validTo the end date for activation interval 222 * @param contactInfo contact information, can be null 223 * @param comment the comment for the activation, can be null 224 * @param deviceId id of device 225 * @param buildInfo information about android build 226 * @return ActivationSecurity - entity contains unique secure random identifier which represents activation ordered, production number and activation id. 227 * @throws com.srv4pos.server.api.exceptions.ReferenceNotFoundJsonException if application package is not found 228 * @throws com.srv4pos.server.api.exceptions.NonUniqueJsonException if cash register name is not unique within a seller 229 * @throws com.srv4pos.server.api.exceptions.InconsistentDataJsonException see: 230 * {@link InconsistentDataErrorType#VALID_FROM_AND_VALID_TO_INCONSISTENCY}, 231 * {@link InconsistentDataErrorType#REGISTRATION_EXISTS_FOR_SELLER_AND_INSTALLATION} 232 */ 233 public ActivationSecurityInfo claim(String country, String corporateId, String cashRegisterName, String applicationPackage, 234 Set<Feature> features, Date validFrom, Date validTo, ContactInfo contactInfo, String comment, 235 String deviceId, Map<String, String> buildInfo) { 236 return claim(country, corporateId, cashRegisterName, applicationPackage, features, validFrom, validTo, contactInfo, comment, null, null, 237 null, deviceId, buildInfo, null); 238 } 239 240 /** 241 * Prolongation for the activation, which is identified by country, corporateId, cashRegisterName. 242 * 243 * @param country where seller is located 244 * @param corporateId of the seller 245 * @param cashRegisterName the name for the cash register 246 * @param features the features 247 * @param validFrom the start date for activation interval 248 * @param validTo the end date for activation interval 249 * @param contactInfo contact information, can be null 250 * @param comment the comment for the activation, can be null 251 * @param controlUnitSerial the control unit serial, can be null 252 * @param controlUnitLocation the control unit location, can be null 253 * @param controlUnitGeolocation the control unit geolocation, can be null 254 * @return ActivationSecurity - entity contains unique secure random identifier which represents activation ordered, production number and activation id. 255 * @throws com.srv4pos.server.api.exceptions.ReferenceNotFoundJsonException if installation or control unit are not found 256 * @throws com.srv4pos.server.api.exceptions.NonUniqueJsonException if cash register name is not unique within a seller 257 * @throws com.srv4pos.server.api.exceptions.InconsistentDataJsonException see: 258 * {@link InconsistentDataErrorType#VALID_FROM_AND_VALID_TO_INCONSISTENCY}, 259 * {@link InconsistentDataErrorType#SV_MISMATCH_IN_APP_DESC_AND_CU} 260 * @throws com.srv4pos.server.api.exceptions.UnprocessableEntityJsonException see: 261 * {@link UnprocessableEntityErrorType#CU_LOCATION_MUST_BE_SET} 262 * {@link UnprocessableEntityErrorType#CU_IS_MISSING_FOR_ACTIVATION} 263 * {@link UnprocessableEntityErrorType#PRODUCTION_NUMBER_IS_OUT_OF_DATE} 264 */ 265 public ActivationSecurityInfo up(String country, String corporateId, String cashRegisterName, 266 Set<Feature> features, Date validFrom, Date validTo, ContactInfo contactInfo, String comment, 267 String controlUnitSerial, ControlUnitLocation controlUnitLocation, GeolocationInfo controlUnitGeolocation) { 268 ActivationCreationInfo activationCreationInfo = new ActivationCreationInfo(); 269 270 activationCreationInfo.setCorporateId(corporateId); 271 activationCreationInfo.setCountry(country); 272 activationCreationInfo.setCashRegisterName(cashRegisterName); 273 activationCreationInfo.setComment(comment); 274 activationCreationInfo.setContactInfo(contactInfo); 275 activationCreationInfo.setControlUnitGeolocation(controlUnitGeolocation); 276 activationCreationInfo.setControlUnitLocation(controlUnitLocation); 277 activationCreationInfo.setControlUnitSerial(controlUnitSerial); 278 activationCreationInfo.setFeatures(features); 279 activationCreationInfo.setValidFrom(validFrom); 280 activationCreationInfo.setValidTo(validTo); 281 282 return create(activationCreationInfo); 283 } 284 285 /** 286 * Prolongation for the activation, which is identified by country, corporateId, cashRegisterName. 287 * 288 * @param country where seller is located 289 * @param corporateId of the seller 290 * @param cashRegisterName the name for the cash register 291 * @param features the features 292 * @param validFrom the start date for activation interval 293 * @param validTo the end date for activation interval 294 * @param contactInfo contact information, can be null 295 * @param comment the comment for the activation, can be null 296 * @return ActivationSecurity - entity contains unique secure random identifier which represents activation ordered, production number and activation id. 297 * @throws com.srv4pos.server.api.exceptions.ReferenceNotFoundJsonException if installation is not found 298 * @throws com.srv4pos.server.api.exceptions.NonUniqueJsonException if cash register name is not unique within a seller 299 * @throws com.srv4pos.server.api.exceptions.InconsistentDataJsonException see: 300 * {@link InconsistentDataErrorType#VALID_FROM_AND_VALID_TO_INCONSISTENCY}, 301 * @throws com.srv4pos.server.api.exceptions.UnprocessableEntityJsonException see: 302 * {@link UnprocessableEntityErrorType#PRODUCTION_NUMBER_IS_OUT_OF_DATE} 303 */ 304 public ActivationSecurityInfo up(String country, String corporateId, String cashRegisterName, 305 Set<Feature> features, Date validFrom, Date validTo, ContactInfo contactInfo, String comment) { 306 return up(country, corporateId, cashRegisterName, features, validFrom, validTo, contactInfo, comment, null, null, null); 307 } 308 309 310 public static ActivationCreationInfo newActivationCreationInfo(String country, String corporateId, String cashRegisterName, 311 String applicationPackage, Set<Feature> features, Date validFrom, Date validTo, 312 ContactInfo contactInfo, String comment, String controlUnitSerial, 313 ControlUnitLocation controlUnitLocation, GeolocationInfo controlUnitGeolocation, 314 String deviceId, 315 Map<String, String> buildInfo, String gcmRegistrationId, 316 GeolocationInfo registrationLocation) { 317 ActivationCreationInfo activationCreationInfo = new ActivationCreationInfo(); 318 319 if (deviceId != null) { 320 InstallationCreationInfo installationCreationInfo = new InstallationCreationInfo(); 321 installationCreationInfo.setDeviceId(deviceId); 322 installationCreationInfo.setBuildInfo(buildInfo); 323 installationCreationInfo.setGcmRegistrationId(gcmRegistrationId); 324 325 activationCreationInfo.setInstallationCreationInfo(installationCreationInfo); 326 } 327 328 activationCreationInfo.setCorporateId(corporateId); 329 activationCreationInfo.setCountry(country); 330 activationCreationInfo.setApplicationPackage(applicationPackage); 331 activationCreationInfo.setCashRegisterName(cashRegisterName); 332 activationCreationInfo.setComment(comment); 333 activationCreationInfo.setContactInfo(contactInfo); 334 activationCreationInfo.setControlUnitGeolocation(controlUnitGeolocation); 335 activationCreationInfo.setControlUnitLocation(controlUnitLocation); 336 activationCreationInfo.setControlUnitSerial(controlUnitSerial); 337 activationCreationInfo.setFeatures(features); 338 activationCreationInfo.setValidFrom(validFrom); 339 activationCreationInfo.setValidTo(validTo); 340 activationCreationInfo.setRegistrationGeolocation(registrationLocation); 341 return activationCreationInfo; 342 } 343 }