Fix URL Generation, Make feature, Refactor the module

vpp-v2
Pasindu Rupasinghe 2 years ago
parent 34ae40e233
commit 07c157e9d6

@ -27,21 +27,13 @@
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <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> <packaging>bundle</packaging>
<name>Entgra - Device API Management Extension Publisher API</name> <name>Entgra - Device Management Extension for APIM REST API</name>
<description>Entgra - Device API Management Extension Publisher API</description> <description>Entgra - Device Management Extension for APIM REST API</description>
<url>https://entgra.io</url> <url>https://entgra.io</url>
<dependencies> <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> <dependency>
<groupId>org.wso2.carbon</groupId> <groupId>org.wso2.carbon</groupId>
<artifactId>org.wso2.carbon.logging</artifactId> <artifactId>org.wso2.carbon.logging</artifactId>
@ -90,6 +82,11 @@
<groupId>com.google.code.gson</groupId> <groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId> <artifactId>gson</artifactId>
</dependency> </dependency>
<dependency>
<groupId>org.wso2.carbon.apimgt</groupId>
<artifactId>org.wso2.carbon.apimgt.impl</artifactId>
<scope>provided</scope>
</dependency>
</dependencies> </dependencies>
<build> <build>
@ -110,31 +107,23 @@
<Bundle-Version>${carbon.device.mgt.version}</Bundle-Version> <Bundle-Version>${carbon.device.mgt.version}</Bundle-Version>
<Bundle-Description>Publisher API Management Bundle</Bundle-Description> <Bundle-Description>Publisher API Management Bundle</Bundle-Description>
<Private-Package> <Private-Package>
io.entgra.devicemgt.apimgt.extension.rest.api.internal io.entgra.device.mgt.core.apimgt.extension.rest.api.internal
</Private-Package> </Private-Package>
<Export-Package> <Export-Package>
!io.entgra.devicemgt.apimgt.extension.rest.api.internal, !io.entgra.device.mgt.core.apimgt.extension.rest.api.internal,
io.entgra.devicemgt.apimgt.extension.rest.api.* io.entgra.device.mgt.core.apimgt.extension.rest.api.*
</Export-Package> </Export-Package>
<Import-Package> <Import-Package>
javax.net.ssl, 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.framework.*;version="${imp.package.version.osgi.framework}",
org.osgi.service.*;version="${imp.package.version.osgi.service}", org.osgi.service.*;version="${imp.package.version.osgi.service}",
org.wso2.carbon.core, org.wso2.carbon.core;version="4.6",
org.apache.commons.ssl, org.wso2.carbon.core.util;version="4.6",
org.apache.commons.httpclient,
org.wso2.carbon.apimgt.api.model, org.wso2.carbon.apimgt.api.model,
okhttp3.*, 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 org.json
</Import-Package> </Import-Package>
</instructions> </instructions>
@ -166,22 +155,7 @@
</execution> </execution>
</executions> </executions>
</plugin> </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> </plugins>
</build> </build>
<properties>
<maven.test.skip>true</maven.test.skip>
</properties>
</project> </project>

