Skip to content
Merged
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
7 changes: 1 addition & 6 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.10.5</version>
<version>2.9.10.8</version>
<type>jar</type>
</dependency>
<dependency>
Expand All @@ -70,11 +70,6 @@
<artifactId>gson</artifactId>
<version>2.8.5</version>
</dependency>
<dependency>
<groupId>org.java-websocket</groupId>
<artifactId>Java-WebSocket</artifactId>
<version>1.5.1</version>
</dependency>
</dependencies>


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.oneidentity.safeguard.safeguardjava;

import com.oneidentity.safeguard.safeguardjava.data.TransferProgress;

public interface IProgressCallback {
public void checkProgress(TransferProgress transferProgress);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.oneidentity.safeguard.safeguardjava.exceptions.ArgumentException;
import com.oneidentity.safeguard.safeguardjava.exceptions.ObjectDisposedException;
import com.oneidentity.safeguard.safeguardjava.exceptions.SafeguardForJavaException;
import java.time.Duration;
import java.util.Map;

/**
Expand Down Expand Up @@ -45,14 +46,15 @@ public interface ISafeguardConnection {
* @param body Request body to pass to the method.
* @param parameters Additional parameters to add to the URL.
* @param additionalHeaders Additional headers to add to the request.
* @param timeout Per-request timeout in milliseconds (null for default)
* @return Response body as a string.
* @throws ObjectDisposedException Object has already been disposed.
* @throws SafeguardForJavaException General Safeguard for Java exception.
* @throws ArgumentException Invalid argument.
*/
String invokeMethod(Service service, Method method, String relativeUrl,
String body, Map<String, String> parameters,
Map<String, String> additionalHeaders)
Map<String, String> additionalHeaders, Integer timeout)
throws ObjectDisposedException, SafeguardForJavaException, ArgumentException;

/**
Expand All @@ -65,14 +67,15 @@ String invokeMethod(Service service, Method method, String relativeUrl,
* @param body Request body to pass to the method.
* @param parameters Additional parameters to add to the URL.
* @param additionalHeaders Additional headers to add to the request.
* @param timeout Per-request timeout in milliseconds (null for default)
* @return Response with status code, headers, and body as string.
* @throws ObjectDisposedException Object has already been disposed.
* @throws SafeguardForJavaException General Safeguard for Java exception.
* @throws ArgumentException Invalid argument.
*/
FullResponse invokeMethodFull(Service service, Method method, String relativeUrl,
String body, Map<String, String> parameters,
Map<String, String> additionalHeaders)
Map<String, String> additionalHeaders, Integer timeout)
throws ObjectDisposedException, SafeguardForJavaException, ArgumentException;

/*
Expand All @@ -86,16 +89,23 @@ FullResponse invokeMethodFull(Service service, Method method, String relativeUrl
* @param body Request body to pass to the method.
* @param parameters Additional parameters to add to the URL.
* @param additionalHeaders Additional headers to add to the request.
* @param timeout Per-request timeout in milliseconds (null for default)
* @returns Response body as a CSV string.
* @throws ObjectDisposedException Object has already been disposed.
* @throws SafeguardForJavaException General Safeguard for Java exception.
* @throws ArgumentException Invalid argument.
*/
String invokeMethodCsv(Service service, Method method, String relativeUrl,
String body, Map<String, String> parameters,
Map<String, String> additionalHeaders)
Map<String, String> additionalHeaders, Integer timeout)
throws ObjectDisposedException, SafeguardForJavaException, ArgumentException;

/**
* Provides support for HTTP streaming requests
* @return IStreamingRequest
*/
IStreamingRequest getStreamingRequest();

