-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #106 from bnicholesdell/implementspsstreaming
Implement the SPS streaming upload functions
- Loading branch information
Showing
8 changed files
with
365 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
src/main/java/com/oneidentity/safeguard/safeguardjava/ISpsStreamingRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
src/main/java/com/oneidentity/safeguard/safeguardjava/SpsStreamingRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
} |
Oops, something went wrong.