Java Code Examples for io.vertx.core.streams.ReadStream#resume()

The following examples show how to use io.vertx.core.streams.ReadStream#resume() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ProxyService.java    From okapi with Apache License 2.0 5 votes vote down vote up
private void proxyStreamToBuffer(ReadStream<Buffer> stream, Buffer bcontent,
                                 Handler<Buffer> handle) {
  if (bcontent != null) {
    handle.handle(bcontent);
  } else {
    final Buffer incoming = Buffer.buffer();
    stream.handler(incoming::appendBuffer);
    stream.endHandler(v -> handle.handle(incoming));
    stream.resume();
  }
}
 
Example 2
Source File: ProxyService.java    From okapi with Apache License 2.0 4 votes vote down vote up
/**
 * Routing context hander (handling all requests for Okapi).
 * @param ctx routing context
 */
public void proxy(RoutingContext ctx) {
  ctx.request().pause();
  ReadStream<Buffer> stream = ctx.request();
  // Pause the request data stream before doing any slow ops, otherwise
  // it will get read into a buffer somewhere.

  ProxyContext pc = new ProxyContext(ctx, waitMs);

  // It would be nice to pass the request-id to the client, so it knows what
  // to look for in Okapi logs. But that breaks the schemas, and RMB-based
  // modules will not accept the response. Maybe later...
  String tenantId = tenantHeader(pc);
  if (tenantId == null) {
    stream.resume();
    return; // Error code already set in ctx
  }

  final MultiMap headers = ctx.request().headers();
  sanitizeAuthHeaders(headers);
  tenantManager.get(tenantId, gres -> {
    if (gres.failed()) {
      stream.resume();
      pc.responseError(400, messages.getMessage("10106", tenantId));
      return;
    }
    Tenant tenant = gres.result();
    moduleManager.getEnabledModules(tenant, mres -> {
      if (mres.failed()) {
        stream.resume();
        pc.responseError(mres.getType(), mres.cause());
        return;
      }
      List<ModuleDescriptor> enabledModules = mres.result();

      String metricKey = "proxy." + tenantId + "."
          + ctx.request().method() + "." + ctx.normalisedPath();
      DropwizardHelper.markEvent(metricKey);

      List<ModuleInstance> l = getModulesForRequest(pc, enabledModules);
      if (l == null) {
        stream.resume();
        return; // ctx already set up
      }

      // check delegate CORS and reroute if necessary
      if (CorsHelper.checkCorsDelegate(ctx, l)) {
        stream.resume();
        ctx.reroute(ctx.request().path());
        return;
      }

      pc.setModList(l);

      pc.logRequest(ctx, tenantId);

      headers.set(XOkapiHeaders.URL, okapiUrl);
      headers.remove(XOkapiHeaders.MODULE_ID);
      headers.set(XOkapiHeaders.REQUEST_IP, ctx.request().remoteAddress().host());
      headers.set(XOkapiHeaders.REQUEST_TIMESTAMP, "" + System.currentTimeMillis());
      headers.set(XOkapiHeaders.REQUEST_METHOD, ctx.request().rawMethod());

      resolveUrls(l.iterator(), res -> {
        if (res.failed()) {
          stream.resume();
          pc.responseError(res.getType(), res.cause());
        } else {
          List<HttpClientRequest> clientRequest = new LinkedList<>();
          proxyR(l.iterator(), pc, stream, null, clientRequest);
        }
      });
    });

  });
}
 
Example 3
Source File: ProxyService.java    From okapi with Apache License 2.0 4 votes vote down vote up
private void proxyR(Iterator<ModuleInstance> it,
                    ProxyContext pc, ReadStream<Buffer> stream, Buffer bcontent,
                    List<HttpClientRequest> clientRequestList) {

  RoutingContext ctx = pc.getCtx();
  if (!it.hasNext()) {
    stream.resume();
    pc.debug("proxyR: Not found");
    pc.responseError(404, ""); // Should have been caught earlier
  } else {
    ModuleInstance mi = it.next();
    String tenantId = ctx.request().getHeader(XOkapiHeaders.TENANT);
    if (tenantId == null || tenantId.isEmpty()) {
      tenantId = "???"; // Should not happen, we have validated earlier
    }
    String metricKey = "proxy." + tenantId
        + ".module." + mi.getModuleDescriptor().getId();
    pc.startTimer(metricKey);

    // Pass the right token
    ctx.request().headers().remove(XOkapiHeaders.TOKEN);
    String token = mi.getAuthToken();
    if (token != null && !token.isEmpty()) {
      ctx.request().headers().add(XOkapiHeaders.TOKEN, token);
    }

    // Pass headers for filters
    passFilterHeaders(ctx, pc, mi);

    // Do proxy work
    ProxyType proxyType = mi.getRoutingEntry().getProxyType();
    if (proxyType != ProxyType.REDIRECT) {
      pc.debug("Invoking module " + mi.getModuleDescriptor().getId()
          + " type " + proxyType
          + " level " + mi.getRoutingEntry().getPhaseLevel()
          + " path " + mi.getPath()
          + " url " + mi.getUrl());
    }
    final String pathPattern = mi.getRoutingEntry().getPathPattern();
    if (pathPattern != null) {
      ctx.request().headers().set(XOkapiHeaders.MATCH_PATH_PATTERN, pathPattern);
    }
    switch (proxyType) {
      case REQUEST_ONLY:
        proxyRequestOnly(it, pc, stream, bcontent, clientRequestList, mi);
        break;
      case REQUEST_RESPONSE:
        proxyRequestResponse(it, pc, stream, bcontent, clientRequestList, mi);
        break;
      case HEADERS:
        proxyHeaders(it, pc, stream, bcontent, clientRequestList, mi);
        break;
      case REDIRECT:
        proxyRedirect(it, pc, stream, bcontent, clientRequestList, mi);
        break;
      case INTERNAL:
        proxyInternal(it, pc, stream, bcontent, clientRequestList, mi);
        break;
      case REQUEST_RESPONSE_1_0:
        proxyRequestResponse10(it, pc, stream, bcontent, clientRequestList, mi);
        break;
      case REQUEST_LOG:
        proxyRequestLog(it, pc, stream, bcontent, clientRequestList, mi);
        break;
      default:
        // Should not happen
        pc.responseError(500, messages.getMessage("10110",
            proxyType, mi.getModuleDescriptor().getId()));
        break;
    }
  }
}