@ -16,18 +16,18 @@
* under the License. * 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.device.mgt.core.apimgt.extension.rest.api.dto.APIApplicationKey;
import io.entgra.devicemgt.apimgt.extension.rest.api.dto.AccessTokenInfo; import io.entgra.device.mgt.core.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.exceptions.APIServicesException;
public interface APIApplicationServices { 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. * 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 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 org.json.JSONObject;
import io.entgra.devicemgt.apimgt.extension.rest.api.constants.Constants; import io.entgra.device.mgt.core.apimgt.extension.rest.api.constants.Constants;
import io.entgra.devicemgt.apimgt.extension.rest.api.dto.AccessTokenInfo; import io.entgra.device.mgt.core.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.exceptions.APIServicesException;
import okhttp3.MediaType; import okhttp3.MediaType;
import okhttp3.OkHttpClient; import okhttp3.OkHttpClient;
import okhttp3.Request; import okhttp3.Request;
@ -33,6 +33,8 @@ import okhttp3.Credentials;
import okhttp3.ConnectionPool; import okhttp3.ConnectionPool;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; 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.SSLContext;
import javax.net.ssl.SSLSocketFactory; import javax.net.ssl.SSLSocketFactory;
@ -50,22 +52,24 @@ public class APIApplicationServicesImpl implements APIApplicationServices {
private static final Gson gson = new Gson(); private static final Gson gson = new Gson();
private static final MediaType JSON = MediaType.parse("application/json; charset=utf-8"); private static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
String msg = null; String msg = null;
// private ApiApplicationConfigurations ApplicationConfig = null; APIManagerConfiguration config = ServiceReferenceHolder.getInstance().
getAPIManagerConfigurationService().getAPIManagerConfiguration();
@Override @Override
public APIApplicationKey createAndRetrieveApplicationCredentials() 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 jsonObject = new JSONObject();
jsonObject.put("callbackUrl", Constants.EMPTY_STRING); jsonObject.put("callbackUrl", Constants.EMPTY_STRING);
jsonObject.put("clientName", Constants.CLIENT_NAME); jsonObject.put("clientName", Constants.CLIENT_NAME);
jsonObject.put("grantType", Constants.GRANT_TYPE); jsonObject.put("grantType", Constants.GRANT_TYPE);
jsonObject.put("owner", Constants.OWNER); jsonObject.put("owner", owner);
jsonObject.put("saasApp", true); jsonObject.put("saasApp", true);
RequestBody requestBody = RequestBody.Companion.create(jsonObject.toString(), JSON); RequestBody requestBody = RequestBody.Companion.create(jsonObject.toString(), JSON);
String applicationEndpoint = "https://localhost:9443/client-registration/v0.17/register";
Request request = new Request.Builder() Request request = new Request.Builder()
.url(applicationEndpoint) .url(applicationEndpoint)
.addHeader(Constants.AUTHORIZATION_HEADER_NAME, Credentials.basic("admin", "admin")) .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); return gson.fromJson(response.body().string(), APIApplicationKey.class);
} catch (IOException e) { } catch (IOException e) {
msg = "Error occurred while processing the response"; msg = "Error occurred while processing the response";
log.error(msg); log.error(msg, e);
throw new APIApplicationServicesException(msg); throw new APIServicesException(e);
} }
} }
@Override @Override
public AccessTokenInfo generateAccessTokenFromRegisteredApplication(String consumerKey, String consumerSecret) 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(); JSONObject params = new JSONObject();
params.put(Constants.GRANT_TYPE_PARAM_NAME, Constants.PASSWORD_GRANT_TYPE); params.put(Constants.GRANT_TYPE_PARAM_NAME, Constants.PASSWORD_GRANT_TYPE);
//ToDo: Remove hardcoded value params.put(Constants.PASSWORD_GRANT_TYPE_USERNAME, userName);
params.put(Constants.PASSWORD_GRANT_TYPE_USERNAME, "admin"); params.put(Constants.PASSWORD_GRANT_TYPE_PASSWORD, userPassword);
params.put(Constants.PASSWORD_GRANT_TYPE_PASSWORD, "admin");
params.put(Constants.SCOPE_PARAM_NAME, Constants.SCOPES); params.put(Constants.SCOPE_PARAM_NAME, Constants.SCOPES);
return getToken(params, consumerKey, consumerSecret); return getToken(params, consumerKey, consumerSecret);
} }
@Override @Override
public AccessTokenInfo generateAccessTokenFromRefreshToken(String refreshToken, String consumerKey, String consumerSecret) public AccessTokenInfo generateAccessTokenFromRefreshToken(String refreshToken, String consumerKey, String consumerSecret)
throws APIApplicationServicesException { throws APIServicesException {
JSONObject params = new JSONObject(); JSONObject params = new JSONObject();
params.put(Constants.GRANT_TYPE_PARAM_NAME, Constants.REFRESH_TOKEN_GRANT_TYPE); 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) public AccessTokenInfo getToken(JSONObject nameValuePairs, String clientId, String clientSecret)
throws APIApplicationServicesException { throws APIServicesException {
RequestBody requestBody = RequestBody.Companion.create(nameValuePairs.toString(), JSON); String tokenEndPoint = config.getFirstProperty(Constants.TOKE_END_POINT);
String tokenEndPoint = "https://localhost:9443/oauth2/token";
RequestBody requestBody = RequestBody.Companion.create(nameValuePairs.toString(), JSON);
Request request = new Request.Builder() Request request = new Request.Builder()
.url(tokenEndPoint) .url(tokenEndPoint)
.addHeader(Constants.AUTHORIZATION_HEADER_NAME, Credentials.basic(clientId, clientSecret)) .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); return gson.fromJson(response.body().string(), AccessTokenInfo.class);
} catch (IOException e) { } catch (IOException e) {
msg = "Error occurred while processing the response"; msg = "Error occurred while processing the response";
log.error(msg); log.error(msg, e);
throw new APIApplicationServicesException(msg); throw new APIServicesException(e);
} }
} }

