HCP System Management Help
Here’s the partial implementation of a Java class named HTTPCompression. The implementation shows the WriteToHCP method, which stores an object (or version) in an HCP namespace. The method compresses the data before sending it and uses the Content-Encoding header to tell HCP that the data is compressed.
The WriteToHCP method uses the GZIPCompressedInputStream helper class. For an implementation of this class, see GZIPCompressedInputStream class.
import org.apache.http.client.methods.HttpPut;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import com.hds.hcp.examples.GZIPCompressedInputStream;
class HTTPCompression {
.
.
.
void WriteToHCP() throws Exception {
/*
* Set up the PUT request.
*
* This method assumes that the HTTP client has already been
* initialized.
*/
HttpPut httpRequest = new HttpPut(sHCPFilePath);
// Indicate that the content encoding is gzip.
httpRequest.setHeader("Content-Encoding", "gzip");
// Open an input stream to the file that will be sent to HCP.
// This file will be processed by the GZIPCompressedInputStream to
// produce gzip-compressed content when read by the Apache HTTP client.
GZIPCompressedInputStream compressedInputFile
= new GZIPCompressedInputStream(new FileInputStream(
sBaseFileName + ".toHCP"));
// Point the HttpRequest to the input stream.
httpRequest.setEntity(new InputStreamEntity(compressedInputFile, -1));
// Create the HCP Authorization header.
httpRequest.setHeader("Authorization", "HCP " + sEncodedUserName
+ ":" + sEncodedPassword);
/*
* Now execute the PUT request.
*/
HttpResponse httpResponse = mHttpClient.execute(httpRequest);
/*
* Process the HTTP response.
*/
// If the return code is anything but in the 200 range indicating
// success, throw an exception.
if (2 != (int)(httpResponse.getStatusLine().getStatusCode() / 100))
{
// Clean up after ourselves and release the HTTP connection to the
// connection manager.
EntityUtils.consume(httpResponse.getEntity());
throw new Exception("Unexpected HTTP status code: " +
httpResponse.getStatusLine().getStatusCode() + " (" +
httpResponse.getStatusLine().getReasonPhrase() + ")");
}
// Clean up after ourselves and release the HTTP connection to the
// connection manager.
EntityUtils.consume(httpResponse.getEntity());
}
.
.
.
}
Trademarks and Legal Disclaimer
© 2017 Hitachi Vantara Corporation. All rights reserved.