Search in sources :

Example 31 with NotAuthorizedException

use of io.milton.http.exceptions.NotAuthorizedException in project lobcder by skoulouzis.

the class WebDataFileResource method delete.

@Override
public void delete() throws NotAuthorizedException, BadRequestException, ConflictException {
    Logger.getLogger(WebDataFileResource.class.getName()).log(Level.FINEST, "delete() file {0}", getPath());
    try (Connection connection = getCatalogue().getConnection()) {
        try {
            getCatalogue().remove(getLogicalData(), getPrincipal(), connection);
            connection.commit();
        } catch (Exception e) {
            Logger.getLogger(WebDataFileResource.class.getName()).log(Level.SEVERE, null, e);
            connection.rollback();
            throw new BadRequestException(this, e.getMessage());
        }
    } catch (SQLException e) {
        Logger.getLogger(WebDataFileResource.class.getName()).log(Level.SEVERE, null, e);
        throw new BadRequestException(this, e.getMessage());
    }
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) BadRequestException(io.milton.http.exceptions.BadRequestException) ConflictException(io.milton.http.exceptions.ConflictException) URISyntaxException(java.net.URISyntaxException) SQLException(java.sql.SQLException) BadRequestException(io.milton.http.exceptions.BadRequestException) IOException(java.io.IOException) NotAuthorizedException(io.milton.http.exceptions.NotAuthorizedException) UnknownHostException(java.net.UnknownHostException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) NotFoundException(io.milton.http.exceptions.NotFoundException)

Example 32 with NotAuthorizedException

use of io.milton.http.exceptions.NotAuthorizedException 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 33 with NotAuthorizedException

use of io.milton.http.exceptions.NotAuthorizedException 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 34 with NotAuthorizedException

use of io.milton.http.exceptions.NotAuthorizedException in project lobcder by skoulouzis.

the class WebDataDirResource method getChildren.

@Override
public List<? extends Resource> getChildren() throws NotAuthorizedException {
    // Logger.getLogger(WebDataDirResource.class.getName()).log(Level.FINEST, "getChildren() for {0}", getPath());
    try {
        try (Connection connection = getCatalogue().getConnection()) {
            try {
                List<Resource> children = new ArrayList<>();
                Collection<LogicalData> childrenLD = getCatalogue().getChildrenByParentRef(getLogicalData().getUid(), connection);
                if (childrenLD != null) {
                    for (LogicalData childLD : childrenLD) {
                        if (childLD.getType().equals(Constants.LOGICAL_FOLDER)) {
                            children.add(new WebDataDirResource(childLD, Path.path(getPath(), childLD.getName()), getCatalogue(), authList));
                        } else {
                            children.add(new WebDataFileResource(childLD, Path.path(getPath(), childLD.getName()), getCatalogue(), authList));
                        }
                    }
                }
                getCatalogue().addViewForRes(getLogicalData().getUid(), connection);
                connection.commit();
                connection.close();
                return children;
            } catch (Exception e) {
                if (connection != null && !connection.isClosed()) {
                    connection.rollback();
                    connection.close();
                }
                throw e;
            }
        }
    } catch (Exception e) {
        Logger.getLogger(WebDataDirResource.class.getName()).log(Level.SEVERE, null, e);
        return null;
    }
}
Also used : LogicalData(nl.uva.cs.lobcder.resources.LogicalData) Connection(java.sql.Connection) ConflictException(io.milton.http.exceptions.ConflictException) URISyntaxException(java.net.URISyntaxException) SQLException(java.sql.SQLException) BadRequestException(io.milton.http.exceptions.BadRequestException) PreConditionFailedException(io.milton.http.exceptions.PreConditionFailedException) NotAuthorizedException(io.milton.http.exceptions.NotAuthorizedException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException)

Example 35 with NotAuthorizedException

use of io.milton.http.exceptions.NotAuthorizedException in project lobcder by skoulouzis.

the class PropertySourcePatchSetter method setProperties.

