use of uk.gov.gchq.gaffer.named.operation.ExtendedNamedOperation in project Gaffer by gchq.
the class NamedOperationJCSCacheTest method shouldBeAbleToReturnFullExtendedOperationChain.
@Test
public void shouldBeAbleToReturnFullExtendedOperationChain() throws CacheOperationFailedException {
cache.addNamedOperation(standard, false, standardUser);
ExtendedNamedOperation alt = alternative;
alt.setOperationName("alt");
cache.addNamedOperation(alt, false, advancedUser);
Set<NamedOperation> actual = Sets.newHashSet(cache.getAllNamedOperations(standardUser, false));
assert (actual.contains(standard));
assert (actual.contains(alt));
assert (actual.size() == 2);
}
use of uk.gov.gchq.gaffer.named.operation.ExtendedNamedOperation in project Gaffer by gchq.
the class AbstractNamedOperationCache method addNamedOperation.
/**
* If the user is just adding to the cache, ie the overwrite flag is set to false, then no security is added.
* However if the user is overwriting the named operation stored in the cache, then their opAuths must be checked
* against the write roles associated with the {@link ExtendedNamedOperation}. If it turns out the user is overwriting a
* non-existent ExtendedNamedOperation, then the users ExtendedNamedOperation will be added normally.
*
* @param namedOperation The ExtendedNamedOperation that the user wants to store
* @param overwrite Flag relating to whether the user is adding (false) or updating/overwriting (true)
* @throws CacheOperationFailedException thrown if the user doesn't have write access to the ExtendedNamedOperation requested,
* or if the add operation fails for some reason.
*/
@Override
public void addNamedOperation(final ExtendedNamedOperation namedOperation, final boolean overwrite, final User user) throws CacheOperationFailedException {
String name;
try {
name = namedOperation.getOperationName();
} catch (NullPointerException e) {
throw new CacheOperationFailedException("NamedOperation cannot be null", e);
}
if (!overwrite) {
addToCache(name, namedOperation, false);
return;
}
ExtendedNamedOperation existing = null;
try {
existing = getFromCache(name);
} catch (CacheOperationFailedException e) {
// if there is no existing named Operation add one
addToCache(name, namedOperation, false);
return;
}
if (existing.hasWriteAccess(user)) {
addToCache(name, namedOperation, true);
} else {
throw new CacheOperationFailedException("User " + namedOperation.getCreatorId() + " does not have permission to overwrite");
}
}
use of uk.gov.gchq.gaffer.named.operation.ExtendedNamedOperation in project Gaffer by gchq.
the class AddNamedOperationHandler method validate.
private void validate(final User user, final List<String> parentOperations, final OperationChain<?> operationChain, final INamedOperationCache cache) throws CacheOperationFailedException, OperationException {
for (final Operation op : operationChain.getOperations()) {
if (op instanceof NamedOperation) {
if (parentOperations.contains(((NamedOperation) op).getOperationName())) {
throw new OperationException("The Operation Chain must not be recursive");
}
ExtendedNamedOperation operation = cache.getNamedOperation(((NamedOperation) op).getOperationName(), user);
if (operation == null) {
throw new OperationException("The Operation " + ((NamedOperation) op).getOperationName() + " references an operation which doesn't exist. Unable to create operation");
}
parentOperations.add(((NamedOperation) op).getOperationName());
validate(user, parentOperations, operation.getOperationChain(), cache);
parentOperations.remove(((NamedOperation) op).getOperationName());
}
}
}
use of uk.gov.gchq.gaffer.named.operation.ExtendedNamedOperation in project Gaffer by gchq.
the class NamedOperationHandler method doOperation.
/**
* Gets the requested NamedOperation, updates the input and the view, then executes the operation chain, bypassing
* the Graph.executes graph hooks.
*
* @param operation the {@link Operation} to be executed
* @param context the operation chain context, containing the user who executed the operation
* @param store the {@link Store} the operation should be run on
* @return an object - whatever the last operation in the chain returns
* @throws OperationException thrown when the operation fails
*/
@Override
public Object doOperation(final NamedOperation operation, final Context context, final Store store) throws OperationException {
try {
if (cache == null) {
throw new OperationException("Cache should be initialised in " + "resources/NamedOperationsDeclarations.json and referenced in store.properties");
}
ExtendedNamedOperation namedOperation = cache.getNamedOperation(operation.getOperationName(), context.getUser());
OperationChain<?> operationChain = namedOperation.getOperationChain();
operationChain = new OperationChain<>(exposeNamedOperations(operationChain, context.getUser(), cache));
updateOperationInput(operationChain.getOperations().get(0), operation.getSeeds());
operationChain = updateView(operation.getView(), operationChain);
return store._execute(operationChain, context);
} catch (CacheOperationFailedException e) {
throw new OperationException(e.getMessage(), e);
} catch (ClassCastException e) {
throw new OperationException("Input type " + operation.getInput().getClass().getName() + " was not valid for the operation", e);
}
}
use of uk.gov.gchq.gaffer.named.operation.ExtendedNamedOperation in project Gaffer by gchq.
the class NamedOperationJCSCache method getFromCache.
/**
* Checks the supplied name is not null, an Object exists with this name in the cache, that Object is not null and
* is an instance of ExtendedNamedOperation. If all these conditions are met, the ExtendedNamedOperation is returned, if any are not
* met then an exception is thrown.
*
* @param name the key stored in the cache
* @return a ExtendedNamedOperation stored against the key
* @throws CacheOperationFailedException thrown when the operation fails
*/
public ExtendedNamedOperation getFromCache(final String name) throws CacheOperationFailedException {
if (name == null) {
throw new CacheOperationFailedException("Operation name cannot be null");
}
ExtendedNamedOperation namedOperation;
Object obj = getCache().getFromGroup(name, CACHE_GROUP);
if (obj != null && obj.getClass().equals(ExtendedNamedOperation.class)) {
namedOperation = (ExtendedNamedOperation) obj;
return namedOperation;
}
throw new CacheOperationFailedException("No ExtendedNamedOperation with a name '" + name + "' could be found in the cache");
}
Aggregations