Example #1
0
  private MultiMap<String, String> paramMap(ServerWebSocket vertxServerWebSocket) {
    String query = vertxServerWebSocket.query();
    MultiMap<String, String> paramMap = MultiMap.empty();

    if (!Str.isEmpty(query)) {
      final String[] params = StringScanner.split(query, '&');

      if (params.length > 0) {
        paramMap = new MultiMapImpl<>();

        for (String param : params) {
          final String[] keyValue = StringScanner.split(param, '=');

          if (keyValue.length == 2) {

            String key = keyValue[0];
            String value = keyValue[1];
            try {
              key = URLDecoder.decode(key, "UTF-8");
              value = URLDecoder.decode(value, "UTF-8");
              paramMap.add(key, value);
            } catch (UnsupportedEncodingException e) {
              logger.warn(sputs("Unable to url decode key or value in param", key, value), e);
            }
          }
        }
      }
    }
    return paramMap;
  }
Example #2
0
  @Test
  public void test() throws Exception {

    final int openPortStartAt = PortUtils.findOpenPortStartAt(7777);
    HttpServer server = HttpServerBuilder.httpServerBuilder().setPort(openPortStartAt).build();

    server.setHttpRequestConsumer(
        serverRequest -> {
          final MultiMap<String, String> headers = MultiMap.multiMap();

          headers.add("foo", "bar").add("foo", "baz");
          serverRequest.getReceiver().response(200, "application/json", "true", headers);
        });

    server.startServer();

    HttpClient client =
        HttpClientBuilder.httpClientBuilder().setPort(openPortStartAt).setHost("localhost").build();

    client.start();

    final HttpTextResponse httpResponse = client.get("/hi");

    Sys.sleep(1000);
    puts(httpResponse.headers());

    boolean foundFoo = httpResponse.headers().keySet().contains("foo");
    assertTrue(foundFoo);
  }
 public void overridesFromParams() {
   if (params != null && params.size() > 0) {
     final String _addr = params.get(Protocol.ADDRESS_KEY);
     final String _objectName = params.get(Protocol.OBJECT_NAME_KEY);
     final String _methodName = params.get(Protocol.METHOD_NAME_KEY);
     final String _returnAddress = params.get(Protocol.RETURN_ADDRESS_KEY);
     this.address = _addr == null || _addr.isEmpty() ? address : _addr;
     this.returnAddress =
         _returnAddress == null || _returnAddress.isEmpty() ? returnAddress : _returnAddress;
     this.name = _methodName == null || _methodName.isEmpty() ? name : _methodName;
     this.objectName = _objectName == null || _objectName.isEmpty() ? objectName : _objectName;
   }
 }
Example #4
0
  public WebSocket createWebSocket(final ServerWebSocket vertxServerWebSocket) {

    final MultiMap<String, String> params = paramMap(vertxServerWebSocket);

    final MultiMap<String, String> headers =
        vertxServerWebSocket.headers().size() == 0
            ? MultiMap.empty()
            : new MultiMapWrapper(vertxServerWebSocket.headers());

    /* Create a websocket that uses vertxServerWebSocket to forwardEvent messages. */
    final WebSocket webSocket =
        webSocketBuilder()
            .setUri(vertxServerWebSocket.uri())
            .setRemoteAddress(vertxServerWebSocket.remoteAddress().toString())
            .setWebSocketSender(
                new WebSocketSender() {
                  @Override
                  public void sendText(String message) {
                    vertxServerWebSocket.writeFinalTextFrame(message);
                  }

                  @Override
                  public void sendBytes(byte[] message) {
                    vertxServerWebSocket.writeFinalBinaryFrame(Buffer.buffer(message));
                  }

                  @Override
                  public void close() {
                    vertxServerWebSocket.close();
                  }
                })
            .setHeaders(headers)
            .setParams(params)
            .build();

    /* Handle open. */
    webSocket.onOpen();

    /* Handle close. */
    vertxServerWebSocket.closeHandler(event -> webSocket.onClose());

    /* Handle message. */
    vertxServerWebSocket.handler(
        buffer -> {
          final String message = buffer.toString("UTF-8");
          webSocket.onTextMessage(message);
        });

    /* Handle error. */
    vertxServerWebSocket.exceptionHandler(
        event -> {
          if (event instanceof Exception) {
            webSocket.onError((Exception) event);
          } else {
            webSocket.onError(new Exception(event));
          }
        });

    return webSocket;
  }