@ -16,14 +16,14 @@
* under the License. * 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.device.mgt.core.apimgt.extension.rest.api.constants.Constants;
import io.entgra.devicemgt.apimgt.extension.rest.api.dto.APIApplicationKey; import io.entgra.device.mgt.core.apimgt.extension.rest.api.dto.APIApplicationKey;
import io.entgra.devicemgt.apimgt.extension.rest.api.dto.AccessTokenInfo; import io.entgra.device.mgt.core.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.exceptions.APIServicesException;
import io.entgra.devicemgt.apimgt.extension.rest.api.exceptions.BadRequestException; import io.entgra.device.mgt.core.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.util.ScopeUtils;
import okhttp3.MediaType; import okhttp3.MediaType;
import okhttp3.OkHttpClient; import okhttp3.OkHttpClient;
@ -39,26 +39,31 @@ import org.wso2.carbon.apimgt.api.model.Scope;
import java.io.IOException; 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 { public class PublisherRESTAPIServices {
private static final Log log = LogFactory.getLog(PublisherRESTAPIServices.class); private static final Log log = LogFactory.getLog(PublisherRESTAPIServices.class);
private static final OkHttpClient client = getOkHttpClient(); private static final OkHttpClient client = getOkHttpClient();
private static final MediaType JSON = MediaType.parse("application/json; charset=utf-8"); private static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
private static final String host = System.getProperty(Constants.IOT_CORE_HOST);
private static final String port = System.getProperty(Constants.IOT_CORE_HTTPS_PORT);
public JSONObject getScopes(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo) public JSONObject getScopes(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo)
throws APIApplicationServicesException, BadRequestException { throws APIServicesException, BadRequestException {
String getScopesUrl = "https://localhost:9443/api/am/publisher/v2/scopes?limit=1000"; String getAllScopesUrl = "https://" + "://" + host + ":" + port + Constants.GET_ALL_SCOPES;
Request request = new Request.Builder() Request request = new Request.Builder()
.url(getScopesUrl) .url(getAllScopesUrl)
.addHeader(Constants.AUTHORIZATION_HEADER_NAME, "Bearer " + accessTokenInfo.getAccess_token()) .addHeader(Constants.AUTHORIZATION_HEADER_NAME, Constants.AUTHORIZATION_HEADER_PREFIX_BEARER
+ accessTokenInfo.getAccess_token())
.get() .get()
.build(); .build();
try { try {
Response response = client.newCall(request).execute(); 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()); JSONObject jsonObject = new JSONObject(response.body().string());
return jsonObject; return jsonObject;
} else if (HttpStatus.SC_UNAUTHORIZED == response.code()) { } else if (HttpStatus.SC_UNAUTHORIZED == response.code()) {
@ -68,22 +73,24 @@ public class PublisherRESTAPIServices {
//TODO: max attempt count //TODO: max attempt count
return getScopes(apiApplicationKey, refreshedAccessToken); return getScopes(apiApplicationKey, refreshedAccessToken);
} else if (HttpStatus.SC_BAD_REQUEST == response.code()) { } else if (HttpStatus.SC_BAD_REQUEST == response.code()) {
log.info(response); String msg = "Bad Request, Invalid request";
throw new BadRequestException(response.toString()); log.error(msg);
throw new BadRequestException(msg);
} else { } else {
return null; return null;
} }
} catch (IOException e) { } catch (IOException e) {
String msg = "Error occurred while processing the response"; 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) 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 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() Request request = new Request.Builder()
.url(getScopeUrl) .url(getScopeUrl)
@ -101,21 +108,23 @@ public class PublisherRESTAPIServices {
//TODO: max attempt count //TODO: max attempt count
return isSharedScopeNameExists(apiApplicationKey, refreshedAccessToken, key); return isSharedScopeNameExists(apiApplicationKey, refreshedAccessToken, key);
} else if (HttpStatus.SC_BAD_REQUEST == response.code()) { } else if (HttpStatus.SC_BAD_REQUEST == response.code()) {
log.info(response); String msg = "Bad Request, Invalid request";
throw new BadRequestException(response.toString()); log.error(msg);
throw new BadRequestException(msg);
} else { } else {
return false; return false;
} }
} catch (IOException e) { } catch (IOException e) {
String msg = "Error occurred while processing the response"; 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) 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(); ScopeUtils scopeUtil = new ScopeUtils();
scopeUtil.setKey(scope.getKey()); scopeUtil.setKey(scope.getKey());
@ -142,14 +151,16 @@ public class PublisherRESTAPIServices {
//TODO: max attempt count //TODO: max attempt count
return updateSharedScope(apiApplicationKey, refreshedAccessToken, scope); return updateSharedScope(apiApplicationKey, refreshedAccessToken, scope);
} else if (HttpStatus.SC_BAD_REQUEST == response.code()) { } else if (HttpStatus.SC_BAD_REQUEST == response.code()) {
log.info(response); String msg = "Bad Request, Invalid scope object";
throw new BadRequestException(response.toString()); log.error(msg);
throw new BadRequestException(msg);
} else { } else {
return false; return false;
} }
} catch (IOException e) { } catch (IOException e) {
String msg = "Error occurred while processing the response"; 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. * 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 * 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 { public final class Constants {
@ -7,7 +25,8 @@ public final class Constants {
public static final String EMPTY_STRING = ""; public static final String EMPTY_STRING = "";
public static final String CLIENT_NAME = "rest_api_publisher_code"; 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 GRANT_TYPE = "client_credentials password refresh_token";
public static final String REFRESH_TOKEN_GRANT_TYPE_PARAM_NAME = "refresh_token"; public static final String REFRESH_TOKEN_GRANT_TYPE_PARAM_NAME = "refresh_token";
public static final String OAUTH_EXPIRES_IN = "expires_in"; 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 REFRESH_TOKEN_GRANT_TYPE = "refresh_token";
public static final String SCOPE_PARAM_NAME = "scope"; 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 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 String ADAPTER_CONF_KEEP_ALIVE = "keepAlive";
public static final int ADAPTER_CONF_DEFAULT_KEEP_ALIVE = 60000; 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_ID = "clientId";
public static final String CLIENT_SECRET = "clientSecret"; 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 PASSWORD_GRANT_TYPE_SCOPES = "scopes";
public static final String ACCESS_TOKEN_GRANT_TYPE_PARAM_NAME = "access_token"; 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 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. * 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 * This holds the consumer application information that return from the register application endpoint

@ -16,7 +16,7 @@
* under the License. * 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. * This holds the token information that return from the token endpoint.

@ -16,23 +16,23 @@
* under the License. * 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(); super();
} }
public APIApplicationServicesException(String message) { public APIServicesException(String message) {
super(); super(message);
} }
public APIApplicationServicesException(String message, Throwable cause) { public APIServicesException(String message, Throwable cause) {
super(); super(message);
} }
public APIApplicationServicesException(Throwable cause) { public APIServicesException(Throwable cause) {
super(); super(cause);
} }
} }

