2015-10-02 11:46:32 +02:00
|
|
|
package org.gcube.portlets.widgets.workspaceuploader.server;
|
|
|
|
|
2016-12-01 14:15:12 +01:00
|
|
|
import javax.servlet.http.HttpServletRequest;
|
2016-05-19 15:38:24 +02:00
|
|
|
import javax.servlet.http.HttpSession;
|
|
|
|
|
2015-10-02 11:46:32 +02:00
|
|
|
import org.gcube.common.homelibary.model.items.type.WorkspaceItemType;
|
|
|
|
import org.gcube.common.homelibrary.home.workspace.Workspace;
|
|
|
|
import org.gcube.common.homelibrary.home.workspace.WorkspaceItem;
|
2016-11-24 12:28:48 +01:00
|
|
|
import org.gcube.common.portal.PortalContext;
|
2015-10-02 11:46:32 +02:00
|
|
|
import org.gcube.portlets.widgets.workspaceuploader.client.WorkspaceUploaderService;
|
|
|
|
import org.gcube.portlets.widgets.workspaceuploader.server.util.WsUtil;
|
|
|
|
import org.gcube.portlets.widgets.workspaceuploader.shared.UploadProgress;
|
|
|
|
import org.gcube.portlets.widgets.workspaceuploader.shared.WorkspaceUploaderItem;
|
|
|
|
import org.gcube.portlets.widgets.workspaceuploader.shared.WorkspaceUploaderItem.UPLOAD_STATUS;
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
|
|
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The server side implementation of the RPC service.
|
2016-05-12 15:12:45 +02:00
|
|
|
*
|
|
|
|
* @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it
|
|
|
|
* May 12, 2016
|
2015-10-02 11:46:32 +02:00
|
|
|
*/
|
|
|
|
@SuppressWarnings("serial")
|
|
|
|
public class WorkspaceUploaderServiceImpl extends RemoteServiceServlet implements WorkspaceUploaderService {
|
|
|
|
|
|
|
|
public static Logger logger = LoggerFactory.getLogger(WorkspaceUploaderServiceImpl.class);
|
|
|
|
|
2016-05-12 15:12:45 +02:00
|
|
|
/* (non-Javadoc)
|
|
|
|
* @see org.gcube.portlets.widgets.workspaceuploader.client.WorkspaceUploaderService#getUploadStatus(java.lang.String)
|
|
|
|
*/
|
2015-10-02 11:46:32 +02:00
|
|
|
@Override
|
|
|
|
public WorkspaceUploaderItem getUploadStatus(String identifier) throws Exception {
|
|
|
|
|
|
|
|
if (identifier == null || identifier.isEmpty()) {
|
|
|
|
String exception = "Invalid upload identifier, it is null or empty";
|
|
|
|
logger.error(exception);
|
|
|
|
throw new Exception(exception);
|
|
|
|
}
|
|
|
|
logger.trace("Get UploadStatus for id: "+identifier);
|
2016-11-24 12:28:48 +01:00
|
|
|
WorkspaceUploaderItem uploader = WsUtil.getWorkspaceUploaderInSession(getThreadLocalRequest(), identifier);
|
2016-05-19 15:38:24 +02:00
|
|
|
logger.trace("Uploader status for id: "+identifier +" returns "+uploader);
|
2015-10-02 11:46:32 +02:00
|
|
|
|
|
|
|
if (uploader == null) {
|
|
|
|
WorkspaceUploaderItem waitingUpload = new WorkspaceUploaderItem(identifier, null, UPLOAD_STATUS.WAIT, "Upload waiting..", new UploadProgress());
|
|
|
|
waitingUpload.setClientUploadKey(identifier);
|
|
|
|
return waitingUpload;
|
|
|
|
/*
|
|
|
|
String exception = "Item upload not found";
|
|
|
|
logger.error(exception);
|
|
|
|
throw new Exception(exception);*/
|
|
|
|
}
|
2016-05-12 15:12:45 +02:00
|
|
|
|
2015-10-02 11:46:32 +02:00
|
|
|
// getThreadIds();
|
2015-10-13 17:24:11 +02:00
|
|
|
String progress = "";
|
|
|
|
if(uploader.getUploadProgress()!=null && uploader.getUploadProgress().getLastEvent()!=null)
|
|
|
|
progress = uploader.getUploadProgress().getLastEvent().toString();
|
|
|
|
else
|
|
|
|
progress = "upload progress is null or last event is null";
|
2016-05-12 15:12:45 +02:00
|
|
|
|
2015-11-05 18:00:44 +01:00
|
|
|
logger.info("returning uploader: "+uploader.getClientUploadKey() +" status: "+uploader.getUploadStatus() +", file: "+uploader.getFile().toString() +", progress: "+progress);
|
2016-05-19 15:38:24 +02:00
|
|
|
|
2016-12-01 14:15:12 +01:00
|
|
|
checkUploaderErasable(this.getThreadLocalRequest(), uploader);
|
2015-10-02 11:46:32 +02:00
|
|
|
return uploader;
|
|
|
|
|
|
|
|
}
|
2016-05-12 15:12:45 +02:00
|
|
|
|
2016-05-19 15:38:24 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Check uploader erasable.
|
|
|
|
*
|
|
|
|
* @param httpSession the http session
|
|
|
|
* @param uploader the uploader
|
|
|
|
*/
|
2016-12-01 14:15:12 +01:00
|
|
|
private void checkUploaderErasable(final HttpServletRequest httpRequest, final WorkspaceUploaderItem uploader){
|
2016-05-19 15:38:24 +02:00
|
|
|
logger.trace("Checking Uploader erasable...");
|
|
|
|
|
|
|
|
if(uploader==null){
|
|
|
|
logger.error("Uploader is null, returning..");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-12-01 14:15:12 +01:00
|
|
|
final HttpSession session = httpRequest.getSession();
|
|
|
|
|
2016-05-19 15:38:24 +02:00
|
|
|
new Thread(){
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
try {
|
|
|
|
|
2016-12-01 14:15:12 +01:00
|
|
|
logger.trace("Uploader: "+uploader.getClientUploadKey() +", is erasable? "+uploader.isErasable());
|
|
|
|
WsUtil.eraseWorkspaceUploaderInSession(session, uploader);
|
2016-05-19 15:38:24 +02:00
|
|
|
}
|
|
|
|
catch (Exception e) {
|
|
|
|
logger.warn("Error during checkUploaderErasable: ", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}.start();
|
|
|
|
}
|
|
|
|
|
2016-05-12 15:12:45 +02:00
|
|
|
/* (non-Javadoc)
|
|
|
|
* @see org.gcube.portlets.widgets.workspaceuploader.client.WorkspaceUploaderService#getWorkspaceId()
|
|
|
|
*/
|
2015-10-02 11:46:32 +02:00
|
|
|
@Override
|
|
|
|
public String getWorkspaceId() throws Exception {
|
|
|
|
logger.trace("getWorkspaceId");
|
|
|
|
Workspace workspace;
|
|
|
|
try {
|
2016-11-24 12:28:48 +01:00
|
|
|
String scopeGroupId = ""+PortalContext.getConfiguration().getCurrentGroupId(getThreadLocalRequest());
|
2016-12-21 14:20:48 +01:00
|
|
|
String currUserId = ""+PortalContext.getConfiguration().getCurrentUser(getThreadLocalRequest());
|
|
|
|
workspace = WsUtil.getWorkspace(getThreadLocalRequest(), scopeGroupId, currUserId);
|
2015-10-02 11:46:32 +02:00
|
|
|
if(workspace!=null)
|
|
|
|
return workspace.getRoot().getId();
|
|
|
|
} catch (Exception e) {
|
|
|
|
logger.error("Get workspace id error", e);
|
|
|
|
String error = "An error occurred getting root id";
|
|
|
|
throw new Exception(error);
|
|
|
|
}
|
2016-05-12 15:12:45 +02:00
|
|
|
|
2015-10-02 11:46:32 +02:00
|
|
|
return null;
|
|
|
|
}
|
2016-05-12 15:12:45 +02:00
|
|
|
|
|
|
|
|
2015-10-02 11:46:32 +02:00
|
|
|
/* (non-Javadoc)
|
|
|
|
* @see org.gcube.portlets.user.workspace.client.rpc.GWTWorkspaceService#itemExistsInWorkpaceFolder(java.lang.String, java.lang.String)
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public String itemExistsInWorkpaceFolder(String parentId, String itemName) throws Exception {
|
|
|
|
logger.trace("get itemExistsInWorkpace by parentId: "+parentId);
|
|
|
|
try {
|
2016-11-24 12:28:48 +01:00
|
|
|
String scopeGroupId = ""+PortalContext.getConfiguration().getCurrentGroupId(getThreadLocalRequest());
|
2016-12-21 14:20:48 +01:00
|
|
|
String currUserId = ""+PortalContext.getConfiguration().getCurrentUser(getThreadLocalRequest());
|
|
|
|
Workspace workspace = WsUtil.getWorkspace(getThreadLocalRequest(), scopeGroupId, currUserId);
|
2015-10-02 11:46:32 +02:00
|
|
|
WorkspaceItem wsItem = workspace.getItem(parentId); //GET PARENT
|
2016-05-12 15:12:45 +02:00
|
|
|
|
2015-10-02 11:46:32 +02:00
|
|
|
if(wsItem.getType().equals(WorkspaceItemType.FOLDER) || wsItem.getType().equals(WorkspaceItemType.SHARED_FOLDER)){
|
2016-05-12 15:12:45 +02:00
|
|
|
|
2015-10-02 11:46:32 +02:00
|
|
|
WorkspaceItem itemFound = workspace.find(itemName, parentId);
|
2016-05-12 15:12:45 +02:00
|
|
|
|
2015-10-02 11:46:32 +02:00
|
|
|
if(itemFound==null)
|
|
|
|
return null;
|
2016-05-12 15:12:45 +02:00
|
|
|
|
2015-10-02 11:46:32 +02:00
|
|
|
return itemFound.getId();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
throw new Exception("Invalid Folder parent");
|
|
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
String error = "Sorry an error occurred when searching item id, please refresh and try again";
|
|
|
|
logger.error(error, e);
|
|
|
|
throw new Exception(e.getMessage());
|
|
|
|
}
|
|
|
|
}
|
2016-05-12 15:12:45 +02:00
|
|
|
|
2015-10-02 11:46:32 +02:00
|
|
|
// protected void getThreadIds(){
|
|
|
|
// WorkspaceUploadServletStream.printAllThreadIds();
|
|
|
|
// }
|
|
|
|
}
|