Search in sources :

Example 1 with PreConditionFailedException

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);
    }
}
Also used : PreConditionFailedException(io.milton.http.exceptions.PreConditionFailedException) URISyntaxException(java.net.URISyntaxException) BadRequestException(io.milton.http.exceptions.BadRequestException) PreConditionFailedException(io.milton.http.exceptions.PreConditionFailedException) IOException(java.io.IOException) NotAuthorizedException(io.milton.http.exceptions.NotAuthorizedException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) LockedException(io.milton.http.exceptions.LockedException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 2 with PreConditionFailedException

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);
    }
}
Also used : PreConditionFailedException(io.milton.http.exceptions.PreConditionFailedException) URISyntaxException(java.net.URISyntaxException) BadRequestException(io.milton.http.exceptions.BadRequestException) PreConditionFailedException(io.milton.http.exceptions.PreConditionFailedException) IOException(java.io.IOException) NotAuthorizedException(io.milton.http.exceptions.NotAuthorizedException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) LockedException(io.milton.http.exceptions.LockedException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 3 with PreConditionFailedException

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);
    }
}
Also used : LockedException(io.milton.http.exceptions.LockedException) PreConditionFailedException(io.milton.http.exceptions.PreConditionFailedException) URISyntaxException(java.net.URISyntaxException) BadRequestException(io.milton.http.exceptions.BadRequestException) PreConditionFailedException(io.milton.http.exceptions.PreConditionFailedException) IOException(java.io.IOException) NotAuthorizedException(io.milton.http.exceptions.NotAuthorizedException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) LockedException(io.milton.http.exceptions.LockedException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 4 with PreConditionFailedException

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;
}
Also used : Path(io.milton.common.Path) LogicalData(nl.uva.cs.lobcder.resources.LogicalData) PreConditionFailedException(io.milton.http.exceptions.PreConditionFailedException) SQLException(java.sql.SQLException) Connection(java.sql.Connection)

Example 5 with PreConditionFailedException

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);
    }
}
Also used : LockedException(io.milton.http.exceptions.LockedException) PreConditionFailedException(io.milton.http.exceptions.PreConditionFailedException) LockingCollectionResource(io.milton.resource.LockingCollectionResource) Resource(io.milton.resource.Resource) LockableResource(io.milton.resource.LockableResource) BadRequestException(io.milton.http.exceptions.BadRequestException) IOException(java.io.IOException) SAXException(org.xml.sax.SAXException)

Aggregations

PreConditionFailedException (io.milton.http.exceptions.PreConditionFailedException)6 BadRequestException (io.milton.http.exceptions.BadRequestException)4 LockedException (io.milton.http.exceptions.LockedException)4 IOException (java.io.IOException)4 NotAuthorizedException (io.milton.http.exceptions.NotAuthorizedException)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)3 URISyntaxException (java.net.URISyntaxException)3 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)3 LockableResource (io.milton.resource.LockableResource)2 Path (io.milton.common.Path)1 LockingCollectionResource (io.milton.resource.LockingCollectionResource)1 Resource (io.milton.resource.Resource)1 Connection (java.sql.Connection)1 SQLException (java.sql.SQLException)1 LogicalData (nl.uva.cs.lobcder.resources.LogicalData)1 SAXException (org.xml.sax.SAXException)1