@ -19,7 +19,7 @@
/** /**
* Custom exception class for handling bad request exceptions. * 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 { public class BadRequestException extends Exception {

@ -16,17 +16,22 @@
* under the License. * 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 { public class PublisherRESTAPIDataHolder {
private static final PublisherRESTAPIDataHolder thisInstance = new PublisherRESTAPIDataHolder();
private APIApplicationServices apiApplicationServices; private APIApplicationServices apiApplicationServices;
private APIManagerConfigurationService apiManagerConfigurationService;
private static PublisherRESTAPIDataHolder thisInstance = new PublisherRESTAPIDataHolder();
private PublisherRESTAPIDataHolder() {
}
public static PublisherRESTAPIDataHolder getInstance() { static PublisherRESTAPIDataHolder getInstance() {
return thisInstance; return thisInstance;
} }
@ -38,4 +43,15 @@ public class PublisherRESTAPIDataHolder {
this.apiApplicationServices = apiApplicationServices; 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. * 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 io.entgra.devicemgt.apimgt.extension.rest.api.APIApplicationServicesImpl; import io.entgra.device.mgt.core.apimgt.extension.rest.api.APIApplicationServicesImpl;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.osgi.framework.BundleContext; import org.osgi.framework.BundleContext;
import org.osgi.service.component.ComponentContext; 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" * 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 { public class PublisherRESTAPIServiceComponent {
@ -55,4 +62,18 @@ public class PublisherRESTAPIServiceComponent {
protected void deactivate(ComponentContext componentContext) { protected void deactivate(ComponentContext componentContext) {
//do nothing //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. * 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. * This class represents the scope data.

@ -124,7 +124,7 @@
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.wso2.carbon.devicemgt</groupId> <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> <scope>provided</scope>
</dependency> </dependency>
<dependency> <dependency>
@ -174,9 +174,9 @@
org.scannotation;version="1.0", org.scannotation;version="1.0",
org.scannotation.archiveiterator;version="1.0", org.scannotation.archiveiterator;version="1.0",
org.w3c.dom, org.w3c.dom,
io.entgra.devicemgt.apimgt.extension.rest.api, io.entgra.device.mgt.core.apimgt.extension.rest.api,
io.entgra.devicemgt.apimgt.extension.rest.api.dto, io.entgra.device.mgt.core.apimgt.extension.rest.api.dto,
io.entgra.devicemgt.apimgt.extension.rest.api.exceptions, io.entgra.device.mgt.core.apimgt.extension.rest.api.exceptions,
org.wso2.carbon.apimgt.annotations.api, org.wso2.carbon.apimgt.annotations.api,
org.wso2.carbon.apimgt.api, org.wso2.carbon.apimgt.api,
org.wso2.carbon.apimgt.api.model, org.wso2.carbon.apimgt.api.model,

@ -18,6 +18,7 @@
*/ */
package org.wso2.carbon.apimgt.webapp.publisher; package org.wso2.carbon.apimgt.webapp.publisher;
import org.wso2.carbon.apimgt.webapp.publisher.exception.APIManagerPublisherException; import org.wso2.carbon.apimgt.webapp.publisher.exception.APIManagerPublisherException;
/** /**

@ -18,13 +18,13 @@
*/ */
package org.wso2.carbon.apimgt.webapp.publisher; package org.wso2.carbon.apimgt.webapp.publisher;
import io.entgra.devicemgt.apimgt.extension.rest.api.APIApplicationServices; import io.entgra.device.mgt.core.apimgt.extension.rest.api.APIApplicationServices;
import io.entgra.devicemgt.apimgt.extension.rest.api.APIApplicationServicesImpl; import io.entgra.device.mgt.core.apimgt.extension.rest.api.APIApplicationServicesImpl;
import io.entgra.devicemgt.apimgt.extension.rest.api.PublisherRESTAPIServices; import io.entgra.device.mgt.core.apimgt.extension.rest.api.PublisherRESTAPIServices;
import io.entgra.devicemgt.apimgt.extension.rest.api.dto.APIApplicationKey; import io.entgra.device.mgt.core.apimgt.extension.rest.api.dto.APIApplicationKey;
import io.entgra.devicemgt.apimgt.extension.rest.api.dto.AccessTokenInfo; import io.entgra.device.mgt.core.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.exceptions.APIServicesException;
import io.entgra.devicemgt.apimgt.extension.rest.api.exceptions.BadRequestException; import io.entgra.device.mgt.core.apimgt.extension.rest.api.exceptions.BadRequestException;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
@ -373,7 +373,7 @@ public class APIPublisherServiceImpl implements APIPublisherService {
apiApplicationKey = apiApplicationServices.createAndRetrieveApplicationCredentials(); apiApplicationKey = apiApplicationServices.createAndRetrieveApplicationCredentials();
accessTokenInfo = apiApplicationServices.generateAccessTokenFromRegisteredApplication( accessTokenInfo = apiApplicationServices.generateAccessTokenFromRegisteredApplication(
apiApplicationKey.getClientId(), apiApplicationKey.getClientSecret()); apiApplicationKey.getClientId(), apiApplicationKey.getClientSecret());
} catch (APIApplicationServicesException e) { } catch (APIServicesException e) {
String errorMsg = "Error while generating application"; String errorMsg = "Error while generating application";
log.error(errorMsg, e); log.error(errorMsg, e);
throw new APIManagerPublisherException(e); throw new APIManagerPublisherException(e);
@ -436,7 +436,8 @@ public class APIPublisherServiceImpl implements APIPublisherService {
for (int i = 0; i < scopeList.length(); i++) { for (int i = 0; i < scopeList.length(); i++) {
JSONObject scopeObj = null; JSONObject scopeObj = null;
scopeObj = scopeList.getJSONObject(i); 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; jsonObject = scopeObj;
} }
} }
@ -454,20 +455,22 @@ public class APIPublisherServiceImpl implements APIPublisherService {
} }
} catch (IOException | DirectoryIteratorException ex) { } catch (IOException | DirectoryIteratorException ex) {
log.error("failed to read scopes from file.", ex); log.error("failed to read scopes from file.", ex);
} catch (APIApplicationServicesException | BadRequestException e) { } catch (APIServicesException | BadRequestException e) {
String errorMsg = "Error while Calling APIs"; String errorMsg = "Error while calling APIs";
log.error(errorMsg, e); log.error(errorMsg, e);
throw new APIManagerPublisherException(e); throw new APIManagerPublisherException(e);
} }
} }
} }
catch (APIApplicationServicesException e) { catch (APIServicesException e) {
log.error(e); String errorMsg = "Error while processing Publisher REST API response";
throw new RuntimeException(e); log.error(errorMsg, e);
throw new APIManagerPublisherException(e);
} catch (BadRequestException e) { } catch (BadRequestException e) {
log.error(e); String errorMsg = "Error while calling Publisher REST APIs";
throw new RuntimeException(e); log.error(errorMsg, e);
throw new APIManagerPublisherException(e);
} finally { } finally {
PrivilegedCarbonContext.endTenantFlow(); PrivilegedCarbonContext.endTenantFlow();
} }

