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; }
@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); }
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()); } }