mirror of
https://repository.entgra.net/community/device-mgt-core.git
synced 2025-10-06 02:01:45 +00:00
Fix URL Generation, Make feature, Refactor the module
This commit is contained in:
parent
34ae40e233
commit
07c157e9d6
@ -27,21 +27,13 @@
|
||||
</parent>
|
||||
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>io.entgra.devicemgt.apimgt.extension.rest.api</artifactId>
|
||||
<artifactId>io.entgra.device.mgt.core.apimgt.extension.rest.api</artifactId>
|
||||
<packaging>bundle</packaging>
|
||||
<name>Entgra - Device API Management Extension Publisher API</name>
|
||||
<description>Entgra - Device API Management Extension Publisher API</description>
|
||||
<name>Entgra - Device Management Extension for APIM REST API</name>
|
||||
<description>Entgra - Device Management Extension for APIM REST API</description>
|
||||
<url>https://entgra.io</url>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.apache.httpcomponents.wso2</groupId>
|
||||
<artifactId>httpcore</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.wso2.orbit.org.apache.httpcomponents</groupId>
|
||||
<artifactId>httpclient</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.wso2.carbon</groupId>
|
||||
<artifactId>org.wso2.carbon.logging</artifactId>
|
||||
@ -90,6 +82,11 @@
|
||||
<groupId>com.google.code.gson</groupId>
|
||||
<artifactId>gson</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.wso2.carbon.apimgt</groupId>
|
||||
<artifactId>org.wso2.carbon.apimgt.impl</artifactId>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
@ -110,31 +107,23 @@
|
||||
<Bundle-Version>${carbon.device.mgt.version}</Bundle-Version>
|
||||
<Bundle-Description>Publisher API Management Bundle</Bundle-Description>
|
||||
<Private-Package>
|
||||
io.entgra.devicemgt.apimgt.extension.rest.api.internal
|
||||
io.entgra.device.mgt.core.apimgt.extension.rest.api.internal
|
||||
</Private-Package>
|
||||
<Export-Package>
|
||||
!io.entgra.devicemgt.apimgt.extension.rest.api.internal,
|
||||
io.entgra.devicemgt.apimgt.extension.rest.api.*
|
||||
!io.entgra.device.mgt.core.apimgt.extension.rest.api.internal,
|
||||
io.entgra.device.mgt.core.apimgt.extension.rest.api.*
|
||||
</Export-Package>
|
||||
<Import-Package>
|
||||
javax.net.ssl,
|
||||
org.apache.commons.logging,
|
||||
org.apache.http;version="${httpclient.version.range}",
|
||||
org.apache.http.client;version="${httpclient.version.range}",
|
||||
org.apache.http.client.methods;version="${httpclient.version.range}",
|
||||
org.apache.http.conn.socket,
|
||||
org.apache.http.conn.ssl,
|
||||
org.apache.http.entity,
|
||||
org.apache.http.impl.client,
|
||||
org.apache.http.util;version="${httpclient.version.range}",
|
||||
|
||||
org.osgi.framework.*;version="${imp.package.version.osgi.framework}",
|
||||
org.osgi.service.*;version="${imp.package.version.osgi.service}",
|
||||
org.wso2.carbon.core,
|
||||
org.apache.commons.ssl,
|
||||
org.apache.commons.httpclient,
|
||||
org.wso2.carbon.core;version="4.6",
|
||||
org.wso2.carbon.core.util;version="4.6",
|
||||
org.wso2.carbon.apimgt.api.model,
|
||||
okhttp3.*,
|
||||
org.wso2.carbon.apimgt.impl;version="${carbon.api.mgt.version.range}",
|
||||
org.wso2.carbon.apimgt.impl.utils;version="${carbon.api.mgt.version.range}",
|
||||
org.wso2.carbon.apimgt.impl.internal;version="${carbon.api.mgt.version.range}",
|
||||
org.json
|
||||
</Import-Package>
|
||||
</instructions>
|
||||
@ -166,22 +155,7 @@
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
<!-- <plugin>-->
|
||||
<!-- <groupId>org.apache.maven.plugins</groupId>-->
|
||||
<!-- <artifactId>maven-surefire-plugin</artifactId>-->
|
||||
<!-- <configuration>-->
|
||||
<!-- <systemPropertyVariables>-->
|
||||
<!-- <log4j.configuration>file:src/test/resources/log4j.properties</log4j.configuration>-->
|
||||
<!-- </systemPropertyVariables>-->
|
||||
<!-- <suiteXmlFiles>-->
|
||||
<!-- <suiteXmlFile>src/test/resources/testng.xml</suiteXmlFile>-->
|
||||
<!-- </suiteXmlFiles>-->
|
||||
<!-- </configuration>-->
|
||||
<!-- </plugin>-->
|
||||
</plugins>
|
||||
</build>
|
||||
<properties>
|
||||
<maven.test.skip>true</maven.test.skip>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
@ -16,18 +16,18 @@
|
||||
* under the License.
|
||||
*/
|
||||
|
||||
package io.entgra.devicemgt.apimgt.extension.rest.api;
|
||||
package io.entgra.device.mgt.core.apimgt.extension.rest.api;
|
||||
|
||||
import io.entgra.devicemgt.apimgt.extension.rest.api.dto.APIApplicationKey;
|
||||
import io.entgra.devicemgt.apimgt.extension.rest.api.dto.AccessTokenInfo;
|
||||
import io.entgra.devicemgt.apimgt.extension.rest.api.exceptions.APIApplicationServicesException;
|
||||
import io.entgra.device.mgt.core.apimgt.extension.rest.api.dto.APIApplicationKey;
|
||||
import io.entgra.device.mgt.core.apimgt.extension.rest.api.dto.AccessTokenInfo;
|
||||
import io.entgra.device.mgt.core.apimgt.extension.rest.api.exceptions.APIServicesException;
|
||||
|
||||
public interface APIApplicationServices {
|
||||
|
||||
APIApplicationKey createAndRetrieveApplicationCredentials() throws APIApplicationServicesException;
|
||||
APIApplicationKey createAndRetrieveApplicationCredentials() throws APIServicesException;
|
||||
|
||||
AccessTokenInfo generateAccessTokenFromRegisteredApplication(String clientId, String clientSecret) throws APIApplicationServicesException;
|
||||
AccessTokenInfo generateAccessTokenFromRegisteredApplication(String clientId, String clientSecret) throws APIServicesException;
|
||||
|
||||
AccessTokenInfo generateAccessTokenFromRefreshToken(String refreshToken, String clientId, String clientSecret) throws APIApplicationServicesException;
|
||||
AccessTokenInfo generateAccessTokenFromRefreshToken(String refreshToken, String clientId, String clientSecret) throws APIServicesException;
|
||||
|
||||
}
|
||||
@ -16,14 +16,14 @@
|
||||
* under the License.
|
||||
*/
|
||||
|
||||
package io.entgra.devicemgt.apimgt.extension.rest.api;
|
||||
package io.entgra.device.mgt.core.apimgt.extension.rest.api;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import io.entgra.devicemgt.apimgt.extension.rest.api.dto.APIApplicationKey;
|
||||
import io.entgra.device.mgt.core.apimgt.extension.rest.api.dto.APIApplicationKey;
|
||||
import org.json.JSONObject;
|
||||
import io.entgra.devicemgt.apimgt.extension.rest.api.constants.Constants;
|
||||
import io.entgra.devicemgt.apimgt.extension.rest.api.dto.AccessTokenInfo;
|
||||
import io.entgra.devicemgt.apimgt.extension.rest.api.exceptions.APIApplicationServicesException;
|
||||
import io.entgra.device.mgt.core.apimgt.extension.rest.api.constants.Constants;
|
||||
import io.entgra.device.mgt.core.apimgt.extension.rest.api.dto.AccessTokenInfo;
|
||||
import io.entgra.device.mgt.core.apimgt.extension.rest.api.exceptions.APIServicesException;
|
||||
import okhttp3.MediaType;
|
||||
import okhttp3.OkHttpClient;
|
||||
import okhttp3.Request;
|
||||
@ -33,6 +33,8 @@ import okhttp3.Credentials;
|
||||
import okhttp3.ConnectionPool;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.wso2.carbon.apimgt.impl.APIManagerConfiguration;
|
||||
import org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder;
|
||||
|
||||
import javax.net.ssl.SSLContext;
|
||||
import javax.net.ssl.SSLSocketFactory;
|
||||
@ -50,22 +52,24 @@ public class APIApplicationServicesImpl implements APIApplicationServices {
|
||||
private static final Gson gson = new Gson();
|
||||
private static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
|
||||
String msg = null;
|
||||
// private ApiApplicationConfigurations ApplicationConfig = null;
|
||||
APIManagerConfiguration config = ServiceReferenceHolder.getInstance().
|
||||
getAPIManagerConfigurationService().getAPIManagerConfiguration();
|
||||
|
||||
@Override
|
||||
public APIApplicationKey createAndRetrieveApplicationCredentials()
|
||||
throws APIApplicationServicesException {
|
||||
throws APIServicesException {
|
||||
|
||||
String applicationEndpoint = config.getFirstProperty(Constants.DCR_END_POINT);
|
||||
String owner = config.getFirstProperty(Constants.SERVER_USER);
|
||||
|
||||
JSONObject jsonObject = new JSONObject();
|
||||
jsonObject.put("callbackUrl", Constants.EMPTY_STRING);
|
||||
jsonObject.put("clientName", Constants.CLIENT_NAME);
|
||||
jsonObject.put("grantType", Constants.GRANT_TYPE);
|
||||
jsonObject.put("owner", Constants.OWNER);
|
||||
jsonObject.put("owner", owner);
|
||||
jsonObject.put("saasApp", true);
|
||||
|
||||
RequestBody requestBody = RequestBody.Companion.create(jsonObject.toString(), JSON);
|
||||
String applicationEndpoint = "https://localhost:9443/client-registration/v0.17/register";
|
||||
|
||||
Request request = new Request.Builder()
|
||||
.url(applicationEndpoint)
|
||||
.addHeader(Constants.AUTHORIZATION_HEADER_NAME, Credentials.basic("admin", "admin"))
|
||||
@ -76,26 +80,29 @@ public class APIApplicationServicesImpl implements APIApplicationServices {
|
||||
return gson.fromJson(response.body().string(), APIApplicationKey.class);
|
||||
} catch (IOException e) {
|
||||
msg = "Error occurred while processing the response";
|
||||
log.error(msg);
|
||||
throw new APIApplicationServicesException(msg);
|
||||
log.error(msg, e);
|
||||
throw new APIServicesException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public AccessTokenInfo generateAccessTokenFromRegisteredApplication(String consumerKey, String consumerSecret)
|
||||
throws APIApplicationServicesException {
|
||||
throws APIServicesException {
|
||||
|
||||
String userName = config.getFirstProperty(Constants.SERVER_USER);
|
||||
String userPassword = config.getFirstProperty(Constants.SERVER_PASSWORD);
|
||||
|
||||
JSONObject params = new JSONObject();
|
||||
params.put(Constants.GRANT_TYPE_PARAM_NAME, Constants.PASSWORD_GRANT_TYPE);
|
||||
//ToDo: Remove hardcoded value
|
||||
params.put(Constants.PASSWORD_GRANT_TYPE_USERNAME, "admin");
|
||||
params.put(Constants.PASSWORD_GRANT_TYPE_PASSWORD, "admin");
|
||||
params.put(Constants.PASSWORD_GRANT_TYPE_USERNAME, userName);
|
||||
params.put(Constants.PASSWORD_GRANT_TYPE_PASSWORD, userPassword);
|
||||
params.put(Constants.SCOPE_PARAM_NAME, Constants.SCOPES);
|
||||
return getToken(params, consumerKey, consumerSecret);
|
||||
}
|
||||
|
||||
@Override
|
||||
public AccessTokenInfo generateAccessTokenFromRefreshToken(String refreshToken, String consumerKey, String consumerSecret)
|
||||
throws APIApplicationServicesException {
|
||||
throws APIServicesException {
|
||||
|
||||
JSONObject params = new JSONObject();
|
||||
params.put(Constants.GRANT_TYPE_PARAM_NAME, Constants.REFRESH_TOKEN_GRANT_TYPE);
|
||||
@ -105,11 +112,11 @@ public class APIApplicationServicesImpl implements APIApplicationServices {
|
||||
}
|
||||
|
||||
public AccessTokenInfo getToken(JSONObject nameValuePairs, String clientId, String clientSecret)
|
||||
throws APIApplicationServicesException {
|
||||
throws APIServicesException {
|
||||
|
||||
String tokenEndPoint = config.getFirstProperty(Constants.TOKE_END_POINT);
|
||||
|
||||
RequestBody requestBody = RequestBody.Companion.create(nameValuePairs.toString(), JSON);
|
||||
String tokenEndPoint = "https://localhost:9443/oauth2/token";
|
||||
|
||||
Request request = new Request.Builder()
|
||||
.url(tokenEndPoint)
|
||||
.addHeader(Constants.AUTHORIZATION_HEADER_NAME, Credentials.basic(clientId, clientSecret))
|
||||
@ -121,8 +128,8 @@ public class APIApplicationServicesImpl implements APIApplicationServices {
|
||||
return gson.fromJson(response.body().string(), AccessTokenInfo.class);
|
||||
} catch (IOException e) {
|
||||
msg = "Error occurred while processing the response";
|
||||
log.error(msg);
|
||||
throw new APIApplicationServicesException(msg);
|
||||
log.error(msg, e);
|
||||
throw new APIServicesException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@ -16,14 +16,14 @@
|
||||
* under the License.
|
||||
*/
|
||||
|
||||
package io.entgra.devicemgt.apimgt.extension.rest.api;
|
||||
package io.entgra.device.mgt.core.apimgt.extension.rest.api;
|
||||
|
||||
import io.entgra.devicemgt.apimgt.extension.rest.api.constants.Constants;
|
||||
import io.entgra.devicemgt.apimgt.extension.rest.api.dto.APIApplicationKey;
|
||||
import io.entgra.devicemgt.apimgt.extension.rest.api.dto.AccessTokenInfo;
|
||||
import io.entgra.devicemgt.apimgt.extension.rest.api.exceptions.APIApplicationServicesException;
|
||||
import io.entgra.devicemgt.apimgt.extension.rest.api.exceptions.BadRequestException;
|
||||
import io.entgra.devicemgt.apimgt.extension.rest.api.util.ScopeUtils;
|
||||
import io.entgra.device.mgt.core.apimgt.extension.rest.api.constants.Constants;
|
||||
import io.entgra.device.mgt.core.apimgt.extension.rest.api.dto.APIApplicationKey;
|
||||
import io.entgra.device.mgt.core.apimgt.extension.rest.api.dto.AccessTokenInfo;
|
||||
import io.entgra.device.mgt.core.apimgt.extension.rest.api.exceptions.APIServicesException;
|
||||
import io.entgra.device.mgt.core.apimgt.extension.rest.api.exceptions.BadRequestException;
|
||||
import io.entgra.device.mgt.core.apimgt.extension.rest.api.util.ScopeUtils;
|
||||
|
||||
import okhttp3.MediaType;
|
||||
import okhttp3.OkHttpClient;
|
||||
@ -39,26 +39,31 @@ import org.wso2.carbon.apimgt.api.model.Scope;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import static io.entgra.devicemgt.apimgt.extension.rest.api.APIApplicationServicesImpl.getOkHttpClient;
|
||||
import static io.entgra.device.mgt.core.apimgt.extension.rest.api.APIApplicationServicesImpl.getOkHttpClient;
|
||||
|
||||
public class PublisherRESTAPIServices {
|
||||
private static final Log log = LogFactory.getLog(PublisherRESTAPIServices.class);
|
||||
private static final OkHttpClient client = getOkHttpClient();
|
||||
private static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
|
||||
|
||||
public JSONObject getScopes(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo)
|
||||
throws APIApplicationServicesException, BadRequestException {
|
||||
private static final String host = System.getProperty(Constants.IOT_CORE_HOST);
|
||||
private static final String port = System.getProperty(Constants.IOT_CORE_HTTPS_PORT);
|
||||
|
||||
String getScopesUrl = "https://localhost:9443/api/am/publisher/v2/scopes?limit=1000";
|
||||
public JSONObject getScopes(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo)
|
||||
throws APIServicesException, BadRequestException {
|
||||
|
||||
|
||||
String getAllScopesUrl = "https://" + "://" + host + ":" + port + Constants.GET_ALL_SCOPES;
|
||||
Request request = new Request.Builder()
|
||||
.url(getScopesUrl)
|
||||
.addHeader(Constants.AUTHORIZATION_HEADER_NAME, "Bearer " + accessTokenInfo.getAccess_token())
|
||||
.url(getAllScopesUrl)
|
||||
.addHeader(Constants.AUTHORIZATION_HEADER_NAME, Constants.AUTHORIZATION_HEADER_PREFIX_BEARER
|
||||
+ accessTokenInfo.getAccess_token())
|
||||
.get()
|
||||
.build();
|
||||
|
||||
try {
|
||||
Response response = client.newCall(request).execute();
|
||||
if (response.code() == HttpStatus.SC_OK) {
|
||||
if (HttpStatus.SC_OK == response.code()) {
|
||||
JSONObject jsonObject = new JSONObject(response.body().string());
|
||||
return jsonObject;
|
||||
} else if (HttpStatus.SC_UNAUTHORIZED == response.code()) {
|
||||
@ -68,22 +73,24 @@ public class PublisherRESTAPIServices {
|
||||
//TODO: max attempt count
|
||||
return getScopes(apiApplicationKey, refreshedAccessToken);
|
||||
} else if (HttpStatus.SC_BAD_REQUEST == response.code()) {
|
||||
log.info(response);
|
||||
throw new BadRequestException(response.toString());
|
||||
String msg = "Bad Request, Invalid request";
|
||||
log.error(msg);
|
||||
throw new BadRequestException(msg);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
} catch (IOException e) {
|
||||
String msg = "Error occurred while processing the response";
|
||||
throw new APIApplicationServicesException(msg);
|
||||
log.error(msg, e);
|
||||
throw new APIServicesException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isSharedScopeNameExists(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo, String key)
|
||||
throws APIApplicationServicesException, BadRequestException {
|
||||
throws APIServicesException, BadRequestException {
|
||||
|
||||
String keyValue = new String(Base64.encodeBase64((key).getBytes())).replace("=", "");
|
||||
String getScopeUrl = "https://localhost:9443/api/am/publisher/v2/scopes/" + keyValue;
|
||||
String getScopeUrl = "https://" + "://" + host + ":" + port + Constants.GET_SCOPE + keyValue;
|
||||
|
||||
Request request = new Request.Builder()
|
||||
.url(getScopeUrl)
|
||||
@ -101,21 +108,23 @@ public class PublisherRESTAPIServices {
|
||||
//TODO: max attempt count
|
||||
return isSharedScopeNameExists(apiApplicationKey, refreshedAccessToken, key);
|
||||
} else if (HttpStatus.SC_BAD_REQUEST == response.code()) {
|
||||
log.info(response);
|
||||
throw new BadRequestException(response.toString());
|
||||
String msg = "Bad Request, Invalid request";
|
||||
log.error(msg);
|
||||
throw new BadRequestException(msg);
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
} catch (IOException e) {
|
||||
String msg = "Error occurred while processing the response";
|
||||
throw new APIApplicationServicesException(msg);
|
||||
log.error(msg, e);
|
||||
throw new APIServicesException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean updateSharedScope(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo, Scope scope)
|
||||
throws APIApplicationServicesException, BadRequestException {
|
||||
throws APIServicesException, BadRequestException {
|
||||
|
||||
String updateScopeUrl = "https://localhost:9443/api/am/publisher/v2/scopes/" + scope.getId();
|
||||
String updateScopeUrl = "https://" + "://" + host + ":" + port + Constants.GET_SCOPE + scope.getId();
|
||||
|
||||
ScopeUtils scopeUtil = new ScopeUtils();
|
||||
scopeUtil.setKey(scope.getKey());
|
||||
@ -142,14 +151,16 @@ public class PublisherRESTAPIServices {
|
||||
//TODO: max attempt count
|
||||
return updateSharedScope(apiApplicationKey, refreshedAccessToken, scope);
|
||||
} else if (HttpStatus.SC_BAD_REQUEST == response.code()) {
|
||||
log.info(response);
|
||||
throw new BadRequestException(response.toString());
|
||||
String msg = "Bad Request, Invalid scope object";
|
||||
log.error(msg);
|
||||
throw new BadRequestException(msg);
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
} catch (IOException e) {
|
||||
String msg = "Error occurred while processing the response";
|
||||
throw new APIApplicationServicesException(msg);
|
||||
log.error(msg, e);
|
||||
throw new APIServicesException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -16,7 +16,7 @@
|
||||
* under the License.
|
||||
*/
|
||||
|
||||
package io.entgra.devicemgt.apimgt.extension.rest.api.bean;
|
||||
package io.entgra.device.mgt.core.apimgt.extension.rest.api.bean;
|
||||
|
||||
/**
|
||||
* This class represents the data that are required to register
|
||||
@ -1,4 +1,22 @@
|
||||
package io.entgra.devicemgt.apimgt.extension.rest.api.constants;
|
||||
/*
|
||||
* Copyright (c) 2023, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved.
|
||||
*
|
||||
* Entgra (Pvt) Ltd. 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 io.entgra.device.mgt.core.apimgt.extension.rest.api.constants;
|
||||
|
||||
public final class Constants {
|
||||
|
||||
@ -7,7 +25,8 @@ public final class Constants {
|
||||
|
||||
public static final String EMPTY_STRING = "";
|
||||
public static final String CLIENT_NAME = "rest_api_publisher_code";
|
||||
public static final String OWNER = "admin";
|
||||
public static final String SERVER_USER = "WorkflowConfigurations.ServerUser";
|
||||
public static final String SERVER_PASSWORD = "ServerPassword";
|
||||
public static final String GRANT_TYPE = "client_credentials password refresh_token";
|
||||
public static final String REFRESH_TOKEN_GRANT_TYPE_PARAM_NAME = "refresh_token";
|
||||
public static final String OAUTH_EXPIRES_IN = "expires_in";
|
||||
@ -16,14 +35,10 @@ public final class Constants {
|
||||
public static final String REFRESH_TOKEN_GRANT_TYPE = "refresh_token";
|
||||
public static final String SCOPE_PARAM_NAME = "scope";
|
||||
public static final String SCOPES = "apim:api_create apim:api_view apim:shared_scope_manage";
|
||||
|
||||
public static final String DCR_END_POINT = "WorkflowConfigurations.DCREndPoint";
|
||||
public static final String TOKE_END_POINT = "WorkflowConfigurations.TokenEndPoint";
|
||||
public static final String ADAPTER_CONF_KEEP_ALIVE = "keepAlive";
|
||||
public static final int ADAPTER_CONF_DEFAULT_KEEP_ALIVE = 60000;
|
||||
|
||||
public static final String DEFAULT_CALLBACK = "";
|
||||
public static final String DEFAULT_PASSWORD = "";
|
||||
public static final String TOKEN_SCOPE = "production";
|
||||
public static final String APPLICATION_NAME_PREFIX = "OutputAdapter_";
|
||||
public static final String CLIENT_ID = "clientId";
|
||||
public static final String CLIENT_SECRET = "clientSecret";
|
||||
|
||||
@ -38,6 +53,10 @@ public final class Constants {
|
||||
public static final String PASSWORD_GRANT_TYPE_SCOPES = "scopes";
|
||||
public static final String ACCESS_TOKEN_GRANT_TYPE_PARAM_NAME = "access_token";
|
||||
public static final String GRANT_TYPE_PARAM_NAME = "grant_type";
|
||||
public static final String IOT_CORE_HOST = "iot.core.host";
|
||||
public static final String IOT_CORE_HTTPS_PORT = "iot.core.https.port";
|
||||
public static final String GET_ALL_SCOPES = "/api/am/publisher/v2/scopes?limit=1000";
|
||||
public static final String GET_SCOPE = "/api/am/publisher/v2/scopes/";
|
||||
}
|
||||
|
||||
|
||||
@ -16,7 +16,7 @@
|
||||
* under the License.
|
||||
*/
|
||||
|
||||
package io.entgra.devicemgt.apimgt.extension.rest.api.dto;
|
||||
package io.entgra.device.mgt.core.apimgt.extension.rest.api.dto;
|
||||
|
||||
/**
|
||||
* This holds the consumer application information that return from the register application endpoint
|
||||
@ -16,7 +16,7 @@
|
||||
* under the License.
|
||||
*/
|
||||
|
||||
package io.entgra.devicemgt.apimgt.extension.rest.api.dto;
|
||||
package io.entgra.device.mgt.core.apimgt.extension.rest.api.dto;
|
||||
|
||||
/**
|
||||
* This holds the token information that return from the token endpoint.
|
||||
@ -16,23 +16,23 @@
|
||||
* under the License.
|
||||
*/
|
||||
|
||||
package io.entgra.devicemgt.apimgt.extension.rest.api.exceptions;
|
||||
package io.entgra.device.mgt.core.apimgt.extension.rest.api.exceptions;
|
||||
|
||||
public class APIApplicationServicesException extends Exception {
|
||||
public class APIServicesException extends Exception {
|
||||
|
||||
public APIApplicationServicesException() {
|
||||
public APIServicesException() {
|
||||
super();
|
||||
}
|
||||
|
||||
public APIApplicationServicesException(String message) {
|
||||
super();
|
||||
public APIServicesException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
public APIApplicationServicesException(String message, Throwable cause) {
|
||||
super();
|
||||
public APIServicesException(String message, Throwable cause) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
public APIApplicationServicesException(Throwable cause) {
|
||||
super();
|
||||
public APIServicesException(Throwable cause) {
|
||||
super(cause);
|
||||
}
|
||||
}
|
||||
@ -19,7 +19,7 @@
|
||||
/**
|
||||
* Custom exception class for handling bad request exceptions.
|
||||
*/
|
||||
package io.entgra.devicemgt.apimgt.extension.rest.api.exceptions;
|
||||
package io.entgra.device.mgt.core.apimgt.extension.rest.api.exceptions;
|
||||
|
||||
public class BadRequestException extends Exception {
|
||||
|
||||
@ -16,17 +16,22 @@
|
||||
* under the License.
|
||||
*/
|
||||
|
||||
package io.entgra.devicemgt.apimgt.extension.rest.api.internal;
|
||||
package io.entgra.device.mgt.core.apimgt.extension.rest.api.internal;
|
||||
|
||||
import io.entgra.devicemgt.apimgt.extension.rest.api.APIApplicationServices;
|
||||
import io.entgra.device.mgt.core.apimgt.extension.rest.api.APIApplicationServices;
|
||||
import org.wso2.carbon.apimgt.impl.APIManagerConfigurationService;
|
||||
|
||||
public class PublisherRESTAPIDataHolder {
|
||||
|
||||
private static final PublisherRESTAPIDataHolder thisInstance = new PublisherRESTAPIDataHolder();
|
||||
|
||||
private APIApplicationServices apiApplicationServices;
|
||||
private APIManagerConfigurationService apiManagerConfigurationService;
|
||||
|
||||
public static PublisherRESTAPIDataHolder getInstance() {
|
||||
private static PublisherRESTAPIDataHolder thisInstance = new PublisherRESTAPIDataHolder();
|
||||
|
||||
private PublisherRESTAPIDataHolder() {
|
||||
}
|
||||
|
||||
static PublisherRESTAPIDataHolder getInstance() {
|
||||
return thisInstance;
|
||||
}
|
||||
|
||||
@ -38,4 +43,15 @@ public class PublisherRESTAPIDataHolder {
|
||||
this.apiApplicationServices = apiApplicationServices;
|
||||
}
|
||||
|
||||
public void setAPIManagerConfiguration(APIManagerConfigurationService apiManagerConfigurationService) {
|
||||
this.apiManagerConfigurationService = apiManagerConfigurationService;
|
||||
}
|
||||
|
||||
public APIManagerConfigurationService getAPIManagerConfigurationService() {
|
||||
if (apiManagerConfigurationService == null) {
|
||||
throw new IllegalStateException("API Manager Configuration service is not initialized properly");
|
||||
}
|
||||
return apiManagerConfigurationService;
|
||||
}
|
||||
|
||||
}
|
||||
@ -16,18 +16,25 @@
|
||||
* under the License.
|
||||
*/
|
||||
|
||||
package io.entgra.devicemgt.apimgt.extension.rest.api.internal;
|
||||
package io.entgra.device.mgt.core.apimgt.extension.rest.api.internal;
|
||||
|
||||
import io.entgra.devicemgt.apimgt.extension.rest.api.APIApplicationServices;
|
||||
import io.entgra.devicemgt.apimgt.extension.rest.api.APIApplicationServicesImpl;
|
||||
import io.entgra.device.mgt.core.apimgt.extension.rest.api.APIApplicationServices;
|
||||
import io.entgra.device.mgt.core.apimgt.extension.rest.api.APIApplicationServicesImpl;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.osgi.framework.BundleContext;
|
||||
import org.osgi.service.component.ComponentContext;
|
||||
import org.wso2.carbon.apimgt.impl.APIManagerConfigurationService;
|
||||
|
||||
/**
|
||||
* @scr.component name="io.entgra.devicemgt.apimgt.extension.rest.api.internal.PublisherRESTAPIServiceComponent"
|
||||
* @scr.component name="internal.io.entgra.device.mgt.core.apimgt.extension.rest.api.PublisherRESTAPIServiceComponent"
|
||||
* immediate="true"
|
||||
* @scr.reference name="user.apimanagerconfigurationservice.default"
|
||||
* interface="org.wso2.carbon.apimgt.impl.APIManagerConfigurationService"
|
||||
* cardinality="1..1"
|
||||
* policy="dynamic"
|
||||
* bind="setAPIManagerConfigurationService"
|
||||
* unbind="unsetAPIManagerConfigurationService"
|
||||
*/
|
||||
public class PublisherRESTAPIServiceComponent {
|
||||
|
||||
@ -55,4 +62,18 @@ public class PublisherRESTAPIServiceComponent {
|
||||
protected void deactivate(ComponentContext componentContext) {
|
||||
//do nothing
|
||||
}
|
||||
|
||||
protected void setAPIManagerConfigurationService(APIManagerConfigurationService apiManagerConfigurationService) {
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Setting API Manager Configuration Service");
|
||||
}
|
||||
PublisherRESTAPIDataHolder.getInstance().setAPIManagerConfiguration(apiManagerConfigurationService);
|
||||
}
|
||||
|
||||
protected void unsetAPIManagerConfigurationService(APIManagerConfigurationService apiManagerConfigurationService) {
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Unsetting API Manager Configuration Service");
|
||||
}
|
||||
PublisherRESTAPIDataHolder.getInstance().setAPIManagerConfiguration(null);
|
||||
}
|
||||
}
|
||||
@ -16,7 +16,7 @@
|
||||
* under the License.
|
||||
*/
|
||||
|
||||
package io.entgra.devicemgt.apimgt.extension.rest.api.util;
|
||||
package io.entgra.device.mgt.core.apimgt.extension.rest.api.util;
|
||||
|
||||
/**
|
||||
* This class represents the scope data.
|
||||
@ -124,7 +124,7 @@
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.wso2.carbon.devicemgt</groupId>
|
||||
<artifactId>io.entgra.devicemgt.apimgt.extension.rest.api</artifactId>
|
||||
<artifactId>io.entgra.device.mgt.core.apimgt.extension.rest.api</artifactId>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
@ -174,9 +174,9 @@
|
||||
org.scannotation;version="1.0",
|
||||
org.scannotation.archiveiterator;version="1.0",
|
||||
org.w3c.dom,
|
||||
io.entgra.devicemgt.apimgt.extension.rest.api,
|
||||
io.entgra.devicemgt.apimgt.extension.rest.api.dto,
|
||||
io.entgra.devicemgt.apimgt.extension.rest.api.exceptions,
|
||||
io.entgra.device.mgt.core.apimgt.extension.rest.api,
|
||||
io.entgra.device.mgt.core.apimgt.extension.rest.api.dto,
|
||||
io.entgra.device.mgt.core.apimgt.extension.rest.api.exceptions,
|
||||
org.wso2.carbon.apimgt.annotations.api,
|
||||
org.wso2.carbon.apimgt.api,
|
||||
org.wso2.carbon.apimgt.api.model,
|
||||
|
||||
@ -18,6 +18,7 @@
|
||||
*/
|
||||
package org.wso2.carbon.apimgt.webapp.publisher;
|
||||
|
||||
|
||||
import org.wso2.carbon.apimgt.webapp.publisher.exception.APIManagerPublisherException;
|
||||
|
||||
/**
|
||||
|
||||
@ -18,13 +18,13 @@
|
||||
*/
|
||||
package org.wso2.carbon.apimgt.webapp.publisher;
|
||||
|
||||
import io.entgra.devicemgt.apimgt.extension.rest.api.APIApplicationServices;
|
||||
import io.entgra.devicemgt.apimgt.extension.rest.api.APIApplicationServicesImpl;
|
||||
import io.entgra.devicemgt.apimgt.extension.rest.api.PublisherRESTAPIServices;
|
||||
import io.entgra.devicemgt.apimgt.extension.rest.api.dto.APIApplicationKey;
|
||||
import io.entgra.devicemgt.apimgt.extension.rest.api.dto.AccessTokenInfo;
|
||||
import io.entgra.devicemgt.apimgt.extension.rest.api.exceptions.APIApplicationServicesException;
|
||||
import io.entgra.devicemgt.apimgt.extension.rest.api.exceptions.BadRequestException;
|
||||
import io.entgra.device.mgt.core.apimgt.extension.rest.api.APIApplicationServices;
|
||||
import io.entgra.device.mgt.core.apimgt.extension.rest.api.APIApplicationServicesImpl;
|
||||
import io.entgra.device.mgt.core.apimgt.extension.rest.api.PublisherRESTAPIServices;
|
||||
import io.entgra.device.mgt.core.apimgt.extension.rest.api.dto.APIApplicationKey;
|
||||
import io.entgra.device.mgt.core.apimgt.extension.rest.api.dto.AccessTokenInfo;
|
||||
import io.entgra.device.mgt.core.apimgt.extension.rest.api.exceptions.APIServicesException;
|
||||
import io.entgra.device.mgt.core.apimgt.extension.rest.api.exceptions.BadRequestException;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
@ -373,7 +373,7 @@ public class APIPublisherServiceImpl implements APIPublisherService {
|
||||
apiApplicationKey = apiApplicationServices.createAndRetrieveApplicationCredentials();
|
||||
accessTokenInfo = apiApplicationServices.generateAccessTokenFromRegisteredApplication(
|
||||
apiApplicationKey.getClientId(), apiApplicationKey.getClientSecret());
|
||||
} catch (APIApplicationServicesException e) {
|
||||
} catch (APIServicesException e) {
|
||||
String errorMsg = "Error while generating application";
|
||||
log.error(errorMsg, e);
|
||||
throw new APIManagerPublisherException(e);
|
||||
@ -436,7 +436,8 @@ public class APIPublisherServiceImpl implements APIPublisherService {
|
||||
for (int i = 0; i < scopeList.length(); i++) {
|
||||
JSONObject scopeObj = null;
|
||||
scopeObj = scopeList.getJSONObject(i);
|
||||
if (scopeObj.getString("name").equals(scopeMapping[2] != null ? StringUtils.trim(scopeMapping[2]) : StringUtils.EMPTY)) {
|
||||
if (scopeObj.getString("name").equals(scopeMapping[2] != null ?
|
||||
StringUtils.trim(scopeMapping[2]) : StringUtils.EMPTY)) {
|
||||
jsonObject = scopeObj;
|
||||
}
|
||||
}
|
||||
@ -454,20 +455,22 @@ public class APIPublisherServiceImpl implements APIPublisherService {
|
||||
}
|
||||
} catch (IOException | DirectoryIteratorException ex) {
|
||||
log.error("failed to read scopes from file.", ex);
|
||||
} catch (APIApplicationServicesException | BadRequestException e) {
|
||||
String errorMsg = "Error while Calling APIs";
|
||||
} catch (APIServicesException | BadRequestException e) {
|
||||
String errorMsg = "Error while calling APIs";
|
||||
log.error(errorMsg, e);
|
||||
throw new APIManagerPublisherException(e);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
catch (APIApplicationServicesException e) {
|
||||
log.error(e);
|
||||
throw new RuntimeException(e);
|
||||
catch (APIServicesException e) {
|
||||
String errorMsg = "Error while processing Publisher REST API response";
|
||||
log.error(errorMsg, e);
|
||||
throw new APIManagerPublisherException(e);
|
||||
} catch (BadRequestException e) {
|
||||
log.error(e);
|
||||
throw new RuntimeException(e);
|
||||
String errorMsg = "Error while calling Publisher REST APIs";
|
||||
log.error(errorMsg, e);
|
||||
throw new APIManagerPublisherException(e);
|
||||
} finally {
|
||||
PrivilegedCarbonContext.endTenantFlow();
|
||||
}
|
||||
|
||||
@ -40,7 +40,7 @@
|
||||
<module>org.wso2.carbon.apimgt.keymgt.extension</module>
|
||||
<module>org.wso2.carbon.apimgt.keymgt.extension.api</module>
|
||||
<module>io.entgra.device.mgt.core.apimgt.analytics.extension</module>
|
||||
<module>io.entgra.devicemgt.apimgt.extension.rest.api</module>
|
||||
<module>io.entgra.device.mgt.core.apimgt.extension.rest.api</module>
|
||||
</modules>
|
||||
|
||||
<build>
|
||||
|
||||
@ -51,6 +51,10 @@
|
||||
<groupId>io.swagger</groupId>
|
||||
<artifactId>swagger-annotations</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.wso2.carbon.devicemgt</groupId>
|
||||
<artifactId>io.entgra.device.mgt.core.apimgt.extension.rest.api</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
@ -143,6 +147,9 @@
|
||||
<bundleDef>
|
||||
io.swagger:swagger-annotations:${swagger.version}
|
||||
</bundleDef>
|
||||
<bundleDef>
|
||||
org.wso2.carbon.devicemgt:io.entgra.device.mgt.core.apimgt.extension.rest.api:${carbon.device.mgt.version}
|
||||
</bundleDef>
|
||||
</bundles>
|
||||
<importFeatures>
|
||||
<importFeatureDef>org.wso2.carbon.core.server:${carbon.kernel.version}</importFeatureDef>
|
||||
|
||||
2
pom.xml
2
pom.xml
@ -240,7 +240,7 @@
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.wso2.carbon.devicemgt</groupId>
|
||||
<artifactId>io.entgra.devicemgt.apimgt.extension.rest.api</artifactId>
|
||||
<artifactId>io.entgra.device.mgt.core.apimgt.extension.rest.api</artifactId>
|
||||
<version>${carbon.device.mgt.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
|
||||
Loading…
Reference in New Issue
Block a user