From aab94d8fdd5263762c90a9ae3978c3b963b5009f Mon Sep 17 00:00:00 2001 From: pasindu Date: Fri, 21 Apr 2023 00:26:16 +0530 Subject: [PATCH] Implement get scopesID API call and rename the module --- .../PublisherRESTAPIServices.java | 96 ------------ .../dto/AccessTokenInfo.java | 53 ------- .../internal/PublisherRESTAPIDataHolder.java | 30 ---- .../util/PublisherRESTAPIUtil.java | 67 --------- .../util/ScopeUtils.java | 59 -------- .../pom.xml | 12 +- .../rest/api}/APIApplicationServices.java | 8 +- .../rest/api}/APIApplicationServicesImpl.java | 57 +++++--- .../rest/api/PublisherRESTAPIServices.java | 137 ++++++++++++++++++ .../rest/api}/bean/RegistrationProfile.java | 2 +- .../rest/api}/constants/Constants.java | 2 +- .../rest/api}/dto/APIApplicationKey.java | 2 +- .../rest/api/dto/AccessTokenInfo.java | 53 +++++++ .../APIApplicationServicesException.java | 2 +- .../api}/exceptions/BadRequestException.java | 4 +- .../internal/PublisherRESTAPIDataHolder.java | 21 +++ .../PublisherRESTAPIServiceComponent.java | 13 +- .../extension/rest/api/util/ScopeUtils.java | 57 ++++++++ .../pom.xml | 15 +- .../webapp/publisher/APIPublisherService.java | 2 - .../publisher/APIPublisherServiceImpl.java | 49 ++++--- .../publisher/APIPublisherStartupHandler.java | 6 - components/apimgt-extensions/pom.xml | 18 ++- pom.xml | 2 +- 24 files changed, 377 insertions(+), 390 deletions(-) delete mode 100644 components/apimgt-extensions/io.entgra.devicemgt.apimgt.extension.publisher.api/src/main/java/io.entgra.devicemgt.apimgt.extension.publisher.api/PublisherRESTAPIServices.java delete mode 100644 components/apimgt-extensions/io.entgra.devicemgt.apimgt.extension.publisher.api/src/main/java/io.entgra.devicemgt.apimgt.extension.publisher.api/dto/AccessTokenInfo.java delete mode 100644 components/apimgt-extensions/io.entgra.devicemgt.apimgt.extension.publisher.api/src/main/java/io.entgra.devicemgt.apimgt.extension.publisher.api/internal/PublisherRESTAPIDataHolder.java delete mode 100644 components/apimgt-extensions/io.entgra.devicemgt.apimgt.extension.publisher.api/src/main/java/io.entgra.devicemgt.apimgt.extension.publisher.api/util/PublisherRESTAPIUtil.java delete mode 100644 components/apimgt-extensions/io.entgra.devicemgt.apimgt.extension.publisher.api/src/main/java/io.entgra.devicemgt.apimgt.extension.publisher.api/util/ScopeUtils.java rename components/apimgt-extensions/{io.entgra.devicemgt.apimgt.extension.publisher.api => io.entgra.devicemgt.apimgt.extension.rest.api}/pom.xml (96%) rename components/apimgt-extensions/{io.entgra.devicemgt.apimgt.extension.publisher.api/src/main/java/io.entgra.devicemgt.apimgt.extension.publisher.api => io.entgra.devicemgt.apimgt.extension.rest.api/src/main/java/io/entgra/devicemgt/apimgt/extension/rest/api}/APIApplicationServices.java (58%) rename components/apimgt-extensions/{io.entgra.devicemgt.apimgt.extension.publisher.api/src/main/java/io.entgra.devicemgt.apimgt.extension.publisher.api => io.entgra.devicemgt.apimgt.extension.rest.api/src/main/java/io/entgra/devicemgt/apimgt/extension/rest/api}/APIApplicationServicesImpl.java (73%) create mode 100644 components/apimgt-extensions/io.entgra.devicemgt.apimgt.extension.rest.api/src/main/java/io/entgra/devicemgt/apimgt/extension/rest/api/PublisherRESTAPIServices.java rename components/apimgt-extensions/{io.entgra.devicemgt.apimgt.extension.publisher.api/src/main/java/io.entgra.devicemgt.apimgt.extension.publisher.api => io.entgra.devicemgt.apimgt.extension.rest.api/src/main/java/io/entgra/devicemgt/apimgt/extension/rest/api}/bean/RegistrationProfile.java (96%) rename components/apimgt-extensions/{io.entgra.devicemgt.apimgt.extension.publisher.api/src/main/java/io.entgra.devicemgt.apimgt.extension.publisher.api => io.entgra.devicemgt.apimgt.extension.rest.api/src/main/java/io/entgra/devicemgt/apimgt/extension/rest/api}/constants/Constants.java (97%) rename components/apimgt-extensions/{io.entgra.devicemgt.apimgt.extension.publisher.api/src/main/java/io.entgra.devicemgt.apimgt.extension.publisher.api => io.entgra.devicemgt.apimgt.extension.rest.api/src/main/java/io/entgra/devicemgt/apimgt/extension/rest/api}/dto/APIApplicationKey.java (97%) create mode 100644 components/apimgt-extensions/io.entgra.devicemgt.apimgt.extension.rest.api/src/main/java/io/entgra/devicemgt/apimgt/extension/rest/api/dto/AccessTokenInfo.java rename components/apimgt-extensions/{io.entgra.devicemgt.apimgt.extension.publisher.api/src/main/java/io.entgra.devicemgt.apimgt.extension.publisher.api => io.entgra.devicemgt.apimgt.extension.rest.api/src/main/java/io/entgra/devicemgt/apimgt/extension/rest/api}/exceptions/APIApplicationServicesException.java (83%) rename components/apimgt-extensions/{io.entgra.devicemgt.apimgt.extension.publisher.api/src/main/java/io.entgra.devicemgt.apimgt.extension.publisher.api => io.entgra.devicemgt.apimgt.extension.rest.api/src/main/java/io/entgra/devicemgt/apimgt/extension/rest/api}/exceptions/BadRequestException.java (91%) create mode 100644 components/apimgt-extensions/io.entgra.devicemgt.apimgt.extension.rest.api/src/main/java/io/entgra/devicemgt/apimgt/extension/rest/api/internal/PublisherRESTAPIDataHolder.java rename components/apimgt-extensions/{io.entgra.devicemgt.apimgt.extension.publisher.api/src/main/java/io.entgra.devicemgt.apimgt.extension.publisher.api => io.entgra.devicemgt.apimgt.extension.rest.api/src/main/java/io/entgra/devicemgt/apimgt/extension/rest/api}/internal/PublisherRESTAPIServiceComponent.java (62%) create mode 100644 components/apimgt-extensions/io.entgra.devicemgt.apimgt.extension.rest.api/src/main/java/io/entgra/devicemgt/apimgt/extension/rest/api/util/ScopeUtils.java diff --git a/components/apimgt-extensions/io.entgra.devicemgt.apimgt.extension.publisher.api/src/main/java/io.entgra.devicemgt.apimgt.extension.publisher.api/PublisherRESTAPIServices.java b/components/apimgt-extensions/io.entgra.devicemgt.apimgt.extension.publisher.api/src/main/java/io.entgra.devicemgt.apimgt.extension.publisher.api/PublisherRESTAPIServices.java deleted file mode 100644 index a0bb43217d0..00000000000 --- a/components/apimgt-extensions/io.entgra.devicemgt.apimgt.extension.publisher.api/src/main/java/io.entgra.devicemgt.apimgt.extension.publisher.api/PublisherRESTAPIServices.java +++ /dev/null @@ -1,96 +0,0 @@ -package io.entgra.devicemgt.apimgt.extension.publisher.api; - -import io.entgra.devicemgt.apimgt.extension.publisher.api.constants.Constants; -import io.entgra.devicemgt.apimgt.extension.publisher.api.dto.APIApplicationKey; -import io.entgra.devicemgt.apimgt.extension.publisher.api.dto.AccessTokenInfo; -import io.entgra.devicemgt.apimgt.extension.publisher.api.exceptions.APIApplicationServicesException; -import io.entgra.devicemgt.apimgt.extension.publisher.api.exceptions.BadRequestException; -import okhttp3.*; -import org.apache.commons.httpclient.HttpStatus; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.apache.commons.ssl.Base64; -import org.json.JSONObject; -import org.wso2.carbon.apimgt.api.model.Scope; - -import java.io.IOException; -import static io.entgra.devicemgt.apimgt.extension.publisher.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 boolean isSharedScopeNameExists(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo, String key) - throws APIApplicationServicesException, BadRequestException { - - String keyValue = new String(Base64.encodeBase64((key).getBytes())).replace("=", ""); - String getScopeUrl = "https://localhost:9443/api/am/publisher/v2/scopes/" + keyValue; - - Request request = new Request.Builder() - .url(getScopeUrl) - .addHeader(Constants.AUTHORIZATION_HEADER_NAME, "Bearer " + accessTokenInfo.getAccessToken()) - .head() - .build(); - try { - Response response = client.newCall(request).execute(); - if (response.code() == HttpStatus.SC_OK){ - return true; - }else if(HttpStatus.SC_UNAUTHORIZED == response.code()) { - APIApplicationServices apiApplicationServices = new APIApplicationServicesImpl(); - AccessTokenInfo refreshedAccessToken = apiApplicationServices. - generateAccessTokenFromRefreshToken(accessTokenInfo.getRefreshToken(), apiApplicationKey.getClientId(), apiApplicationKey.getClientSecret() ); - //max attempt count - return isSharedScopeNameExists(apiApplicationKey,refreshedAccessToken, key); - } else if (HttpStatus.SC_BAD_REQUEST == response.code()){ - log.info(response); - throw new BadRequestException(response.toString()); - } else { - return false; - } - } catch (IOException e) { - String msg = "Error occurred while processing the response"; - throw new APIApplicationServicesException(msg); - } - } - - public boolean updateSharedScope(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo, Scope scope) - throws APIApplicationServicesException, BadRequestException { - - String updateScopeUrl = "https://localhost:9443/api/am/publisher/v2/scopes/" + scope.getId(); - - JSONObject setScope = new JSONObject(); - setScope.put("name", scope.getKey()); - setScope.put("displayName", scope.getName()); - setScope.put("description", scope.getDescription()); - setScope.put("bindings", scope.getRoles()); - - - RequestBody requestBody = RequestBody.Companion.create(setScope.toString(), JSON); - Request request = new Request.Builder() - .url(updateScopeUrl) - .addHeader(Constants.AUTHORIZATION_HEADER_NAME, "Bearer " + accessTokenInfo.getAccessToken()) - .put(requestBody) - .build(); - - try { - Response response = client.newCall(request).execute(); - if (response.code() == HttpStatus.SC_OK) { - return true; - } else if (HttpStatus.SC_UNAUTHORIZED == response.code()) { - APIApplicationServices apiApplicationServices = new APIApplicationServicesImpl(); - AccessTokenInfo refreshedAccessToken = apiApplicationServices. - generateAccessTokenFromRefreshToken(accessTokenInfo.getRefreshToken(), apiApplicationKey.getClientId(), apiApplicationKey.getClientSecret()); - return updateSharedScope(apiApplicationKey, refreshedAccessToken, scope); - } else if (HttpStatus.SC_BAD_REQUEST == response.code()) { - log.info(response); - throw new BadRequestException(response.toString()); - } else { - return false; - } - } catch (IOException e) { - String msg = "Error occurred while processing the response"; - throw new APIApplicationServicesException(msg); - } - } -} diff --git a/components/apimgt-extensions/io.entgra.devicemgt.apimgt.extension.publisher.api/src/main/java/io.entgra.devicemgt.apimgt.extension.publisher.api/dto/AccessTokenInfo.java b/components/apimgt-extensions/io.entgra.devicemgt.apimgt.extension.publisher.api/src/main/java/io.entgra.devicemgt.apimgt.extension.publisher.api/dto/AccessTokenInfo.java deleted file mode 100644 index e738c71f458..00000000000 --- a/components/apimgt-extensions/io.entgra.devicemgt.apimgt.extension.publisher.api/src/main/java/io.entgra.devicemgt.apimgt.extension.publisher.api/dto/AccessTokenInfo.java +++ /dev/null @@ -1,53 +0,0 @@ -package io.entgra.devicemgt.apimgt.extension.publisher.api.dto; - -/** - * This holds the token information that return from the token endpoint. - */ -public class AccessTokenInfo { - - private String tokenType; - private long expiresIn; - private String refreshToken; - private String accessToken; - private String scope; - - public String getTokenType() { - return tokenType; - } - - public void setTokenType(String tokenType) { - this.tokenType = tokenType; - } - - public long getExpiresIn() { - return expiresIn; - } - - public void setExpiresIn(long expiresIn) { - this.expiresIn = expiresIn; - } - - public String getRefreshToken() { - return refreshToken; - } - - public void setRefreshToken(String refreshToken) { - this.refreshToken = refreshToken; - } - - public String getAccessToken() { - return accessToken; - } - - public void setAccessToken(String accessToken) { - this.accessToken = accessToken; - } - - public String getScope() { - return scope; - } - - public void setScope(String scope) { - this.scope = scope; - } -} diff --git a/components/apimgt-extensions/io.entgra.devicemgt.apimgt.extension.publisher.api/src/main/java/io.entgra.devicemgt.apimgt.extension.publisher.api/internal/PublisherRESTAPIDataHolder.java b/components/apimgt-extensions/io.entgra.devicemgt.apimgt.extension.publisher.api/src/main/java/io.entgra.devicemgt.apimgt.extension.publisher.api/internal/PublisherRESTAPIDataHolder.java deleted file mode 100644 index 885f3ead7aa..00000000000 --- a/components/apimgt-extensions/io.entgra.devicemgt.apimgt.extension.publisher.api/src/main/java/io.entgra.devicemgt.apimgt.extension.publisher.api/internal/PublisherRESTAPIDataHolder.java +++ /dev/null @@ -1,30 +0,0 @@ -package io.entgra.devicemgt.apimgt.extension.publisher.api.internal; - -import io.entgra.devicemgt.apimgt.extension.publisher.api.APIApplicationServices; -import io.entgra.devicemgt.apimgt.extension.publisher.api.PublisherRESTAPIServices; - -public class PublisherRESTAPIDataHolder { - - private static final PublisherRESTAPIDataHolder thisInstance = new PublisherRESTAPIDataHolder(); - - private APIApplicationServices apiApplicationServices; -// -// private PublisherRESTAPIServices publisherRESTAPIServices; - - public static PublisherRESTAPIDataHolder getInstance(){ - return thisInstance; - } - public APIApplicationServices getApiApplicationServices() { - return apiApplicationServices; - } - public void setApiApplicationServices(APIApplicationServices apiApplicationServices) { - this.apiApplicationServices = apiApplicationServices; - } -// public PublisherRESTAPIServices getPublisherRESTAPIServices() { -// return publisherRESTAPIServices; -// } -// public void setPublisherRESTAPIServices(PublisherRESTAPIServices publisherRESTAPIServices) { -// this.publisherRESTAPIServices = publisherRESTAPIServices; -// } - -} diff --git a/components/apimgt-extensions/io.entgra.devicemgt.apimgt.extension.publisher.api/src/main/java/io.entgra.devicemgt.apimgt.extension.publisher.api/util/PublisherRESTAPIUtil.java b/components/apimgt-extensions/io.entgra.devicemgt.apimgt.extension.publisher.api/src/main/java/io.entgra.devicemgt.apimgt.extension.publisher.api/util/PublisherRESTAPIUtil.java deleted file mode 100644 index 1d532d17a7c..00000000000 --- a/components/apimgt-extensions/io.entgra.devicemgt.apimgt.extension.publisher.api/src/main/java/io.entgra.devicemgt.apimgt.extension.publisher.api/util/PublisherRESTAPIUtil.java +++ /dev/null @@ -1,67 +0,0 @@ -package io.entgra.devicemgt.apimgt.extension.publisher.api.util; - -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.apache.http.HttpResponse; -import org.apache.http.client.HttpClient; -import org.apache.http.conn.ssl.SSLConnectionSocketFactory; -import org.apache.http.conn.ssl.SSLContextBuilder; -import org.apache.http.conn.ssl.TrustSelfSignedStrategy; -import org.apache.http.impl.client.HttpClients; -import org.apache.http.util.EntityUtils; -import org.wso2.carbon.utils.CarbonUtils; - -import java.io.BufferedReader; -import java.io.File; -import java.io.IOException; -import java.io.InputStreamReader; -import java.security.KeyManagementException; -import java.security.KeyStoreException; -import java.security.NoSuchAlgorithmException; - -public class PublisherRESTAPIUtil { - private static final Log log = LogFactory.getLog(PublisherRESTAPIUtil.class); - private static final String HTTPS_PROTOCOL = "https"; - - /** - * Return a http client instance - * - * @param protocol- service endpoint protocol http/https - * @return - */ - public static HttpClient getHttpClient(String protocol) - throws IOException, KeyStoreException, NoSuchAlgorithmException, KeyManagementException { - HttpClient httpclient; - if (HTTPS_PROTOCOL.equals(protocol)) { - SSLContextBuilder builder = new SSLContextBuilder(); - builder.loadTrustMaterial(null, new TrustSelfSignedStrategy()); - SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(builder.build()); - httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).useSystemProperties().build(); - } else { - httpclient = HttpClients.createDefault(); - } - return httpclient; - } - - public static String getResponseString(HttpResponse httpResponse) throws IOException { - BufferedReader br = null; - try { - br = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent())); - String readLine; - String response = ""; - while (((readLine = br.readLine()) != null)) { - response += readLine; - } - return response; - } finally { - EntityUtils.consumeQuietly(httpResponse.getEntity()); - if (br != null) { - try { - br.close(); - } catch (IOException e) { - log.warn("Error while closing the connection! " + e.getMessage()); - } - } - } - } -} diff --git a/components/apimgt-extensions/io.entgra.devicemgt.apimgt.extension.publisher.api/src/main/java/io.entgra.devicemgt.apimgt.extension.publisher.api/util/ScopeUtils.java b/components/apimgt-extensions/io.entgra.devicemgt.apimgt.extension.publisher.api/src/main/java/io.entgra.devicemgt.apimgt.extension.publisher.api/util/ScopeUtils.java deleted file mode 100644 index db8011521dd..00000000000 --- a/components/apimgt-extensions/io.entgra.devicemgt.apimgt.extension.publisher.api/src/main/java/io.entgra.devicemgt.apimgt.extension.publisher.api/util/ScopeUtils.java +++ /dev/null @@ -1,59 +0,0 @@ -package io.entgra.devicemgt.apimgt.extension.publisher.api.util; - -/** - * This class represents the data that are required to register - * the oauth application. - */ -public class ScopeUtils { - - private String key; - private String name; - private String roles; - private String description; - - public String getKey() { - return key; - } - - public void setKey(String key) { - this.key = key; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public String getRoles() { - return roles; - } - - public void setRoles(String roles) { - this.roles = roles; - } - - public String getDescription() { - return description; - } - - public void setDescription(String description) { - this.description = description; - } - - public String toJSON() { - String jsonString = - "{\"name\": \"" + key + "\",\"displayName\": \"" + name + - "\", \"description\": \"" + description + "\"," + "\"bindings\": [" + - roles + "]" + " }"; - -// String jsonString = -// "{\"name\": \"" + name + "\",\"displayName\": \"" + name + -// "\", \"description\": \"" + description + "\"," + "\"bindings\": [" + -// "\"Internal/devicemgt-user\"" + -// "]" + " }"; - return jsonString; - } -} \ No newline at end of file diff --git a/components/apimgt-extensions/io.entgra.devicemgt.apimgt.extension.publisher.api/pom.xml b/components/apimgt-extensions/io.entgra.devicemgt.apimgt.extension.rest.api/pom.xml similarity index 96% rename from components/apimgt-extensions/io.entgra.devicemgt.apimgt.extension.publisher.api/pom.xml rename to components/apimgt-extensions/io.entgra.devicemgt.apimgt.extension.rest.api/pom.xml index 0b2d3f034b1..2471b48fed9 100644 --- a/components/apimgt-extensions/io.entgra.devicemgt.apimgt.extension.publisher.api/pom.xml +++ b/components/apimgt-extensions/io.entgra.devicemgt.apimgt.extension.rest.api/pom.xml @@ -27,7 +27,7 @@ 4.0.0 - io.entgra.devicemgt.apimgt.extension.publisher.api + io.entgra.devicemgt.apimgt.extension.rest.api bundle Entgra - Device API Management Extension Publisher API Entgra - Device API Management Extension Publisher API @@ -86,6 +86,10 @@ org.json.wso2 json + + com.google.code.gson + gson + @@ -106,11 +110,11 @@ ${carbon.device.mgt.version} Publisher API Management Bundle - io.entgra.devicemgt.apimgt.extension.publisher.api.internal + io.entgra.devicemgt.apimgt.extension.rest.api.internal - !io.entgra.devicemgt.apimgt.extension.publisher.api.internal, - io.entgra.devicemgt.apimgt.extension.publisher.api.* + !io.entgra.devicemgt.apimgt.extension.rest.api.internal, + io.entgra.devicemgt.apimgt.extension.rest.api.* javax.net.ssl, diff --git a/components/apimgt-extensions/io.entgra.devicemgt.apimgt.extension.publisher.api/src/main/java/io.entgra.devicemgt.apimgt.extension.publisher.api/APIApplicationServices.java b/components/apimgt-extensions/io.entgra.devicemgt.apimgt.extension.rest.api/src/main/java/io/entgra/devicemgt/apimgt/extension/rest/api/APIApplicationServices.java similarity index 58% rename from components/apimgt-extensions/io.entgra.devicemgt.apimgt.extension.publisher.api/src/main/java/io.entgra.devicemgt.apimgt.extension.publisher.api/APIApplicationServices.java rename to components/apimgt-extensions/io.entgra.devicemgt.apimgt.extension.rest.api/src/main/java/io/entgra/devicemgt/apimgt/extension/rest/api/APIApplicationServices.java index 401f680214f..1816df83d8a 100644 --- a/components/apimgt-extensions/io.entgra.devicemgt.apimgt.extension.publisher.api/src/main/java/io.entgra.devicemgt.apimgt.extension.publisher.api/APIApplicationServices.java +++ b/components/apimgt-extensions/io.entgra.devicemgt.apimgt.extension.rest.api/src/main/java/io/entgra/devicemgt/apimgt/extension/rest/api/APIApplicationServices.java @@ -1,8 +1,8 @@ -package io.entgra.devicemgt.apimgt.extension.publisher.api; +package io.entgra.devicemgt.apimgt.extension.rest.api; -import io.entgra.devicemgt.apimgt.extension.publisher.api.dto.APIApplicationKey; -import io.entgra.devicemgt.apimgt.extension.publisher.api.dto.AccessTokenInfo; -import io.entgra.devicemgt.apimgt.extension.publisher.api.exceptions.APIApplicationServicesException; +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; public interface APIApplicationServices { diff --git a/components/apimgt-extensions/io.entgra.devicemgt.apimgt.extension.publisher.api/src/main/java/io.entgra.devicemgt.apimgt.extension.publisher.api/APIApplicationServicesImpl.java b/components/apimgt-extensions/io.entgra.devicemgt.apimgt.extension.rest.api/src/main/java/io/entgra/devicemgt/apimgt/extension/rest/api/APIApplicationServicesImpl.java similarity index 73% rename from components/apimgt-extensions/io.entgra.devicemgt.apimgt.extension.publisher.api/src/main/java/io.entgra.devicemgt.apimgt.extension.publisher.api/APIApplicationServicesImpl.java rename to components/apimgt-extensions/io.entgra.devicemgt.apimgt.extension.rest.api/src/main/java/io/entgra/devicemgt/apimgt/extension/rest/api/APIApplicationServicesImpl.java index 5e6439edbbd..50c6c7d4d45 100644 --- a/components/apimgt-extensions/io.entgra.devicemgt.apimgt.extension.publisher.api/src/main/java/io.entgra.devicemgt.apimgt.extension.publisher.api/APIApplicationServicesImpl.java +++ b/components/apimgt-extensions/io.entgra.devicemgt.apimgt.extension.rest.api/src/main/java/io/entgra/devicemgt/apimgt/extension/rest/api/APIApplicationServicesImpl.java @@ -1,12 +1,18 @@ -package io.entgra.devicemgt.apimgt.extension.publisher.api; +package io.entgra.devicemgt.apimgt.extension.rest.api; import com.google.gson.Gson; +import io.entgra.devicemgt.apimgt.extension.rest.api.dto.APIApplicationKey; import org.json.JSONObject; -import io.entgra.devicemgt.apimgt.extension.publisher.api.constants.Constants; -import io.entgra.devicemgt.apimgt.extension.publisher.api.dto.APIApplicationKey; -import io.entgra.devicemgt.apimgt.extension.publisher.api.dto.AccessTokenInfo; -import io.entgra.devicemgt.apimgt.extension.publisher.api.exceptions.APIApplicationServicesException; -import okhttp3.*; +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 okhttp3.MediaType; +import okhttp3.OkHttpClient; +import okhttp3.Request; +import okhttp3.Response; +import okhttp3.RequestBody; +import okhttp3.Credentials; +import okhttp3.ConnectionPool; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -17,6 +23,7 @@ import javax.net.ssl.X509TrustManager; import java.io.IOException; import java.security.KeyManagementException; import java.security.NoSuchAlgorithmException; +import java.util.concurrent.TimeUnit; public class APIApplicationServicesImpl implements APIApplicationServices { @@ -33,16 +40,16 @@ public class APIApplicationServicesImpl implements APIApplicationServices { 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("saasApp",true); + jsonObject.put("clientName", Constants.CLIENT_NAME); + jsonObject.put("grantType", Constants.GRANT_TYPE); + jsonObject.put("owner", Constants.OWNER); + jsonObject.put("saasApp", true); RequestBody requestBody = RequestBody.Companion.create(jsonObject.toString(), JSON); - String keyManagerEndpoint = "https://localhost:9443/client-registration/v0.17/register"; + String applicationEndpoint = "https://localhost:9443/client-registration/v0.17/register"; Request request = new Request.Builder() - .url(keyManagerEndpoint) + .url(applicationEndpoint) .addHeader(Constants.AUTHORIZATION_HEADER_NAME, Credentials.basic("admin", "admin")) .post(requestBody) .build(); @@ -58,13 +65,13 @@ public class APIApplicationServicesImpl implements APIApplicationServices { @Override public AccessTokenInfo generateAccessTokenFromRegisteredApplication(String consumerKey, String consumerSecret) - throws APIApplicationServicesException { + throws APIApplicationServicesException { 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,"admin"); - params.put(Constants.PASSWORD_GRANT_TYPE_PASSWORD,"admin"); - params.put(Constants.SCOPE_PARAM_NAME,Constants.SCOPES); + params.put(Constants.PASSWORD_GRANT_TYPE_USERNAME, "admin"); + params.put(Constants.PASSWORD_GRANT_TYPE_PASSWORD, "admin"); + params.put(Constants.SCOPE_PARAM_NAME, Constants.SCOPES); return getToken(params, consumerKey, consumerSecret); } @@ -73,18 +80,17 @@ public class APIApplicationServicesImpl implements APIApplicationServices { throws APIApplicationServicesException { 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); //ToDo: Remove hardcoded value params.put(Constants.REFRESH_TOKEN_GRANT_TYPE_PARAM_NAME, refreshToken); - params.put(Constants.SCOPE_PARAM_NAME,Constants.SCOPES); + params.put(Constants.SCOPE_PARAM_NAME, Constants.SCOPES); return getToken(params, consumerKey, consumerSecret); } public AccessTokenInfo getToken(JSONObject nameValuePairs, String clientId, String clientSecret) - throws APIApplicationServicesException{ + throws APIApplicationServicesException { RequestBody requestBody = RequestBody.Companion.create(nameValuePairs.toString(), JSON); - //application/x-www-form-urlencoded String tokenEndPoint = "https://localhost:9443/oauth2/token"; Request request = new Request.Builder() @@ -117,9 +123,14 @@ public class APIApplicationServicesImpl implements APIApplicationServices { java.security.cert.X509Certificate[] certs, String authType) { } }; - return new OkHttpClient.Builder() + OkHttpClient okHttpClient = new OkHttpClient.Builder() + .connectTimeout(300, TimeUnit.SECONDS) + .writeTimeout(300, TimeUnit.SECONDS) + .readTimeout(300, TimeUnit.SECONDS) + .connectionPool(new ConnectionPool(500, 500, TimeUnit.SECONDS)) .sslSocketFactory(getSimpleTrustedSSLSocketFactory(), trustAllCerts) .hostnameVerifier((hostname, sslSession) -> true).build(); + return okHttpClient; } private static SSLSocketFactory getSimpleTrustedSSLSocketFactory() { @@ -129,9 +140,11 @@ public class APIApplicationServicesImpl implements APIApplicationServices { public java.security.cert.X509Certificate[] getAcceptedIssuers() { return null; } + public void checkClientTrusted( java.security.cert.X509Certificate[] certs, String authType) { } + public void checkServerTrusted( java.security.cert.X509Certificate[] certs, String authType) { } diff --git a/components/apimgt-extensions/io.entgra.devicemgt.apimgt.extension.rest.api/src/main/java/io/entgra/devicemgt/apimgt/extension/rest/api/PublisherRESTAPIServices.java b/components/apimgt-extensions/io.entgra.devicemgt.apimgt.extension.rest.api/src/main/java/io/entgra/devicemgt/apimgt/extension/rest/api/PublisherRESTAPIServices.java new file mode 100644 index 00000000000..c2c0202c8a7 --- /dev/null +++ b/components/apimgt-extensions/io.entgra.devicemgt.apimgt.extension.rest.api/src/main/java/io/entgra/devicemgt/apimgt/extension/rest/api/PublisherRESTAPIServices.java @@ -0,0 +1,137 @@ +package io.entgra.devicemgt.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 okhttp3.MediaType; +import okhttp3.OkHttpClient; +import okhttp3.Request; +import okhttp3.Response; +import okhttp3.RequestBody; +import org.apache.commons.httpclient.HttpStatus; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.apache.commons.ssl.Base64; +import org.json.JSONObject; +import org.wso2.carbon.apimgt.api.model.Scope; + +import java.io.IOException; + +import static io.entgra.devicemgt.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 { + + String getScopesUrl = "https://localhost:9443/api/am/publisher/v2/scopes?limit=1000"; + Request request = new Request.Builder() + .url(getScopesUrl) + .addHeader(Constants.AUTHORIZATION_HEADER_NAME, "Bearer " + accessTokenInfo.getAccess_token()) + .get() + .build(); + + try { + Response response = client.newCall(request).execute(); + if (response.code() == HttpStatus.SC_OK) { + JSONObject jsonObject = new JSONObject(response.body().string()); + return jsonObject; + } else if (HttpStatus.SC_UNAUTHORIZED == response.code()) { + APIApplicationServices apiApplicationServices = new APIApplicationServicesImpl(); + AccessTokenInfo refreshedAccessToken = apiApplicationServices. + generateAccessTokenFromRefreshToken(accessTokenInfo.getRefresh_token(), apiApplicationKey.getClientId(), apiApplicationKey.getClientSecret()); + //TODO: max attempt count + return getScopes(apiApplicationKey, refreshedAccessToken); + } else if (HttpStatus.SC_BAD_REQUEST == response.code()) { + log.info(response); + throw new BadRequestException(response.toString()); + } else { + return null; + } + } catch (IOException e) { + String msg = "Error occurred while processing the response"; + throw new APIApplicationServicesException(msg); + } + } + + public boolean isSharedScopeNameExists(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo, String key) + throws APIApplicationServicesException, BadRequestException { + + String keyValue = new String(Base64.encodeBase64((key).getBytes())).replace("=", ""); + String getScopeUrl = "https://localhost:9443/api/am/publisher/v2/scopes/" + keyValue; + + Request request = new Request.Builder() + .url(getScopeUrl) + .addHeader(Constants.AUTHORIZATION_HEADER_NAME, "Bearer " + accessTokenInfo.getAccess_token()) + .head() + .build(); + try { + Response response = client.newCall(request).execute(); + if (response.code() == HttpStatus.SC_OK) { + return true; + } else if (HttpStatus.SC_UNAUTHORIZED == response.code()) { + APIApplicationServices apiApplicationServices = new APIApplicationServicesImpl(); + AccessTokenInfo refreshedAccessToken = apiApplicationServices. + generateAccessTokenFromRefreshToken(accessTokenInfo.getRefresh_token(), apiApplicationKey.getClientId(), apiApplicationKey.getClientSecret()); + //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()); + } else { + return false; + } + } catch (IOException e) { + String msg = "Error occurred while processing the response"; + throw new APIApplicationServicesException(msg); + } + } + + public boolean updateSharedScope(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo, Scope scope) + throws APIApplicationServicesException, BadRequestException { + + String updateScopeUrl = "https://localhost:9443/api/am/publisher/v2/scopes/" + scope.getId(); + + ScopeUtils scopeUtil = new ScopeUtils(); + scopeUtil.setKey(scope.getKey()); + scopeUtil.setName(scope.getName()); + scopeUtil.setDescription(scope.getDescription()); + scopeUtil.setRoles(scope.getRoles()); + String scopeString = scopeUtil.toJSON(); + + RequestBody requestBody = RequestBody.create(JSON, scopeString); + Request request = new Request.Builder() + .url(updateScopeUrl) + .addHeader(Constants.AUTHORIZATION_HEADER_NAME, "Bearer " + accessTokenInfo.getAccess_token()) + .put(requestBody) + .build(); + + try { + Response response = client.newCall(request).execute(); + if (response.code() == HttpStatus.SC_OK) { + return true; + } else if (HttpStatus.SC_UNAUTHORIZED == response.code()) { + APIApplicationServices apiApplicationServices = new APIApplicationServicesImpl(); + AccessTokenInfo refreshedAccessToken = apiApplicationServices. + generateAccessTokenFromRefreshToken(accessTokenInfo.getRefresh_token(), apiApplicationKey.getClientId(), apiApplicationKey.getClientSecret()); + //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()); + } else { + return false; + } + } catch (IOException e) { + String msg = "Error occurred while processing the response"; + throw new APIApplicationServicesException(msg); + } + } +} diff --git a/components/apimgt-extensions/io.entgra.devicemgt.apimgt.extension.publisher.api/src/main/java/io.entgra.devicemgt.apimgt.extension.publisher.api/bean/RegistrationProfile.java b/components/apimgt-extensions/io.entgra.devicemgt.apimgt.extension.rest.api/src/main/java/io/entgra/devicemgt/apimgt/extension/rest/api/bean/RegistrationProfile.java similarity index 96% rename from components/apimgt-extensions/io.entgra.devicemgt.apimgt.extension.publisher.api/src/main/java/io.entgra.devicemgt.apimgt.extension.publisher.api/bean/RegistrationProfile.java rename to components/apimgt-extensions/io.entgra.devicemgt.apimgt.extension.rest.api/src/main/java/io/entgra/devicemgt/apimgt/extension/rest/api/bean/RegistrationProfile.java index 242788ed26e..c4544445980 100644 --- a/components/apimgt-extensions/io.entgra.devicemgt.apimgt.extension.publisher.api/src/main/java/io.entgra.devicemgt.apimgt.extension.publisher.api/bean/RegistrationProfile.java +++ b/components/apimgt-extensions/io.entgra.devicemgt.apimgt.extension.rest.api/src/main/java/io/entgra/devicemgt/apimgt/extension/rest/api/bean/RegistrationProfile.java @@ -1,4 +1,4 @@ -package io.entgra.devicemgt.apimgt.extension.publisher.api.bean; +package io.entgra.devicemgt.apimgt.extension.rest.api.bean; /** * This class represents the data that are required to register diff --git a/components/apimgt-extensions/io.entgra.devicemgt.apimgt.extension.publisher.api/src/main/java/io.entgra.devicemgt.apimgt.extension.publisher.api/constants/Constants.java b/components/apimgt-extensions/io.entgra.devicemgt.apimgt.extension.rest.api/src/main/java/io/entgra/devicemgt/apimgt/extension/rest/api/constants/Constants.java similarity index 97% rename from components/apimgt-extensions/io.entgra.devicemgt.apimgt.extension.publisher.api/src/main/java/io.entgra.devicemgt.apimgt.extension.publisher.api/constants/Constants.java rename to components/apimgt-extensions/io.entgra.devicemgt.apimgt.extension.rest.api/src/main/java/io/entgra/devicemgt/apimgt/extension/rest/api/constants/Constants.java index 0144e129f10..4e92ad6d93e 100644 --- a/components/apimgt-extensions/io.entgra.devicemgt.apimgt.extension.publisher.api/src/main/java/io.entgra.devicemgt.apimgt.extension.publisher.api/constants/Constants.java +++ b/components/apimgt-extensions/io.entgra.devicemgt.apimgt.extension.rest.api/src/main/java/io/entgra/devicemgt/apimgt/extension/rest/api/constants/Constants.java @@ -1,4 +1,4 @@ -package io.entgra.devicemgt.apimgt.extension.publisher.api.constants; +package io.entgra.devicemgt.apimgt.extension.rest.api.constants; public final class Constants { diff --git a/components/apimgt-extensions/io.entgra.devicemgt.apimgt.extension.publisher.api/src/main/java/io.entgra.devicemgt.apimgt.extension.publisher.api/dto/APIApplicationKey.java b/components/apimgt-extensions/io.entgra.devicemgt.apimgt.extension.rest.api/src/main/java/io/entgra/devicemgt/apimgt/extension/rest/api/dto/APIApplicationKey.java similarity index 97% rename from components/apimgt-extensions/io.entgra.devicemgt.apimgt.extension.publisher.api/src/main/java/io.entgra.devicemgt.apimgt.extension.publisher.api/dto/APIApplicationKey.java rename to components/apimgt-extensions/io.entgra.devicemgt.apimgt.extension.rest.api/src/main/java/io/entgra/devicemgt/apimgt/extension/rest/api/dto/APIApplicationKey.java index d0fc3b9f68b..e18c4b3d90a 100644 --- a/components/apimgt-extensions/io.entgra.devicemgt.apimgt.extension.publisher.api/src/main/java/io.entgra.devicemgt.apimgt.extension.publisher.api/dto/APIApplicationKey.java +++ b/components/apimgt-extensions/io.entgra.devicemgt.apimgt.extension.rest.api/src/main/java/io/entgra/devicemgt/apimgt/extension/rest/api/dto/APIApplicationKey.java @@ -1,4 +1,4 @@ -package io.entgra.devicemgt.apimgt.extension.publisher.api.dto; +package io.entgra.devicemgt.apimgt.extension.rest.api.dto; /** * This holds api application consumer id and secret. diff --git a/components/apimgt-extensions/io.entgra.devicemgt.apimgt.extension.rest.api/src/main/java/io/entgra/devicemgt/apimgt/extension/rest/api/dto/AccessTokenInfo.java b/components/apimgt-extensions/io.entgra.devicemgt.apimgt.extension.rest.api/src/main/java/io/entgra/devicemgt/apimgt/extension/rest/api/dto/AccessTokenInfo.java new file mode 100644 index 00000000000..f9c9a4db673 --- /dev/null +++ b/components/apimgt-extensions/io.entgra.devicemgt.apimgt.extension.rest.api/src/main/java/io/entgra/devicemgt/apimgt/extension/rest/api/dto/AccessTokenInfo.java @@ -0,0 +1,53 @@ +package io.entgra.devicemgt.apimgt.extension.rest.api.dto; + +/** + * This holds the token information that return from the token endpoint. + */ +public class AccessTokenInfo { + + private String token_type; + private long expires_in; + private String refresh_token; + private String access_token; + private String scope; + + public String getToken_type() { + return token_type; + } + + public void setToken_type(String token_type) { + this.token_type = token_type; + } + + public long getExpires_in() { + return expires_in; + } + + public void setExpires_in(long expires_in) { + this.expires_in = expires_in; + } + + public String getRefresh_token() { + return refresh_token; + } + + public void setRefresh_token(String refresh_token) { + this.refresh_token = refresh_token; + } + + public String getAccess_token() { + return access_token; + } + + public void setAccess_token(String access_token) { + this.access_token = access_token; + } + + public String getScope() { + return scope; + } + + public void setScope(String scope) { + this.scope = scope; + } +} diff --git a/components/apimgt-extensions/io.entgra.devicemgt.apimgt.extension.publisher.api/src/main/java/io.entgra.devicemgt.apimgt.extension.publisher.api/exceptions/APIApplicationServicesException.java b/components/apimgt-extensions/io.entgra.devicemgt.apimgt.extension.rest.api/src/main/java/io/entgra/devicemgt/apimgt/extension/rest/api/exceptions/APIApplicationServicesException.java similarity index 83% rename from components/apimgt-extensions/io.entgra.devicemgt.apimgt.extension.publisher.api/src/main/java/io.entgra.devicemgt.apimgt.extension.publisher.api/exceptions/APIApplicationServicesException.java rename to components/apimgt-extensions/io.entgra.devicemgt.apimgt.extension.rest.api/src/main/java/io/entgra/devicemgt/apimgt/extension/rest/api/exceptions/APIApplicationServicesException.java index bd7aa85cd96..897146bbcd5 100644 --- a/components/apimgt-extensions/io.entgra.devicemgt.apimgt.extension.publisher.api/src/main/java/io.entgra.devicemgt.apimgt.extension.publisher.api/exceptions/APIApplicationServicesException.java +++ b/components/apimgt-extensions/io.entgra.devicemgt.apimgt.extension.rest.api/src/main/java/io/entgra/devicemgt/apimgt/extension/rest/api/exceptions/APIApplicationServicesException.java @@ -1,4 +1,4 @@ -package io.entgra.devicemgt.apimgt.extension.publisher.api.exceptions; +package io.entgra.devicemgt.apimgt.extension.rest.api.exceptions; public class APIApplicationServicesException extends Exception{ diff --git a/components/apimgt-extensions/io.entgra.devicemgt.apimgt.extension.publisher.api/src/main/java/io.entgra.devicemgt.apimgt.extension.publisher.api/exceptions/BadRequestException.java b/components/apimgt-extensions/io.entgra.devicemgt.apimgt.extension.rest.api/src/main/java/io/entgra/devicemgt/apimgt/extension/rest/api/exceptions/BadRequestException.java similarity index 91% rename from components/apimgt-extensions/io.entgra.devicemgt.apimgt.extension.publisher.api/src/main/java/io.entgra.devicemgt.apimgt.extension.publisher.api/exceptions/BadRequestException.java rename to components/apimgt-extensions/io.entgra.devicemgt.apimgt.extension.rest.api/src/main/java/io/entgra/devicemgt/apimgt/extension/rest/api/exceptions/BadRequestException.java index 427c4b5e358..00d4b423fa2 100644 --- a/components/apimgt-extensions/io.entgra.devicemgt.apimgt.extension.publisher.api/src/main/java/io.entgra.devicemgt.apimgt.extension.publisher.api/exceptions/BadRequestException.java +++ b/components/apimgt-extensions/io.entgra.devicemgt.apimgt.extension.rest.api/src/main/java/io/entgra/devicemgt/apimgt/extension/rest/api/exceptions/BadRequestException.java @@ -22,9 +22,7 @@ /** * Custom exception class for handling bad request exceptions. */ -package io.entgra.devicemgt.apimgt.extension.publisher.api.exceptions; - -import okhttp3.Response; +package io.entgra.devicemgt.apimgt.extension.rest.api.exceptions; public class BadRequestException extends Exception { diff --git a/components/apimgt-extensions/io.entgra.devicemgt.apimgt.extension.rest.api/src/main/java/io/entgra/devicemgt/apimgt/extension/rest/api/internal/PublisherRESTAPIDataHolder.java b/components/apimgt-extensions/io.entgra.devicemgt.apimgt.extension.rest.api/src/main/java/io/entgra/devicemgt/apimgt/extension/rest/api/internal/PublisherRESTAPIDataHolder.java new file mode 100644 index 00000000000..ce651eeb368 --- /dev/null +++ b/components/apimgt-extensions/io.entgra.devicemgt.apimgt.extension.rest.api/src/main/java/io/entgra/devicemgt/apimgt/extension/rest/api/internal/PublisherRESTAPIDataHolder.java @@ -0,0 +1,21 @@ +package io.entgra.devicemgt.apimgt.extension.rest.api.internal; + +import io.entgra.devicemgt.apimgt.extension.rest.api.APIApplicationServices; + +public class PublisherRESTAPIDataHolder { + + private static final PublisherRESTAPIDataHolder thisInstance = new PublisherRESTAPIDataHolder(); + + private APIApplicationServices apiApplicationServices; + + public static PublisherRESTAPIDataHolder getInstance(){ + return thisInstance; + } + public APIApplicationServices getApiApplicationServices() { + return apiApplicationServices; + } + public void setApiApplicationServices(APIApplicationServices apiApplicationServices) { + this.apiApplicationServices = apiApplicationServices; + } + +} diff --git a/components/apimgt-extensions/io.entgra.devicemgt.apimgt.extension.publisher.api/src/main/java/io.entgra.devicemgt.apimgt.extension.publisher.api/internal/PublisherRESTAPIServiceComponent.java b/components/apimgt-extensions/io.entgra.devicemgt.apimgt.extension.rest.api/src/main/java/io/entgra/devicemgt/apimgt/extension/rest/api/internal/PublisherRESTAPIServiceComponent.java similarity index 62% rename from components/apimgt-extensions/io.entgra.devicemgt.apimgt.extension.publisher.api/src/main/java/io.entgra.devicemgt.apimgt.extension.publisher.api/internal/PublisherRESTAPIServiceComponent.java rename to components/apimgt-extensions/io.entgra.devicemgt.apimgt.extension.rest.api/src/main/java/io/entgra/devicemgt/apimgt/extension/rest/api/internal/PublisherRESTAPIServiceComponent.java index d34012c1f46..3817e365a4f 100644 --- a/components/apimgt-extensions/io.entgra.devicemgt.apimgt.extension.publisher.api/src/main/java/io.entgra.devicemgt.apimgt.extension.publisher.api/internal/PublisherRESTAPIServiceComponent.java +++ b/components/apimgt-extensions/io.entgra.devicemgt.apimgt.extension.rest.api/src/main/java/io/entgra/devicemgt/apimgt/extension/rest/api/internal/PublisherRESTAPIServiceComponent.java @@ -1,15 +1,14 @@ -package io.entgra.devicemgt.apimgt.extension.publisher.api.internal; +package io.entgra.devicemgt.apimgt.extension.rest.api.internal; -import io.entgra.devicemgt.apimgt.extension.publisher.api.APIApplicationServices; -import io.entgra.devicemgt.apimgt.extension.publisher.api.APIApplicationServicesImpl; -import io.entgra.devicemgt.apimgt.extension.publisher.api.PublisherRESTAPIServices; +import io.entgra.devicemgt.apimgt.extension.rest.api.APIApplicationServices; +import io.entgra.devicemgt.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; /** - * @scr.component name="io.entgra.devicemgt.apimgt.extension.publisher.api.internal.PublisherRESTAPIServiceComponent" + * @scr.component name="io.entgra.devicemgt.apimgt.extension.rest.api.internal.PublisherRESTAPIServiceComponent" * immediate="true" */ public class PublisherRESTAPIServiceComponent { @@ -23,10 +22,6 @@ public class PublisherRESTAPIServiceComponent { try { BundleContext bundleContext = componentContext.getBundleContext(); -// PublisherRESTAPIServices publisherRESTAPIServices = new PublisherRESTAPIServices(); -// bundleContext.registerService(PublisherRESTAPIServices.class.getName(), publisherRESTAPIServices, null); -// PublisherRESTAPIDataHolder.getInstance().setPublisherRESTAPIServices(publisherRESTAPIServices); - APIApplicationServices apiApplicationServices = new APIApplicationServicesImpl(); bundleContext.registerService(APIApplicationServices.class.getName(), apiApplicationServices, null); PublisherRESTAPIDataHolder.getInstance().setApiApplicationServices(apiApplicationServices); diff --git a/components/apimgt-extensions/io.entgra.devicemgt.apimgt.extension.rest.api/src/main/java/io/entgra/devicemgt/apimgt/extension/rest/api/util/ScopeUtils.java b/components/apimgt-extensions/io.entgra.devicemgt.apimgt.extension.rest.api/src/main/java/io/entgra/devicemgt/apimgt/extension/rest/api/util/ScopeUtils.java new file mode 100644 index 00000000000..8a7e6d2fbbb --- /dev/null +++ b/components/apimgt-extensions/io.entgra.devicemgt.apimgt.extension.rest.api/src/main/java/io/entgra/devicemgt/apimgt/extension/rest/api/util/ScopeUtils.java @@ -0,0 +1,57 @@ +package io.entgra.devicemgt.apimgt.extension.rest.api.util; + +/** + * This class represents the data that are required to register + * the oauth application. + */ +public class ScopeUtils { + + private String key; + private String name; + private String roles; + private String description; + + public String getKey() { + return key; + } + + public void setKey(String key) { + this.key = key; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getRoles() { + return roles; + } + + public void setRoles(String roles) { + this.roles = roles; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public String toJSON() { + String jsonString = "{\n" + + " \"name\":\" " + key + "\",\n" + + " \"displayName\":\" " + name + "\",\n" + + " \"description\":\" " + description + " \",\n" + + " \"bindings\":[\n" + + " \" " + roles + " \"\n" + + " ]\n" + + "}"; + return jsonString; + } +} \ No newline at end of file diff --git a/components/apimgt-extensions/org.wso2.carbon.apimgt.webapp.publisher/pom.xml b/components/apimgt-extensions/org.wso2.carbon.apimgt.webapp.publisher/pom.xml index c08735db3fd..d1bc922c545 100644 --- a/components/apimgt-extensions/org.wso2.carbon.apimgt.webapp.publisher/pom.xml +++ b/components/apimgt-extensions/org.wso2.carbon.apimgt.webapp.publisher/pom.xml @@ -124,9 +124,13 @@ org.wso2.carbon.devicemgt - io.entgra.devicemgt.apimgt.extension.publisher.api + io.entgra.devicemgt.apimgt.extension.rest.api provided + + org.json.wso2 + json + @@ -170,9 +174,9 @@ org.scannotation;version="1.0", org.scannotation.archiveiterator;version="1.0", org.w3c.dom, - io.entgra.devicemgt.apimgt.extension.publisher.api, - io.entgra.devicemgt.apimgt.extension.publisher.api.dto, - io.entgra.devicemgt.apimgt.extension.publisher.api.exceptions, + io.entgra.devicemgt.apimgt.extension.rest.api, + io.entgra.devicemgt.apimgt.extension.rest.api.dto, + io.entgra.devicemgt.apimgt.extension.rest.api.exceptions, org.wso2.carbon.apimgt.annotations.api, org.wso2.carbon.apimgt.api, org.wso2.carbon.apimgt.api.model, @@ -195,7 +199,8 @@ org.wso2.carbon.utils;version="4.6", org.wso2.carbon.utils.multitenancy;version="4.6", org.wso2.carbon.apimgt.impl.definitions, - org.apache.commons.lang + org.apache.commons.lang, + org.json jsr311-api;scope=compile|runtime;inline=false diff --git a/components/apimgt-extensions/org.wso2.carbon.apimgt.webapp.publisher/src/main/java/org/wso2/carbon/apimgt/webapp/publisher/APIPublisherService.java b/components/apimgt-extensions/org.wso2.carbon.apimgt.webapp.publisher/src/main/java/org/wso2/carbon/apimgt/webapp/publisher/APIPublisherService.java index 85dcf72d138..fd111a46de9 100644 --- a/components/apimgt-extensions/org.wso2.carbon.apimgt.webapp.publisher/src/main/java/org/wso2/carbon/apimgt/webapp/publisher/APIPublisherService.java +++ b/components/apimgt-extensions/org.wso2.carbon.apimgt.webapp.publisher/src/main/java/org/wso2/carbon/apimgt/webapp/publisher/APIPublisherService.java @@ -18,8 +18,6 @@ */ package org.wso2.carbon.apimgt.webapp.publisher; - -import io.entgra.devicemgt.apimgt.extension.publisher.api.exceptions.BadRequestException; import org.wso2.carbon.apimgt.webapp.publisher.exception.APIManagerPublisherException; /** diff --git a/components/apimgt-extensions/org.wso2.carbon.apimgt.webapp.publisher/src/main/java/org/wso2/carbon/apimgt/webapp/publisher/APIPublisherServiceImpl.java b/components/apimgt-extensions/org.wso2.carbon.apimgt.webapp.publisher/src/main/java/org/wso2/carbon/apimgt/webapp/publisher/APIPublisherServiceImpl.java index 2fe4883df58..0aebefbaa49 100644 --- a/components/apimgt-extensions/org.wso2.carbon.apimgt.webapp.publisher/src/main/java/org/wso2/carbon/apimgt/webapp/publisher/APIPublisherServiceImpl.java +++ b/components/apimgt-extensions/org.wso2.carbon.apimgt.webapp.publisher/src/main/java/org/wso2/carbon/apimgt/webapp/publisher/APIPublisherServiceImpl.java @@ -18,18 +18,20 @@ */ package org.wso2.carbon.apimgt.webapp.publisher; -import io.entgra.devicemgt.apimgt.extension.publisher.api.APIApplicationServices; -import io.entgra.devicemgt.apimgt.extension.publisher.api.APIApplicationServicesImpl; -import io.entgra.devicemgt.apimgt.extension.publisher.api.PublisherRESTAPIServices; -import io.entgra.devicemgt.apimgt.extension.publisher.api.dto.APIApplicationKey; -import io.entgra.devicemgt.apimgt.extension.publisher.api.dto.AccessTokenInfo; -import io.entgra.devicemgt.apimgt.extension.publisher.api.exceptions.APIApplicationServicesException; -import io.entgra.devicemgt.apimgt.extension.publisher.api.exceptions.BadRequestException; +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 org.apache.commons.lang.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.wso2.carbon.apimgt.api.model.Documentation; import org.wso2.carbon.apimgt.api.model.DocumentationType; +import org.json.JSONArray; +import org.json.JSONObject; import org.wso2.carbon.apimgt.api.APIManagementException; import org.wso2.carbon.apimgt.api.APIProvider; import org.wso2.carbon.apimgt.api.FaultGatewaysException; @@ -382,6 +384,7 @@ public class APIPublisherServiceImpl implements APIPublisherService { PrivilegedCarbonContext.startTenantFlow(); PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(tenantDomain, true); PublisherRESTAPIServices publisherRESTAPIServices = new PublisherRESTAPIServices(); + JSONObject scopeObject = publisherRESTAPIServices.getScopes(apiApplicationKey, accessTokenInfo); try { String fileName = @@ -427,8 +430,18 @@ public class APIPublisherServiceImpl implements APIPublisherService { } scope.setRoles(roleString); -// if (apiProvider.isSharedScopeNameExists(scope.getKey(), tenantDomain)) { -// apiProvider.updateSharedScope(scope, tenantDomain); + //Set scope id which related to the scope key + JSONArray scopeList = (JSONArray) scopeObject.get("list"); + JSONObject object = null; + for (int i = 0; i < scopeList.length(); i++) { + JSONObject obj = null; + obj = scopeList.getJSONObject(i); + if (obj.getString("name").equals(scopeMapping[2] != null ? StringUtils.trim(scopeMapping[2]) : StringUtils.EMPTY)) { + object = obj; + } + } + scope.setId(object.getString("id")); + if (publisherRESTAPIServices.isSharedScopeNameExists(apiApplicationKey, accessTokenInfo, scope.getKey())) { publisherRESTAPIServices.updateSharedScope(apiApplicationKey, accessTokenInfo, scope); } else { @@ -449,17 +462,13 @@ public class APIPublisherServiceImpl implements APIPublisherService { } } -// catch (UserStoreException e) { -// String msg = "Error occurred while reading tenant admin username"; -// log.error(msg, e); -// throw new APIManagerPublisherException(e); -// } -// catch (APIManagementException e) { -// String msg = "Error occurred while loading api provider"; -// log.error(msg, e); -// throw new APIManagerPublisherException(e); -// } - finally { + catch (APIApplicationServicesException e) { + log.error(e); + throw new RuntimeException(e); + } catch (BadRequestException e) { + log.error(e); + throw new RuntimeException(e); + } finally { PrivilegedCarbonContext.endTenantFlow(); } } diff --git a/components/apimgt-extensions/org.wso2.carbon.apimgt.webapp.publisher/src/main/java/org/wso2/carbon/apimgt/webapp/publisher/APIPublisherStartupHandler.java b/components/apimgt-extensions/org.wso2.carbon.apimgt.webapp.publisher/src/main/java/org/wso2/carbon/apimgt/webapp/publisher/APIPublisherStartupHandler.java index 3933f60716d..01b1cd810cf 100644 --- a/components/apimgt-extensions/org.wso2.carbon.apimgt.webapp.publisher/src/main/java/org/wso2/carbon/apimgt/webapp/publisher/APIPublisherStartupHandler.java +++ b/components/apimgt-extensions/org.wso2.carbon.apimgt.webapp.publisher/src/main/java/org/wso2/carbon/apimgt/webapp/publisher/APIPublisherStartupHandler.java @@ -19,12 +19,6 @@ package org.wso2.carbon.apimgt.webapp.publisher; -import io.entgra.devicemgt.apimgt.extension.publisher.api.APIApplicationServices; -import io.entgra.devicemgt.apimgt.extension.publisher.api.APIApplicationServicesImpl; -import io.entgra.devicemgt.apimgt.extension.publisher.api.dto.APIApplicationKey; -import io.entgra.devicemgt.apimgt.extension.publisher.api.dto.AccessTokenInfo; -import io.entgra.devicemgt.apimgt.extension.publisher.api.exceptions.APIApplicationServicesException; -import io.entgra.devicemgt.apimgt.extension.publisher.api.exceptions.BadRequestException; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.wso2.carbon.apimgt.webapp.publisher.exception.APIManagerPublisherException; diff --git a/components/apimgt-extensions/pom.xml b/components/apimgt-extensions/pom.xml index 137f35b5724..2c175c1ea1e 100644 --- a/components/apimgt-extensions/pom.xml +++ b/components/apimgt-extensions/pom.xml @@ -31,17 +31,25 @@ org.wso2.carbon.apimgt.api - org.json.wso2 - json + com.squareup.okhttp3 + okhttp - io.github.openfeign - feign-okhttp + org.json.wso2 + json com.google.code.gson gson + + org.eclipse.osgi + org.eclipse.osgi + + + org.eclipse.osgi + org.eclipse.osgi.services + 4.0.0 @@ -58,7 +66,7 @@ org.wso2.carbon.apimgt.keymgt.extension org.wso2.carbon.apimgt.keymgt.extension.api io.entgra.device.mgt.core.apimgt.analytics.extension - io.entgra.devicemgt.apimgt.extension.publisher.api + io.entgra.devicemgt.apimgt.extension.rest.api diff --git a/pom.xml b/pom.xml index b5435d623d5..e52350555b1 100644 --- a/pom.xml +++ b/pom.xml @@ -240,7 +240,7 @@ org.wso2.carbon.devicemgt - io.entgra.devicemgt.apimgt.extension.publisher.api + io.entgra.devicemgt.apimgt.extension.rest.api ${carbon.device.mgt.version}