diff --git a/README.md b/README.md index 68cd076c5161f7647f39704915873382b94d5535..cd753fb10040eb53e55bfe7df7df88f162cba749 100644 --- a/README.md +++ b/README.md @@ -87,3 +87,11 @@ Enabled transaction logger and slf4jlogger | --- | --- | --- | | `azure.blobStore.required` | `true` | - | | `azure.storage.account-name` | ex `testStorage` | storage account name | + +# Default retry and timeout values for service-to-service communication +| name | default value | +| --- | --- | +| `maxRetry` | `3` | +| `connectTimeout` | `60000` | +| `requestTimeout` | `60000` | +| `socketTimeout` | `60000` | diff --git a/src/main/java/org/opengroup/osdu/azure/di/RetryAndTimeoutConfiguration.java b/src/main/java/org/opengroup/osdu/azure/di/RetryAndTimeoutConfiguration.java new file mode 100644 index 0000000000000000000000000000000000000000..c519c66013078670863b9c53f871f361b114393c --- /dev/null +++ b/src/main/java/org/opengroup/osdu/azure/di/RetryAndTimeoutConfiguration.java @@ -0,0 +1,34 @@ +// Copyright © Microsoft Corporation +// +// Licensed 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 org.opengroup.osdu.azure.di; + +import lombok.Data; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.stereotype.Component; + +/** + * Configuration bean for setting up retry and timeouts variables. + */ + +@Data +@Component +@ConfigurationProperties(prefix = "azure.service.retry.config") +public class RetryAndTimeoutConfiguration { + + private int maxRetry = 3; + private int connectTimeout = 60000; + private int requestTimeout = 60000; + private int socketTimeout = 60000; +} diff --git a/src/main/java/org/opengroup/osdu/azure/entitlements/EntilementsAPIConfigBean.java b/src/main/java/org/opengroup/osdu/azure/entitlements/EntilementsAPIConfigBean.java new file mode 100644 index 0000000000000000000000000000000000000000..cd139977461edd2c1ee39c24f638e5fa2592c5c3 --- /dev/null +++ b/src/main/java/org/opengroup/osdu/azure/entitlements/EntilementsAPIConfigBean.java @@ -0,0 +1,59 @@ +// Copyright © Microsoft Corporation +// +// Licensed 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 org.opengroup.osdu.azure.entitlements; + +import lombok.Data; +import org.opengroup.osdu.core.common.entitlements.EntitlementsAPIConfig; +import org.springframework.beans.factory.config.AbstractFactoryBean; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.stereotype.Component; + +/** + * Creates bean of EntitlementsAPIConfig. + */ +@Data +@Component +@ConfigurationProperties +public class EntilementsAPIConfigBean extends AbstractFactoryBean { + + private String authorizeAPI; + + private String authorizeAPIKey; + + /** + * Abstract method of AbstractBeanFactory. + * + * @return class type + */ + @Override + public Class getObjectType() { + return EntitlementsAPIConfig.class; + } + + /** + * Abstract method of AbstractBeanFactory type. + * + * @return EntitlementsAPIConfig + * @throws Exception + */ + @Override + protected EntitlementsAPIConfig createInstance() throws Exception { + return EntitlementsAPIConfig + .builder() + .rootUrl(authorizeAPI) + .apiKey(authorizeAPIKey) + .build(); + } +} diff --git a/src/main/java/org/opengroup/osdu/azure/entitlements/EntitlementsFactoryAzure.java b/src/main/java/org/opengroup/osdu/azure/entitlements/EntitlementsFactoryAzure.java new file mode 100644 index 0000000000000000000000000000000000000000..f96f0b530476b936cc3aac61b953d13a15e46fac --- /dev/null +++ b/src/main/java/org/opengroup/osdu/azure/entitlements/EntitlementsFactoryAzure.java @@ -0,0 +1,72 @@ +// Copyright © Microsoft Corporation +// +// Licensed 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 org.opengroup.osdu.azure.entitlements; + +import org.opengroup.osdu.core.common.entitlements.EntitlementsAPIConfig; +import org.opengroup.osdu.core.common.entitlements.EntitlementsService; +import org.opengroup.osdu.core.common.entitlements.IEntitlementsFactory; +import org.opengroup.osdu.core.common.entitlements.IEntitlementsService; +import org.opengroup.osdu.core.common.http.IHttpClient; +import org.opengroup.osdu.core.common.http.json.HttpResponseBodyMapper; +import org.opengroup.osdu.core.common.model.http.DpsHeaders; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.context.annotation.Primary; +import org.springframework.stereotype.Component; + +import java.util.Objects; + +/** + * Implements IEntitlementsFactory. + */ +@Component +@Primary +@ConditionalOnProperty(value = "azure.entitlements.factory.enabled", havingValue = "true", matchIfMissing = true) +public class EntitlementsFactoryAzure implements IEntitlementsFactory { + + private final EntitlementsAPIConfig config; + private final HttpResponseBodyMapper mapper; + private final IHttpClient client; + + /** + * Constructor Injection for above 3 fields. + * + * @param entitlementsConfig EntitlementsAPIConfig + * @param httpMapper HttpResponseBodyMapper + * @param httpClient IHttpClient + */ + @Autowired + public EntitlementsFactoryAzure(final EntitlementsAPIConfig entitlementsConfig, final HttpResponseBodyMapper httpMapper, final IHttpClient httpClient) { + this.config = entitlementsConfig; + this.mapper = httpMapper; + this.client = httpClient; + } + + /** + * returns instance of EntitlementsService. + * + * @param headers DpsHeaders + * @return IEntitlementsService + */ + @Override + public IEntitlementsService create(final DpsHeaders headers) { + Objects.requireNonNull(headers, "headers cannot be null"); + + return new EntitlementsService(this.config, + this.client, + headers, + this.mapper); + } +} diff --git a/src/main/java/org/opengroup/osdu/azure/httpconfig/HttpClientAzure.java b/src/main/java/org/opengroup/osdu/azure/httpconfig/HttpClientAzure.java new file mode 100644 index 0000000000000000000000000000000000000000..7ba8efb8fd839750bc3a2d53b6208c6f66c3f3db --- /dev/null +++ b/src/main/java/org/opengroup/osdu/azure/httpconfig/HttpClientAzure.java @@ -0,0 +1,63 @@ +// Copyright © Microsoft Corporation +// +// Licensed 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 org.opengroup.osdu.azure.httpconfig; + +import org.apache.http.HttpStatus; +import org.opengroup.osdu.core.common.http.FetchServiceHttpRequest; +import org.opengroup.osdu.core.common.http.HttpRequest; +import org.opengroup.osdu.core.common.http.IHttpClient; +import org.opengroup.osdu.core.common.http.HttpResponse; +import org.opengroup.osdu.core.common.http.UrlFetchServiceImpl; +import org.opengroup.osdu.core.common.model.http.AppException; +import org.springframework.context.annotation.Primary; +import org.springframework.stereotype.Component; + +import java.net.URISyntaxException; + +/** + * Extends URlFetchService and implements IHttpClient to send requests. + */ +@Primary +@Component +public class HttpClientAzure extends UrlFetchServiceImpl implements IHttpClient { + + /** + * calls urlfetchservice's send request. + * + * @param httpRequest made by user class + * @return HttpResponse + */ + @Override + public HttpResponse send(final HttpRequest httpRequest) { + org.opengroup.osdu.core.common.model.http.HttpResponse response = null; + try { + response = super.sendRequest(FetchServiceHttpRequest.builder() + .body(httpRequest.getBody()) + .httpMethod(httpRequest.getHttpMethod()) + .queryParams(httpRequest.getQueryParams()) + .url(httpRequest.getUrl()) + .headers(httpRequest.getHeaders()) + .build()); + } catch (URISyntaxException e) { + throw new AppException(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getReason(), "URI Syntax is not correct", e); + } + HttpResponse httpResponse = new HttpResponse(); + httpResponse.setBody(response.getBody()); + httpResponse.setResponseCode(response.getResponseCode()); + httpResponse.setContentType(response.getContentType()); + httpResponse.setRequest(httpRequest); + return httpResponse; + } +} diff --git a/src/main/java/org/opengroup/osdu/azure/httpconfig/HttpClientHandlerAzure.java b/src/main/java/org/opengroup/osdu/azure/httpconfig/HttpClientHandlerAzure.java new file mode 100644 index 0000000000000000000000000000000000000000..27e0016ae5424cd89c0f318954ee6d8997f95acd --- /dev/null +++ b/src/main/java/org/opengroup/osdu/azure/httpconfig/HttpClientHandlerAzure.java @@ -0,0 +1,49 @@ +// Copyright © Microsoft Corporation +// +// Licensed 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 org.opengroup.osdu.azure.httpconfig; + +import org.apache.http.client.config.RequestConfig; +import org.opengroup.osdu.azure.di.RetryAndTimeoutConfiguration; +import org.opengroup.osdu.core.common.http.HttpClientHandler; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Primary; +import org.springframework.stereotype.Component; + +/** + * Extends HttpClientHandler. + */ +@Primary +@Component +public class HttpClientHandlerAzure extends HttpClientHandler { + + private RetryAndTimeoutConfiguration configuration; + + /** + * Constuctor injection for RetryAndTimeoutConfiguration. + * + * @param retryAndTimeoutConfiguration of type RetryAndTimeoutConfiguration + */ + @Autowired + public HttpClientHandlerAzure(final RetryAndTimeoutConfiguration retryAndTimeoutConfiguration) { + this.configuration = retryAndTimeoutConfiguration; + super.REQUEST_CONFIG = RequestConfig.custom() + .setConnectTimeout(configuration.getConnectTimeout()) + .setConnectionRequestTimeout(configuration.getRequestTimeout()) + .setSocketTimeout(configuration.getSocketTimeout()).build(); + + super.RETRY_COUNT = configuration.getMaxRetry(); + } + +}