You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cloudstack.apache.org by ra...@apache.org on 2015/01/16 23:37:12 UTC
[1/2] git commit: updated refs/heads/volume-upload to ef0c5d3
Repository: cloudstack
Updated Branches:
refs/heads/volume-upload d9a3268db -> ef0c5d35c
volume upload: added post request parser to get the file content
Project: http://git-wip-us.apache.org/repos/asf/cloudstack/repo
Commit: http://git-wip-us.apache.org/repos/asf/cloudstack/commit/ef0c5d35
Tree: http://git-wip-us.apache.org/repos/asf/cloudstack/tree/ef0c5d35
Diff: http://git-wip-us.apache.org/repos/asf/cloudstack/diff/ef0c5d35
Branch: refs/heads/volume-upload
Commit: ef0c5d35cf39b7c2a8c2ea6d3c639b6ffb3a6319
Parents: 5e1bd63
Author: Rajani Karuturi <ra...@gmail.com>
Authored: Sat Jan 17 04:04:55 2015 +0530
Committer: Rajani Karuturi <ra...@gmail.com>
Committed: Sat Jan 17 04:06:29 2015 +0530
----------------------------------------------------------------------
.../resource/NfsSecondaryStorageResource.java | 42 ++++++++++++++++++--
1 file changed, 39 insertions(+), 3 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/cloudstack/blob/ef0c5d35/services/secondary-storage/server/src/org/apache/cloudstack/storage/resource/NfsSecondaryStorageResource.java
----------------------------------------------------------------------
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 cf10a6e..2cebcd8 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
@@ -25,6 +25,8 @@ import static org.apache.commons.lang.StringUtils.substringAfterLast;
import java.io.BufferedReader;
import java.io.BufferedWriter;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
@@ -32,7 +34,9 @@ import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
+import java.io.InputStreamReader;
import java.io.InterruptedIOException;
+import java.io.PrintWriter;
import java.math.BigInteger;
import java.net.InetAddress;
import java.net.InetSocketAddress;
@@ -2681,11 +2685,43 @@ public class NfsSecondaryStorageResource extends ServerResourceBase implements S
} else {
data = EntityUtils.toByteArray(entity);
}
+
+ InputStream is = new ByteArrayInputStream(data);
+ BufferedReader reader = new BufferedReader(new InputStreamReader(is));
+ ByteArrayOutputStream output = new ByteArrayOutputStream();
+
+ String currentLine = reader.readLine();
+ String boundary = currentLine.replace("-","");
+
+ while(reader.ready()) {
+ currentLine = reader.readLine();
+ if (currentLine.contains("Content-Type: ")) {
+ // File Content here
+ reader.readLine();
+
+ String prevLine = reader.readLine();
+ currentLine = reader.readLine();
+
+ //writing the data to a output stream
+ while (true) {
+ if (currentLine.contains(boundary)) {
+ output.write(prevLine.getBytes());
+ break;
+ }
+ else {
+ output.write(currentLine.getBytes());
+ }
+ prevLine = currentLine;
+ currentLine = reader.readLine();
+ }
+
+ }
+ }
//call handle upload method.
- //handleuplod();
+ //TODO: get entityid, absolute path from metadata and filename from post data
+ handleuplod(1, null, "file", output.size(), output.toByteArray());
- s_logger.info(new String(data));
- //TODO: post request is available in data. Need to parse and save file.
+ s_logger.error(new String(data));
httpResponse.setEntity(new StringEntity("upload successful"));
}
[2/2] git commit: updated refs/heads/volume-upload to ef0c5d3
Posted by ra...@apache.org.
Added changes to maintain and send the upload progress.
Signed-off-by: Rajani Karuturi <ra...@gmail.com>
Project: http://git-wip-us.apache.org/repos/asf/cloudstack/repo
Commit: http://git-wip-us.apache.org/repos/asf/cloudstack/commit/5e1bd634
Tree: http://git-wip-us.apache.org/repos/asf/cloudstack/tree/5e1bd634
Diff: http://git-wip-us.apache.org/repos/asf/cloudstack/diff/5e1bd634
Branch: refs/heads/volume-upload
Commit: 5e1bd634a496ce621371f9e3b1e9129d209b329d
Parents: d9a3268
Author: Bharat Kumar <bh...@citrix.com>
Authored: Fri Jan 16 16:18:42 2015 +0530
Committer: Rajani Karuturi <ra...@gmail.com>
Committed: Sat Jan 17 04:06:29 2015 +0530
----------------------------------------------------------------------
.../resource/NfsSecondaryStorageResource.java | 61 +++++++++-
.../storage/template/uploadEntity.java | 113 +++++++++++++++++++
2 files changed, 173 insertions(+), 1 deletion(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/cloudstack/blob/5e1bd634/services/secondary-storage/server/src/org/apache/cloudstack/storage/resource/NfsSecondaryStorageResource.java
----------------------------------------------------------------------
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 894a3bb..cf10a6e 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<Long,UploadEntity> uploadEntityStateMap = new HashMap<Long,UploadEntity>();
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<HttpRequest> {
+ //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.
http://git-wip-us.apache.org/repos/asf/cloudstack/blob/5e1bd634/services/secondary-storage/server/src/org/apache/cloudstack/storage/template/uploadEntity.java
----------------------------------------------------------------------
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 0000000..6001bcb
--- /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;
+ }
+
+
+}