Skip to content

Commit

Permalink
Merge pull request #106 from bnicholesdell/implementspsstreaming
Browse files Browse the repository at this point in the history
Implement the SPS streaming upload functions
  • Loading branch information
bnicholesdell authored Feb 8, 2022
2 parents 65b4edf + e3ce148 commit 030a6ff
Show file tree
Hide file tree
Showing 8 changed files with 365 additions and 56 deletions.
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@
<artifactId>httpclient</artifactId>
<version>4.5.13</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>4.5.13</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,11 @@ String InvokeMethod(Method method, String relativeUrl, String body)
FullResponse InvokeMethodFull(Method method, String relativeUrl, String body)
throws ObjectDisposedException, SafeguardForJavaException, ArgumentException;

/**
* Provides support for HTTP streaming requests
*
* @return returns ISpsStreamingRequest
* @throws com.oneidentity.safeguard.safeguardjava.exceptions.ObjectDisposedException
*/
ISpsStreamingRequest getStreamingRequest() throws ObjectDisposedException;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.oneidentity.safeguard.safeguardjava;

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

/**
* SPS streaming request methods
*/
public interface ISpsStreamingRequest {

/**
* Call a Safeguard Sps POST API providing a stream as request content. If
* there is a failure a SafeguardDotNetException will be thrown.
*
* @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.
* @throws
* com.oneidentity.safeguard.safeguardjava.exceptions.SafeguardForJavaException
* @throws
* com.oneidentity.safeguard.safeguardjava.exceptions.ArgumentException
*/
String uploadStream(String relativeUrl, byte[] stream, IProgressCallback progressCallback,
Map<String, String> parameters, Map<String, String> additionalHeaders)
throws SafeguardForJavaException, ArgumentException;

/**
* Call a Safeguard Sps POST API providing a file as request content. If
* there is a failure a SafeguardDotNetException will be thrown.
*
* @param relativeUrl Relative URL of the service to use.
* @param fileName File to upload as request content.
* @param parameters Additional parameters to add to the URL.
* @param additionalHeaders Additional headers to add to the request.
* @return Response body as a string.
* @throws
* com.oneidentity.safeguard.safeguardjava.exceptions.SafeguardForJavaException
* @throws
* com.oneidentity.safeguard.safeguardjava.exceptions.ArgumentException
*/
String uploadStream(String relativeUrl, String fileName,
Map<String, String> parameters, Map<String, String> additionalHeaders)
throws SafeguardForJavaException, ArgumentException;

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,131 +15,141 @@
import java.util.logging.Logger;
import javax.net.ssl.HostnameVerifier;
import org.apache.http.Header;
import org.apache.http.HeaderElement;
import org.apache.http.client.methods.CloseableHttpResponse;

/**
* This is the reusable connection interface that can be used to call SPS API.
* This is the reusable connection interface that can be used to call SPS API.
*/
class SafeguardSessionsConnection implements ISafeguardSessionsConnection {

private boolean disposed;
private RestClient _client;
private Header _authCookie = null;

private RestClient client;
private final Header authCookie = null;

public SafeguardSessionsConnection(String networkAddress, String username,
char[] password, boolean ignoreSsl, HostnameVerifier validationCallback)
char[] password, boolean ignoreSsl, HostnameVerifier validationCallback)
throws SafeguardForJavaException {

String spsApiUrl = String.format("https://%s/api", networkAddress);
_client = new RestClient(spsApiUrl, username, password, ignoreSsl, validationCallback);
client = new RestClient(spsApiUrl, username, password, ignoreSsl, validationCallback);

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

Logger.getLogger(SafeguardSessionsConnection.class.getName()).log(Level.FINEST, "Starting authentication.");
logRequestDetails(Method.Get, _client.getBaseURL() + "/" + "authentication", null, null);
logRequestDetails(Method.Get, client.getBaseURL() + "/" + "authentication", null, null);

CloseableHttpResponse response = client.execGET("authentication", null, null, null);

CloseableHttpResponse response = _client.execGET("authentication", null, null, null);

if (response == null) {
throw new SafeguardForJavaException(String.format("Unable to authenticate to SPS %s", networkAddress));
}

String reply = Utils.getResponse(response);

if (!Utils.isSuccessful(response.getStatusLine().getStatusCode())) {
throw new SafeguardForJavaException("Error returned from Safeguard API, Error: "
+ String.format("%d %s", response.getStatusLine().getStatusCode(), reply));
}

Header authCookie = response.getFirstHeader("Set-Cookie");
if (authCookie != null) {
_client.addSessionId(authCookie.getValue());
client.addSessionId(authCookie.getValue());
}

Logger.getLogger(SafeguardSessionsConnection.class.getName()).log(Level.FINEST, String.format("Response content: $s", reply));
}

@Override
public String InvokeMethod(Method method, String relativeUrl, String body)
public String InvokeMethod(Method method, String relativeUrl, String body)
throws ObjectDisposedException, SafeguardForJavaException, ArgumentException {

return InvokeMethodFull(method, relativeUrl, body).getBody();
}

/**
* Call a SafeguardForPrivilegedSessions API method and get a detailed response with status code, headers,
* and body. If there is a failure a SafeguardDotNetException will be thrown.
* Call a SafeguardForPrivilegedSessions API method and get a detailed
* response with status code, headers, and body. If there is a failure a
* SafeguardDotNetException will be thrown.
*
* @param method HTTP method type to use.
* @param relativeUrl The url.
* @param body Request body to pass to the method.
*
* @param method HTTP method type to use.
* @param relativeUrl The url.
* @param body Request body to pass to the method.
*
* @return Response with status code, headers, and body as string.
*/
* @return Response with status code, headers, and body as string.
*/
@Override
//TODO: This API should have an additionalHeaders parameter
//TODO: This API should have an parameters parameter
//TODO: This API should have an timeout parameter
public FullResponse InvokeMethodFull(Method method, String relativeUrl, String body)
public FullResponse InvokeMethodFull(Method method, String relativeUrl, String body)
throws ObjectDisposedException, SafeguardForJavaException, ArgumentException {

if (disposed) {
throw new ObjectDisposedException("SafeguardSessionsConnection");
}
if (Utils.isNullOrEmpty(relativeUrl))
if (Utils.isNullOrEmpty(relativeUrl)) {
throw new ArgumentException("Parameter relativeUrl may not be null or empty");

}

Logger.getLogger(SafeguardSessionsConnection.class.getName()).log(Level.FINEST, String.format("Invoking method on sps: $s", relativeUrl));

CloseableHttpResponse response = null;

logRequestDetails(method, _client.getBaseURL() + "/" + relativeUrl, null, null);
logRequestDetails(method, client.getBaseURL() + "/" + relativeUrl, null, null);

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

if (response == null) {
throw new SafeguardForJavaException(String.format("Unable to connect to web service %s", _client.getBaseURL()));
throw new SafeguardForJavaException(String.format("Unable to connect to web service %s", client.getBaseURL()));
}

String reply = Utils.getResponse(response);

if (!Utils.isSuccessful(response.getStatusLine().getStatusCode())) {
throw new SafeguardForJavaException("Error returned from Safeguard API, Error: "
+ String.format("%d %s", response.getStatusLine().getStatusCode(), reply));
}

Logger.getLogger(SafeguardSessionsConnection.class.getName()).log(Level.FINEST, String.format("Invoking method finished: $s", reply));

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

logResponseDetails(fullResponse);

return fullResponse;
}


@Override
public ISpsStreamingRequest getStreamingRequest() throws ObjectDisposedException {
if (disposed) {
throw new ObjectDisposedException("SafeguardSessionsConnection");
}

return new SpsStreamingRequest(this.client);
}

boolean isDisposed() {
return disposed;
}

public void dispose()
{
if (_client != null)
_client = null;
public void dispose() {
if (client != null) {
client = null;
}
disposed = true;
}

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

import com.oneidentity.safeguard.safeguardjava.data.FullResponse;
import com.oneidentity.safeguard.safeguardjava.data.Method;
import com.oneidentity.safeguard.safeguardjava.exceptions.ArgumentException;
import com.oneidentity.safeguard.safeguardjava.exceptions.SafeguardForJavaException;
import com.oneidentity.safeguard.safeguardjava.restclient.RestClient;
import java.util.Map;
import org.apache.http.client.methods.CloseableHttpResponse;

class SpsStreamingRequest implements ISpsStreamingRequest {

private RestClient client;

SpsStreamingRequest(RestClient client) {
this.client = client;
}

@Override
public String uploadStream(String relativeUrl, byte[] stream, IProgressCallback progressCallback,
Map<String, String> parameters, Map<String, String> additionalHeaders)
throws SafeguardForJavaException, ArgumentException {

if (Utils.isNullOrEmpty(relativeUrl)) {
throw new ArgumentException("Parameter relativeUrl cannot be null or empty");
}
if (client == null) {
throw new ArgumentException("Invalid or unauthenticated SPS connection");
}

CloseableHttpResponse response = null;

SafeguardConnection.logRequestDetails(Method.Post, client.getBaseURL() + "/" + relativeUrl, parameters, additionalHeaders);

response = client.execPOSTBytes(relativeUrl, parameters, additionalHeaders, null, stream, progressCallback);

if (response == null) {
throw new SafeguardForJavaException(String.format("Unable to connect to SPS service %s", client.getBaseURL()));
}

String reply = Utils.getResponse(response);

if (!Utils.isSuccessful(response.getStatusLine().getStatusCode())) {
throw new SafeguardForJavaException("Error returned from SPS API, Error: "
+ String.format("%d %s", response.getStatusLine().getStatusCode(), reply));
}

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

SafeguardConnection.logResponseDetails(fullResponse);

return fullResponse.getBody();
}

@Override
public String uploadStream(String relativeUrl, String fileName,
Map<String, String> parameters, Map<String, String> additionalHeaders)
throws SafeguardForJavaException, ArgumentException {

if (Utils.isNullOrEmpty(relativeUrl)) {
throw new ArgumentException("Parameter relativeUrl cannot be null or empty");
}
if (client == null) {
throw new ArgumentException("Invalid or unauthenticated SPS connection");
}

CloseableHttpResponse response = null;

SafeguardConnection.logRequestDetails(Method.Post, client.getBaseURL() + "/" + relativeUrl, parameters, additionalHeaders);

response = client.execPOSTFile(relativeUrl, parameters, additionalHeaders, null, fileName);

if (response == null) {
throw new SafeguardForJavaException(String.format("Unable to connect to SPS service %s", client.getBaseURL()));
}

String reply = Utils.getResponse(response);

if (!Utils.isSuccessful(response.getStatusLine().getStatusCode())) {
throw new SafeguardForJavaException("Error returned from SPS API, Error: "
+ String.format("%d %s", response.getStatusLine().getStatusCode(), reply));
}

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

SafeguardConnection.logResponseDetails(fullResponse);

return fullResponse.getBody();
}

}
Loading

0 comments on commit 030a6ff

Please sign in to comment.