Search in sources :

Example 1 with MangoStoreClient

use of com.infiniteautomation.mango.rest.v2.util.MangoStoreClient in project ma-modules-public by infiniteautomation.

the class ModulesRestController method downloadLicense.

@PreAuthorize("isAdmin()")
@ApiOperation(value = "Download your license from the store", notes = "Admin Only")
@RequestMapping(method = RequestMethod.PUT, consumes = { "application/json" }, produces = { "application/json" }, value = "/download-license")
public ResponseEntity<Void> downloadLicense(@ApiParam(value = "Connection retries", required = false, defaultValue = "0", allowMultiple = false) @RequestParam(required = false, defaultValue = "0") int retries, @ApiParam(value = "User Credentials", required = true) @RequestBody(required = true) CredentialsModel model, HttpServletRequest request) {
    try {
        String storeUrl = Common.envProps.getString("store.url");
        // Login to the store
        MangoStoreClient client = new MangoStoreClient(storeUrl);
        client.login(model.getUsername(), model.getPassword(), retries);
        // Send the token request
        String guid = Providers.get(ICoreLicense.class).getGuid();
        String distributor = Common.envProps.getString("distributor");
        String token = client.getLicenseToken(guid, distributor, retries);
        // With the token we can make the request to download the file
        String license = client.getLicense(token, retries);
        saveLicense(license);
        return new ResponseEntity<Void>(HttpStatus.OK);
    } catch (Exception e) {
        throw new GenericRestException(HttpStatus.INTERNAL_SERVER_ERROR, e);
    }
}
Also used : MangoStoreClient(com.infiniteautomation.mango.rest.v2.util.MangoStoreClient) ResponseEntity(org.springframework.http.ResponseEntity) JsonString(com.serotonin.json.type.JsonString) ICoreLicense(com.serotonin.m2m2.ICoreLicense) BadRequestException(com.infiniteautomation.mango.rest.v2.exception.BadRequestException) GenericRestException(com.infiniteautomation.mango.rest.v2.exception.GenericRestException) FileNotFoundException(java.io.FileNotFoundException) ModuleRestV2Exception(com.infiniteautomation.mango.rest.v2.exception.ModuleRestV2Exception) SocketTimeoutException(java.net.SocketTimeoutException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) GenericRestException(com.infiniteautomation.mango.rest.v2.exception.GenericRestException) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

BadRequestException (com.infiniteautomation.mango.rest.v2.exception.BadRequestException)1 GenericRestException (com.infiniteautomation.mango.rest.v2.exception.GenericRestException)1 ModuleRestV2Exception (com.infiniteautomation.mango.rest.v2.exception.ModuleRestV2Exception)1 MangoStoreClient (com.infiniteautomation.mango.rest.v2.util.MangoStoreClient)1 JsonString (com.serotonin.json.type.JsonString)1 ICoreLicense (com.serotonin.m2m2.ICoreLicense)1 ApiOperation (com.wordnik.swagger.annotations.ApiOperation)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 SocketTimeoutException (java.net.SocketTimeoutException)1 UnknownHostException (java.net.UnknownHostException)1 ResponseEntity (org.springframework.http.ResponseEntity)1 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1