diff options
| author | Selvasundaram <selvam@gluster.com> | 2011-07-26 16:51:05 +0530 |
|---|---|---|
| committer | Selvasundaram <selvam@gluster.com> | 2011-07-26 16:51:05 +0530 |
| commit | d4776431b2681fc8692fd74644e255883b17dc0e (patch) | |
| tree | d4942bcb8c126672e1b29e5a895356b713c814ab /src/com.gluster.storage.management.server | |
| parent | 8c340ec5bc88f7a310304e5eb88d71a6ca2554b6 (diff) | |
Export key errors - bug fix
Diffstat (limited to 'src/com.gluster.storage.management.server')
3 files changed, 42 insertions, 98 deletions
diff --git a/src/com.gluster.storage.management.server/src/com/gluster/storage/management/server/resources/v1_0/AbstractResource.java b/src/com.gluster.storage.management.server/src/com/gluster/storage/management/server/resources/v1_0/AbstractResource.java index c28c0da0..a9eb4874 100644 --- a/src/com.gluster.storage.management.server/src/com/gluster/storage/management/server/resources/v1_0/AbstractResource.java +++ b/src/com.gluster.storage.management.server/src/com/gluster/storage/management/server/resources/v1_0/AbstractResource.java @@ -19,21 +19,14 @@ package com.gluster.storage.management.server.resources.v1_0; import java.net.URI; -import java.util.ArrayList; import javax.ws.rs.core.Context; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; -import javax.ws.rs.core.Response.ResponseBuilder; import javax.ws.rs.core.Response.Status; import javax.ws.rs.core.StreamingOutput; import javax.ws.rs.core.UriInfo; -import com.gluster.storage.management.core.constants.RESTConstants; -import com.gluster.storage.management.core.exceptions.GlusterRuntimeException; -import com.gluster.storage.management.core.model.Volume; -import com.gluster.storage.management.core.response.VolumeListResponse; - /** * */ diff --git a/src/com.gluster.storage.management.server/src/com/gluster/storage/management/server/resources/v1_0/GenericExceptionMapper.java b/src/com.gluster.storage.management.server/src/com/gluster/storage/management/server/resources/v1_0/GenericExceptionMapper.java deleted file mode 100644 index 4cd7f4f9..00000000 --- a/src/com.gluster.storage.management.server/src/com/gluster/storage/management/server/resources/v1_0/GenericExceptionMapper.java +++ /dev/null @@ -1,45 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2011 Gluster, Inc. <http://www.gluster.com> - * This file is part of Gluster Management Console. - * - * Gluster Management Console is free software; you can redistribute it and/or - * modify it under the terms of the GNU Affero General Public License as published - * by the Free Software Foundation; either version 3 of the License, or - * (at your option) any later version. - * - * Gluster Management Console is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY - * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License - * for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see - * <http://www.gnu.org/licenses/>. - *******************************************************************************/ -package com.gluster.storage.management.server.resources.v1_0; - -import javax.ws.rs.core.MediaType; -import javax.ws.rs.core.Response; -import javax.ws.rs.core.Response.ResponseBuilder; -import javax.ws.rs.ext.ExceptionMapper; -import javax.ws.rs.ext.Provider; - -import com.gluster.storage.management.core.exceptions.GlusterValidationException; - -@Provider -public class GenericExceptionMapper implements ExceptionMapper<Exception> { - - /* (non-Javadoc) - * @see javax.ws.rs.ext.ExceptionMapper#toResponse(java.lang.Throwable) - */ - @Override - public Response toResponse(Exception exception) { - ResponseBuilder builder; - if (exception instanceof GlusterValidationException) { - builder = Response.status(Response.Status.BAD_REQUEST); - } else { - builder = Response.status(Response.Status.INTERNAL_SERVER_ERROR); - } - return builder.entity(exception.getMessage()).type(MediaType.TEXT_PLAIN).build(); - } -} diff --git a/src/com.gluster.storage.management.server/src/com/gluster/storage/management/server/resources/v1_0/KeysResource.java b/src/com.gluster.storage.management.server/src/com/gluster/storage/management/server/resources/v1_0/KeysResource.java index af64af47..90656cc6 100644 --- a/src/com.gluster.storage.management.server/src/com/gluster/storage/management/server/resources/v1_0/KeysResource.java +++ b/src/com.gluster.storage.management.server/src/com/gluster/storage/management/server/resources/v1_0/KeysResource.java @@ -35,11 +35,12 @@ import javax.ws.rs.GET; import javax.ws.rs.POST; import javax.ws.rs.Path; import javax.ws.rs.Produces; -import javax.ws.rs.WebApplicationException; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; import javax.ws.rs.core.StreamingOutput; +import org.apache.log4j.Logger; + import com.gluster.storage.management.core.exceptions.GlusterRuntimeException; import com.gluster.storage.management.core.utils.FileUtil; import com.gluster.storage.management.core.utils.ProcessResult; @@ -49,31 +50,27 @@ import com.sun.jersey.multipart.FormDataParam; @Path(RESOURCE_PATH_KEYS) public class KeysResource extends AbstractResource { - ProcessUtil processUtil = new ProcessUtil(); + private static final Logger logger = Logger.getLogger(KeysResource.class); + private ProcessUtil processUtil = new ProcessUtil(); @GET @Produces(MediaType.APPLICATION_OCTET_STREAM) public Response exportSshkeys() { - try { - StreamingOutput output = new StreamingOutput() { - - @Override - public void write(OutputStream output) throws IOException, WebApplicationException { - try { - File archiveFile = new File(createSskKeyZipFile()); - output.write(FileUtil.readFileAsByteArray(archiveFile)); - archiveFile.delete(); - } catch (Exception e) { - output.write(("Exception while archiving SSH Key files : " + e.getMessage()).getBytes()); - } - } - }; - return streamingOutputResponse(output); - } catch (Exception e) { - return errorResponse("Exporting SSH keys failed! [" + e.getMessage() + "]"); - } + File archiveFile = new File(createSskKeyZipFile()); + byte[] data = FileUtil.readFileAsByteArray(archiveFile); + archiveFile.delete(); + return streamingOutputResponse(createStreamingOutput(data)); } + private StreamingOutput createStreamingOutput(final byte[] data) { + return new StreamingOutput() { + @Override + public void write(OutputStream output) throws IOException { + output.write(data); + } + }; + } + private String createSskKeyZipFile() { String targetDir = System.getProperty("java.io.tmpdir"); String zipFile = targetDir + "ssh-keys.tar"; @@ -108,8 +105,11 @@ public class KeysResource extends AbstractResource { } // To remove the copied key files - processUtil.executeCommand("rm", "-f", targetPubKeyFile, targetPubKeyFile); // Ignore the errors if any - + try { + processUtil.executeCommand("rm", "-f", targetPemFile, targetPubKeyFile); // Ignore the errors if any + } catch (Exception e) { + logger.warn(e.toString()); + } return zipFile; } @@ -118,36 +118,32 @@ public class KeysResource extends AbstractResource { public Response importSshKeys(@FormDataParam("file") InputStream uploadedInputStream) { File uploadedFile = new File(System.getProperty("java.io.tmpdir") + File.separator + "keys.tar"); String timestamp = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()); - try { - writeToFile(uploadedInputStream, uploadedFile.getAbsolutePath()); - // To backup existing SSH pem and public keys - if (SshUtil.PEM_FILE.isFile()) { - if (!SshUtil.PEM_FILE.renameTo(new File(SshUtil.PEM_FILE.getAbsolutePath() + "-" + timestamp))) { - throw new GlusterRuntimeException("Unable to backup pem key!"); - } - } + writeToFile(uploadedInputStream, uploadedFile.getAbsolutePath()); - if (SshUtil.PUBLIC_KEY_FILE.isFile()) { - if (!SshUtil.PUBLIC_KEY_FILE.renameTo(new File(SshUtil.PUBLIC_KEY_FILE.getAbsolutePath() + "-" - + timestamp))) { - throw new GlusterRuntimeException("Unable to backup public key!"); - } + // To backup existing SSH pem and public keys, if exist. + if (SshUtil.PEM_FILE.isFile()) { + if (!SshUtil.PEM_FILE.renameTo(new File(SshUtil.PEM_FILE.getAbsolutePath() + "-" + timestamp))) { + throw new GlusterRuntimeException("Unable to backup pem key!"); } - // Extract SSH pem and public key files. - ProcessResult output = processUtil.executeCommand("tar", "xvf", uploadedFile.getName(), "-C", - SshUtil.SSH_AUTHORIZED_KEYS_DIR); - uploadedFile.delete(); - if (output.isSuccess()) { - return createdResponse("SSH Key imported successfully"); - } else { - return errorResponse(output.getOutput()); + } + + if (SshUtil.PUBLIC_KEY_FILE.isFile()) { + if (!SshUtil.PUBLIC_KEY_FILE + .renameTo(new File(SshUtil.PUBLIC_KEY_FILE.getAbsolutePath() + "-" + timestamp))) { + throw new GlusterRuntimeException("Unable to backup public key!"); } - } catch (Exception e) { - return errorResponse(e.getMessage()); } + // Extract SSH pem and public key files. + ProcessResult output = processUtil.executeCommand("tar", "xvf", uploadedFile.getName(), "-C", + SshUtil.SSH_AUTHORIZED_KEYS_DIR); + uploadedFile.delete(); + if (!output.isSuccess()) { + throw new GlusterRuntimeException(output.getOutput()); + } + return createdResponse("SSH Key imported successfully"); } - + // save uploaded file to the file (with path) private void writeToFile(InputStream inputStream, String toFile) { try { |