Example #5
0
  private MultiMap<String, String> buildParams(
      final HttpServerRequest request, final String contentType) {

    if (HttpContentTypes.isFormContentType(contentType)) {

      if (request.params().size() == 0) {
        return new MultiMapWrapper(request.formAttributes());
      } else {
        MultiMap<String, String> newParams = new MultiMapImpl<>();

        request.formAttributes().forEach(entry -> newParams.add(entry.getKey(), entry.getValue()));

        request.params().forEach(entry -> newParams.add(entry.getKey(), entry.getValue()));
        return newParams;
      }
    } else {
      return request.params().size() == 0
          ? MultiMap.empty()
          : new MultiMapWrapper(request.params());
    }
  }
Example #6
0
  public HttpRequest createRequest(
      final HttpServerRequest request,
      final Buffer buffer,
      final CopyOnWriteArrayList<HttpResponseDecorator> decorators,
      final HttpResponseCreator httpResponseCreator) {

    final MultiMap<String, String> headers =
        request.headers().size() == 0 ? MultiMap.empty() : new MultiMapWrapper(request.headers());

    final String contentType = request.headers().get("Content-Type");

    final byte[] body =
        HttpContentTypes.isFormContentType(contentType) || buffer == null
            ? new byte[0]
            : buffer.getBytes();

    final MultiMap<String, String> params = buildParams(request, contentType);

    final HttpRequestBuilder httpRequestBuilder = HttpRequestBuilder.httpRequestBuilder();

    final String requestPath = request.path();

    httpRequestBuilder
        .setId(requestId.incrementAndGet())
        .setUri(requestPath)
        .setMethod(request.method().toString())
        .setParams(params)
        .setBodyBytes(body)
        .setRemoteAddress(request.remoteAddress().toString())
        .setResponse(
            createResponse(
                requestPath, headers, params, request.response(), decorators, httpResponseCreator))
        .setTimestamp(time == 0L ? Timer.timer().now() : time)
        .setHeaders(headers);

    return httpRequestBuilder.build();
  }
/**
 * This allows you to construct method calls. MethodCallBuilder created by rhightower on 1/16/15.
 */
public class MethodCallBuilder {

  private long timestamp;
  private long id = -1;
  private String name = "";
  private String address = "";
  private MultiMap<String, String> params = MultiMap.empty();
  private MultiMap<String, String> headers = MultiMap.empty();
  private Object body = Collections.emptyList();
  private String objectName;
  private String returnAddress;
  private Request<Object> originatingRequest;

  public static MethodCallBuilder methodCallBuilder() {
    return new MethodCallBuilder();
  }

  public static MethodCall<Object> transformed(
      final MethodCall<Object> methodCall, final Object arg) {

    return new MethodCallBuilder()
        .setTimestamp(methodCall.timestamp())
        .setId(methodCall.id())
        .setName(methodCall.name())
        .setAddress(methodCall.address())
        .setParams(methodCall.params())
        .setHeaders(methodCall.headers())
        .setBody(arg)
        .setObjectName(methodCall.objectName())
        .setReturnAddress(methodCall.returnAddress())
        .setOriginatingRequest(methodCall.originatingRequest())
        .build();
  }

