private String getEntityBodyFromRequest(Request<String> request) throws IOException {
    String contentTypeHeader = request.header("Content-Type");
    Charset charset = BrowserMobHttpUtil.deriveCharsetFromContentTypeHeader(contentTypeHeader);
    ByteArrayOutputStream entityBodyBytes = new ByteArrayOutputStream();
    request.readTo(entityBodyBytes);

    return new String(entityBodyBytes.toByteArray(), charset);
  }
Ejemplo n.º 2
0
  @Post
  @At("/:port/interceptor/request")
  public Reply<?> addRequestInterceptor(@Named("port") int port, Request<String> request)
      throws IOException, ScriptException {
    LegacyProxyServer proxy = proxyManager.get(port);
    if (proxy == null) {
      return Reply.saying().notFound();
    }

    if (!(proxy instanceof ProxyServer)) {
      return Reply.saying().badRequest();
    }

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    request.readTo(baos);

    ScriptEngineManager mgr = new ScriptEngineManager();
    final ScriptEngine engine = mgr.getEngineByName("JavaScript");
    Compilable compilable = (Compilable) engine;
    final CompiledScript script = compilable.compile(baos.toString());

    proxy.addRequestInterceptor(
        new RequestInterceptor() {
          @Override
          public void process(BrowserMobHttpRequest request, Har har) {
            Bindings bindings = engine.createBindings();
            bindings.put("request", request);
            bindings.put("har", har);
            bindings.put("log", LOG);
            try {
              script.eval(bindings);
            } catch (ScriptException e) {
              LOG.error("Could not execute JS-based response interceptor", e);
            }
          }
        });

    return Reply.saying().ok();
  }
Ejemplo n.º 3
0
  private String getEntityBodyFromRequest(Request<String> request) throws IOException {
    String contentTypeHeader = request.header("Content-Type");
    Charset charset = null;
    try {
      charset = BrowserMobHttpUtil.readCharsetInContentTypeHeader(contentTypeHeader);
    } catch (UnsupportedCharsetException e) {
      java.nio.charset.UnsupportedCharsetException cause = e.getUnsupportedCharsetExceptionCause();
      LOG.error(
          "Character set declared in Content-Type header is not supported. Content-Type header: {}",
          contentTypeHeader,
          cause);

      throw cause;
    }

    if (charset == null) {
      charset = BrowserMobHttpUtil.DEFAULT_HTTP_CHARSET;
    }

    ByteArrayOutputStream entityBodyBytes = new ByteArrayOutputStream();
    request.readTo(entityBodyBytes);

    return new String(entityBodyBytes.toByteArray(), charset);
  }