@Override
  public void handle(HttpClientResponse clientResponse) {
    delegate.handle(clientResponse);

    MultiMap headers = clientResponse.headers();
    for (String headerName : rewriteHeaders) {
      List<String> headerValues = headers.getAll(headerName);
      int size = headerValues.size();
      if (size > 0) {
        List<String> newHeaders = new ArrayList<String>(size);
        for (String headerValue : headerValues) {
          String newValue = headerValue;
          if (headerValue != null && headerValue.length() > 0) {
            newValue = proxyMappingDetails.rewriteBackendUrl(headerValue);
          }
        }
        LOG.info(
            "Rewriting header " + headerName + " from: " + headerValues + " to: " + newHeaders);
        headers.set(headerName, newHeaders);
      }
    }
  }
  @Override
  public void write(
      T object, MediaType contentType, HttpClientRequest httpClientRequest, boolean endRequest)
      throws HttpMessageConverterException {
    try {
      final MultiMap headers = httpClientRequest.headers();
      if (headers.get(HttpHeaders.CONTENT_TYPE) == null) {
        MediaType contentTypeToUse = contentType;
        if (contentType == null
            || contentType.isWildcardType()
            || contentType.isWildcardSubtype()) {
          contentTypeToUse = getDefaultContentType(object);
        }
        if (contentTypeToUse != null) {
          headers.set(HttpHeaders.CONTENT_TYPE, contentTypeToUse.toString());
        }
      }

      final byte[] buffer = writeInternal(object, httpClientRequest.headers());
      if (endRequest) {
        if (log.isDebugEnabled()) {
          log.debug("Request body: {}", new String(buffer, Charsets.UTF_8));
        }
        httpClientRequest.end(new Buffer(buffer));
      } else {
        if (log.isDebugEnabled()) {
          log.debug("Partial request body: {}", new String(buffer, Charsets.UTF_8));
        }
        httpClientRequest.write(new Buffer(buffer));
      }
    } catch (HttpMessageConverterException e) {
      throw e;
    } catch (Throwable t) {
      throw new HttpMessageConverterException("Error writing object", t);
    }
  }
Exemple #3
0
  private void route(
      final YokeRequest request, final PatternBinding binding, final Handler<Object> next) {
    final Matcher m = binding.pattern.matcher(request.path());
    final Vertx vertx = vertx();

    if (m.matches()) {
      final MultiMap params = request.params();

      if (binding.paramNames != null) {
        // Named params
        new AsyncIterator<String>(binding.paramNames) {
          @Override
          public void handle(String param) {
            if (hasNext()) {
              params.set(param, m.group(param));
              final Middleware paramMiddleware = paramProcessors.get(param);
              if (paramMiddleware != null) {
                // do not block main loop
                vertx.runOnContext(
                    new Handler<Void>() {
                      @Override
                      public void handle(Void event) {
                        paramMiddleware.handle(
                            request,
                            new Handler<Object>() {
                              @Override
                              public void handle(Object err) {
                                if (err == null) {
                                  next();
                                } else {
                                  next.handle(err);
                                }
                              }
                            });
                      }
                    });
              } else {
                next();
              }
            } else {
              // middlewares
              new AsyncIterator<Middleware>(binding.middleware) {
                @Override
                public void handle(final Middleware middleware) {
                  if (hasNext()) {
                    // do not block main loop
                    vertx.runOnContext(
                        new Handler<Void>() {
                          @Override
                          public void handle(Void event) {
                            middleware.handle(
                                request,
                                new Handler<Object>() {
                                  @Override
                                  public void handle(Object err) {
                                    if (err == null) {
                                      next();
                                    } else {
                                      next.handle(err);
                                    }
                                  }
                                });
                          }
                        });
                  } else {
                    next.handle(null);
                  }
                }
              };
            }
          }
        };
      } else {
        // Un-named params
        for (int i = 0; i < m.groupCount(); i++) {
          params.set("param" + i, m.group(i + 1));
        }

        // middlewares
        new AsyncIterator<Middleware>(binding.middleware) {
          @Override
          public void handle(final Middleware middleware) {
            if (hasNext()) {
              // do not block main loop
              vertx.runOnContext(
                  new Handler<Void>() {
                    @Override
                    public void handle(Void event) {
                      middleware.handle(
                          request,
                          new Handler<Object>() {
                            @Override
                            public void handle(Object err) {
                              if (err == null) {
                                next();
                              } else {
                                next.handle(err);
                              }
                            }
                          });
                    }
                  });
            } else {
              next.handle(null);
            }
          }
        };
      }
    } else {
      next.handle(null);
    }
  }