Search in sources :

Example 11 with ProcedureException

use of org.neo4j.kernel.api.exceptions.ProcedureException in project neo4j by neo4j.

the class ProcedureRegistry method register.

/**
     * Register a new function.
     *
     * @param function the function.
     */
public void register(CallableUserAggregationFunction function, boolean overrideCurrentImplementation) throws ProcedureException {
    UserFunctionSignature signature = function.signature();
    QualifiedName name = signature.name();
    CallableUserFunction oldImplementation = functions.get(name);
    if (oldImplementation == null) {
        aggregationFunctions.put(name, function);
    } else {
        if (overrideCurrentImplementation) {
            aggregationFunctions.put(name, function);
        } else {
            throw new ProcedureException(Status.Procedure.ProcedureRegistrationFailed, "Unable to register aggregation function, because the name `%s` is already in use.", name);
        }
    }
}
Also used : CallableUserFunction(org.neo4j.kernel.api.proc.CallableUserFunction) QualifiedName(org.neo4j.kernel.api.proc.QualifiedName) ProcedureException(org.neo4j.kernel.api.exceptions.ProcedureException) UserFunctionSignature(org.neo4j.kernel.api.proc.UserFunctionSignature)

Example 12 with ProcedureException

use of org.neo4j.kernel.api.exceptions.ProcedureException in project neo4j by neo4j.

the class TypeMappers method converterFor.

public NeoValueConverter converterFor(Type javaType) throws ProcedureException {
    NeoValueConverter converter = javaToNeo.get(javaType);
    if (converter != null) {
        return converter;
    }
    if (javaType instanceof ParameterizedType) {
        ParameterizedType pt = (ParameterizedType) javaType;
        Type rawType = pt.getRawType();
        if (rawType == List.class) {
            Type type = pt.getActualTypeArguments()[0];
            return toList(converterFor(type), type);
        } else if (rawType == Map.class) {
            Type type = pt.getActualTypeArguments()[0];
            if (type != String.class) {
                throw new ProcedureException(Status.Procedure.ProcedureRegistrationFailed, "Maps are required to have `String` keys - but this map has `%s` keys.", type.getTypeName());
            }
            return TO_MAP;
        }
    }
    throw javaToNeoMappingError(javaType);
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) AnyType(org.neo4j.kernel.api.proc.Neo4jTypes.AnyType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) NTString(org.neo4j.kernel.api.proc.Neo4jTypes.NTString) ProcedureException(org.neo4j.kernel.api.exceptions.ProcedureException) HashMap(java.util.HashMap) Map(java.util.Map) NTMap(org.neo4j.kernel.api.proc.Neo4jTypes.NTMap)

Example 13 with ProcedureException

use of org.neo4j.kernel.api.exceptions.ProcedureException in project neo4j by neo4j.

the class ResourceInjectionTest method shouldFailNicelyWhenUnsafeAPISafeModeFunction.

@Test
public void shouldFailNicelyWhenUnsafeAPISafeModeFunction() throws Throwable {
    //When
    List<CallableUserFunction> procList = compiler.compileFunction(FunctionWithUnsafeAPI.class);
    verify(log).warn("org.neo4j.kernel.impl.proc.listCoolPeople is not " + "available due to having restricted access rights, check configuration.");
    assertThat(procList.size(), equalTo(1));
    try {
        procList.get(0).apply(new BasicContext(), new Object[0]);
        fail();
    } catch (ProcedureException e) {
        assertThat(e.getMessage(), containsString("org.neo4j.kernel.impl.proc.listCoolPeople is not " + "available due to having restricted access rights, check configuration."));
    }
}
Also used : CallableUserFunction(org.neo4j.kernel.api.proc.CallableUserFunction) BasicContext(org.neo4j.kernel.api.proc.BasicContext) ProcedureException(org.neo4j.kernel.api.exceptions.ProcedureException) Test(org.junit.Test)

Example 14 with ProcedureException

use of org.neo4j.kernel.api.exceptions.ProcedureException in project neo4j by neo4j.

the class ResourceInjectionTest method shouldFailNicelyWhenUnsafeAPISafeMode.

@Test
public void shouldFailNicelyWhenUnsafeAPISafeMode() throws Throwable {
    //When
    List<CallableProcedure> procList = compiler.compileProcedure(ProcedureWithUnsafeAPI.class, Optional.empty(), false);
    verify(log).warn("org.neo4j.kernel.impl.proc.listCoolPeople is not " + "available due to having restricted access rights, check configuration.");
    assertThat(procList.size(), equalTo(1));
    try {
        procList.get(0).apply(new BasicContext(), new Object[0]);
        fail();
    } catch (ProcedureException e) {
        assertThat(e.getMessage(), containsString("org.neo4j.kernel.impl.proc.listCoolPeople is not " + "available due to having restricted access rights, check configuration."));
    }
}
Also used : BasicContext(org.neo4j.kernel.api.proc.BasicContext) CallableProcedure(org.neo4j.kernel.api.proc.CallableProcedure) ProcedureException(org.neo4j.kernel.api.exceptions.ProcedureException) Test(org.junit.Test)

Example 15 with ProcedureException

use of org.neo4j.kernel.api.exceptions.ProcedureException in project neo4j by neo4j.

the class UserFunctionsTest method shouldMakeContextAvailable.

@Test
public void shouldMakeContextAvailable() throws Throwable {
    // Given
    Key<String> someKey = key("someKey", String.class);
    procs.register(new CallableUserFunction.BasicUserFunction(signature) {

        @Override
        public Object apply(Context ctx, Object[] input) throws ProcedureException {
            return ctx.get(someKey);
        }
    });
    BasicContext ctx = new BasicContext();
    ctx.put(someKey, "hello, world");
    // When
    Object result = procs.callFunction(ctx, signature.name(), new Object[0]);
    // Then
    assertThat(result, equalTo("hello, world"));
}
Also used : BasicContext(org.neo4j.kernel.api.proc.BasicContext) Context(org.neo4j.kernel.api.proc.Context) CallableUserFunction(org.neo4j.kernel.api.proc.CallableUserFunction) BasicContext(org.neo4j.kernel.api.proc.BasicContext) ProcedureException(org.neo4j.kernel.api.exceptions.ProcedureException) Test(org.junit.Test)

Aggregations

ProcedureException (org.neo4j.kernel.api.exceptions.ProcedureException)49 Test (org.junit.Test)28 CallableProcedure (org.neo4j.kernel.api.proc.CallableProcedure)14 BasicContext (org.neo4j.kernel.api.proc.BasicContext)10 MethodHandle (java.lang.invoke.MethodHandle)8 ArrayList (java.util.ArrayList)8 CallableUserFunction (org.neo4j.kernel.api.proc.CallableUserFunction)8 FieldSignature (org.neo4j.kernel.api.proc.FieldSignature)8 QualifiedName (org.neo4j.kernel.api.proc.QualifiedName)8 ComponentInjectionException (org.neo4j.kernel.api.exceptions.ComponentInjectionException)7 UserFunctionSignature (org.neo4j.kernel.api.proc.UserFunctionSignature)7 RawIterator (org.neo4j.collection.RawIterator)6 Context (org.neo4j.kernel.api.proc.Context)6 Log (org.neo4j.logging.Log)6 List (java.util.List)5 Statement (org.neo4j.kernel.api.Statement)5 ProcedureSignature (org.neo4j.kernel.api.proc.ProcedureSignature)5 Procedure (org.neo4j.procedure.Procedure)5 Method (java.lang.reflect.Method)4 Comparator (java.util.Comparator)4