Package com.tinkerpop.rexster.extension

Examples of com.tinkerpop.rexster.extension.ExtensionMethod


    @ExtensionDescriptor(description = "pass a string parameter to be used in the response.")
    public ExtensionResponse evaluateSomeString(@RexsterContext RexsterResourceContext context,
                                                @RexsterContext Graph graph,
                                                @ExtensionRequestParameter(name = "some-string", description = "a string to reply with") String reply) {
        if (reply == null || reply.isEmpty()) {
            ExtensionMethod extMethod = context.getExtensionMethod();
            return ExtensionResponse.error(
                    "the some-string parameter cannot be empty",
                    null,
                    Response.Status.BAD_REQUEST.getStatusCode(),
                    null,
                    generateErrorJson(extMethod.getExtensionApiAsJson()));
        }

        Map<String, String> map = new HashMap<String, String>();
        map.put("some-string", reply);
        return ExtensionResponse.ok(map);
View Full Code Here


    @ExtensionDescriptor(description = "pass an integer parameter to be used in the response.")
    public ExtensionResponse evaluateSomeInteger(@RexsterContext RexsterResourceContext context,
                                                 @RexsterContext Graph graph,
                                                 @ExtensionRequestParameter(name = "some-integer", description = "an integer to reply with") Integer reply) {
        if (reply == null) {
            ExtensionMethod extMethod = context.getExtensionMethod();
            return ExtensionResponse.error(
                    "the some-integer parameter cannot be empty",
                    null,
                    Response.Status.BAD_REQUEST.getStatusCode(),
                    null,
                    generateErrorJson(extMethod.getExtensionApiAsJson()));
        }

        Map<String, Integer> map = new HashMap<String, Integer>();
        map.put("some-integer", reply.intValue());
        return ExtensionResponse.ok(map);
View Full Code Here

    @ExtensionDescriptor(description = "pass a list parameter to be used in the response.")
    public ExtensionResponse evaluateSomeList(@RexsterContext RexsterResourceContext context,
                                              @RexsterContext Graph graph,
                                              @ExtensionRequestParameter(name = "some-list", description = "a list to reply with") JSONArray reply) {
        if (reply == null) {
            ExtensionMethod extMethod = context.getExtensionMethod();
            return ExtensionResponse.error(
                    "the some-integer parameter cannot be empty",
                    null,
                    Response.Status.BAD_REQUEST.getStatusCode(),
                    null,
                    generateErrorJson(extMethod.getExtensionApiAsJson()));
        }

        Map<String, JSONArray> map = new HashMap<String, JSONArray>();
        map.put("some-list", reply);
        return ExtensionResponse.ok(map);
View Full Code Here

    @ExtensionDescriptor(description = "pass a square bracket enclosed string parameter to be used in the response.")
    public ExtensionResponse evaluateSomeListRaw(@RexsterContext RexsterResourceContext context,
                                                 @RexsterContext Graph graph,
                                                 @ExtensionRequestParameter(name = "some-list", description = "a list to reply with", parseToJson = false) String reply) {
        if (reply == null || reply.isEmpty()) {
            ExtensionMethod extMethod = context.getExtensionMethod();
            return ExtensionResponse.error(
                    "the some-list parameter cannot be empty",
                    null,
                    Response.Status.BAD_REQUEST.getStatusCode(),
                    null,
                    generateErrorJson(extMethod.getExtensionApiAsJson()));
        }

        Map<String, String> map = new HashMap<String, String>();
        map.put("some-list", reply);
        return ExtensionResponse.ok(map);
View Full Code Here

            if (directionString.equals(TOKEN_IN)) {
                direction = Direction.IN;
            } else if (directionString.equals(TOKEN_OUT)) {
                direction = Direction.OUT;
            } else {
                ExtensionMethod extMethod = rexsterResourceContext.getExtensionMethod();
                return ExtensionResponse.error(
                        "the direction parameter must be \"" + TOKEN_BOTH + "\", \"" + TOKEN_IN + "\", or \"" + TOKEN_OUT + "\"",
                        null,
                        Response.Status.BAD_REQUEST.getStatusCode(),
                        null,
                        generateErrorJson(extMethod.getExtensionApiAsJson()));
            }
        }

        return this.frameItUp(rexsterResourceContext, graph, edge, direction);
    }
View Full Code Here

    @Test
    public void doFramesWorkOnEdgeBadDirection() {
        final UriInfo uri = mockTheUri(true, "created");

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

        ExtensionResponse extResp = this.framesExtension.doFramesWorkOnEdge(this.ctx, this.graph, this.graph.getEdge(11), "bad-direction");

        Assert.assertNotNull(extResp);
        Assert.assertNotNull(extResp.getJerseyResponse());
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

     */
    protected static ExtensionMethod findExtensionMethod(final List<RexsterExtension> rexsterExtensions,
                                                         final ExtensionPoint extensionPoint,
                                                         final String extensionAction,
                                                         final HttpMethod httpMethodRequested) {
        ExtensionMethod methodToCall = null;
        for (RexsterExtension rexsterExtension : rexsterExtensions) {
            final Class rexsterExtensionClass = rexsterExtension.getClass();
            final Method[] methods = rexsterExtensionClass.getMethods();

            for (Method method : methods) {
                // looks for the first method that matches.  methods that multi-match will be ignored right now
                final ExtensionDefinition extensionDefinition = method.getAnnotation(ExtensionDefinition.class);
                final ExtensionDescriptor extensionDescriptor = method.getAnnotation(ExtensionDescriptor.class);

                // checks if the extension point is graph, and if the method path matches the specified action on
                // the uri (if it exists) or if the method has no path.
                if (extensionDefinition != null && extensionDefinition.extensionPoint() == extensionPoint
                        && (extensionDefinition.method() == HttpMethod.ANY || extensionDefinition.method() == httpMethodRequested)) {

                    if ((!extensionAction.isEmpty() && extensionDefinition.path().equals(extensionAction))
                            || (extensionAction.isEmpty() && extensionDefinition.path().isEmpty())) {
                        methodToCall = new ExtensionMethod(method, extensionDefinition, extensionDescriptor, rexsterExtension);
                        break;
                    }
                }
            }

            if (methodToCall == null) {
                for (Method method : methods) {
                    final ExtensionDefinition extensionDefinition = method.getAnnotation(ExtensionDefinition.class);
                    final ExtensionDescriptor extensionDescriptor = method.getAnnotation(ExtensionDescriptor.class);

                    if (extensionDefinition != null && extensionDefinition.extensionPoint() == extensionPoint
                            && (extensionDefinition.method() == HttpMethod.ANY || extensionDefinition.method() == httpMethodRequested)) {

                        if (!extensionAction.isEmpty() && extensionDefinition.path().isEmpty()) {
                            methodToCall = new ExtensionMethod(method, extensionDefinition, extensionDescriptor, rexsterExtension);
                            break;
                        }
                    }
                }
            }
View Full Code Here

  public ExtensionResponse handlePost(Graph graph,
    RexsterResourceContext context, int type) {

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

    try {
      vlabel = tx.getString(VLABEL_KEY);
      if (tx.has(DELAY_KEY)) {
        backoffDelay = tx.getInt(DELAY_KEY);
      }
      if (tx.has(RETRY_KEY)) {
        backoffRetry = tx.getInt(RETRY_KEY);
      }
      JSONArray array = tx.optJSONArray(TX_KEY);

      for (int i = 0; i < array.length(); i++) {
        JSONObject element = array.optJSONObject(i);
        createElement(element, graph, type);
      }

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

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

    } catch (IllegalArgumentException iae) {
      logger.error(iae);
      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

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.