Package com.tinkerpop.rexster.extension

Examples of com.tinkerpop.rexster.extension.ExtensionMethod


    @ExtensionDefinition(extensionPoint = ExtensionPoint.GRAPH, method = HttpMethod.POST)
    @ExtensionDescriptor(description = "Execute a Faunus job")
    public ExtensionResponse evaluatePost(@RexsterContext final RexsterResourceContext context) {
        final String script = context.getRequestObject().optString("script");
        if (script == null || script.isEmpty()) {
            ExtensionMethod extMethod = context.getExtensionMethod();
            return ExtensionResponse.error("the script parameter cannot be empty", null,
                    Response.Status.BAD_REQUEST.getStatusCode(), null, generateErrorJson(extMethod.getExtensionApiAsJson()));
        }

        final JSONObject config = context.getRequestObject().optJSONObject("config");
        final UUID jobId = UUID.randomUUID();
        final FaunusEvaluationJob job = new FaunusEvaluationJob(script, jobId, context.getRexsterApplicationGraph(),
View Full Code Here


    @ExtensionDefinition(extensionPoint = ExtensionPoint.GRAPH, method = HttpMethod.GET)
    @ExtensionDescriptor(description = "Get Faunus job status")
    public ExtensionResponse evaluateGet(@RexsterContext final RexsterResourceContext context) {
        final String job = context.getRequestObject().optString("job");
        if (job == null || job.isEmpty()) {
            ExtensionMethod extMethod = context.getExtensionMethod();
            return ExtensionResponse.error("the job parameter cannot be empty", null,
                    Response.Status.BAD_REQUEST.getStatusCode(), null, generateErrorJson(extMethod.getExtensionApiAsJson()));
        }

        if (!jobs.containsKey(job)) {
            return new ExtensionResponse(Response.status(Response.Status.NOT_FOUND).build());
        }
View Full Code Here

                                    @RexsterContext Graph graph,
                                    @RexsterContext RexsterApplicationGraph rag) {

        final JSONObject transactionJson = context.getRequestObject();
        if (transactionJson == null) {
            final ExtensionMethod extMethod = context.getExtensionMethod();
            return ExtensionResponse.error(
                    "no transaction JSON posted",
                    null,
                    Response.Status.BAD_REQUEST.getStatusCode(),
                    null,
                    generateErrorJson(extMethod.getExtensionApiAsJson()));
        }

        try {
            final JSONArray txArray = transactionJson.optJSONArray("tx");
            String currentAction;
            for (int ix = 0; ix < txArray.length(); ix++) {
                final JSONObject txElement = txArray.optJSONObject(ix);
                currentAction = txElement.optString("_action");
                if (currentAction.equals("create")) {
                    create(txElement, graph);
                } else if (currentAction.equals("update")) {
                    update(txElement, graph);
                } else if (currentAction.equals("delete")) {
                    delete(txElement, graph);
                }
            }

            final Map<String, Object> resultMap = new HashMap<String, Object>();
            resultMap.put(Tokens.SUCCESS, true);
            resultMap.put("txProcessed", txArray.length());

            return ExtensionResponse.ok(new JSONObject(resultMap));

        } catch (IllegalArgumentException iae) {
            logger.error(iae);

            final ExtensionMethod extMethod = context.getExtensionMethod();
            return ExtensionResponse.error(
                    iae.getMessage(),
                    null,
                    Response.Status.BAD_REQUEST.getStatusCode(),
                    null,
                    generateErrorJson(extMethod.getExtensionApiAsJson()));
        } catch (Exception ex) {
            logger.error(ex);
            return ExtensionResponse.error(
                    "Error executing transaction: " + ex.getMessage(), generateErrorJson());
        }
View Full Code Here

            }
        }
    }

    private ExtensionResponse checkParameters(RexsterResourceContext context, JSONArray values, String type, String key) {
        final ExtensionMethod extMethod = context.getExtensionMethod();
        String errorMessage = null;

        if (values == null || values.length() == 0) {
            errorMessage = "the values parameter cannot be empty";
        else if ((type.equals("index") || type.equals("keyindex")) && key.isEmpty()) {
            errorMessage = "the key parameter cannot be empty";
        }

        return (errorMessage != null)
            ? ExtensionResponse.error(
                    errorMessage,
                    null,
                    Response.Status.BAD_REQUEST.getStatusCode(),
                    null,
                    extMethod != null ? generateErrorJson(extMethod.getExtensionApiAsJson()) : null)
            : null;
    }
View Full Code Here

    private Response executeVertexExtension(final String graphName, final String id, final HttpMethod httpMethodRequested) {

        final Vertex vertex = this.getRexsterApplicationGraph(graphName).getGraph().getVertex(id);

        ExtensionResponse extResponse;
        ExtensionMethod methodToCall;
        final ExtensionSegmentSet extensionSegmentSet = parseUriForExtensionSegment(graphName, ExtensionPoint.VERTEX);

        // determine if the namespace and extension are enabled for this graph
        final RexsterApplicationGraph rag = this.getRexsterApplicationGraph(graphName);

        if (rag.isExtensionAllowed(extensionSegmentSet)) {

            final Object returnValue;

            // namespace was allowed so try to run the extension
            try {

                // look for the extension as loaded through serviceloader
                final List<RexsterExtension> rexsterExtensions;
                try {
                    rexsterExtensions = findExtensionClasses(extensionSegmentSet);
                } catch (ServiceConfigurationError sce) {
                    logger.error("ServiceLoader could not find a class referenced in com.tinkerpop.rexster.extension.RexsterExtension.");
                    final JSONObject error = generateErrorObject(
                            "Class specified in com.tinkerpop.rexster.extension.RexsterExtension could not be found.",
                            sce);
                    throw new WebApplicationException(Response.status(Status.NOT_FOUND).entity(error).build());
                }

                if (rexsterExtensions == null || rexsterExtensions.size() == 0) {
                    // extension was not found for some reason
                    logger.error("The [" + extensionSegmentSet + "] extension was not found for [" + graphName + "].  Check com.tinkerpop.rexster.extension.RexsterExtension file in META-INF.services.");
                    final JSONObject error = generateErrorObject(
                            "The [" + extensionSegmentSet + "] extension was not found for [" + graphName + "]");
                    throw new WebApplicationException(Response.status(Status.NOT_FOUND).entity(error).build());
                }

                // look up the method on the extension that needs to be called.
                methodToCall = findExtensionMethod(rexsterExtensions, ExtensionPoint.VERTEX, extensionSegmentSet.getExtensionMethod(), httpMethodRequested);

                if (methodToCall == null) {
                    // extension method was not found for some reason
                    if (httpMethodRequested == HttpMethod.OPTIONS) {
                        // intercept the options call and return the standard business
                        // no need to stop the transaction here
                        return buildOptionsResponse();
                    }

                    logger.error("The [" + extensionSegmentSet + "] extension was not found for [" + graphName + "] with a HTTP method of [" + httpMethodRequested.name() + "].  Check com.tinkerpop.rexster.extension.RexsterExtension file in META-INF.services.");
                    final JSONObject error = generateErrorObject(
                            "The [" + extensionSegmentSet + "] extension was not found for [" + graphName + "] with a HTTP method of [" + httpMethodRequested.name() + "]");
                    throw new WebApplicationException(Response.status(Status.NOT_FOUND).entity(error).build());
                }

                // found the method...time to do work
                returnValue = invokeExtension(rag, methodToCall, vertex);

            } catch (WebApplicationException wae) {
                // already logged this...just throw it  up.
                rag.tryRollback();
                throw wae;
            } catch (Exception ex) {
                logger.error("Dynamic invocation of the [" + extensionSegmentSet + "] extension failed.", ex);

                if (ex.getCause() != null) {
                    final Throwable cause = ex.getCause();
                    logger.error("It would be smart to trap this this exception within the extension and supply a good response to the user:" + cause.getMessage(), cause);
                }

                rag.tryRollback();

                final JSONObject error = generateErrorObjectJsonFail(ex);
                throw new WebApplicationException(Response.status(Status.INTERNAL_SERVER_ERROR).entity(error).build());
            }

            if (returnValue instanceof ExtensionResponse) {
                extResponse = (ExtensionResponse) returnValue;

                if (extResponse.isErrorResponse()) {
                    // an error was raised within the extension.  pass it back out as an error.
                    logger.warn("The [" + extensionSegmentSet + "] extension raised an error response.");

                    if (methodToCall.getExtensionDefinition().autoCommitTransaction()) {
                        rag.tryRollback();
                    }

                    throw new WebApplicationException(Response.fromResponse(extResponse.getJerseyResponse()).build());
                }

                if (methodToCall.getExtensionDefinition().autoCommitTransaction()) {
                    rag.tryCommit();
                }

            } else {
                // extension method is not returning the correct type...needs to be an ExtensionResponse
                logger.error("The [" + extensionSegmentSet + "] extension does not return an ExtensionResponse.");
                final JSONObject error = generateErrorObject(
                        "The [" + extensionSegmentSet + "] extension does not return an ExtensionResponse.");

                rag.tryRollback();

                throw new WebApplicationException(Response.status(Status.INTERNAL_SERVER_ERROR).entity(error).build());
            }

        } else {
            // namespace was not allowed
            logger.error("The [" + extensionSegmentSet + "] extension was not configured for [" + graphName + "]");
            final JSONObject error = generateErrorObject(
                    "The [" + extensionSegmentSet + "] extension was not configured for [" + graphName + "]");
            throw new WebApplicationException(Response.status(Status.INTERNAL_SERVER_ERROR).entity(error).build());
        }

        String mediaType = MediaType.APPLICATION_JSON;
        if (methodToCall != null) {
            mediaType = methodToCall.getExtensionDefinition().produces();
            extResponse = tryAppendRexsterAttributesIfJson(extResponse, methodToCall, mediaType);
        }

        return Response.fromResponse(extResponse.getJerseyResponse()).type(mediaType).build();
    }
View Full Code Here

    @Test
    public void findExtensionMethodNotPresent() {
        List<RexsterExtension> rexsterExtensions = new ArrayList<RexsterExtension>();
        rexsterExtensions.add(new MockRexsterExtension());
        ExtensionMethod m = this.mockResource.findExtensionMethodExposed(rexsterExtensions, ExtensionPoint.VERTEX, "action", HttpMethod.ANY);
        Assert.assertNull(m);
    }
View Full Code Here

    @Test
    public void findExtensionMethodFoundRoot() {
        List<RexsterExtension> rexsterExtensions = new ArrayList<RexsterExtension>();
        rexsterExtensions.add(new MockRexsterExtension());
        ExtensionMethod m = this.mockResource.findExtensionMethodExposed(rexsterExtensions, ExtensionPoint.GRAPH, "", HttpMethod.ANY);
        Assert.assertNotNull(m);

        Method methodFound = m.getMethod();
        Assert.assertNotNull(methodFound);
        Assert.assertEquals("doRoot", methodFound.getName());
    }
View Full Code Here

    @Test
    public void findExtensionMethodFoundSpecificAction() {
        List<RexsterExtension> rexsterExtensions = new ArrayList<RexsterExtension>();
        rexsterExtensions.add(new MockRexsterExtension());
        ExtensionMethod m = this.mockResource.findExtensionMethodExposed(rexsterExtensions, ExtensionPoint.GRAPH, "action", HttpMethod.ANY);
        Assert.assertNotNull(m);

        Method methodFound = m.getMethod();
        Assert.assertNotNull(methodFound);
        Assert.assertEquals("doAction", methodFound.getName());
    }
View Full Code Here

    @Test
    public void findExtensionMethodFoundSpecificActionMultipleExtensionClasses() {
        List<RexsterExtension> rexsterExtensions = new ArrayList<RexsterExtension>();
        rexsterExtensions.add(new MockRexsterExtension());
        rexsterExtensions.add(new MockAddOnRexsterExtension());
        ExtensionMethod m = this.mockResource.findExtensionMethodExposed(rexsterExtensions, ExtensionPoint.GRAPH, "action", HttpMethod.ANY);
        Assert.assertNotNull(m);

        Method methodFound = m.getMethod();
        Assert.assertNotNull(methodFound);
        Assert.assertEquals("doAction", methodFound.getName());

        m = this.mockResource.findExtensionMethodExposed(rexsterExtensions, ExtensionPoint.GRAPH, "addon", HttpMethod.ANY);
        Assert.assertNotNull(m);

        methodFound = m.getMethod();
        Assert.assertNotNull(methodFound);
        Assert.assertEquals("doAddOnAction", methodFound.getName());
    }
View Full Code Here

    @Test
    public void findExtensionMethodFoundUseRootMethod() {
        List<RexsterExtension> rexsterExtensions = new ArrayList<RexsterExtension>();
        rexsterExtensions.add(new MockRexsterExtension());
        ExtensionMethod m = this.mockResource.findExtensionMethodExposed(rexsterExtensions, ExtensionPoint.GRAPH, "headonly", HttpMethod.POST);
        Assert.assertNotNull(m);

        Method methodFound = m.getMethod();
        Assert.assertNotNull(methodFound);
        Assert.assertEquals("doRoot", methodFound.getName());
    }
View Full Code Here

TOP

Related Classes of com.tinkerpop.rexster.extension.ExtensionMethod

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.