Package com.tinkerpop.rexster.extension

Examples of com.tinkerpop.rexster.extension.ExtensionResponse


    }

    @Test
    public void doEdgeToXml() {
        Edge e = this.graph.getEdge(11);
        ExtensionResponse extensionResponse = this.extension.doEdgeToXml(e);

        Assert.assertNotNull(extensionResponse);
        Response response = extensionResponse.getJerseyResponse();
        Assert.assertNotNull(response);
        Assert.assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());

        String xml = (String) response.getEntity();
        Assert.assertEquals("<edge><id>11</id><label>created</label></edge>", xml);
View Full Code Here


        this.graph = TinkerGraphFactory.createTinkerGraph();
    }

    @Test
    public void doWorkOnGraphValid() {
        ExtensionResponse response = this.simpleRootExtension.doWorkOnGraph(this.graph);

        doTests(response, "tinkergraph[vertices:6 edges:6]");
    }
View Full Code Here

        doTests(response, "tinkergraph[vertices:6 edges:6]");
    }

    @Test
    public void doWorkOnEdgeValid() {
        ExtensionResponse response = this.simpleRootExtension.doWorkOnEdge(this.graph.getEdge(11));

        doTests(response, "e[11][4-created->3]");
    }
View Full Code Here

        doTests(response, "e[11][4-created->3]");
    }

    @Test
    public void doWorkOnVertexValid() {
        ExtensionResponse response = this.simpleRootExtension.doWorkOnVertex(this.graph.getVertex(1));

        doTests(response, "v[1]");
    }
View Full Code Here

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

        final Edge edge = this.getRexsterApplicationGraph(graphName).getGraph().getEdge(id);

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

        // 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.EDGE, 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, edge);

            } 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.");
                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 evaluateGetOnGraphNoScript() {

        ExtensionResponse extensionResponse = this.gremlinExtension.evaluateGetOnGraph(
                rexsterResourceContext, graph, "");

        JSONObject jsonResponse = assertResponseAndGetEntity(extensionResponse,
                true,
                Response.Status.BAD_REQUEST.getStatusCode());
View Full Code Here

        Assert.assertEquals("no scripts provided", jsonResponse.optString(Tokens.MESSAGE));
    }

    @Test
    public void evaluateGetOnGraphNoKeysNoTypesReturnVertex() {
        ExtensionResponse extensionResponse = this.gremlinExtension.evaluateGetOnGraph(
                rexsterResourceContext, graph, "g.v(1)");
        JSONObject jsonResponse = assertResponseAndGetEntity(extensionResponse,
                Response.Status.OK.getStatusCode());

        Assert.assertNotNull(jsonResponse);
View Full Code Here

    public void evaluateGetOnGraphWithBindings() throws Exception {
        String json = "{\"params\":{\"x\":1, \"y\":2, \"z\":\"test\", \"list\":[3,2,1,0], \"map\":{\"mapx\":[300,200,100]}}}";
        RexsterResourceContext rexsterResourceContext = new RexsterResourceContext(null, uriInfo,
                httpServletRequest, new JSONObject(new JSONTokener(json)), null, extensionMethodNoApi, null, new MetricRegistry());

        ExtensionResponse extensionResponse = this.gremlinExtension.evaluateGetOnGraph(
                rexsterResourceContext, graph, "[x+y, z, list.size, map.mapx.size]");
        JSONObject jsonResponse = assertResponseAndGetEntity(extensionResponse,
                Response.Status.OK.getStatusCode());

        Assert.assertNotNull(jsonResponse);
View Full Code Here

    }

    @Test
    public void evaluateGetOnVertexNoKeysNoTypesReturnOutEdges() {
        ExtensionResponse extensionResponse = this.gremlinExtension.evaluateGetOnVertex(
                rexsterResourceContext, graph, graph.getVertex(6), "v.outEdges");
        JSONObject jsonResponse = assertResponseAndGetEntity(extensionResponse,
                Response.Status.OK.getStatusCode());

        Assert.assertNotNull(jsonResponse);
View Full Code Here

        Assert.assertEquals(1, jsonResults.length());
    }

    @Test
    public void evaluateGetOnEdgeNoKeysNoTypesReturnOutVertex() {
        ExtensionResponse extensionResponse = this.gremlinExtension.evaluateGetOnEdge(
                rexsterResourceContext, graph, graph.getEdge(7), "e.outVertex");
        JSONObject jsonResponse = assertResponseAndGetEntity(extensionResponse,
                Response.Status.OK.getStatusCode());

        Assert.assertNotNull(jsonResponse);
View Full Code Here

TOP

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

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.