Examples of ExtensionResponse


Examples of com.tinkerpop.rexster.extension.ExtensionResponse

    public void tryAppendRexsterAttributesIfJsonValid() {
        HashMap map = new HashMap() {{
            put("me", "you");
        }};

        ExtensionResponse responseFromExtension = ExtensionResponse.ok(new JSONObject(map));

        final ExtensionDefinition extensionDefinition = this.mockery.mock(ExtensionDefinition.class);
        this.mockery.checking(new Expectations() {{
            allowing(extensionDefinition).tryIncludeRexsterAttributes();
            will(returnValue(true));
        }});

        ExtensionMethod extensionMethod = new ExtensionMethod(null, extensionDefinition, null, new MockRexsterExtension());
        ExtensionResponse extResponse = this.mockResource.tryAppendRexsterAttributesIfJsonExposed(
                responseFromExtension, extensionMethod, MediaType.APPLICATION_JSON);

        Assert.assertNotNull(extResponse);

        JSONObject jsonObjectWithAttributes = (JSONObject) extResponse.getJerseyResponse().getEntity();
        Assert.assertNotNull(jsonObjectWithAttributes);
        Assert.assertTrue(jsonObjectWithAttributes.has("me"));
        Assert.assertTrue(jsonObjectWithAttributes.has(Tokens.VERSION));
        Assert.assertTrue(jsonObjectWithAttributes.has(Tokens.QUERY_TIME));
    }
View Full Code Here

Examples of com.tinkerpop.rexster.extension.ExtensionResponse

        return this.executeGraphExtension(graphName, HttpMethod.GET);
    }

    private Response executeGraphExtension(final String graphName, final HttpMethod httpMethodRequested) {

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

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

        if (rag.isExtensionAllowed(extensionSegmentSet)) {

            Object returnValue = null;

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

                // look for the extension as loaded through serviceloader
                List<RexsterExtension> rexsterExtensions = null;
                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.GRAPH, 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);
                rag.tryCommit();

            } 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

Examples of com.tinkerpop.rexster.extension.ExtensionResponse

        String sparqlQuery = "SELECT ?x ?y WHERE { ?x <http://tinkerpop.com#knows> ?y }";

        this.ctx = new RexsterResourceContext(null, null, null, new JSONObject(), null, null, null, null);

        SparqlExtension extension = new SparqlExtension();
        ExtensionResponse extensionResponse = extension.evaluateSparql(this.ctx, this.graph, sparqlQuery);

        Assert.assertNotNull(extensionResponse);
        Assert.assertFalse(extensionResponse.isErrorResponse());

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

        JSONObject jsonResponseEntity = (JSONObject) jerseyResponse.getEntity();
        Assert.assertNotNull(jsonResponseEntity);
View Full Code Here

Examples of com.tinkerpop.rexster.extension.ExtensionResponse

        final MetricRegistry metricRegistry = rexsterResourceContext.getMetricRegistry();
        final Timer scriptTimer = metricRegistry.timer(MetricRegistry.name("http", "script-engine"));
        final Counter successfulExecutions = metricRegistry.counter(MetricRegistry.name("http", "script-engine", "success"));
        final Counter failedExecutions = metricRegistry.counter(MetricRegistry.name("http", "script-engine", "fail"));

        ExtensionResponse extensionResponse;

        final JSONObject requestObject = rexsterResourceContext.getRequestObject();

        // can't initialize this statically because the configure() method won't get called before it.
        // need to think a bit on how to best initialized the controller.
View Full Code Here

Examples of com.tinkerpop.rexster.extension.ExtensionResponse

     */
    @ExtensionDefinition(extensionPoint = ExtensionPoint.VERTEX, produces = MediaType.APPLICATION_XML)
    @ExtensionDescriptor(description = "returns standard properties of a vertex as XML.")
    public ExtensionResponse doVertexToXml(@RexsterContext Vertex vertex) {
        String xml = "<vertex><id>" + vertex.getId().toString() + "</id></vertex>";
        return new ExtensionResponse(Response.ok(xml).build());
    }
