Here’s the partial implementation of a Java class named HTTPCompression. The implementation shows the ReadFromHCP method, which retrieves an object from an HCP namespace. It uses the Accept-Encoding header to tell HCP to compress the object before returning it and then decompresses the results.
import org.apache.http.client.methods.HttpGet;
import org.apache.http.HttpResponse;
import org.apache.http.EntityUtils;
import java.util.zip.GZIPInputStream;
class HTTPCompression {
.
.
.
void ReadFromHCP() throws Exception {
/*
* Set up the GET request.
*
* This method assumes that the HTTP client has already been
* initialized.
*/
HttpGet httpRequest = new HttpGet(sHCPFilePath);
// Indicate that you want HCP to compress the returned data with gzip.
httpRequest.setHeader("Accept-Encoding", "gzip");
// Create the HCP authorization header.
httpRequest.setHeader("Authorization", "HCP " + sEncodedUserName
+ ":" + sEncodedPassword);
/*
* Now execute the GET 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() + ")");
}
/*
* Write the decompressed file to disk.
*/
FileOutputStream outputFile = new FileOutputStream(
sBaseFileName + ".fromHCP");
// Build the string that contains the response body for return to the
// caller.
GZIPInputStream bodyISR = new
GZIPInputStream(httpResponse.getEntity().getContent());
byte partialRead[] = new byte[1024];
int readSize = 0;
while (-1 != (readSize = bodyISR.read(partialRead))) {
outputFile.write(partialRead, 0, readSize);
}
// Clean up after ourselves and release the HTTP connection to the
// connection manager.
EntityUtils.consume(httpResponse.getEntity());
}
.
.
.
}
© 2015 Hitachi Data Systems Corporation. All rights reserved.