From c0db2695f0979b400ebea561bccd72a8cd9aee75 Mon Sep 17 00:00:00 2001 From: Shireesh Anjal Date: Mon, 9 May 2011 15:20:04 +0530 Subject: Story #42 - Volume logs download --- .../storage/management/client/AbstractClient.java | 33 +++++++++++++++------- .../storage/management/client/VolumesClient.java | 6 ++-- 2 files changed, 26 insertions(+), 13 deletions(-) (limited to 'src/com.gluster.storage.management.client') diff --git a/src/com.gluster.storage.management.client/src/com/gluster/storage/management/client/AbstractClient.java b/src/com.gluster.storage.management.client/src/com/gluster/storage/management/client/AbstractClient.java index b9a0ef56..a077c721 100644 --- a/src/com.gluster.storage.management.client/src/com/gluster/storage/management/client/AbstractClient.java +++ b/src/com.gluster.storage.management.client/src/com/gluster/storage/management/client/AbstractClient.java @@ -1,12 +1,15 @@ package com.gluster.storage.management.client; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; import java.net.URI; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.MultivaluedMap; import com.gluster.storage.management.client.utils.ClientUtil; -import com.gluster.storage.management.core.constants.RESTConstants; +import com.gluster.storage.management.core.exceptions.GlusterRuntimeException; import com.sun.jersey.api.client.Client; import com.sun.jersey.api.client.ClientResponse; import com.sun.jersey.api.client.WebResource; @@ -49,15 +52,25 @@ public abstract class AbstractClient { .get(responseClass); } - private Object downloadResource(WebResource res, MultivaluedMap queryParams, Class responseClass) { - return res.queryParams(queryParams).header(HTTP_HEADER_AUTH, authHeader).accept(MediaType.TEXT_XML) - .get(responseClass); - } - - protected Object downloadResource(WebResource res) { + protected void downloadResource(WebResource res, String filePath) { ClientResponse response = res.header(HTTP_HEADER_AUTH, authHeader).accept(MediaType.APPLICATION_OCTET_STREAM) .get(ClientResponse.class); - return response; + try { + if(!response.hasEntity()) { + throw new GlusterRuntimeException("No entity in response!"); + } + + InputStream inputStream = response.getEntityInputStream(); + byte[] data = new byte[inputStream.available()]; + inputStream.read(data); + inputStream.close(); + + FileOutputStream os = new FileOutputStream(filePath); + os.write(data); + os.close(); + } catch (IOException e) { + throw new GlusterRuntimeException("Error while downloading resource [" + res.getURI().getPath() + "]", e); + } } /** @@ -104,8 +117,8 @@ public abstract class AbstractClient { return fetchResource(resource.path(subResourceName), NO_PARAMS, responseClass); } - protected Object downloadSubResource(String subResourceName) { - return downloadResource(resource.path(subResourceName)); + protected void downloadSubResource(String subResourceName, String filePath) { + downloadResource(resource.path(subResourceName), filePath); } /** diff --git a/src/com.gluster.storage.management.client/src/com/gluster/storage/management/client/VolumesClient.java b/src/com.gluster.storage.management.client/src/com/gluster/storage/management/client/VolumesClient.java index f1464211..c0ce8620 100644 --- a/src/com.gluster.storage.management.client/src/com/gluster/storage/management/client/VolumesClient.java +++ b/src/com.gluster.storage.management.client/src/com/gluster/storage/management/client/VolumesClient.java @@ -141,8 +141,8 @@ public class VolumesClient extends AbstractClient { queryParams, LogMessageListResponse.class); } - public void downloadLogs(String volumeName) { - downloadSubResource((volumeName) + "/" + RESTConstants.SUBRESOURCE_LOGS + "/" + RESTConstants.SUBRESOURCE_DOWNLOAD); + public void downloadLogs(String volumeName, String filePath) { + downloadSubResource((volumeName) + "/" + RESTConstants.SUBRESOURCE_LOGS + "/" + RESTConstants.SUBRESOURCE_DOWNLOAD, filePath); } public Status removeBricks(String volumeName, List diskList, boolean deleteOption) { @@ -258,7 +258,7 @@ public class VolumesClient extends AbstractClient { // // Status status = client.addDisks("Volume3", disks); // System.out.println(status.getMessage()); - client.downloadLogs("vol1"); + client.downloadLogs("vol1", "/tmp/temp1.tar.gz"); } } } -- cgit