Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Httpclient5 as an OSGI service #4022

Draft
wants to merge 1 commit into
base: 4.10.x
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 111 additions & 0 deletions core/org.wso2.carbon.http.client/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright (c) WSO2 LLC. (http://wso2.com) All Rights Reserved.
~
~ 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.
-->

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

<parent>
<groupId>org.wso2.carbon</groupId>
<artifactId>carbon-kernel</artifactId>
<version>4.10.17-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

<modelVersion>4.0.0</modelVersion>
<artifactId>org.wso2.carbon.http.client</artifactId>
<packaging>bundle</packaging>
<name>WSO2 Carbon - Http Client</name>
<description>OSGi Bundle for Carbon Http Client</description>
<url>http://wso2.org</url>

<dependencies>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.service.component.annotations</artifactId>
</dependency>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.annotation</artifactId>
</dependency>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.service.metatype.annotations</artifactId>
</dependency>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.compendium</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.wso2.org.ops4j.pax.logging</groupId>
<artifactId>pax-logging-api</artifactId>
</dependency>
<dependency>
<groupId>org.wso2.orbit.org.apache.httpcomponents</groupId>
<artifactId>httpclient5</artifactId>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
</dependency>
<!-- Test dependencies -->
<dependency>
<groupId>org.wso2.orbit.org.apache.httpcomponents</groupId>
<artifactId>httpcore5</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<extensions>true</extensions>
<configuration>
<instructions>
<Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName>
<Bundle-Name>${project.artifactId}</Bundle-Name>
<Axis2Module>${project.artifactId}-${project.version}</Axis2Module>
<Private-Package>
org.wso2.carbon.http.client.internal,
</Private-Package>
<Import-Package>
org.osgi.framework.*,
org.apache.commons.logging.*; version="${import.package.version.commons.logging}",
org.wso2.carbon.caching.impl.*,
*;resolution:=optional
</Import-Package>
<Export-Package>
!org.wso2.carbon.http.client.internal,
org.wso2.carbon.http.client.*,
</Export-Package>
</instructions>
</configuration>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright (c) 2024, WSO2 LLC. (https://www.wso2.com).
*
* WSO2 LLC. 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 org.wso2.carbon.http.client;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.HttpClientBuilder;

public class ClientUtils {

private static final Log log = LogFactory.getLog(ClientUtils.class);

private ClientUtils() {
}

public static CloseableHttpClient createClient() {

HttpClientBuilder httpClientBuilder = HttpClientBuilder.create().useSystemProperties();
if (log.isDebugEnabled()) {
log.debug("Creating a new HttpClient instance");
}
return httpClientBuilder.build();

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package org.wso2.carbon.http.client;

public class HttpClientConstants {

public enum Error {

RESPONSE_ENTITY_EMPTY("12001", "Response entity is empty"),
RESPONSE_PARSE_ERROR("12002", "Error occurred while parsing the response");

private final String code;
private final String message;
private static final String API_ERROR_CODE_PREFIX = "HTC-";

Error(String code, String message) {

this.code = code;
this.message = message;
}

public String getCode() {

return API_ERROR_CODE_PREFIX + code;
}

public String getMessage() {

return message;
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* Copyright (c) 2024, WSO2 LLC. (https://www.wso2.com).
*
* WSO2 LLC. 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 org.wso2.carbon.http.client.cache;

import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.RemovalListener;

import java.io.Serializable;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

/**
* A base class for all cache implementations in Identity modules. This maintains caches in the tenanted space.
* A copy of this class is maintained at org.wso2.carbon.identity.organization.management.service.cache component.
*
* @param <K> cache key type.
* @param <V> cache value type.
*/
public abstract class ClientBaseCache<K extends Serializable, V> {

private final Cache<K, V> cache;

protected ClientBaseCache(int cacheSize, int expireAfterAccess, RemovalListener<K, V> removalListener) {

cache = CacheBuilder.newBuilder()
.maximumSize(cacheSize)
.expireAfterAccess(expireAfterAccess, TimeUnit.MILLISECONDS)
.removalListener(removalListener)
.build();
}

public void put(K key, V value) {

cache.put(key, value);
}

public V get(K key, Callable<V> loader) {

try {
return cache.get(key, loader);
} catch (ExecutionException e) {
// TODO: handle exception
return null;
}
}

public V get(K key) {

return cache.getIfPresent(key);
}

public void cleanUp() {

cache.cleanUp();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright (c) 2024, WSO2 LLC. (https://www.wso2.com).
*
* WSO2 LLC. 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 org.wso2.carbon.http.client.cache;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;

import java.io.IOException;

public class HttpClientCache extends ClientBaseCache<String, CloseableHttpClient> {

private static final Log log = LogFactory.getLog(HttpClientCache.class);
private static HttpClientCache instance;


protected HttpClientCache(int cacheSize, int expireAfterAccess) {

super(cacheSize, expireAfterAccess, httpClientCacheEntry -> {
// This is not called at expire
// https://stackoverflow.com/questions/21986551/guava-cachebuilder-doesnt-call-removal-listener
try {
if (httpClientCacheEntry.getValue() != null) {
CloseableHttpClient client = httpClientCacheEntry.getValue();
client.close();
if (log.isDebugEnabled()) {
log.debug("Http Client - " + httpClientCacheEntry.getKey() + " removed due to " +
httpClientCacheEntry.getCause());
}
}
} catch (IOException e) {
log.error("Error occurred while closing the http client for key: " + httpClientCacheEntry.getKey(), e);
}
});
}

public static HttpClientCache getInstance() {

if (instance == null) {
int ttlMinutes = 1;
int cacheTimeoutMinutes = 5;
instance = new HttpClientCache(100, (ttlMinutes + cacheTimeoutMinutes) * 60 * 1000);
}
return instance;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright (c) 2024, WSO2 LLC. (https://www.wso2.com).
*
* WSO2 LLC. 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 org.wso2.carbon.http.client.exception;

import java.io.IOException;

public class HttpClientException extends IOException {

private String errorCode;

public HttpClientException(String message) {
super(message);
}

public HttpClientException(String errorCode, String message) {

super(message);
this.errorCode = errorCode;
}

public HttpClientException(String message, Throwable cause) {

super(message, cause);
}

public HttpClientException(String errorCode, String message, Throwable cause) {

super(message, cause);
this.errorCode = errorCode;
}

public String getErrorCode() {

return errorCode;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package org.wso2.carbon.http.client.handler;

import org.apache.hc.core5.http.ClassicHttpResponse;
import org.apache.hc.core5.http.HttpEntity;
import org.apache.hc.core5.http.io.HttpClientResponseHandler;
import org.wso2.carbon.http.client.HttpClientConstants;
import org.wso2.carbon.http.client.exception.HttpClientException;

public abstract class AbstractResponseHandler<T> implements HttpClientResponseHandler<T> {

public T handleResponse(ClassicHttpResponse response) throws HttpClientException {

// TODO: handle response status codes
HttpEntity entity = response.getEntity();
if (entity == null) {
throw new HttpClientException(HttpClientConstants.Error.RESPONSE_ENTITY_EMPTY.getCode(),
HttpClientConstants.Error.RESPONSE_ENTITY_EMPTY.getMessage());
}
return this.handleEntity(entity);
}

protected abstract T handleEntity(HttpEntity httpEntity) throws HttpClientException;
}
Loading