@ -40,7 +40,7 @@
<module>org.wso2.carbon.apimgt.keymgt.extension</module> <module>org.wso2.carbon.apimgt.keymgt.extension</module>
<module>org.wso2.carbon.apimgt.keymgt.extension.api</module> <module>org.wso2.carbon.apimgt.keymgt.extension.api</module>
<module>io.entgra.device.mgt.core.apimgt.analytics.extension</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> </modules>
<build> <build>

@ -51,6 +51,10 @@
<groupId>io.swagger</groupId> <groupId>io.swagger</groupId>
<artifactId>swagger-annotations</artifactId> <artifactId>swagger-annotations</artifactId>
</dependency> </dependency>
<dependency>
<groupId>org.wso2.carbon.devicemgt</groupId>
<artifactId>io.entgra.device.mgt.core.apimgt.extension.rest.api</artifactId>
</dependency>
</dependencies> </dependencies>
<build> <build>
@ -143,6 +147,9 @@
<bundleDef> <bundleDef>
io.swagger:swagger-annotations:${swagger.version} io.swagger:swagger-annotations:${swagger.version}
</bundleDef> </bundleDef>
<bundleDef>
org.wso2.carbon.devicemgt:io.entgra.device.mgt.core.apimgt.extension.rest.api:${carbon.device.mgt.version}
</bundleDef>
</bundles> </bundles>
<importFeatures> <importFeatures>
<importFeatureDef>org.wso2.carbon.core.server:${carbon.kernel.version}</importFeatureDef> <importFeatureDef>org.wso2.carbon.core.server:${carbon.kernel.version}</importFeatureDef>

@ -240,7 +240,7 @@
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.wso2.carbon.devicemgt</groupId> <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> <version>${carbon.device.mgt.version}</version>
</dependency> </dependency>
<dependency> <dependency>

Loading…
Cancel
Save