119 lines
4.5 KiB
Java
119 lines
4.5 KiB
Java
/**
|
|
*
|
|
*/
|
|
package org.gcube.portlets.user.workspace;
|
|
|
|
import java.io.BufferedInputStream;
|
|
import java.io.File;
|
|
import java.io.FileInputStream;
|
|
import java.io.FileOutputStream;
|
|
import java.io.IOException;
|
|
import java.io.InputStream;
|
|
import java.io.OutputStream;
|
|
|
|
import org.gcube.common.core.scope.GCUBEScope;
|
|
import org.gcube.common.core.scope.GCUBEScope.MalformedScopeExpressionException;
|
|
import org.gcube.portlets.user.homelibrary.home.HomeLibrary;
|
|
import org.gcube.portlets.user.homelibrary.home.HomeManagerFactory;
|
|
import org.gcube.portlets.user.homelibrary.home.exceptions.HomeNotFoundException;
|
|
import org.gcube.portlets.user.homelibrary.home.exceptions.InternalErrorException;
|
|
import org.gcube.portlets.user.homelibrary.home.exceptions.UserNotFoundException;
|
|
import org.gcube.portlets.user.homelibrary.home.workspace.Workspace;
|
|
import org.gcube.portlets.user.homelibrary.home.workspace.WorkspaceFolder;
|
|
import org.gcube.portlets.user.homelibrary.home.workspace.exceptions.InsufficientPrivilegesException;
|
|
import org.gcube.portlets.user.homelibrary.home.workspace.exceptions.ItemAlreadyExistException;
|
|
import org.gcube.portlets.user.homelibrary.home.workspace.exceptions.WorkspaceFolderNotFoundException;
|
|
import org.gcube.portlets.user.homelibrary.home.workspace.exceptions.WrongDestinationException;
|
|
import org.gcube.portlets.user.homelibrary.home.workspace.folder.FolderItem;
|
|
import org.gcube.portlets.user.homelibrary.home.workspace.folder.items.ExternalFile;
|
|
import org.gcube.portlets.user.homelibrary.home.workspace.folder.items.ExternalImage;
|
|
import org.gcube.portlets.user.homelibrary.util.MimeTypeUtil;
|
|
import org.gcube.portlets.user.homelibrary.util.WorkspaceUtil;
|
|
|
|
/**
|
|
* @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it
|
|
* @Jun 20, 2013
|
|
*
|
|
*/
|
|
public class UploadServletTest {
|
|
|
|
public static void main(String[] args) throws InternalErrorException, MalformedScopeExpressionException, WorkspaceFolderNotFoundException, HomeNotFoundException, UserNotFoundException, InsufficientPrivilegesException, ItemAlreadyExistException, WrongDestinationException, IOException {
|
|
|
|
// HomeManagerFactory factory = HomeLibrary.getHomeManagerFactory();
|
|
// Workspace workspace = factory.getHomeManager().getHome("francesco.mangiacrapa",GCUBEScope.getScope("/gcube/devsec")).getWorkspace();
|
|
|
|
Workspace workspace = HomeLibrary
|
|
.getHomeManagerFactory()
|
|
.getHomeManager()
|
|
.getHome("francesco.mangiacrapa",
|
|
GCUBEScope.getScope("/gcube/devsec"))
|
|
.getWorkspace();
|
|
|
|
String filePath = "/home/francesco-mangiacrapa/Desktop/icongeoexplorer";
|
|
|
|
String fileName = "testupload.txt";
|
|
|
|
String fullPath = filePath+"/"+fileName;
|
|
|
|
// String newFilePath = "/home/francesco-mangiacrapa/Desktop/icongeoexplorer/testupload.txt";
|
|
|
|
// String name = "TestReplaceContentImage";
|
|
|
|
// ExternalPDFFile file = workspace.createExternalPDFFile(name, "", null, new FileInputStream(new File(filePath)),
|
|
// workspace.getRoot().getId());
|
|
|
|
WorkspaceFolder root = workspace.getRoot();
|
|
// ExternalImage file = (ExternalImage) root.find(name);
|
|
|
|
|
|
//UPLOAD FILE
|
|
String contentType = MimeTypeUtil.getMimeType(new BufferedInputStream(new FileInputStream(new File(fullPath))));
|
|
|
|
String itemName = WorkspaceUtil.getUniqueName(fileName, root);
|
|
System.out.println("Storing data....");
|
|
System.out.println("content type "+contentType);
|
|
System.out.println("itemName "+itemName);
|
|
|
|
ExternalFile file = (ExternalFile) WorkspaceUtil.createExternalFile(root, itemName, "", contentType, new FileInputStream(new File(fullPath)));
|
|
|
|
System.out.println("Storing data - OK" + "File [id: "+file.getId() + ", name: "+file.getName()+"]");
|
|
//
|
|
// System.out.println("File mimeType" + file.getMimeType());
|
|
// System.out.println("File size" + file.getLength());
|
|
//
|
|
|
|
//
|
|
// file.setData(new FileInputStream(new File(newFilePath)));
|
|
// System.out.println("New File mimeType" + file.getMimeType());
|
|
// System.out.println("New File size" + file.getLength());
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
System.out.println("Recovering data");
|
|
InputStream inputStream = file.getData();
|
|
// write the inputStream to a FileOutputStream
|
|
OutputStream out = new FileOutputStream(new File("/home/francesco-mangiacrapa/Desktop/result"));
|
|
|
|
int read = 0;
|
|
byte[] bytes = new byte[1024];
|
|
|
|
while ((read = inputStream.read(bytes)) != -1) {
|
|
out.write(bytes, 0, read);
|
|
}
|
|
|
|
inputStream.close();
|
|
out.flush();
|
|
out.close();
|
|
|
|
System.out.println("Recovering data - OK");
|
|
|
|
// File file = new File("/home/gioia/Desktop/test");
|
|
// InputStream is = new FileInputStream(file);
|
|
|
|
}
|
|
|
|
}
|