Search in sources :

Example 1 with APIManagementProviderService

use of org.wso2.carbon.apimgt.application.extension.APIManagementProviderService in project product-iots by wso2.

the class APIUtil method getAPIManagementProviderService.

/**
 * @return api management service of current context
 */
public static APIManagementProviderService getAPIManagementProviderService() {
    PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext();
    APIManagementProviderService apiManagementProviderService = (APIManagementProviderService) ctx.getOSGiService(APIManagementProviderService.class, null);
    if (apiManagementProviderService == null) {
        String msg = "API management provider service has not initialized.";
        log.error(msg);
        throw new IllegalStateException(msg);
    }
    return apiManagementProviderService;
}
Also used : APIManagementProviderService(org.wso2.carbon.apimgt.application.extension.APIManagementProviderService) PrivilegedCarbonContext(org.wso2.carbon.context.PrivilegedCarbonContext)

Example 2 with APIManagementProviderService

use of org.wso2.carbon.apimgt.application.extension.APIManagementProviderService in project product-iots by wso2.

the class DeviceTypeServiceImpl method createDownloadFile.

private ZipArchive createDownloadFile(String owner, String deviceName, String sketchType) throws DeviceManagementException, JWTClientException, APIManagerException, UserStoreException {
    // create new device id
    String deviceId = shortUUID();
    if (apiApplicationKey == null) {
        String applicationUsername = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUserRealm().getRealmConfiguration().getAdminUserName();
        applicationUsername = applicationUsername + "@" + APIUtil.getAuthenticatedUserTenantDomain();
        APIManagementProviderService apiManagementProviderService = APIUtil.getAPIManagementProviderService();
        String[] tags = { DeviceTypeConstants.DEVICE_TYPE };
        apiApplicationKey = apiManagementProviderService.generateAndRetrieveApplicationKeys(DeviceTypeConstants.DEVICE_TYPE, tags, KEY_TYPE, applicationUsername, true, "3600");
    }
    JWTClient jwtClient = APIUtil.getJWTClientManagerService().getJWTClient();
    String scopes = "device_type_" + DeviceTypeConstants.DEVICE_TYPE + " device_" + deviceId;
    AccessTokenInfo accessTokenInfo = jwtClient.getAccessToken(apiApplicationKey.getConsumerKey(), apiApplicationKey.getConsumerSecret(), owner + "@" + APIUtil.getAuthenticatedUserTenantDomain(), scopes);
    // create token
    String accessToken = accessTokenInfo.getAccessToken();
    String refreshToken = accessTokenInfo.getRefreshToken();
    boolean status = register(deviceId, deviceName);
    if (!status) {
        String msg = "Error occurred while registering the device with " + "id: " + deviceId + " owner:" + owner;
        throw new DeviceManagementException(msg);
    }
    ZipUtil ziputil = new ZipUtil();
    ZipArchive zipFile = ziputil.createZipFile(owner, APIUtil.getTenantDomainOftheUser(), sketchType, deviceId, deviceName, accessToken, refreshToken, apiApplicationKey.toString());
    return zipFile;
}
Also used : AccessTokenInfo(org.wso2.carbon.identity.jwt.client.extension.dto.AccessTokenInfo) DeviceManagementException(org.wso2.carbon.device.mgt.common.DeviceManagementException) APIManagementProviderService(org.wso2.carbon.apimgt.application.extension.APIManagementProviderService) JWTClient(org.wso2.carbon.identity.jwt.client.extension.JWTClient) ZipUtil(org.wso2.iot.sampledevice.api.util.ZipUtil) ZipArchive(org.wso2.iot.sampledevice.api.util.ZipArchive)

Aggregations

APIManagementProviderService (org.wso2.carbon.apimgt.application.extension.APIManagementProviderService)2 PrivilegedCarbonContext (org.wso2.carbon.context.PrivilegedCarbonContext)1 DeviceManagementException (org.wso2.carbon.device.mgt.common.DeviceManagementException)1 JWTClient (org.wso2.carbon.identity.jwt.client.extension.JWTClient)1 AccessTokenInfo (org.wso2.carbon.identity.jwt.client.extension.dto.AccessTokenInfo)1 ZipArchive (org.wso2.iot.sampledevice.api.util.ZipArchive)1 ZipUtil (org.wso2.iot.sampledevice.api.util.ZipUtil)1