  public static MethodCall<Object> createMethodCallToBeEncodedAndSent(
      long id,
      String address,
      String returnAddress,
      String objectName,
      String methodName,
      long timestamp,
      Object body,
      MultiMap<String, String> params) {

    return new MethodCallBuilder()
        .setId(id)
        .setAddress(address)
        .setReturnAddress(returnAddress)
        .setObjectName(objectName)
        .setName(methodName)
        .setTimestamp(timestamp)
        .setBody(body)
        .setParams(params)
        .build();
  }

  public static MethodCall<Object> methodWithArgs(String name, Object... args) {
    return new MethodCallBuilder().setName(name).setBody(args).build();
  }

  public static MethodCall<Object> method(String name, List body) {
    return new MethodCallBuilder().setName(name).setBody(body).build();
  }

  public static MethodCall<Object> method(String name, String body) {
    return new MethodCallBuilder().setName(name).setBody(body).build();
  }

  public static MethodCall<Object> method(String name, String address, String body) {
    return new MethodCallBuilder().setName(name).setBody(body).setAddress(address).build();
  }

  public long getTimestamp() {
    return timestamp;
  }

  public MethodCallBuilder setTimestamp(long timestamp) {
    this.timestamp = timestamp;
    return this;
  }

  public long getId() {
    return id;
  }

  public MethodCallBuilder setId(long id) {
    this.id = id;
    return this;
  }

  public String getName() {
    return name;
  }

  public MethodCallBuilder setName(String name) {
    this.name = name;
    return this;
  }

  public String getAddress() {
    return address;
  }

  public MethodCallBuilder setAddress(String address) {

    if (address == null) {
      this.address = "";
    } else {
      this.address = address;
    }
    return this;
  }

  public MultiMap<String, String> getParams() {
    return params;
  }

  public MethodCallBuilder setParams(MultiMap<String, String> params) {

    this.params = params;
    return this;
  }

  public MultiMap<String, String> getHeaders() {
    return headers;
  }

  public MethodCallBuilder setHeaders(MultiMap<String, String> headers) {
    this.headers = headers;
    return this;
  }

  public Object getBody() {
    return body;
  }

  public MethodCallBuilder setBody(Object body) {
    this.body = body;
    return this;
  }

  public String getObjectName() {
    return objectName;
  }

  public MethodCallBuilder setObjectName(String objectName) {
    this.objectName = objectName;
    return this;
  }

  public String getReturnAddress() {
    return returnAddress;
  }

  public MethodCallBuilder setReturnAddress(String returnAddress) {
    this.returnAddress = returnAddress;
    return this;
  }

  public Request<Object> getOriginatingRequest() {
    return originatingRequest;
  }

  public MethodCallBuilder setOriginatingRequest(Request<Object> originatingRequest) {
    this.originatingRequest = originatingRequest;
    return this;
  }

  public MethodCall<Object> build() {

    return new MethodCallImpl(
        getTimestamp(),
        getId(),
        getName(),
        getAddress(),
        getParams(),
        getHeaders(),
        getBody(),
        getObjectName(),
        getReturnAddress(),
        getOriginatingRequest());
  }

  public boolean hasParams() {
    return params != null && params.size() > 0;
  }

  public void overridesFromParams() {
    if (params != null && params.size() > 0) {
      final String _addr = params.get(Protocol.ADDRESS_KEY);
      final String _objectName = params.get(Protocol.OBJECT_NAME_KEY);
      final String _methodName = params.get(Protocol.METHOD_NAME_KEY);
      final String _returnAddress = params.get(Protocol.RETURN_ADDRESS_KEY);
      this.address = _addr == null || _addr.isEmpty() ? address : _addr;
      this.returnAddress =
          _returnAddress == null || _returnAddress.isEmpty() ? returnAddress : _returnAddress;
      this.name = _methodName == null || _methodName.isEmpty() ? name : _methodName;
      this.objectName = _objectName == null || _objectName.isEmpty() ? objectName : _objectName;
    }
  }
}
 public boolean hasParams() {
   return params != null && params.size() > 0;
 }