From c98237dc6f188e8cdcd8e8e2ef8438369a5136ca Mon Sep 17 00:00:00 2001 From: Pasindu Rupasinghe Date: Thu, 1 Jun 2023 09:50:58 +0000 Subject: [PATCH] Implement service for consumer rest APIs Co-authored-by: Pasindu Rupasinghe Co-committed-by: Pasindu Rupasinghe --- .../rest/api/ConsumerRESTAPIServices.java | 64 +++ .../rest/api/ConsumerRESTAPIServicesImpl.java | 404 ++++++++++++++++++ .../rest/api/bean/APIMConsumer/APIKey.java | 41 ++ .../api/bean/APIMConsumer/Application.java | 143 +++++++ .../api/bean/APIMConsumer/Subscription.java | 106 +++++ .../rest/api/constants/Constants.java | 11 +- .../PublisherRESTAPIServiceComponent.java | 5 + 7 files changed, 773 insertions(+), 1 deletion(-) create mode 100644 components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.extension.rest.api/src/main/java/io/entgra/device/mgt/core/apimgt/extension/rest/api/ConsumerRESTAPIServices.java create mode 100644 components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.extension.rest.api/src/main/java/io/entgra/device/mgt/core/apimgt/extension/rest/api/ConsumerRESTAPIServicesImpl.java create mode 100644 components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.extension.rest.api/src/main/java/io/entgra/device/mgt/core/apimgt/extension/rest/api/bean/APIMConsumer/APIKey.java create mode 100644 components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.extension.rest.api/src/main/java/io/entgra/device/mgt/core/apimgt/extension/rest/api/bean/APIMConsumer/Application.java create mode 100644 components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.extension.rest.api/src/main/java/io/entgra/device/mgt/core/apimgt/extension/rest/api/bean/APIMConsumer/Subscription.java diff --git a/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.extension.rest.api/src/main/java/io/entgra/device/mgt/core/apimgt/extension/rest/api/ConsumerRESTAPIServices.java b/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.extension.rest.api/src/main/java/io/entgra/device/mgt/core/apimgt/extension/rest/api/ConsumerRESTAPIServices.java new file mode 100644 index 00000000000..0dd8783663c --- /dev/null +++ b/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.extension.rest.api/src/main/java/io/entgra/device/mgt/core/apimgt/extension/rest/api/ConsumerRESTAPIServices.java @@ -0,0 +1,64 @@ +/* + * Copyright (c) 2018 - 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; + +import io.entgra.device.mgt.core.apimgt.extension.rest.api.bean.APIMConsumer.APIKey; +import io.entgra.device.mgt.core.apimgt.extension.rest.api.bean.APIMConsumer.Application; +import io.entgra.device.mgt.core.apimgt.extension.rest.api.bean.APIMConsumer.Subscription; +import io.entgra.device.mgt.core.apimgt.extension.rest.api.dto.APIApplicationKey; +import io.entgra.device.mgt.core.apimgt.extension.rest.api.dto.AccessTokenInfo; +import io.entgra.device.mgt.core.apimgt.extension.rest.api.exceptions.APIServicesException; +import io.entgra.device.mgt.core.apimgt.extension.rest.api.exceptions.BadRequestException; +import io.entgra.device.mgt.core.apimgt.extension.rest.api.exceptions.UnexpectedResponseException; +import org.json.JSONObject; + +import java.util.List; +import java.util.Map; + +public interface ConsumerRESTAPIServices { + JSONObject getAllApplications(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo, String appName) + throws APIServicesException, BadRequestException, UnexpectedResponseException; + + Application createApplication(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo, + Application application) + throws APIServicesException, BadRequestException, UnexpectedResponseException; + + JSONObject getAllSubscriptions(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo, + String applicationId) + throws APIServicesException, BadRequestException, UnexpectedResponseException; + + JSONObject getAllApis(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo, + Map queryParam) + throws APIServicesException, BadRequestException, UnexpectedResponseException; + + Subscription createSubscription(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo, + Subscription subscriptions) + throws APIServicesException, BadRequestException, UnexpectedResponseException; + + Subscription createSubscriptions(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo, + List subscriptions) + throws APIServicesException, BadRequestException, UnexpectedResponseException; + + APIKey generateApplicationKeys(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo, + String applicationId) + throws APIServicesException, BadRequestException, UnexpectedResponseException; + + JSONObject getAllKeyManagers(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo) + throws APIServicesException, BadRequestException, UnexpectedResponseException; +} diff --git a/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.extension.rest.api/src/main/java/io/entgra/device/mgt/core/apimgt/extension/rest/api/ConsumerRESTAPIServicesImpl.java b/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.extension.rest.api/src/main/java/io/entgra/device/mgt/core/apimgt/extension/rest/api/ConsumerRESTAPIServicesImpl.java new file mode 100644 index 00000000000..383098dd3de --- /dev/null +++ b/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.extension.rest.api/src/main/java/io/entgra/device/mgt/core/apimgt/extension/rest/api/ConsumerRESTAPIServicesImpl.java @@ -0,0 +1,404 @@ +/* + * Copyright (c) 2018 - 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; + +import com.google.gson.Gson; +import io.entgra.device.mgt.core.apimgt.extension.rest.api.bean.APIMConsumer.APIKey; +import io.entgra.device.mgt.core.apimgt.extension.rest.api.bean.APIMConsumer.Application; +import io.entgra.device.mgt.core.apimgt.extension.rest.api.bean.APIMConsumer.Subscription; +import io.entgra.device.mgt.core.apimgt.extension.rest.api.constants.Constants; +import io.entgra.device.mgt.core.apimgt.extension.rest.api.dto.APIApplicationKey; +import io.entgra.device.mgt.core.apimgt.extension.rest.api.dto.AccessTokenInfo; +import io.entgra.device.mgt.core.apimgt.extension.rest.api.exceptions.APIServicesException; +import io.entgra.device.mgt.core.apimgt.extension.rest.api.exceptions.BadRequestException; +import io.entgra.device.mgt.core.apimgt.extension.rest.api.exceptions.UnexpectedResponseException; +import io.entgra.device.mgt.core.apimgt.extension.rest.api.util.HttpsTrustManagerUtils; +import okhttp3.*; +import org.apache.commons.httpclient.HttpStatus; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.json.JSONObject; + +import java.io.IOException; +import java.util.List; +import java.util.Map; + +public class ConsumerRESTAPIServicesImpl implements ConsumerRESTAPIServices { + + private static final Log log = LogFactory.getLog(ConsumerRESTAPIServicesImpl.class); + private static final OkHttpClient client = new OkHttpClient(HttpsTrustManagerUtils.getSSLClient().newBuilder()); + private static final MediaType JSON = MediaType.parse("application/json; charset=utf-8"); + private static final Gson gson = new Gson(); + private static final String host = System.getProperty(Constants.IOT_CORE_HOST); + private static final String port = System.getProperty(Constants.IOT_CORE_HTTPS_PORT); + private static final String endPointPrefix = Constants.HTTPS_PROTOCOL + Constants.SCHEME_SEPARATOR + host + + Constants.COLON + port; + + @Override + public JSONObject getAllApplications(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo, String appName) + throws APIServicesException, BadRequestException, UnexpectedResponseException { + + String getAllApplicationsUrl = endPointPrefix + Constants.APPLICATIONS_API + "?query=" + appName; + Request request = new Request.Builder() + .url(getAllApplicationsUrl) + .addHeader(Constants.AUTHORIZATION_HEADER_NAME, Constants.AUTHORIZATION_HEADER_PREFIX_BEARER + + accessTokenInfo.getAccess_token()) + .get() + .build(); + + try { + Response response = client.newCall(request).execute(); + if (HttpStatus.SC_OK == response.code()) { + 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 getAllApplications(apiApplicationKey, refreshedAccessToken, appName); + } else if (HttpStatus.SC_BAD_REQUEST == response.code()) { + String msg = "Bad Request, Invalid request"; + log.error(msg); + throw new BadRequestException(msg); + } else { + String msg = "Response : " + response.code() + response.body(); + throw new UnexpectedResponseException(msg); + } + } catch (IOException e) { + String msg = "Error occurred while processing the response"; + log.error(msg, e); + throw new APIServicesException(msg, e); + } + } + + @Override + public Application createApplication(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo, + Application application) + throws APIServicesException, BadRequestException, UnexpectedResponseException { + + String getAllScopesUrl = endPointPrefix + Constants.APPLICATIONS_API; + + String applicationInfo = "{\n" + + " \"name\": \"" + application.getName() + "\",\n" + + " \"throttlingPolicy\": \"" + application.getThrottlingPolicy() + "\",\n" + + " \"description\": \"" + application.getDescription() + "\",\n" + + " \"tokenType\": \"" + application.getTokenType() + "\",\n" + + " \"groups\": " + gson.toJson(application.getGroups()) + ",\n" + + " \"attributes\": " + application.getAttributes().toString() + ",\n" + + " \"subscriptionScopes\": " + gson.toJson(application.getSubscriptionScopes()) + "\n" + + "}"; + + RequestBody requestBody = RequestBody.create(JSON, applicationInfo); + Request request = new Request.Builder() + .url(getAllScopesUrl) + .addHeader(Constants.AUTHORIZATION_HEADER_NAME, Constants.AUTHORIZATION_HEADER_PREFIX_BEARER + + accessTokenInfo.getAccess_token()) + .post(requestBody) + .build(); + + try { + Response response = client.newCall(request).execute(); + if (HttpStatus.SC_CREATED == response.code()) { + return gson.fromJson(response.body().string(), Application.class); + } 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 createApplication(apiApplicationKey, refreshedAccessToken, application); + } else if (HttpStatus.SC_BAD_REQUEST == response.code()) { + String msg = "Bad Request, Invalid request body"; + log.error(msg); + throw new BadRequestException(msg); + } else { + String msg = "Response : " + response.code() + response.body(); + throw new UnexpectedResponseException(msg); + } + } catch (IOException e) { + String msg = "Error occurred while processing the response"; + log.error(msg, e); + throw new APIServicesException(msg, e); + } + } + + @Override + public JSONObject getAllSubscriptions(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo, + String applicationId) + throws APIServicesException, BadRequestException, UnexpectedResponseException { + + String getAllScopesUrl = endPointPrefix + Constants.SUBSCRIPTION_API + "?applicationId=" + applicationId; + Request request = new Request.Builder() + .url(getAllScopesUrl) + .addHeader(Constants.AUTHORIZATION_HEADER_NAME, Constants.AUTHORIZATION_HEADER_PREFIX_BEARER + + accessTokenInfo.getAccess_token()) + .get() + .build(); + + try { + Response response = client.newCall(request).execute(); + if (HttpStatus.SC_OK == response.code()) { + 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 getAllSubscriptions(apiApplicationKey, refreshedAccessToken, applicationId); + } else if (HttpStatus.SC_BAD_REQUEST == response.code()) { + String msg = "Bad Request, Invalid request"; + log.error(msg); + throw new BadRequestException(msg); + } else { + String msg = "Response : " + response.code() + response.body(); + throw new UnexpectedResponseException(msg); + } + } catch (IOException e) { + String msg = "Error occurred while processing the response"; + log.error(msg, e); + throw new APIServicesException(msg, e); + } + } + + @Override + public JSONObject getAllApis(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo, + Map queryParams) + throws APIServicesException, BadRequestException, UnexpectedResponseException { + + String getAPIsURL = endPointPrefix + Constants.DEV_PORTAL_API; + + for (Map.Entry query : queryParams.entrySet()) { + getAPIsURL = getAPIsURL + Constants.AMPERSAND + query.getKey() + Constants.EQUAL + query.getValue(); + } + + Request request = new Request.Builder() + .url(getAPIsURL) + .addHeader(Constants.AUTHORIZATION_HEADER_NAME, Constants.AUTHORIZATION_HEADER_PREFIX_BEARER + + accessTokenInfo.getAccess_token()) + .get() + .build(); + + try { + Response response = client.newCall(request).execute(); + if (HttpStatus.SC_OK == response.code()) { + 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 getAllApis(apiApplicationKey, refreshedAccessToken, queryParams); + } else if (HttpStatus.SC_BAD_REQUEST == response.code()) { + String msg = "Bad Request, Invalid request"; + log.error(msg); + throw new BadRequestException(msg); + } else { + String msg = "Response : " + response.code() + response.body(); + throw new UnexpectedResponseException(msg); + } + } catch (IOException e) { + String msg = "Error occurred while processing the response"; + log.error(msg, e); + throw new APIServicesException(msg, e); + } + } + + @Override + public Subscription createSubscription(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo, + Subscription subscriptions) + throws APIServicesException, BadRequestException, UnexpectedResponseException { + + String getAllScopesUrl = endPointPrefix + Constants.SUBSCRIPTION_API; + + String subscriptionObject = "{\n" + + " \"applicationId\": \"" + subscriptions.getApplicationId() + "\",\n" + + " \"apiId\": \"" + subscriptions.getApiId() + "\",\n" + + " \"throttlingPolicy\": \"" + subscriptions.getThrottlingPolicy() + "\",\n" + + " \"requestedThrottlingPolicy\": \"" + subscriptions.getRequestedThrottlingPolicy() + "\"\n" + + "}"; + + RequestBody requestBody = RequestBody.create(JSON, subscriptionObject); + Request request = new Request.Builder() + .url(getAllScopesUrl) + .addHeader(Constants.AUTHORIZATION_HEADER_NAME, Constants.AUTHORIZATION_HEADER_PREFIX_BEARER + + accessTokenInfo.getAccess_token()) + .post(requestBody) + .build(); + + try { + Response response = client.newCall(request).execute(); + if (HttpStatus.SC_CREATED == response.code()) { + return gson.fromJson(response.body().string(), Subscription.class); + } 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 createSubscription(apiApplicationKey, refreshedAccessToken, subscriptions); + } else if (HttpStatus.SC_BAD_REQUEST == response.code()) { + String msg = "Bad Request, Invalid request body"; + log.error(msg); + throw new BadRequestException(msg); + } else { + String msg = "Response : " + response.code() + response.body(); + throw new UnexpectedResponseException(msg); + } + } catch (IOException e) { + String msg = "Error occurred while processing the response"; + log.error(msg, e); + throw new APIServicesException(msg, e); + } + } + + @Override + public Subscription createSubscriptions(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo, + List subscriptions) + throws APIServicesException, BadRequestException, UnexpectedResponseException { + + String getAllScopesUrl = endPointPrefix + Constants.SUBSCRIPTION_API + "/multiple"; + + String subscriptionsList = gson.toJson(subscriptions); + + RequestBody requestBody = RequestBody.create(JSON, subscriptionsList); + Request request = new Request.Builder() + .url(getAllScopesUrl) + .addHeader(Constants.AUTHORIZATION_HEADER_NAME, Constants.AUTHORIZATION_HEADER_PREFIX_BEARER + + accessTokenInfo.getAccess_token()) + .post(requestBody) + .build(); + + try { + Response response = client.newCall(request).execute(); + if (HttpStatus.SC_OK == response.code()) { + return gson.fromJson(response.body().string(), Subscription.class); + } 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 createSubscriptions(apiApplicationKey, refreshedAccessToken, subscriptions); + } else if (HttpStatus.SC_BAD_REQUEST == response.code()) { + String msg = "Bad Request, Invalid request body"; + log.error(msg); + throw new BadRequestException(msg); + } else { + String msg = "Response : " + response.code() + response.body(); + throw new UnexpectedResponseException(msg); + } + } catch (IOException e) { + String msg = "Error occurred while processing the response"; + log.error(msg, e); + throw new APIServicesException(msg, e); + } + } + + @Override + public APIKey generateApplicationKeys(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo, + String applicationId) + throws APIServicesException, BadRequestException, UnexpectedResponseException { + + String getAllScopesUrl = endPointPrefix + Constants.SUBSCRIPTION_API + Constants.SLASH + applicationId + + "/generate-keys"; + + String keyInfo = "{\n" + + " \"validityPeriod\": 3600,\n" + + " \"additionalProperties\": {}\n" + + "}"; + + RequestBody requestBody = RequestBody.create(JSON, keyInfo); + Request request = new Request.Builder() + .url(getAllScopesUrl) + .addHeader(Constants.AUTHORIZATION_HEADER_NAME, Constants.AUTHORIZATION_HEADER_PREFIX_BEARER + + accessTokenInfo.getAccess_token()) + .post(requestBody) + .build(); + + try { + Response response = client.newCall(request).execute(); + if (HttpStatus.SC_OK == response.code()) { + return gson.fromJson(response.body().string(), APIKey.class); + } 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 generateApplicationKeys(apiApplicationKey, refreshedAccessToken, applicationId); + } else if (HttpStatus.SC_BAD_REQUEST == response.code()) { + String msg = "Bad Request, Invalid request body"; + log.error(msg); + throw new BadRequestException(msg); + } else { + String msg = "Response : " + response.code() + response.body(); + throw new UnexpectedResponseException(msg); + } + } catch (IOException e) { + String msg = "Error occurred while processing the response"; + log.error(msg, e); + throw new APIServicesException(msg, e); + } + } + + @Override + public JSONObject getAllKeyManagers(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo) + throws APIServicesException, BadRequestException, UnexpectedResponseException { + + String getAllKeyManagersUrl = endPointPrefix + Constants.KEY_MANAGERS_API; + Request request = new Request.Builder() + .url(getAllKeyManagersUrl) + .addHeader(Constants.AUTHORIZATION_HEADER_NAME, Constants.AUTHORIZATION_HEADER_PREFIX_BEARER + + accessTokenInfo.getAccess_token()) + .get() + .build(); + + try { + Response response = client.newCall(request).execute(); + if (HttpStatus.SC_OK == response.code()) { + 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 getAllKeyManagers(apiApplicationKey, refreshedAccessToken); + } else if (HttpStatus.SC_BAD_REQUEST == response.code()) { + String msg = "Bad Request, Invalid request"; + log.error(msg); + throw new BadRequestException(msg); + } else { + String msg = "Response : " + response.code() + response.body(); + throw new UnexpectedResponseException(msg); + } + } catch (IOException e) { + String msg = "Error occurred while processing the response"; + log.error(msg, e); + throw new APIServicesException(msg, e); + } + } +} diff --git a/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.extension.rest.api/src/main/java/io/entgra/device/mgt/core/apimgt/extension/rest/api/bean/APIMConsumer/APIKey.java b/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.extension.rest.api/src/main/java/io/entgra/device/mgt/core/apimgt/extension/rest/api/bean/APIMConsumer/APIKey.java new file mode 100644 index 00000000000..022f1831b41 --- /dev/null +++ b/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.extension.rest.api/src/main/java/io/entgra/device/mgt/core/apimgt/extension/rest/api/bean/APIMConsumer/APIKey.java @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2018 - 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.bean.APIMConsumer; + +public class APIKey { + + private String apikey; + private int validityTime; + + public String getApikey() { + return apikey; + } + + public void setApikey(String apikey) { + this.apikey = apikey; + } + + public int getValidityTime() { + return validityTime; + } + + public void setValidityTime(int validityTime) { + this.validityTime = validityTime; + } +} diff --git a/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.extension.rest.api/src/main/java/io/entgra/device/mgt/core/apimgt/extension/rest/api/bean/APIMConsumer/Application.java b/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.extension.rest.api/src/main/java/io/entgra/device/mgt/core/apimgt/extension/rest/api/bean/APIMConsumer/Application.java new file mode 100644 index 00000000000..4764f001b91 --- /dev/null +++ b/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.extension.rest.api/src/main/java/io/entgra/device/mgt/core/apimgt/extension/rest/api/bean/APIMConsumer/Application.java @@ -0,0 +1,143 @@ +/* + * Copyright (c) 2018 - 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.bean.APIMConsumer; + +import org.json.JSONObject; + +import java.util.List; + +public class Application { + private String applicationId; + private String name; + private String throttlingPolicy; + private String description; + private String tokenType; + private String status; + private List groups; + private int subscriptionCount; + private List keys; + private JSONObject attributes; + private List subscriptionScopes; + private String owner; + private boolean hashEnabled; + + public String getApplicationId() { + return applicationId; + } + + public void setApplicationId(String applicationId) { + this.applicationId = applicationId; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getThrottlingPolicy() { + return throttlingPolicy; + } + + public void setThrottlingPolicy(String throttlingPolicy) { + this.throttlingPolicy = throttlingPolicy; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public String getTokenType() { + return tokenType; + } + + public void setTokenType(String tokenType) { + this.tokenType = tokenType; + } + + public String getStatus() { + return status; + } + + public void setStatus(String status) { + this.status = status; + } + + public List getGroups() { + return groups; + } + + public void setGroups(List groups) { + this.groups = groups; + } + + public int getSubscriptionCount() { + return subscriptionCount; + } + + public void setSubscriptionCount(int subscriptionCount) { + this.subscriptionCount = subscriptionCount; + } + + public List getKeys() { + return keys; + } + + public void setKeys(List keys) { + this.keys = keys; + } + + public JSONObject getAttributes() { + return attributes; + } + + public void setAttributes(JSONObject attributes) { + this.attributes = attributes; + } + + public List getSubscriptionScopes() { + return subscriptionScopes; + } + + public void setSubscriptionScopes(List subscriptionScopes) { + this.subscriptionScopes = subscriptionScopes; + } + + public String getOwner() { + return owner; + } + + public void setOwner(String owner) { + this.owner = owner; + } + + public boolean isHashEnabled() { + return hashEnabled; + } + + public void setHashEnabled(boolean hashEnabled) { + this.hashEnabled = hashEnabled; + } +} diff --git a/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.extension.rest.api/src/main/java/io/entgra/device/mgt/core/apimgt/extension/rest/api/bean/APIMConsumer/Subscription.java b/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.extension.rest.api/src/main/java/io/entgra/device/mgt/core/apimgt/extension/rest/api/bean/APIMConsumer/Subscription.java new file mode 100644 index 00000000000..edfbce6b6a9 --- /dev/null +++ b/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.extension.rest.api/src/main/java/io/entgra/device/mgt/core/apimgt/extension/rest/api/bean/APIMConsumer/Subscription.java @@ -0,0 +1,106 @@ +/* + * Copyright (c) 2018 - 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.bean.APIMConsumer; + +import org.json.JSONObject; + +public class Subscription { + + private String subscriptionId; + private String applicationId; + private String apiId; + private JSONObject apiInfo; + private JSONObject applicationInfo; + private String throttlingPolicy; + private String requestedThrottlingPolicy; + private String status; + private String redirectionParams; + + public String getSubscriptionId() { + return subscriptionId; + } + + public void setSubscriptionId(String subscriptionId) { + this.subscriptionId = subscriptionId; + } + + public String getApplicationId() { + return applicationId; + } + + public void setApplicationId(String applicationId) { + this.applicationId = applicationId; + } + + public String getApiId() { + return apiId; + } + + public void setApiId(String apiId) { + this.apiId = apiId; + } + + public JSONObject getApiInfo() { + return apiInfo; + } + + public void setApiInfo(JSONObject apiInfo) { + this.apiInfo = apiInfo; + } + + public JSONObject getApplicationInfo() { + return applicationInfo; + } + + public void setApplicationInfo(JSONObject applicationInfo) { + this.applicationInfo = applicationInfo; + } + + public String getThrottlingPolicy() { + return throttlingPolicy; + } + + public void setThrottlingPolicy(String throttlingPolicy) { + this.throttlingPolicy = throttlingPolicy; + } + + public String getRequestedThrottlingPolicy() { + return requestedThrottlingPolicy; + } + + public void setRequestedThrottlingPolicy(String requestedThrottlingPolicy) { + this.requestedThrottlingPolicy = requestedThrottlingPolicy; + } + + public String getStatus() { + return status; + } + + public void setStatus(String status) { + this.status = status; + } + + public String getRedirectionParams() { + return redirectionParams; + } + + public void setRedirectionParams(String redirectionParams) { + this.redirectionParams = redirectionParams; + } +} diff --git a/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.extension.rest.api/src/main/java/io/entgra/device/mgt/core/apimgt/extension/rest/api/constants/Constants.java b/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.extension.rest.api/src/main/java/io/entgra/device/mgt/core/apimgt/extension/rest/api/constants/Constants.java index 5a577e3eb12..5ce6c023c95 100644 --- a/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.extension.rest.api/src/main/java/io/entgra/device/mgt/core/apimgt/extension/rest/api/constants/Constants.java +++ b/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.extension.rest.api/src/main/java/io/entgra/device/mgt/core/apimgt/extension/rest/api/constants/Constants.java @@ -24,6 +24,9 @@ public final class Constants { } public static final String EMPTY_STRING = ""; + public static final String AMPERSAND = "&"; + public static final String SLASH = "/"; + public static final String EQUAL = "="; public static final String CLIENT_NAME = "rest_api_publisher_code"; public static final String SERVER_USER = "WorkflowConfigurations.ServerUser"; public static final String SERVER_PASSWORD = "WorkflowConfigurations.ServerPassword"; @@ -34,7 +37,9 @@ public final class Constants { public static final String OAUTH_TOKEN_TYPE = "token_type"; public static final String REFRESH_TOKEN_GRANT_TYPE = "refresh_token"; public static final String SCOPE_PARAM_NAME = "scope"; - public static final String SCOPES = "apim:api_create apim:api_view apim:shared_scope_manage apim:api_import_export apim:api_publish"; + public static final String SCOPES = "apim:api_create apim:api_view apim:shared_scope_manage apim:api_import_export " + + "apim:api_publish apim:admin apim:api_key apim:app_import_export apim:app_manage apim:store_settings " + + "apim:sub_alert_manage apim:sub_manage apim:subscribe openid"; 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"; @@ -65,6 +70,10 @@ public final class Constants { public static final String SCOPE_API_ENDPOINT = "/api/am/publisher/v2/scopes/"; public static final String API_ENDPOINT = "/api/am/publisher/v2/apis/"; public static final String GET_ALL_APIS = "/api/am/publisher/v2/apis?limit=1000"; + public static final String APPLICATIONS_API = "/api/am/devportal/v3/applications"; + public static final String SUBSCRIPTION_API = "/api/am/devportal/v3/subscriptions"; + public static final String DEV_PORTAL_API = "/api/am/devportal/v3/apis?limit=1000"; + public static final String KEY_MANAGERS_API = "/api/am/devportal/v2/key-managers"; } diff --git a/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.extension.rest.api/src/main/java/io/entgra/device/mgt/core/apimgt/extension/rest/api/internal/PublisherRESTAPIServiceComponent.java b/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.extension.rest.api/src/main/java/io/entgra/device/mgt/core/apimgt/extension/rest/api/internal/PublisherRESTAPIServiceComponent.java index 799d5ee1c9c..86cb5109e3f 100644 --- a/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.extension.rest.api/src/main/java/io/entgra/device/mgt/core/apimgt/extension/rest/api/internal/PublisherRESTAPIServiceComponent.java +++ b/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.extension.rest.api/src/main/java/io/entgra/device/mgt/core/apimgt/extension/rest/api/internal/PublisherRESTAPIServiceComponent.java @@ -20,6 +20,8 @@ package io.entgra.device.mgt.core.apimgt.extension.rest.api.internal; import io.entgra.device.mgt.core.apimgt.extension.rest.api.APIApplicationServices; import io.entgra.device.mgt.core.apimgt.extension.rest.api.APIApplicationServicesImpl; +import io.entgra.device.mgt.core.apimgt.extension.rest.api.ConsumerRESTAPIServices; +import io.entgra.device.mgt.core.apimgt.extension.rest.api.ConsumerRESTAPIServicesImpl; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.osgi.framework.BundleContext; @@ -51,6 +53,9 @@ public class PublisherRESTAPIServiceComponent { bundleContext.registerService(APIApplicationServices.class.getName(), apiApplicationServices, null); PublisherRESTAPIDataHolder.getInstance().setApiApplicationServices(apiApplicationServices); + ConsumerRESTAPIServices consumerRESTAPIServices = new ConsumerRESTAPIServicesImpl(); + bundleContext.registerService(ConsumerRESTAPIServices.class.getName(), consumerRESTAPIServices, null); + if (log.isDebugEnabled()) { log.debug("API Application bundle has been successfully initialized"); }