porting to smartgears 4

This commit is contained in:
Lucio Lelii 2022-06-15 17:57:12 +02:00
parent b4bbaf8816
commit 66ce9b8a23
17 changed files with 43 additions and 186 deletions

View File

@ -2,6 +2,10 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
# Changelog for gxREST
## [v2.0.0-SNAPSHOT]
- removed all the old providers
- removed set of gcube headers
## [v1.2.0]

View File

@ -2,6 +2,11 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
# Changelog for gxHTTP
## [v2.0.0-SNAPSHOT]
- removed all the old providers
- removed set of gcube headers
## [v1.2.0]

View File

@ -7,7 +7,7 @@
<parent>
<groupId>org.gcube.common</groupId>
<artifactId>gxREST</artifactId>
<version>1.2.0</version>
<version>2.0.0-SNAPSHOT</version>
</parent>
<artifactId>gxHTTP</artifactId>
@ -28,11 +28,6 @@
</scm>
<dependencies>
<dependency>
<groupId>org.gcube.common</groupId>
<artifactId>authorization-client</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>

View File

@ -14,8 +14,6 @@ import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
import org.gcube.common.authorization.library.provider.AccessTokenProvider;
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
import org.gcube.common.gxhttp.request.GXHTTPStringRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -119,28 +117,11 @@ public class GXConnection {
public HttpURLConnection send(HTTPMETHOD method) throws Exception {
return send(this.buildURL(), method);
}
private HttpURLConnection addGCubeAuthorizationToken(HttpURLConnection uConn) throws Exception {
if (!this.extCall) {
String oldGcubeAuthToken = SecurityTokenProvider.instance.get();
if (Objects.isNull(oldGcubeAuthToken) || oldGcubeAuthToken.isEmpty()) {
String umaToken = AccessTokenProvider.instance.get();
if(Objects.isNull(umaToken) || umaToken.isEmpty()) {
throw new IllegalStateException("The security token in the current environment is null.");
}else {
uConn.setRequestProperty("Authorization", "Bearer " + umaToken);
}
} else {
uConn.setRequestProperty(org.gcube.common.authorization.client.Constants.TOKEN_HEADER_ENTRY, oldGcubeAuthToken);
}
}
return uConn;
}
private HttpURLConnection send(URL url, HTTPMETHOD method) throws Exception {
HttpURLConnection uConn = (HttpURLConnection) url.openConnection();
uConn = addGCubeAuthorizationToken(uConn);
//uConn = addGCubeAuthorizationToken(uConn);
uConn.setDoOutput(true);
// uConn.setRequestProperty("Content-type", APPLICATION_JSON_CHARSET_UTF_8);

View File

@ -85,17 +85,6 @@ public interface GXHTTP<BODY,RESPONSE> {
* @throws Exception
*/
RESPONSE connect() throws Exception;
/**
* Overrides the default security token.
* @param token the new token
*/
void setSecurityToken(String token);
/**
* States if the service being called in an external service (not gCube).
* @param ext true if external, false otherwise
*/
void isExternalCall(boolean ext);
}

View File

@ -78,24 +78,7 @@ public class GXHTTPRequestBuilder {
public static final String UUID_REGEX = "^([a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}-[a-fA-F0-9]{8,9}){1}$";
/**
* Overrides the default security token.
*
* @param token
*/
public void setSecurityToken(String token) {
if (!this.connection.isExtCall()) {
if(Pattern.matches(UUID_REGEX, token)) {
this.connection.setProperty(org.gcube.common.authorization.client.Constants.TOKEN_HEADER_ENTRY, token);
}else {
this.connection.setProperty("Authorization", "Bearer " + token);
}
}else {
throw new UnsupportedOperationException("Cannot set the security token on an external call");
}
}
/**
* Add headers to the request.
*

View File

@ -84,12 +84,6 @@ class GXHTTPCommonRequest {
return builder.connect();
}
/* (non-Javadoc)
* @see org.gcube.common.gxhttp.reference.GXHTTP#setSecurityToken(java.lang.String)
*/
public void setSecurityToken(String token) {
builder.setSecurityToken(token);
}
/* (non-Javadoc)
* @see org.gcube.common.gxhttp.reference.GXHTTP#isExternalCall(boolean)

View File

@ -10,15 +10,9 @@ import java.util.Map;
import java.util.Properties;
import java.util.WeakHashMap;
import org.gcube.common.authorization.client.Constants;
import org.gcube.common.authorization.client.exceptions.ObjectNotFound;
import org.gcube.common.authorization.library.AuthorizationEntry;
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
import org.gcube.common.gxhttp.request.GXHTTPStringRequest;
import org.gcube.common.gxhttp.util.ContentUtils;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
@ -56,30 +50,6 @@ public class GXHTTPStringRequestTest {
}
}
@BeforeClass
public static void beforeClass() throws Exception {
setContext(DEFAULT_TEST_SCOPE);
}
public static void setContext(String token) throws ObjectNotFound, Exception {
if (DEFAULT_TEST_SCOPE.isEmpty()) {
skipTest = true;
return;
}
SecurityTokenProvider.instance.set(token);
}
public static String getCurrentScope(String token) throws ObjectNotFound, Exception {
AuthorizationEntry authorizationEntry = Constants.authorizationService().get(token);
String context = authorizationEntry.getContext();
return context;
}
@AfterClass
public static void afterClass() throws Exception {
SecurityTokenProvider.instance.reset();
}
/**
* Test method for {@link org.gcube.common.gxhttp.request.GXHTTPStringRequest#newRequest(java.lang.String)}.
*/

View File

@ -2,6 +2,10 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
# Changelog for gxJRS
## [v2.0.0-SNAPSHOT]
- removed all the old providers
- removed set of gcube headers
## [v1.2.0]

View File

@ -5,7 +5,7 @@
<parent>
<groupId>org.gcube.common</groupId>
<artifactId>gxREST</artifactId>
<version>1.2.0</version>
<version>2.0.0-SNAPSHOT</version>
</parent>
<artifactId>gxJRS</artifactId>
@ -37,7 +37,7 @@
<dependency>
<groupId>org.gcube.distribution</groupId>
<artifactId>gcube-bom</artifactId>
<version>2.0.1</version>
<version>3.0.0-SNAPSHOT</version>
<type>pom</type>
<scope>import</scope>
</dependency>

View File

@ -137,22 +137,6 @@ public class GXHTTPStreamRequest implements GXHTTP<InputStream,GXInboundResponse
return new GXInboundResponse(builder.connect());
}
/* (non-Javadoc)
* @see org.gcube.common.gxrest.request.GXHTTP#setSecurityToken(java.lang.String)
*/
@Override
public void setSecurityToken(String token) {
builder.setSecurityToken(token);
}
/* (non-Javadoc)
* @see org.gcube.common.gxrest.request.GXHTTP#isExternalCall(boolean)
*/
@Override
public void isExternalCall(boolean ext) {
builder.isExternalCall(ext);
}
/**
* @param string
* @return the request

View File

@ -155,22 +155,6 @@ public class GXHTTPStringRequest implements GXHTTP<String,GXInboundResponse> {
return new GXInboundResponse(builder.connect());
}
/* (non-Javadoc)
* @see org.gcube.common.gxrest.request.GXHTTP#setSecurityToken(java.lang.String)
*/
@Override
public void setSecurityToken(String token) {
builder.setSecurityToken(token);
}
/* (non-Javadoc)
* @see org.gcube.common.gxrest.request.GXHTTP#isExternalCall(boolean)
*/
@Override
public void isExternalCall(boolean ext) {
builder.isExternalCall(ext);
}
/**
* @param string

View File

@ -7,8 +7,8 @@ import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.Map;
import java.util.Objects;
import java.util.Map.Entry;
import java.util.Objects;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLContext;
@ -25,7 +25,6 @@ import javax.ws.rs.core.MultivaluedHashMap;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.Response;
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
import org.gcube.common.gxhttp.reference.GXConnection;
import org.gcube.common.gxhttp.reference.GXHTTP;
import org.gcube.common.gxrest.response.inbound.GXInboundResponse;
@ -45,7 +44,6 @@ public class GXWebTargetAdapterRequest implements GXHTTP<Entity<?>,GXInboundResp
private static final Logger logger = LoggerFactory.getLogger(GXWebTargetAdapterRequest.class);
private MediaType[] mediaType;
MultivaluedMap<String, Object> headers = new MultivaluedHashMap<String, Object>();
private boolean extCall = false;
/**
* Creates a new request.
@ -121,26 +119,9 @@ public class GXWebTargetAdapterRequest implements GXHTTP<Entity<?>,GXInboundResp
}
}
this.adaptee = client.target(address);
this.headers.add(org.gcube.common.authorization.client.Constants.TOKEN_HEADER_ENTRY,
SecurityTokenProvider.instance.get());
this.headers.add("User-Agent", this.getClass().getSimpleName());
}
/**
* Overrides the default security token.
*
* @param token
* the new token
*/
@Override
public void setSecurityToken(String token) {
if (!this.extCall)
this.headers.add(org.gcube.common.authorization.client.Constants.TOKEN_HEADER_ENTRY, token);
else
throw new UnsupportedOperationException("Cannot set the security token on an external call");
}
/**
* Sets the identity user agent associated to the request.
*
@ -405,13 +386,4 @@ public class GXWebTargetAdapterRequest implements GXHTTP<Entity<?>,GXInboundResp
throw new UnsupportedOperationException("WebTarget does not support CONNECT");
}
/*
* (non-Javadoc)
*
* @see org.gcube.common.gxrest.request.GXHTTP#isExternalCall(boolean)
*/
@Override
public void isExternalCall(boolean ext) {
this.extCall = ext;
}
}

View File

@ -13,7 +13,6 @@ import org.gcube.com.fasterxml.jackson.databind.JsonMappingException;
import org.gcube.common.authorization.client.Constants;
import org.gcube.common.authorization.client.exceptions.ObjectNotFound;
import org.gcube.common.authorization.library.AuthorizationEntry;
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
import org.gcube.common.gxrest.response.inbound.GXInboundResponse;
import org.junit.AfterClass;
import org.junit.Before;
@ -67,7 +66,7 @@ public class GXHTTPStringRequestTest {
skipTest = true;
return;
}
SecurityTokenProvider.instance.set(token);
//SecurityTokenProvider.instance.set(token);
}
public static String getCurrentScope(String token) throws ObjectNotFound, Exception {
@ -78,7 +77,7 @@ public class GXHTTPStringRequestTest {
@AfterClass
public static void afterClass() throws Exception {
SecurityTokenProvider.instance.reset();
//SecurityTokenProvider.instance.reset();
}
/**

View File

@ -1,9 +1,20 @@
package org.gcube.common.gxrest.request;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
import java.util.Properties;
import java.util.WeakHashMap;
import javax.ws.rs.client.Entity;
import javax.ws.rs.core.MediaType;
import org.gcube.common.authorization.client.Constants;
import org.gcube.common.authorization.client.exceptions.ObjectNotFound;
import org.gcube.common.authorization.library.AuthorizationEntry;
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
import org.gcube.common.gxrest.response.inbound.GXInboundResponse;
import org.junit.AfterClass;
import org.junit.Before;
@ -12,17 +23,6 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.BlockJUnit4ClassRunner;
import javax.ws.rs.client.Entity;
import javax.ws.rs.core.MediaType;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
import java.util.Properties;
import java.util.WeakHashMap;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
/**
* Test cases for {@link GXWebTargetAdapterRequest#newHTTPSRequest(String)}
*
@ -70,7 +70,7 @@ public class GXWebTargetAdapterHTTPSRequestTest {
skipTest = true;
return;
} else {
SecurityTokenProvider.instance.set(token);
//SecurityTokenProvider.instance.set(token);
}
}
@ -82,7 +82,7 @@ public class GXWebTargetAdapterHTTPSRequestTest {
@AfterClass
public static void afterClass() throws Exception {
SecurityTokenProvider.instance.reset();
//SecurityTokenProvider.instance.reset();
}
/**

View File

@ -1,6 +1,7 @@
package org.gcube.common.gxrest.request;
import static org.junit.Assert.*;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.io.IOException;
import java.io.InputStream;
@ -11,10 +12,7 @@ import java.util.WeakHashMap;
import javax.ws.rs.client.Entity;
import javax.ws.rs.core.MediaType;
import org.gcube.common.authorization.client.Constants;
import org.gcube.common.authorization.client.exceptions.ObjectNotFound;
import org.gcube.common.authorization.library.AuthorizationEntry;
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
import org.gcube.common.gxrest.response.inbound.GXInboundResponse;
import org.junit.AfterClass;
import org.junit.Before;
@ -67,19 +65,14 @@ public class GXWebTargetAdapterRequestTest {
skipTest = true;
return;
} else {
SecurityTokenProvider.instance.set(token);
//SecurityTokenProvider.instance.set(token);
}
}
public static String getCurrentScope(String token) throws ObjectNotFound, Exception {
AuthorizationEntry authorizationEntry = Constants.authorizationService().get(token);
String context = authorizationEntry.getContext();
return context;
}
@AfterClass
public static void afterClass() throws Exception {
SecurityTokenProvider.instance.reset();
//SecurityTokenProvider.instance.reset();
}
/**

View File

@ -10,7 +10,7 @@
<groupId>org.gcube.common</groupId>
<artifactId>gxREST</artifactId>
<version>1.2.0</version>
<version>2.0.0-SNAPSHOT</version>
<packaging>pom</packaging>
<name>gCube eXtensions to REST</name>
<description>gCube eXtensions to REST</description>