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());
}
}
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);
}
}
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);
}
}
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;
}
}
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;
}
Aggregations