import gr.ebs.gss.server.domain.dto.FileHeaderDTO;
import gr.ebs.gss.server.domain.dto.FolderDTO;
import gr.ebs.gss.server.ejb.ExternalAPI;
+import gr.ebs.gss.server.ejb.TransactionHelper;
import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.util.StringTokenizer;
import java.util.TimeZone;
import java.util.Vector;
+import java.util.concurrent.Callable;
import javax.naming.Context;
import javax.naming.InitialContext;
}
}
+ private void updateAccounting(final User user, final Date date, final long bandwidthDiff) {
+ try {
+ new TransactionHelper<Void>().tryExecute(new Callable<Void>() {
+ @Override
+ public Void call() throws Exception {
+ getService().updateAccounting(user, date, bandwidthDiff);
+ return null;
+ }
+ });
+ } catch (RuntimeException e) {
+ throw e;
+ } catch (Exception e) {
+ // updateAccounting() doesn't throw any checked exceptions
+ assert false;
+ }
+ }
+
@Override
public void service(final HttpServletRequest request, final HttpServletResponse response) throws IOException, ServletException {
String method = request.getMethod();
return;
}
- User user = getUser(req);
+ final User user = getUser(req);
String path = getRelativePath(req);
boolean exists = true;
Object resource = null;
resourceInputStream = req.getInputStream();
try {
- FolderDTO folder = null;
Object parent = getService().getResourceAtPath(user.getId(), getParentPath(path), true);
if (!(parent instanceof FolderDTO)) {
resp.sendError(HttpServletResponse.SC_CONFLICT);
return;
}
- folder = (FolderDTO) parent;
- String name = getLastElement(path);
- String mimeType = getServletContext().getMimeType(name);
+ final FolderDTO folder = (FolderDTO) parent;
+ final String name = getLastElement(path);
+ final String mimeType = getServletContext().getMimeType(name);
File uploadedFile = null;
try {
uploadedFile = getService().uploadFile(resourceInputStream, user.getId());
}
// FIXME: Add attributes
FileHeaderDTO fileDTO = null;
+ final FileHeaderDTO f = file;
+ final File uf = uploadedFile;
if (exists)
- fileDTO = getService().updateFileContents(user.getId(), file.getId(), mimeType, uploadedFile);
+ fileDTO = new TransactionHelper<FileHeaderDTO>().tryExecute(new Callable<FileHeaderDTO>() {
+ @Override
+ public FileHeaderDTO call() throws Exception {
+ return getService().updateFileContents(user.getId(), f.getId(), mimeType, uf.length(), uf.getAbsolutePath());
+ }
+ });
else
- fileDTO = getService().createFile(user.getId(), folder.getId(), name, mimeType, uploadedFile);
- getService().updateAccounting(user, new Date(), fileDTO.getFileSize());
+ fileDTO = new TransactionHelper<FileHeaderDTO>().tryExecute(new Callable<FileHeaderDTO>() {
+ @Override
+ public FileHeaderDTO call() throws Exception {
+ return getService().createFile(user.getId(), folder.getId(), name, mimeType, uf.length(), uf.getAbsolutePath());
+ }
+ });
+ updateAccounting(user, new Date(), fileDTO.getFileSize());
} catch (ObjectNotFoundException e) {
result = false;
} catch (InsufficientPermissionsException e) {
} catch (DuplicateNameException e) {
resp.sendError(HttpServletResponse.SC_CONFLICT);
return;
+ } catch (Exception e) {
+ resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, path);
+ return;
}
if (result) {
resp.sendError(WebdavStatus.SC_LOCKED);
return;
}
- String path = getRelativePath(req);
+ final String path = getRelativePath(req);
if (path.toUpperCase().startsWith("/WEB-INF") || path.toUpperCase().startsWith("/META-INF")) {
resp.sendError(WebdavStatus.SC_FORBIDDEN);
return;
}
- User user = getUser(req);
+ final User user = getUser(req);
boolean exists = true;
try {
getService().getResourceAtPath(user.getId(), path, true);
}
try {
if (parent instanceof FolderDTO) {
- FolderDTO folder = (FolderDTO) parent;
- getService().createFolder(user.getId(), folder.getId(), getLastElement(path));
+ final FolderDTO folder = (FolderDTO) parent;
+ new TransactionHelper<Void>().tryExecute(new Callable<Void>() {
+ @Override
+ public Void call() throws Exception {
+ getService().createFolder(user.getId(), folder.getId(), getLastElement(path));
+ return null;
+ }
+ });
} else {
resp.sendError(WebdavStatus.SC_FORBIDDEN, WebdavStatus.getStatusText(WebdavStatus.SC_FORBIDDEN));
return;
} catch (RpcException e) {
resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, path);
return;
+ } catch (Exception e) {
+ resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, path);
+ return;
}
resp.setStatus(WebdavStatus.SC_CREATED);
}
copy(file, renderResult, ostream, req, null);
else
copy(file, renderResult, writer, req, null);
- getService().updateAccounting(user, new Date(), contentLength);
+ updateAccounting(user, new Date(), contentLength);
}
} else {
if (ranges == null || ranges.isEmpty())
copy(file, ostream, range, req, null);
else
copy(file, writer, range, req, null);
- getService().updateAccounting(user, new Date(), contentLength);
+ updateAccounting(user, new Date(), contentLength);
}
} else {
if (logger.isDebugEnabled())
logger.debug("Copy: " + source + " To: " + dest);
- User user = getUser(req);
+ final User user = getUser(req);
Object object = null;
try {
object = getService().getResourceAtPath(user.getId(), source, true);
}
if (object instanceof FolderDTO) {
- FolderDTO folder = (FolderDTO) object;
+ final FolderDTO folder = (FolderDTO) object;
try {
- getService().copyFolder(user.getId(), folder.getId(), dest);
+ final String des = dest;
+ new TransactionHelper<Void>().tryExecute(new Callable<Void>() {
+ @Override
+ public Void call() throws Exception {
+ getService().copyFolder(user.getId(), folder.getId(), des);
+ return null;
+ }
+ });
} catch (ObjectNotFoundException e) {
errorList.put(dest, new Integer(WebdavStatus.SC_CONFLICT));
return false;
} catch (InsufficientPermissionsException e) {
errorList.put(dest, new Integer(WebdavStatus.SC_FORBIDDEN));
return false;
+ } catch (Exception e) {
+ errorList.put(dest, new Integer(WebdavStatus.SC_INTERNAL_SERVER_ERROR));
+ return false;
}
try {
}
} else if (object instanceof FileHeaderDTO) {
- FileHeaderDTO file = (FileHeaderDTO) object;
+ final FileHeaderDTO file = (FileHeaderDTO) object;
try {
- getService().copyFile(user.getId(), file.getId(), dest);
+ final String des = dest;
+ new TransactionHelper<Void>().tryExecute(new Callable<Void>() {
+ @Override
+ public Void call() throws Exception {
+ getService().copyFile(user.getId(), file.getId(), des);
+ return null;
+ }
+ });
} catch (ObjectNotFoundException e) {
errorList.put(source, new Integer(WebdavStatus.SC_INTERNAL_SERVER_ERROR));
return false;
} catch (GSSIOException e) {
errorList.put(source, new Integer(WebdavStatus.SC_INTERNAL_SERVER_ERROR));
return false;
+ } catch (Exception e) {
+ errorList.put(dest, new Integer(WebdavStatus.SC_INTERNAL_SERVER_ERROR));
+ return false;
}
} else {
errorList.put(source, new Integer(WebdavStatus.SC_INTERNAL_SERVER_ERROR));
return false;
}
- User user = getUser(req);
+ final User user = getUser(req);
boolean exists = true;
Object object = null;
try {
if (file != null)
try {
- getService().deleteFile(user.getId(), file.getId());
+ final FileHeaderDTO f = file;
+ new TransactionHelper<Void>().tryExecute(new Callable<Void>() {
+ @Override
+ public Void call() throws Exception {
+ getService().deleteFile(user.getId(), f.getId());
+ return null;
+ }
+ });
} catch (InsufficientPermissionsException e) {
resp.sendError(WebdavStatus.SC_METHOD_NOT_ALLOWED);
return false;
} catch (RpcException e) {
resp.sendError(WebdavStatus.SC_INTERNAL_SERVER_ERROR);
return false;
+ } catch (Exception e) {
+ resp.sendError(WebdavStatus.SC_INTERNAL_SERVER_ERROR);
+ return false;
}
else if (folder != null) {
Hashtable<String, Integer> errorList = new Hashtable<String, Integer>();
deleteCollection(req, folder, path, errorList);
try {
- getService().deleteFolder(user.getId(), folder.getId());
+ final FolderDTO f = folder;
+ new TransactionHelper<Void>().tryExecute(new Callable<Void>() {
+ @Override
+ public Void call() throws Exception {
+ getService().deleteFolder(user.getId(), f.getId());
+ return null;
+ }
+ });
} catch (InsufficientPermissionsException e) {
errorList.put(path, new Integer(WebdavStatus.SC_METHOD_NOT_ALLOWED));
} catch (ObjectNotFoundException e) {
errorList.put(path, new Integer(WebdavStatus.SC_NOT_FOUND));
} catch (RpcException e) {
errorList.put(path, new Integer(WebdavStatus.SC_INTERNAL_SERVER_ERROR));
+ } catch (Exception e) {
+ errorList.put(path, new Integer(WebdavStatus.SC_INTERNAL_SERVER_ERROR));
}
if (!errorList.isEmpty()) {
errorList.put(childName, new Integer(WebdavStatus.SC_LOCKED));
else
try {
- User user = getUser(req);
+ final User user = getUser(req);
Object object = getService().getResourceAtPath(user.getId(), childName, true);
FolderDTO childFolder = null;
FileHeaderDTO childFile = null;
else
childFile = (FileHeaderDTO) object;
if (childFolder != null) {
+ final FolderDTO cf = childFolder;
deleteCollection(req, childFolder, childName, errorList);
- getService().deleteFolder(user.getId(), childFolder.getId());
- } else if (childFile != null)
- getService().deleteFile(user.getId(), childFile.getId());
+ new TransactionHelper<Void>().tryExecute(new Callable<Void>() {
+ @Override
+ public Void call() throws Exception {
+ getService().deleteFolder(user.getId(), cf.getId());
+ return null;
+ }
+ });
+ } else if (childFile != null) {
+ final FileHeaderDTO cf = childFile;
+ new TransactionHelper<Void>().tryExecute(new Callable<Void>() {
+ @Override
+ public Void call() throws Exception {
+ getService().deleteFile(user.getId(), cf.getId());
+ return null;
+ }
+ });
+ }
} catch (ObjectNotFoundException e) {
errorList.put(childName, new Integer(WebdavStatus.SC_NOT_FOUND));
} catch (InsufficientPermissionsException e) {
errorList.put(childName, new Integer(WebdavStatus.SC_FORBIDDEN));
} catch (RpcException e) {
errorList.put(childName, new Integer(WebdavStatus.SC_INTERNAL_SERVER_ERROR));
+ } catch (Exception e) {
+ errorList.put(childName, new Integer(WebdavStatus.SC_INTERNAL_SERVER_ERROR));
}
}
}