예제 #1
0
  public ResourceState autocreateAce(ResourceResponse createdResourceResponse) {
    ResourceState createdEntries = new DefaultResourceState("createdEntries");

    ResourcePath parentResourcePath = createdResourceResponse.inReplyTo().resourcePath();
    String parentResourceURI = parentResourcePath.toString();
    String createdResourceURI = parentResourceURI + "/" + createdResourceResponse.resource().id();

    List<AutoRuleConfig> autoRules = this.policyConfig.get().getAutoRules();
    autoRules
        .stream()
        .filter(
            (autoRule) -> {
              // We want exact matching (like "/storage/todos"), no support for wildcards for now
              return autoRule.getResourcePath().equals(parentResourceURI);
            })
        .forEach(
            (autoRule) -> {
              ResourceState createdAceState =
                  createACE(
                      createdResourceURI,
                      createdResourceResponse.inReplyTo().requestContext().securityContext(),
                      autoRule);
              createdEntries.addMember(createdAceState);
            });

    return createdEntries;
  }
  @Override
  public void onInbound(InboundInterceptorContext context) throws Exception {
    if (context.request().requestType() == RequestType.CREATE) {
      ResourceResponse response =
          new DefaultResourceResponse(
              context.request(),
              ResourceResponse.ResponseType.CREATED,
              new Resource() {

                @Override
                public Resource parent() {
                  return null;
                }

                @Override
                public String id() {
                  return "mock";
                }
              });

      ResourceState state = new DefaultResourceState();
      state.putProperty("requestCounter", requestCounter.get());
      state.putProperty("responseCounter", responseCounter.get());
      response.setState(state);

      context.replyWith(response);
    } else {
      requestCounter.incrementAndGet();
      super.onInbound(context);
    }
  }
예제 #3
0
  public Object executeScripts(ResourceResponse response) throws Exception {

    String resourcePath = response.inReplyTo().resourcePath().toString();
    ResourceResponse.ResponseType type = response.responseType();

    ResourceTriggeredScript.FUNCTIONS resourceFunction = ResourceTriggeredScript.FUNCTIONS.POSTREAD;
    switch (type) {
      case CREATED:
        resourceFunction = ResourceTriggeredScript.FUNCTIONS.POSTCREATE;
        break;
      case READ:
        resourceFunction = ResourceTriggeredScript.FUNCTIONS.POSTREAD;
        break;
      case UPDATED:
        resourceFunction = ResourceTriggeredScript.FUNCTIONS.POSTUPDATE;
        break;
      case DELETED:
        resourceFunction = ResourceTriggeredScript.FUNCTIONS.POSTDELETE;
        break;
      case ERROR:
        resourceFunction = ResourceTriggeredScript.FUNCTIONS.ONERROR;
        break;
    }

    Set<ResourceTriggeredScript> scripts =
        scriptRegistry.getByTarget(resourcePath, resourceFunction, true);

    // CREATE is special since we apply the create to /foo/bar to create /foo/bar/baz so we should
    // also check /foo/bar/*
    if (resourceFunction == ResourceTriggeredScript.FUNCTIONS.POSTCREATE) {
      scripts.addAll(scriptRegistry.getByPath(resourcePath + "/*", resourceFunction, true));
    }

    for (ResourceTriggeredScript script : scripts) {
      Object reply = runScript(resourceFunction.getFunctionName(), script, response);
      if (reply != null) {
        return reply;
      }
    }

    if (resourceFunction == ResourceTriggeredScript.FUNCTIONS.POSTREAD) {
      // we are on a post read, which means expand could have been used to return expanded members

      // check if the members were set to be expanded or not
      Object handleMembers =
          handleMembers(
              response, response.state(), response.inReplyTo().requestContext().returnFields());
      if (handleMembers != null) {
        return handleMembers;
      }
    }

    return null;
  }
 @Override
 public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise)
     throws Exception {
   if (msg instanceof ResourceResponse) {
     ResourceResponse response = (ResourceResponse) msg;
     if (msg instanceof ResourceErrorResponse) {
       ClientResourceResponse.ResponseType responseType =
           decodeResponseType(((ResourceErrorResponse) msg).errorType());
       ctx.writeAndFlush(
           new ClientResourceResponseImpl(
               response.inReplyTo(),
               responseType,
               response.inReplyTo().resourcePath().toString(),
               ((ResourceErrorResponse) msg).state()));
     } else {
       encode(ctx, response);
     }
   } else {
     super.write(ctx, msg, promise);
   }
 }
예제 #5
0
  public ResourceState deleteAce(ResourceResponse deletedResourceResponse) {
    ResourceState deletedEntries = new DefaultResourceState("deletedEntries");

    ResourcePath deletedResourcePath = deletedResourceResponse.inReplyTo().resourcePath();

    // Delete all ACE entries for this resource
    DBObject query = new BasicDBObject();
    query.put(ACE_RESOURCE_PATH, deletedResourcePath.toString());
    this.aclCollection.remove(query);

    log.debugf("Deleted ACEs for path: %s", deletedResourcePath);
    return deletedEntries;
  }
