use of io.milton.http.exceptions.PreConditionFailedException in project lobcder by skoulouzis.
the class WebDataResource method unlock.
@Override
public void unlock(String token) throws NotAuthorizedException, PreConditionFailedException {
try (Connection connection = getCatalogue().getConnection()) {
try {
String tokenID = getLogicalData().getLockTokenID();
if (tokenID == null || tokenID.length() <= 0) {
return;
} else {
if (tokenID.startsWith("<") && tokenID.endsWith(">") && !token.startsWith("<") && !token.endsWith(">")) {
StringBuilder sb = new StringBuilder();
sb.append("<").append(token).append(">");
token = sb.toString();
}
if (!tokenID.startsWith("<") && !tokenID.endsWith(">") && token.startsWith("<") && token.endsWith(">")) {
token = token.replaceFirst("<", "");
token = token.replaceFirst(">", "");
}
if (!tokenID.equals(token)) {
throw new PreConditionFailedException(this);
}
}
getCatalogue().setLockTokenID(getLogicalData().getUid(), null, connection);
connection.commit();
getLogicalData().setLockTokenID(null);
getLogicalData().setLockScope(null);
getLogicalData().setLockType(null);
getLogicalData().setLockedByUser(null);
getLogicalData().setLockDepth(null);
getLogicalData().setLockTimeout(null);
} catch (Exception ex) {
Logger.getLogger(WebDataResource.class.getName()).log(Level.SEVERE, null, ex);
connection.rollback();
throw new PreConditionFailedException(this);
}
} catch (SQLException e) {
Logger.getLogger(WebDataResource.class.getName()).log(Level.SEVERE, null, e);
throw new PreConditionFailedException(this);
}
}
use of io.milton.http.exceptions.PreConditionFailedException in project lobcder by skoulouzis.
the class WebDataResource method refreshLock.
@Override
public LockResult refreshLock(String token) throws NotAuthorizedException, PreConditionFailedException {
try (Connection connection = getCatalogue().getConnection()) {
try {
if (getLogicalData().getLockTokenID() == null) {
throw new RuntimeException("not locked");
} else if (!getLogicalData().getLockTokenID().equals(token)) {
throw new RuntimeException("invalid lock id");
}
getLogicalData().setLockTimeout(System.currentTimeMillis() + Constants.LOCK_TIME);
getCatalogue().setLockTimeout(getLogicalData().getUid(), getLogicalData().getLockTimeout(), connection);
LockInfo lockInfo = new LockInfo(LockInfo.LockScope.valueOf(getLogicalData().getLockScope()), LockInfo.LockType.valueOf(getLogicalData().getLockType()), getLogicalData().getLockedByUser(), LockInfo.LockDepth.valueOf(getLogicalData().getLockDepth()));
LockTimeout lockTimeOut = new LockTimeout(getLogicalData().getLockTimeout());
LockToken lockToken = new LockToken(token, lockInfo, lockTimeOut);
connection.commit();
return LockResult.success(lockToken);
} catch (Exception ex) {
Logger.getLogger(WebDataResource.class.getName()).log(Level.SEVERE, null, ex);
connection.rollback();
throw new PreConditionFailedException(this);
}
} catch (SQLException e) {
Logger.getLogger(WebDataResource.class.getName()).log(Level.SEVERE, null, e);
throw new PreConditionFailedException(this);
}
}
use of io.milton.http.exceptions.PreConditionFailedException in project lobcder by skoulouzis.
the class WebDataResource method lock.
@Override
public LockResult lock(LockTimeout timeout, LockInfo lockInfo) throws NotAuthorizedException, PreConditionFailedException, LockedException {
if (getCurrentLock() != null) {
throw new LockedException(this);
}
LockToken lockToken = new LockToken(UUID.randomUUID().toString(), lockInfo, timeout);
Long lockTimeout;
try (Connection connection = getCatalogue().getConnection()) {
try {
getLogicalData().setLockTokenID(lockToken.tokenId);
getCatalogue().setLockTokenID(getLogicalData().getUid(), getLogicalData().getLockTokenID(), connection);
getLogicalData().setLockScope(lockToken.info.scope.toString());
getCatalogue().setLockScope(getLogicalData().getUid(), getLogicalData().getLockScope(), connection);
getLogicalData().setLockType(lockToken.info.type.toString());
getCatalogue().setLockType(getLogicalData().getUid(), getLogicalData().getLockType(), connection);
getLogicalData().setLockedByUser(lockToken.info.lockedByUser);
getCatalogue().setLockByUser(getLogicalData().getUid(), getLogicalData().getLockedByUser(), connection);
getLogicalData().setLockDepth(lockToken.info.depth.toString());
getCatalogue().setLockDepth(getLogicalData().getUid(), getLogicalData().getLockDepth(), connection);
lockTimeout = lockToken.timeout.getSeconds();
if (lockTimeout == null) {
lockTimeout = Long.valueOf(System.currentTimeMillis() + Constants.LOCK_TIME);
}
getLogicalData().setLockTimeout(lockTimeout);
getCatalogue().setLockTimeout(getLogicalData().getUid(), lockTimeout, connection);
connection.commit();
return LockResult.success(lockToken);
} catch (Exception ex) {
Logger.getLogger(WebDataResource.class.getName()).log(Level.SEVERE, null, ex);
connection.rollback();
throw new PreConditionFailedException(this);
}
} catch (SQLException e) {
Logger.getLogger(WebDataResource.class.getName()).log(Level.SEVERE, null, e);
throw new PreConditionFailedException(this);
}
}
use of io.milton.http.exceptions.PreConditionFailedException in project lobcder by skoulouzis.
the class WebDataDirResource method createAndLock.
/**
* This means to just lock the name Not to create the resource.
*
* @param name
* @param timeout
* @param lockInfo
* @return
* @throws NotAuthorizedException
*/
@Override
public LockToken createAndLock(String name, LockTimeout timeout, LockInfo lockInfo) throws NotAuthorizedException {
try (Connection connection = getCatalogue().getConnection()) {
Path newPath = Path.path(getPath(), name);
// If the resource exists
LogicalData fileLogicalData = getCatalogue().getLogicalDataByPath(newPath, connection);
if (fileLogicalData != null) {
throw new PreConditionFailedException(new WebDataFileResource(fileLogicalData, Path.path(getPath(), name), getCatalogue(), authList));
}
LockToken lockToken = new LockToken(UUID.randomUUID().toString(), lockInfo, timeout);
return lockToken;
} catch (SQLException | PreConditionFailedException ex) {
Logger.getLogger(WebDataDirResource.class.getName()).log(Level.SEVERE, null, ex);
if (ex instanceof PreConditionFailedException) {
throw new RuntimeException(ex);
}
} catch (UnsupportedEncodingException ex) {
Logger.getLogger(WebDataDirResource.class.getName()).log(Level.SEVERE, null, ex);
}
return null;
}
use of io.milton.http.exceptions.PreConditionFailedException in project lobcder by skoulouzis.
the class LockHandler method process.
@Override
public void process(HttpManager httpManager, Request request, Response response) throws ConflictException, NotAuthorizedException, BadRequestException, NotFoundException {
if (!handlerHelper.checkExpects(responseHandler, request, response)) {
return;
}
String host = request.getHostHeader();
String url = HttpManager.decodeUrl(request.getAbsolutePath());
Resource r = httpManager.getResourceFactory().getResource(host, url);
try {
// Find a resource if it exists
if (r != null) {
processExistingResource(httpManager, request, response, r);
} else {
processNonExistingResource(httpManager, request, response, host, url);
}
} catch (IOException ex) {
throw new BadRequestException(r);
} catch (SAXException ex) {
throw new BadRequestException(r);
} catch (LockedException ex) {
responseHandler.respondLocked(request, response, r);
} catch (PreConditionFailedException ex) {
responseHandler.respondPreconditionFailed(request, response, r);
}
}
Aggregations