diff --git a/services/secondary-storage/server/src/org/apache/cloudstack/storage/resource/NfsSecondaryStorageResource.java b/services/secondary-storage/server/src/org/apache/cloudstack/storage/resource/NfsSecondaryStorageResource.java index 894a3bb01b4..cf10a6e216c 100755 --- a/services/secondary-storage/server/src/org/apache/cloudstack/storage/resource/NfsSecondaryStorageResource.java +++ b/services/secondary-storage/server/src/org/apache/cloudstack/storage/resource/NfsSecondaryStorageResource.java @@ -48,6 +48,7 @@ import java.util.UUID; import javax.naming.ConfigurationException; +import org.apache.cloudstack.storage.template.UploadEntity; import org.apache.commons.codec.digest.DigestUtils; import org.apache.commons.io.FileUtils; import org.apache.commons.lang.StringUtils; @@ -216,6 +217,7 @@ public class NfsSecondaryStorageResource extends ServerResourceBase implements S protected String _parent = "/mnt/SecStorage"; final private String _tmpltpp = "template.properties"; protected String createTemplateFromSnapshotXenScript; + private HashMap uploadEntityStateMap = new HashMap(); public void setParentPath(String path) { _parent = path; @@ -1702,7 +1704,20 @@ public class NfsSecondaryStorageResource extends ServerResourceBase implements S } private UploadStatusAnswer execute(UploadStatusCommand cmd) { - return new UploadStatusAnswer(cmd, UploadStatus.COMPLETED); + long entityId = cmd.getEntityId(); + if (uploadEntityStateMap.containsKey(entityId)) { + UploadEntity uploadEntity = uploadEntityStateMap.get(entityId); + if (uploadEntity.getUploadState()== UploadEntity.Status.ERROR) { + uploadEntityStateMap.remove(uploadEntity); + return new UploadStatusAnswer(cmd, UploadStatus.ERROR, uploadEntity.getErrorMessage()); + }else if (uploadEntity.getUploadState()== UploadEntity.Status.COMPLETED) { + uploadEntityStateMap.remove(uploadEntity); + return new UploadStatusAnswer(cmd, UploadStatus.COMPLETED); + }else if (uploadEntity.getUploadState()==UploadEntity.Status.IN_PROGRESS) { + return new UploadStatusAnswer(cmd,UploadStatus.IN_PROGRESS); + } + } + return new UploadStatusAnswer(cmd, UploadStatus.UNKNOWN); } protected GetStorageStatsAnswer execute(final GetStorageStatsCommand cmd) { @@ -2596,6 +2611,48 @@ public class NfsSecondaryStorageResource extends ServerResourceBase implements S //TODO: move this class to a separate file private class PostUploadRequestHandler implements HttpAsyncRequestHandler { + //private static final Logger s_logger = Logger.getLogger(PostUploadRequestHandler.class); + + public void handleuplod(long entityId, String absolutePath, String entityName, long entitySize, byte[] data) { + UploadEntity uploadEntity=null; + try { + + if (uploadEntityStateMap.containsKey(entityId)) { + //the file upload entity has been created. + uploadEntity = uploadEntityStateMap.get(entityId); + uploadEntity.getFilewriter().write(data); + uploadEntity.incremetByteCount(data.length); + if (uploadEntity.getDownloadedsize() == uploadEntity.getEntitysize()) { + uploadEntity.setStatus(UploadEntity.Status.COMPLETED); + uploadEntity.getFile().renameTo(new File(uploadEntity.getAbsoluteFilePath())); + uploadEntity.getFilewriter().close(); + } else { + uploadEntity.setStatus(UploadEntity.Status.IN_PROGRESS); + } + }else{ + //this is a new upload. + uploadEntity = new UploadEntity(entitySize, UploadEntity.Status.IN_PROGRESS, entityName, absolutePath); + uploadEntityStateMap.put(entityId, uploadEntity); + File tempFile = File.createTempFile("dnld_" + entityId,absolutePath); + FileOutputStream filewriter = new FileOutputStream(tempFile.getAbsolutePath()); + uploadEntity.setFilewriter(filewriter); + filewriter.write(data); + uploadEntity.setFile(tempFile); + uploadEntity.incremetByteCount(data.length); + } + } catch (Exception e) { + uploadEntity.setErrorMessage(e.getMessage()); + uploadEntity.setStatus(UploadEntity.Status.ERROR); + try { + uploadEntity.getFilewriter().close(); + } + catch (Exception io) { + s_logger.debug("exception occured when closing a file object " + io.getMessage()); + } + s_logger.debug("exception occured while writing to a file " + e); + } + } + public void handleInternal(HttpRequest httpRequest, HttpResponse httpResponse, HttpContext httpContext) throws HttpException, IOException { s_logger.info(""); // empty line before each request @@ -2624,6 +2681,8 @@ public class NfsSecondaryStorageResource extends ServerResourceBase implements S } else { data = EntityUtils.toByteArray(entity); } + //call handle upload method. + //handleuplod(); s_logger.info(new String(data)); //TODO: post request is available in data. Need to parse and save file. diff --git a/services/secondary-storage/server/src/org/apache/cloudstack/storage/template/uploadEntity.java b/services/secondary-storage/server/src/org/apache/cloudstack/storage/template/uploadEntity.java new file mode 100644 index 00000000000..6001bcb6063 --- /dev/null +++ b/services/secondary-storage/server/src/org/apache/cloudstack/storage/template/uploadEntity.java @@ -0,0 +1,113 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +package org.apache.cloudstack.storage.template; + + +import java.io.File; +import java.io.FileOutputStream; + +public class UploadEntity { + private long filesize; + private long downloadedsize; + private String filename; + private String absoluteFilePath; + + + + + public static enum Status { + UNKNOWN, IN_PROGRESS, COMPLETED, ERROR + } + + private Status uploadState; + private FileOutputStream filewriter = null; + private String errorMessage=null; + private File file; + + public UploadEntity(long filesize, Status status, String filename, String absoluteFilePath){ + this.filesize=filesize; + this.uploadState=status; + this.downloadedsize=0l; + this.filename=filename; + this.absoluteFilePath=absoluteFilePath; + } + + public void setEntitysize(long filesize){ + this.filesize=filesize; + } + + public void setStatus(Status status) { + this.uploadState = status; + } + + public void setErrorMessage(String errorMessage) { + this.errorMessage=errorMessage; + } + + public FileOutputStream getFilewriter() { + return filewriter; + } + + public long getEntitysize() { + return filesize; + } + + public long getDownloadedsize() { + return downloadedsize; + } + + public String getErrorMessage() { + return errorMessage; + } + + public Status getUploadState() { + return uploadState; + } + + public void setFilewriter(FileOutputStream filewriter) { + this.filewriter = filewriter; + } + + public void incremetByteCount(long numberOfBytes) { + this.downloadedsize+= numberOfBytes; + } + public File getFile() { + return file; + } + + public void setFile(File file) { + this.file = file; + } + + public String getFilename() { + return filename; + } + + public void setFilename(String filename) { + this.filename = filename; + } + public String getAbsoluteFilePath() { + return absoluteFilePath; + } + + public void setAbsoluteFilePath(String absoluteFilePath) { + this.absoluteFilePath = absoluteFilePath; + } + + +}