/**
* Gets a Safeguard event listener. You will need to call the RegisterEventHandler()
* method to establish callbacks. Then, you just have to call Start(). Call Stop()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.oneidentity.safeguard.safeguardjava;

import com.oneidentity.safeguard.safeguardjava.data.Service;
import com.oneidentity.safeguard.safeguardjava.exceptions.ArgumentException;
import com.oneidentity.safeguard.safeguardjava.exceptions.ObjectDisposedException;
import com.oneidentity.safeguard.safeguardjava.exceptions.SafeguardForJavaException;
import java.util.Map;

/**
* HTTP streaming request methods
*/
public interface IStreamingRequest {

/**
* Call a Safeguard POST API providing a stream as request content. If there is a
* failure a SafeguardDotNetException will be thrown.
*
* @param service Safeguard service to call.
* @param relativeUrl Relative URL of the service to use.
* @param stream Stream to upload as request content.
* @param progressCallback Optionally report upload progress.
* @param parameters Additional parameters to add to the URL.
* @param additionalHeaders Additional headers to add to the request.
* @return Response body as a string.
*/
String uploadStream(Service service, String relativeUrl, byte[] stream, IProgressCallback progressCallback, Map<String, String> parameters, Map<String, String> additionalHeaders) throws SafeguardForJavaException, ArgumentException, ObjectDisposedException;

/**
* Call a Safeguard GET API providing an output file path to which streaming download data will
* be written. If there is a failure a SafeguardDotNetException will be thrown.
*
* @param service Safeguard service to call.
* @param relativeUrl Relative URL of the service to use.
* @param outputFilePath Full path to the file where download will be written.
* @param body Optional request body
* @param progressCallback Optionally report upload progress.
* @param parameters Additional parameters to add to the URL.
* @param additionalHeaders Additional headers to add to the request.
*/
void downloadStream(Service service, String relativeUrl, String outputFilePath, IProgressCallback progressCallback, Map<String, String> parameters, Map<String, String> additionalHeaders) throws SafeguardForJavaException, ArgumentException, ObjectDisposedException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public List<A2ARetrievableAccount> getRetrievableAccounts() throws ObjectDispos

Map<String, String> parameters = new HashMap<>();

CloseableHttpResponse response = coreClient.execGET("A2ARegistrations", parameters, headers, clientCertificate);
CloseableHttpResponse response = coreClient.execGET("A2ARegistrations", parameters, headers, null, clientCertificate);

if (response == null) {
throw new SafeguardForJavaException(String.format("Unable to connect to web service %s", a2AClient.getBaseURL()));
Expand All @@ -111,7 +111,7 @@ public List<A2ARetrievableAccount> getRetrievableAccounts() throws ObjectDispos
int registrationId = registration.getId();

response = coreClient.execGET(String.format("A2ARegistrations/%d/RetrievableAccounts", registrationId),
parameters, headers, clientCertificate);
parameters, headers, null, clientCertificate);

if (response == null) {
throw new SafeguardForJavaException(String.format("Unable to connect to web service %s", a2AClient.getBaseURL()));
Expand Down Expand Up @@ -164,7 +164,7 @@ public char[] retrievePassword(char[] apiKey) throws ObjectDisposedException, Sa
Map<String, String> parameters = new HashMap<>();
parameters.put("type", "Password");

CloseableHttpResponse response = a2AClient.execGET("Credentials", parameters, headers, clientCertificate);
CloseableHttpResponse response = a2AClient.execGET("Credentials", parameters, headers, null, clientCertificate);

if (response == null) {
throw new SafeguardForJavaException(String.format("Unable to connect to web service %s", a2AClient.getBaseURL()));
Expand Down Expand Up @@ -200,7 +200,7 @@ public char[] retrievePrivateKey(char[] apiKey, KeyFormat keyFormat) throws Obje
parameters.put("type", "PrivateKey");
parameters.put("keyFormat", keyFormat.name());

CloseableHttpResponse response = a2AClient.execGET("Credentials", parameters, headers, clientCertificate);
CloseableHttpResponse response = a2AClient.execGET("Credentials", parameters, headers, null, clientCertificate);

if (response == null) {
throw new SafeguardForJavaException(String.format("Unable to connect to web service %s", a2AClient.getBaseURL()));
Expand Down Expand Up @@ -308,7 +308,7 @@ public String brokerAccessRequest(char[] apiKey, BrokeredAccessRequest accessReq

Map<String, String> parameters = new HashMap<>();

CloseableHttpResponse response = a2AClient.execPOST("AccessRequests", parameters, headers, accessRequest, clientCertificate);
CloseableHttpResponse response = a2AClient.execPOST("AccessRequests", parameters, headers, null, accessRequest, clientCertificate);

if (response == null) {
throw new SafeguardForJavaException(String.format("Unable to connect to web service %s", a2AClient.getBaseURL()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ class SafeguardConnection implements ISafeguardConnection {
private final RestClient coreClient;
private final RestClient applianceClient;
private final RestClient notificationClient;

private final IStreamingRequest streamingRequest;

public SafeguardConnection(IAuthenticationMechanism authenticationMechanism) {
this.authenticationMechanism = authenticationMechanism;

Expand All @@ -47,6 +48,8 @@ public SafeguardConnection(IAuthenticationMechanism authenticationMechanism) {
String safeguardNotificationUrl = String.format("https://%s/service/notification/v%d",
this.authenticationMechanism.getNetworkAddress(), this.authenticationMechanism.getApiVersion());
notificationClient = new RestClient(safeguardNotificationUrl, authenticationMechanism.isIgnoreSsl(), authenticationMechanism.getValidationCallback());

streamingRequest = new StreamingRequest(this);
}

@Override
Expand Down Expand Up @@ -74,17 +77,17 @@ public void refreshAccessToken() throws ObjectDisposedException, SafeguardForJav

@Override
public String invokeMethod(Service service, Method method, String relativeUrl, String body,
Map<String, String> parameters, Map<String, String> additionalHeaders)
Map<String, String> parameters, Map<String, String> additionalHeaders, Integer timeout)
throws ObjectDisposedException, SafeguardForJavaException, ArgumentException {
if (disposed) {
throw new ObjectDisposedException("SafeguardConnection");
}
return invokeMethodFull(service, method, relativeUrl, body, parameters, additionalHeaders).getBody();
return invokeMethodFull(service, method, relativeUrl, body, parameters, additionalHeaders, timeout).getBody();
}

@Override
public FullResponse invokeMethodFull(Service service, Method method, String relativeUrl,
String body, Map<String, String> parameters, Map<String, String> additionalHeaders)
String body, Map<String, String> parameters, Map<String, String> additionalHeaders, Integer timeout)
throws ObjectDisposedException, SafeguardForJavaException, ArgumentException {

if (disposed) {
Expand All @@ -101,25 +104,20 @@ public FullResponse invokeMethodFull(Service service, Method method, String rela
Map<String,String> headers = prepareHeaders(additionalHeaders, service);
CloseableHttpResponse response = null;

String msg = String.format("Invoking method: %s %s", method.toString().toUpperCase(), client.getBaseURL() + "/" + relativeUrl);
Logger.getLogger(SafeguardConnection.class.getName()).log(Level.FINEST, msg);
msg = parameters == null ? "None" : parameters.keySet().stream().map(key -> key + "=" + parameters.get(key)).collect(Collectors.joining(", ", "{", "}"));
Logger.getLogger(SafeguardConnection.class.getName()).log(Level.FINEST, " Query parameters: {0}", msg);
msg = headers == null ? "None" : headers.keySet().stream().map(key -> key + "=" + headers.get(key)).collect(Collectors.joining(", ", "{", "}"));
Logger.getLogger(SafeguardConnection.class.getName()).log(Level.FINEST, " Additional headers: {0}", msg);
logRequestDetails(method, client.getBaseURL() + "/" + relativeUrl, parameters, additionalHeaders);

switch (method) {
case Get:
response = client.execGET(relativeUrl, parameters, headers);
response = client.execGET(relativeUrl, parameters, headers, timeout);
break;
case Post:
response = client.execPOST(relativeUrl, parameters, headers, new JsonBody(body));
response = client.execPOST(relativeUrl, parameters, headers, timeout, new JsonBody(body));
break;
case Put:
response = client.execPUT(relativeUrl, parameters, headers, new JsonBody(body));
response = client.execPUT(relativeUrl, parameters, headers, timeout, new JsonBody(body));
break;
case Delete:
response = client.execDELETE(relativeUrl, parameters, headers);
response = client.execDELETE(relativeUrl, parameters, headers, timeout);
break;
}

Expand All @@ -136,18 +134,14 @@ public FullResponse invokeMethodFull(Service service, Method method, String rela

FullResponse fullResponse = new FullResponse(response.getStatusLine().getStatusCode(), response.getAllHeaders(), reply);

Logger.getLogger(SafeguardConnection.class.getName()).log(Level.FINEST, "Reponse status code: {0}", fullResponse.getStatusCode());
msg = fullResponse.getHeaders() == null ? "None" : fullResponse.getHeaders().stream().map(header -> header.getName() + "=" + header.getValue()).collect(Collectors.joining(", ", "{", "}"));
Logger.getLogger(SafeguardConnection.class.getName()).log(Level.FINEST, " Response headers: {0}", msg);
msg = fullResponse.getBody() == null ? "None" : String.format("%d",fullResponse.getBody().length());
Logger.getLogger(SafeguardConnection.class.getName()).log(Level.FINEST, " Body size: {0}", msg);
logResponseDetails(fullResponse);

return fullResponse;
}

@Override
public String invokeMethodCsv(Service service, Method method, String relativeUrl,
String body, Map<String, String> parameters, Map<String, String> additionalHeaders)
String body, Map<String, String> parameters, Map<String, String> additionalHeaders, Integer timeout)
throws ObjectDisposedException, SafeguardForJavaException, ArgumentException {

if (disposed) {
Expand All @@ -158,7 +152,7 @@ public String invokeMethodCsv(Service service, Method method, String relativeUrl
}
additionalHeaders.put(HttpHeaders.ACCEPT, "text/csv");

return invokeMethodFull(service, method, relativeUrl, body, parameters, additionalHeaders).getBody();
return invokeMethodFull(service, method, relativeUrl, body, parameters, additionalHeaders, timeout).getBody();
}

@Override
Expand Down Expand Up @@ -194,7 +188,7 @@ public void logOut() throws ObjectDisposedException {
if (!authenticationMechanism.hasAccessToken())
return;
try {
this.invokeMethodFull(Service.Core, Method.Post, "Token/Logout", null, null, null);
this.invokeMethodFull(Service.Core, Method.Post, "Token/Logout", null, null, null, null);
Logger.getLogger(SafeguardConnection.class.getName()).log(Level.FINEST, "Successfully logged out");
}
catch (Exception ex) {
Expand All @@ -204,7 +198,26 @@ public void logOut() throws ObjectDisposedException {
Logger.getLogger(SafeguardConnection.class.getName()).log(Level.FINEST, "Cleared access token");
}

private RestClient getClientForService(Service service) throws SafeguardForJavaException {
static void logRequestDetails(Method method, String uri, Map<String, String> parameters, Map<String, String> headers)
{
String msg = String.format("Invoking method: %s %s", method.toString().toUpperCase(), uri);
Logger.getLogger(SafeguardConnection.class.getName()).log(Level.FINEST, msg);
msg = parameters == null ? "None" : parameters.keySet().stream().map(key -> key + "=" + parameters.get(key)).collect(Collectors.joining(", ", "{", "}"));
Logger.getLogger(SafeguardConnection.class.getName()).log(Level.FINEST, " Query parameters: {0}", msg);
msg = headers == null ? "None" : headers.keySet().stream().map(key -> key + "=" + headers.get(key)).collect(Collectors.joining(", ", "{", "}"));
Logger.getLogger(SafeguardConnection.class.getName()).log(Level.FINEST, " Additional headers: {0}", msg);
}

static void logResponseDetails(FullResponse fullResponse)
{
Logger.getLogger(SafeguardConnection.class.getName()).log(Level.FINEST, "Reponse status code: {0}", fullResponse.getStatusCode());
String msg = fullResponse.getHeaders() == null ? "None" : fullResponse.getHeaders().stream().map(header -> header.getName() + "=" + header.getValue()).collect(Collectors.joining(", ", "{", "}"));
Logger.getLogger(SafeguardConnection.class.getName()).log(Level.FINEST, " Response headers: {0}", msg);
msg = fullResponse.getBody() == null ? "None" : String.format("%d",fullResponse.getBody().length());
Logger.getLogger(SafeguardConnection.class.getName()).log(Level.FINEST, " Body size: {0}", msg);
}

RestClient getClientForService(Service service) throws SafeguardForJavaException {
switch (service) {
case Core:
return coreClient;
Expand All @@ -220,7 +233,7 @@ private RestClient getClientForService(Service service) throws SafeguardForJavaE
}
}

private Map<String,String> prepareHeaders(Map<String,String> additionalHeaders, Service service)
Map<String,String> prepareHeaders(Map<String,String> additionalHeaders, Service service)
throws ObjectDisposedException {

Map<String,String> headers = new HashMap<>();
Expand All @@ -235,6 +248,14 @@ private Map<String,String> prepareHeaders(Map<String,String> additionalHeaders,
}
return headers;
}

boolean isDisposed() {
return disposed;
}

IAuthenticationMechanism getAuthenticationMechanism() {
return authenticationMechanism;
}

@Override
public void dispose()
Expand All @@ -260,4 +281,9 @@ public Object cloneObject() throws SafeguardForJavaException
return new SafeguardConnection((IAuthenticationMechanism)authenticationMechanism.cloneObject());
}

@Override
public IStreamingRequest getStreamingRequest() {
return this.streamingRequest;
}

}
Loading