@Override
public PropFindResponse setProperties(String href, ParseResult parseResult, Resource r) throws NotAuthorizedException, BadRequestException {
    log.trace("setProperties: resource type: {}", r.getClass());
    Map<QName, ValueAndType> knownProps = new HashMap<QName, ValueAndType>();
    Map<Status, List<NameAndError>> errorProps = new EnumMap<Status, List<NameAndError>>(Status.class);
    for (Entry<QName, String> entry : parseResult.getFieldsToSet().entrySet()) {
        QName name = entry.getKey();
        boolean found = false;
        for (PropertySource source : propertySources) {
            PropertyMetaData meta = source.getPropertyMetaData(entry.getKey(), r);
            if (meta != null && !meta.isUnknown()) {
                found = true;
                if (meta.isWritable()) {
                    Object val = parse(name, entry.getValue(), meta.getValueType());
                    try {
                        log.trace("setProperties: name: {} source: {}", name, source.getClass());
                        source.setProperty(name, val, r);
                        knownProps.put(name, new ValueAndType(null, meta.getValueType()));
                        break;
                    } catch (NotAuthorizedException e) {
                        log.warn("setProperties: NotAuthorised to write property: {}", name, e);
                        addErrorProp(errorProps, Response.Status.SC_UNAUTHORIZED, name, "Not authorised");
                        break;
                    } catch (PropertySetException ex) {
                        log.warn("setProperties: PropertySetException when writing property {}", name, ex);
                        addErrorProp(errorProps, ex.getStatus(), name, ex.getErrorNotes());
                        break;
                    }
                } else {
                    log.warn("property is not writable in source: " + source.getClass());
                    addErrorProp(errorProps, Response.Status.SC_FORBIDDEN, name, "Property is read only");
                    break;
                }
            } else {
            // log.debug( "not found in: " + source.getClass().getCanonicalName() );
            }
        }
        if (!found) {
            log.warn("property not found: " + entry.getKey() + " on resource: " + r.getClass());
            addErrorProp(errorProps, Status.SC_NOT_FOUND, entry.getKey(), "Unknown property");
        }
    }
    if (parseResult.getFieldsToRemove() != null) {
        for (QName name : parseResult.getFieldsToRemove()) {
            boolean found = false;
            for (PropertySource source : propertySources) {
                PropertyMetaData meta = source.getPropertyMetaData(name, r);
                if (meta != null && !meta.isUnknown()) {
                    found = true;
                    if (meta.isWritable()) {
                        try {
                            log.trace("clearProperty");
                            source.clearProperty(name, r);
                            knownProps.put(name, new ValueAndType(null, meta.getValueType()));
                            break;
                        } catch (NotAuthorizedException e) {
                            addErrorProp(errorProps, Response.Status.SC_UNAUTHORIZED, name, "Not authorised");
                            break;
                        } catch (PropertySetException ex) {
                            addErrorProp(errorProps, ex.getStatus(), name, ex.getErrorNotes());
                            break;
                        }
                    } else {
                        log.warn("property is not writable in source: " + source.getClass());
                        addErrorProp(errorProps, Response.Status.SC_FORBIDDEN, name, "Property is read only");
                        break;
                    }
                } else {
                // log.debug( "not found in: " + source.getClass().getCanonicalName() );
                }
            }
            if (!found) {
                log.warn("property not found to remove: " + name);
                addErrorProp(errorProps, Status.SC_NOT_FOUND, name, "Unknown property");
            }
        }
    }
    if (log.isDebugEnabled()) {
        if (errorProps.size() > 0) {
            log.debug("errorProps: " + errorProps.size() + " listing property sources:");
            for (PropertySource s : propertySources) {
                log.debug("  source: " + s.getClass().getCanonicalName());
            }
        }
    }
    if (r instanceof CommitableResource) {
        log.trace("resource is commitable, call doCommit");
        CommitableResource cr = (CommitableResource) r;
        cr.doCommit(knownProps, errorProps);
    } else {
        log.trace("resource is not commitable");
    }
    PropFindResponse resp = new PropFindResponse(href, knownProps, errorProps);
    return resp;
}
Also used : Status(io.milton.http.Response.Status) ValueAndType(io.milton.http.values.ValueAndType) HashMap(java.util.HashMap) QName(javax.xml.namespace.QName) PropertyMetaData(io.milton.property.PropertySource.PropertyMetaData) NotAuthorizedException(io.milton.http.exceptions.NotAuthorizedException) PropertySource(io.milton.property.PropertySource) NameAndError(io.milton.http.webdav.PropFindResponse.NameAndError) PropertySetException(io.milton.property.PropertySource.PropertySetException) ArrayList(java.util.ArrayList) List(java.util.List) EnumMap(java.util.EnumMap)

Aggregations

NotAuthorizedException (io.milton.http.exceptions.NotAuthorizedException)40 BadRequestException (io.milton.http.exceptions.BadRequestException)29 IOException (java.io.IOException)12 Resource (io.milton.resource.Resource)10 URISyntaxException (java.net.URISyntaxException)9 Connection (java.sql.Connection)9 SQLException (java.sql.SQLException)9 ConflictException (io.milton.http.exceptions.ConflictException)7 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)7 PreConditionFailedException (io.milton.http.exceptions.PreConditionFailedException)6 UnsupportedEncodingException (java.io.UnsupportedEncodingException)6 Permissions (nl.uva.cs.lobcder.auth.Permissions)6 NotFoundException (io.milton.http.exceptions.NotFoundException)5 CollectionResource (io.milton.resource.CollectionResource)5 ReplaceableResource (io.milton.resource.ReplaceableResource)5 QName (javax.xml.namespace.QName)5 Path (io.milton.common.Path)4 LockedException (io.milton.http.exceptions.LockedException)4 ValueAndType (io.milton.http.values.ValueAndType)4 LogicalData (nl.uva.cs.lobcder.resources.LogicalData)4