View Full Code Here

Examples of com.tinkerpop.rexster.extension.ExtensionResponse

     */
    @ExtensionDefinition(extensionPoint = ExtensionPoint.EDGE, produces = MediaType.APPLICATION_XML)
    @ExtensionDescriptor(description = "returns standard properties of an edge as XML.")
    public ExtensionResponse doEdgeToXml(@RexsterContext Edge edge) {
        String xml = "<edge><id>" + edge.getId().toString() + "</id><label>" + edge.getLabel() + "</label></edge>";
        return new ExtensionResponse(Response.ok(xml).build());
    }
View Full Code Here

Examples of com.tinkerpop.rexster.extension.ExtensionResponse

        if (element instanceof Edge && direction == null) {
            throw new IllegalArgumentException("Direction cannot be null");
        }

        ExtensionResponse extensionResponse;
        FramedGraph framedGraph = factory.create(graph);

        ExtensionConfiguration extensionConfig = rexsterResourceContext.getRexsterApplicationGraph()
                .findExtensionConfiguration(EXTENSION_NAMESPACE, EXTENSION_NAME);
        Map<String, String> mapFrames = extensionConfig.tryGetMapFromConfiguration();
View Full Code Here

Examples of com.tinkerpop.rexster.extension.ExtensionResponse

        final UriInfo uri = mockTheUri(false, "");

        // can do a slimmed down RexsterResourceContext
        this.ctx = new RexsterResourceContext(this.rag, uri, null, null, null, null, null, null);

        ExtensionResponse extResp = this.framesExtension.doFramesWorkOnVertex(this.ctx, this.graph, this.graph.getVertex(1));

        Assert.assertNotNull(extResp);
        Assert.assertNotNull(extResp.getJerseyResponse());

        Response response = extResp.getJerseyResponse();
        Assert.assertEquals(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), response.getStatus());

        JSONObject jsonObject = (JSONObject) response.getEntity();
        Assert.assertNotNull(jsonObject);
        Assert.assertTrue(jsonObject.has("success"));
View Full Code Here

Examples of com.tinkerpop.rexster.extension.ExtensionResponse

        final UriInfo uri = mockTheUri(true, "not-a-frame-in-config");

        // can do a slimmed down RexsterResourceContext
        this.ctx = new RexsterResourceContext(this.rag, uri, null, null, null, null, null, null);

        ExtensionResponse extResp = this.framesExtension.doFramesWorkOnVertex(this.ctx, this.graph, this.graph.getVertex(1));

        Assert.assertNotNull(extResp);
        Assert.assertNotNull(extResp.getJerseyResponse());

        Response response = extResp.getJerseyResponse();
        Assert.assertEquals(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), response.getStatus());

        JSONObject jsonObject = (JSONObject) response.getEntity();
        Assert.assertNotNull(jsonObject);
        Assert.assertTrue(jsonObject.has("success"));
View Full Code Here

Examples of com.tinkerpop.rexster.extension.ExtensionResponse

        final UriInfo uri = mockTheUri(true, "notreal");

        // can do a slimmed down RexsterResourceContext
        this.ctx = new RexsterResourceContext(this.rag, uri, null, null, null, null, null, null);

        ExtensionResponse extResp = this.framesExtension.doFramesWorkOnVertex(this.ctx, this.graph, this.graph.getVertex(1));

        Assert.assertNotNull(extResp);
        Assert.assertNotNull(extResp.getJerseyResponse());

        Response response = extResp.getJerseyResponse();
        Assert.assertEquals(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), response.getStatus());

        JSONObject jsonObject = (JSONObject) response.getEntity();
        Assert.assertNotNull(jsonObject);
        Assert.assertTrue(jsonObject.has("success"));
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. 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.