예제 #6
0
  protected Object runScript(
      String functionName, ResourceTriggeredScript script, ResourceResponse resourceResponse)
      throws Exception {

    Object response =
        runScript(
            functionName,
            script,
            new LiveOakResourceResponse(resourceResponse),
            getLibrary(script));

    ScriptingResourceRequest request = new ScriptingResourceRequest(resourceResponse.inReplyTo());
    return handleResponse(response, request);
  }
  /**
   * Encode (for some cheap value of 'encode') a resulting resource into a ResourceState.
   *
   * @param ctx
   * @param response The response to encode.
   * @throws Exception
   */
  protected void encode(ChannelHandlerContext ctx, ResourceResponse response) {
    final ClientResourceResponse.ResponseType responseType = ClientResourceResponse.ResponseType.OK;
    if (response.resource() == null) {
      ctx.writeAndFlush(
          new ClientResourceResponseImpl(
              response.inReplyTo(),
              responseType,
              response.inReplyTo().resourcePath().toString(),
              null));
      ctx.fireUserEventTriggered(new RequestCompleteEvent(response.requestId()));
      return;
    }

    ctx.writeAndFlush(
        new ClientResourceResponseImpl(
            response.inReplyTo(),
            responseType,
            response.inReplyTo().resourcePath().toString(),
            response.state()));
    ctx.fireUserEventTriggered(new RequestCompleteEvent(response.requestId()));
  }
예제 #8
0
  protected Object handleMembers(
      ResourceResponse response, ResourceState state, ReturnFields returnFields) throws Exception {
    if (!returnFields.child(LiveOak.MEMBERS).isEmpty()) {
      for (ResourceState memberState : state.members()) {
        DefaultResourceResponse memberResponse =
            new DefaultResourceResponse(
                response.inReplyTo(),
                ResourceResponse.ResponseType.READ,
                new Resource() {
                  @Override
                  public Resource parent() {
                    return response.resource();
                  }

                  @Override
                  public String id() {
                    return memberState.id();
                  }
                });
        memberResponse.setState(memberState);

        Set<ResourceTriggeredScript> memberScripts =
            scriptRegistry.getByTarget(
                memberState.uri().toString(), ResourceTriggeredScript.FUNCTIONS.POSTREAD, true);
        for (ResourceTriggeredScript memberScript : memberScripts) {
          Object reply =
              runScript(
                  ResourceTriggeredScript.FUNCTIONS.POSTREAD.getFunctionName(),
                  memberScript,
                  memberResponse);
          if (reply != null) {
            return reply;
          }
        }

        handleMembers(response, memberState, returnFields.child(LiveOak.MEMBERS));
      }
    }
    return null;
  }
예제 #9
0
  @Test
  public void testCorrelationWithRequestReplacement() throws Exception {

    List<Interceptor> interceptors = new ArrayList<>();

    MockInterceptor interceptor1 = new MockInterceptor();
    DefaultInterceptor interceptor2 =
        new DefaultInterceptor() {
          @Override
          public void onInbound(InboundInterceptorContext context) throws Exception {
            ResourceRequest replacement =
                new DefaultResourceRequest.Builder(context.request()).build();
            context.forward(replacement);
          }

          @Override
          public void onOutbound(OutboundInterceptorContext context) throws Exception {
            ResourceResponse replacement =
                new DefaultResourceResponse(context.request(), ResourceResponse.ResponseType.ERROR);
            context.forward(replacement);
          }
        };
    MockInterceptor interceptor3 = new MockInterceptor();

    interceptors.add(interceptor1);
    interceptors.add(interceptor2);
    interceptors.add(interceptor3);

    EmbeddedChannel channel =
        new EmbeddedChannel(
            new ChannelDuplexHandler() {
              @Override
              public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise)
                  throws Exception {
                InterceptorChain chain =
                    new InterceptorChain(ctx, interceptors, (ResourceResponse) msg);
                chain.fireOutbound();
              }

              @Override
              public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                InterceptorChain chain =
                    new InterceptorChain(ctx, interceptors, (ResourceRequest) msg);
                chain.fireInbound();
              }
            });

    ResourceRequest request =
        new DefaultResourceRequest.Builder(RequestType.READ, new ResourcePath("/foo/bar")).build();
    channel.writeInbound(request);
    ResourceRequest endRequest = (ResourceRequest) channel.readInbound();

    assertThat(request).isNotSameAs(endRequest);
    assertThat(request.requestId()).isEqualTo(endRequest.requestId());

    ResourceResponse response =
        new DefaultResourceResponse(endRequest, ResourceResponse.ResponseType.READ);
    channel.writeAndFlush(response);
    ResourceResponse endResponse = (ResourceResponse) channel.readOutbound();

    assertThat(response).isNotSameAs(endResponse);
    assertThat(response.inReplyTo()).isNotSameAs(request);
    assertThat(response.requestId()).isEqualTo(request.requestId());
  }