public static JsonObject call(HttpServerRequest httpServerRequest) { JsonObject jsonObject = new JsonObject(); String query = httpServerRequest.query(); String requestLine = String.format("%s %s%s %s", httpServerRequest.method(), httpServerRequest.path(), query != null ? '?' + query : "", httpServerRequest.version().toString()); jsonObject.put("request_line", requestLine); MultiMap headers = httpServerRequest.headers(); JsonArray jsonHeaders = new JsonArray(); for (String headerName : headers.names()) { List<String> values = headers.getAll(headerName); JsonObject jsonHeader = new JsonObject(); jsonHeader.put(headerName, values); jsonHeaders.add(jsonHeader); } jsonObject.put("headers", jsonHeaders); return jsonObject; }
@Override public String getFormattedElement(AccessLogParam accessLogParam) { HttpServerResponse response = accessLogParam.getRoutingContext().response(); if (null == response) { return RESULT_NOT_FOUND; } MultiMap headers = response.headers(); if (null == headers) { return RESULT_NOT_FOUND; } String result = headers.get(identifier); if (null == result) { return RESULT_NOT_FOUND; } return result; }
/** * Default constructor. * @param params {@link io.vertx.core.http.HttpServerRequest} parameters * required for rendering an image region. * @param omeroSessionKey OMERO session key. */ TileCtx(MultiMap params, String omeroSessionKey) { this.omeroSessionKey = omeroSessionKey; imageId = Long.parseLong(params.get("imageId")); z = Integer.parseInt(params.get("z")); c = Integer.parseInt(params.get("c")); t = Integer.parseInt(params.get("t")); Integer x = Optional.ofNullable(params.get("x")) .map(Integer::parseInt) .orElse(0); Integer y = Optional.ofNullable(params.get("y")) .map(Integer::parseInt) .orElse(0); Integer width = Optional.ofNullable(params.get("w")) .map(Integer::parseInt) .orElse(0); Integer height = Optional.ofNullable(params.get("h")) .map(Integer::parseInt) .orElse(0); region = new RegionDef(x, y, width, height); resolution = Optional.ofNullable(params.get("resolution")) .map(Integer::parseInt) .orElse(null); format = params.get("format"); }
@Override public MultiMap set(final String name, final Iterable<String> values) { Objects.requireNonNull(values, "values"); int h = hash(name); int i = index(h); remove0(h, i, name); for (String v : values) { if (v == null) { break; } add0(h, i, name, v); } return this; }
public static void in(final IpcData data, final Envelop envelop) { if (null != envelop) { // User final User user = envelop.user(); final JsonObject sendData = new JsonObject(); if (null != user) { sendData.put("user", user.principal()); } // Header final MultiMap headers = envelop.headers(); if (null != headers) { final JsonObject headerData = new JsonObject(); headers.forEach((entry) -> headerData.put(entry.getKey(), entry.getValue())); sendData.put("header", headerData); } // Data final Object content = envelop.data(); sendData.put("data", content); sendData.put("config", data.getConfig()); // Data Prepared finished. sendData.put("address", data.getAddress()); data.setData(sendData.toBuffer()); } }
@Override public SfsRequest call(SfsRequest httpServerRequest) { MultiMap headers = httpServerRequest.headers(); String value = headers.get(headerName); if (value != null) { boolean failed = false; BaseEncoding baseEncoding = base64(); try { byte[] decoded = baseEncoding.decode(value); if (decoded == null || !value.equals(baseEncoding.encode(decoded))) { failed = true; } } catch (Throwable e) { // do nothing } if (failed) { JsonObject jsonObject = new JsonObject() .put("message", format("%s must be Base64 encoded", headerName)); throw new HttpRequestValidationException(HTTP_BAD_REQUEST, jsonObject); } } return httpServerRequest; }
@Test() public void testArrayMulti() { String name = "myName"; SerializableParameter parameter = new QueryParameter(); parameter.setName("myName"); parameter.setType("array"); parameter.setCollectionFormat("multi"); List<String> paramsList = new ArrayList<>(); paramsList.add("toto"); paramsList.add("tutu"); paramsList.add("titi"); MultiMap params = MultiMap.caseInsensitiveMultiMap(); params.add(name, paramsList); Object result = abstractSerializableParameterExtractor.extract(name, parameter, params); Assert.assertArrayEquals(paramsList.toArray(), ((List) result).toArray()); }
@Override public SfsRequest call(SfsRequest httpServerRequest) { MultiMap headers = httpServerRequest.headers(); String value = headers.get(headerName); if (value != null) { Long parsed = tryParse(value); if (parsed == null || parsed < min || parsed > max) { JsonObject jsonObject = new JsonObject() .put("message", format("%s must be between %d and %d", headerName, min, max)); throw new HttpRequestValidationException(HTTP_BAD_REQUEST, jsonObject); } } return httpServerRequest; }
@Override public HttpClientResponse call(HttpClientResponse httpClientResponse) { if (LOGGER.isDebugEnabled()) { StringBuilder sb = new StringBuilder(); sb.append("\r\nHttp Header Dump >>>>>\r\n\r\n"); sb.append(format("HTTP/1.1 %d %s\r\n", httpClientResponse.statusCode(), httpClientResponse.statusMessage())); MultiMap headers = httpClientResponse.headers(); for (String headerName : headers.names()) { List<String> values = headers.getAll(headerName); sb.append(format("%s: %s\r\n", headerName, on(',').join(values))); } sb.append("\r\n"); sb.append("Http Header Dump <<<<<\r\n"); LOGGER.debug(sb.toString()); } return httpClientResponse; }
/** * Return the query parameters which do not include the the first & or ? character. * * @return Query string */ default String getQueryParameters() { StringBuilder query = new StringBuilder(); MultiMap params = getParameters(); for (Entry<String, String> entry : params.entries()) { String value = entry.getValue(); if (value != null) { if (query.length() != 0) { query.append("&"); } // try { query.append(entry.getKey() + "=" + value);// URLEncoder.encode(value, "UTF-8")); // } catch (UnsupportedEncodingException e) { // } } } return query.toString(); }
@Override public T call(T httpServerRequest) { if (LOGGER.isDebugEnabled()) { StringBuilder sb = new StringBuilder(); sb.append("\r\nHttp Header Dump >>>>>\r\n\r\n"); String query = httpServerRequest.query(); sb.append(format("%s %s%s %s\r\n", httpServerRequest.method(), httpServerRequest.path(), query != null ? '?' + query : "", httpServerRequest.version().toString())); MultiMap headers = httpServerRequest.headers(); for (String headerName : headers.names()) { List<String> values = headers.getAll(headerName); sb.append(format("%s: %s\r\n", headerName, on(',').join(values))); } sb.append("\r\n"); sb.append("Http Header Dump <<<<<\r\n"); LOGGER.debug(sb.toString()); } return httpServerRequest; }
@Test public void testRequestBegin() { SocketAddress socketMetric = mock(SocketAddress.class); SocketAddress localAddress = mock(SocketAddress.class); SocketAddress remoteAddress = mock(SocketAddress.class); HttpClientRequest request = mock(HttpClientRequest.class); MultiMap headers = mock(MultiMap.class); when(headers.get(eq(Tags.TRACK_HEADER.toString()))).thenReturn("tag"); when(request.headers()).thenReturn(headers); HttpRequestMetrics metrics = victim.requestBegin(null, socketMetric, localAddress, remoteAddress, request); assertNotNull(metrics); assertEquals(remoteAddress, metrics.getAddress()); }
@Override public HttpClientResponse call(HttpClientResponse httpClientResponse) { out.println("Assert #" + assertIndex); MultiMap headers = httpClientResponse.headers(); String etag = headers.get(ETAG); String contentMd5 = headers.get(CONTENT_MD5); String contentSha512 = headers.get(X_CONTENT_SHA512); String versionId = headers.get(X_CONTENT_VERSION); String contentLength = headers.get(CONTENT_LENGTH); String acceptRanges = headers.get(ACCEPT_RANGES); String lastModified = headers.get(LAST_MODIFIED); String date = headers.get(DATE); String serverSideEncryption = headers.get(X_SERVER_SIDE_ENCRYPTION); assertEquals(context, base16().lowerCase().encode(dataMd5), etag); assertEquals(context, base64().encode(dataMd5), contentMd5); assertEquals(context, base64().encode(dataSha512), contentSha512); assertEquals(context, expectedVersion, parseLong(versionId)); //VertxAssert.assertEquals(context, expectedContentLength, Long.parseLong(contentLength)); assertEquals(context, "none", acceptRanges); assertNotNull(context, lastModified); assertNotNull(context, date); assertEquals(context, this.serverSideEncryption, parseBoolean(serverSideEncryption)); return httpClientResponse; }
public Observable<Void> execute(VertxContext<Server> vertxContext, String jobId, MultiMap parameters) { return Defer.aVoid() .doOnNext(aVoid -> checkMaster(vertxContext)) .map(aVoid -> getJob(jobId)) .map(this::newInstance) .flatMap(job -> Observable.using( () -> runningJobs.putIfAbsent(jobId, job) == null, running -> { if (running) { return job.execute(vertxContext, parameters); } else { return Observable.error(new JobAlreadyRunning()); } }, running -> { if (running) { runningJobs.remove(jobId); } })); }
@Override public HttpServerResponse push(HttpMethod method, String host, String path, MultiMap headers, Handler<AsyncResult<HttpServerResponse>> handler) { ObservableFuture<Void> h = RxHelper.observableFuture(); stopKeepAlive(h); h.subscribe(new Subscriber<Void>() { @Override public void onCompleted() { delegate.push(method, host, path, headers, handler); } @Override public void onError(Throwable e) { handleThrowable(e); } @Override public void onNext(Void aVoid) { } }); return this; }
@Override public Map<String, String[]> getParameterMap() { Map<String, String[]> paramMap = new HashMap<>(); MultiMap map = this.vertxRequest.params(); for (String name : map.names()) { List<String> valueList = map.getAll(name); paramMap.put(name, (String[]) map.getAll(name).toArray(new String[valueList.size()])); } return paramMap; }
@Test public void testGetHeader() { MultiMap headers = MultiMap.caseInsensitiveMultiMap(); headers.add("name", "value"); new Expectations() { { clientRequest.headers(); result = headers; } }; Assert.assertEquals("value", request.getHeader("name")); }
@Test public void testGetHeaders() { MultiMap headers = MultiMap.caseInsensitiveMultiMap(); headers.add("name", "value"); new Expectations() { { clientRequest.headers(); result = headers; } }; Assert.assertThat(Collections.list(request.getHeaders("name")), Matchers.contains("value")); }
@Test public void testGetHeaderNames() { MultiMap headers = MultiMap.caseInsensitiveMultiMap(); headers.add("name", "value"); new Expectations() { { clientRequest.headers(); result = headers; } }; Assert.assertThat(Collections.list(request.getHeaderNames()), Matchers.contains("name")); }
@Test public void testSetHeader() { MultiMap headers = MultiMap.caseInsensitiveMultiMap(); new Expectations() { { clientRequest.headers(); result = headers; } }; request.setHeader("name", "v1"); request.setHeader("name", "v2"); Assert.assertThat(headers.getAll("name"), Matchers.contains("v2")); }
@Test public void testAddHeader() { MultiMap headers = MultiMap.caseInsensitiveMultiMap(); new Expectations() { { clientRequest.headers(); result = headers; } }; request.addHeader("name", "v1"); request.addHeader("name", "v2"); Assert.assertThat(headers.getAll("name"), Matchers.contains("v1", "v2")); }
@Test public void getHeaders() { MultiMap headers = MultiMap.caseInsensitiveMultiMap(); headers.add("name", "v1"); headers.add("name", "v2"); new Expectations() { { clientResponse.headers(); result = headers; } }; Assert.assertThat(response.getHeaders("name"), Matchers.contains("v1", "v2")); }
@Test public void getHeaderNames() { MultiMap headers = MultiMap.caseInsensitiveMultiMap(); headers.add("n1", "v1"); headers.add("n2", "v2"); new Expectations() { { clientResponse.headers(); result = headers; } }; Assert.assertThat(response.getHeaderNames(), Matchers.contains("n1", "n2")); }
@Test public void testGetParameterValuesNormal() { MultiMap params = MultiMap.caseInsensitiveMultiMap(); params.add("name", "value"); new Expectations() { { vertxRequest.params(); result = params; } }; Assert.assertThat(request.getParameterValues("name"), Matchers.arrayContaining("value")); }
@Test public void testGetHeaders() { MultiMap headers = MultiMap.caseInsensitiveMultiMap(); headers.add("name", "value"); new Expectations() { { vertxRequest.headers(); result = headers; } }; Assert.assertThat(Collections.list(request.getHeaders("name")), Matchers.contains("value")); }
@Test public void testGetHeaderNames() { MultiMap headers = MultiMap.caseInsensitiveMultiMap(); headers.add("name", "value"); new Expectations() { { vertxRequest.headers(); result = headers; } }; Assert.assertThat(Collections.list(request.getHeaderNames()), Matchers.contains("name")); }
@Test public void defaultHeadersContainServiceRegistryAndAuthentication() throws Exception { MultiMap headers = RestUtils.getDefaultHeaders(); headers.addAll(RestUtils.getSignAuthHeaders(RestUtils.createSignRequest(HttpMethod.GET.toString(), new IpPort("127.0.0.1", 443), new RequestParam().addQueryParam("testParam", "test"), "test", new HashMap<>()))); assertThat(headers.get("Content-Type"), is("application/json")); assertThat(headers.get("User-Agent"), is("cse-serviceregistry-client/1.0.0")); assertThat(headers.get("x-domain-name"), is("default")); assertThat(headers.get("X-Service-AK"), is("blah...")); }
@Override public String getFormattedElement(AccessLogParam accessLogParam) { MultiMap headers = accessLogParam.getRoutingContext().request().headers(); if (null == headers) { return "-"; } String result = headers.get(identifier); if (null == result) { return RESULT_NOT_FOUND; } return result; }
@Test public void testAfterReceiveResponseNormal(@Mocked Invocation invocation, @Mocked HttpServletResponseEx responseEx, @Mocked Buffer bodyBuffer, @Mocked OperationMeta operationMeta, @Mocked ResponseMeta responseMeta, @Mocked RestOperationMeta swaggerRestOperation, @Mocked ProduceProcessor produceProcessor) throws Exception { MultiMap responseHeader = new CaseInsensitiveHeaders(); responseHeader.add("b", "bValue"); Object decodedResult = new Object(); new Expectations() { { responseEx.getHeader(HttpHeaders.CONTENT_TYPE); result = "json"; responseEx.getHeaderNames(); result = Arrays.asList("a", "b"); responseEx.getHeaders("b"); result = responseHeader.getAll("b"); swaggerRestOperation.findProduceProcessor("json"); result = produceProcessor; produceProcessor.decodeResponse(bodyBuffer, responseMeta.getJavaType()); result = decodedResult; invocation.getOperationMeta(); result = operationMeta; operationMeta.getExtData(RestConst.SWAGGER_REST_OPERATION); result = swaggerRestOperation; responseEx.getStatusType(); result = Status.OK; } }; Response response = filter.afterReceiveResponse(invocation, responseEx); Assert.assertSame(decodedResult, response.getResult()); Assert.assertEquals(1, response.getHeaders().getHeaderMap().size()); Assert.assertEquals(response.getHeaders().getHeader("b"), Arrays.asList("bValue")); }
@Override public MultiMap headers() { if (headers == null) { headers = MultiMap.caseInsensitiveMultiMap(); JsonObject requestHeaders = request.getJsonObject("headers"); if (requestHeaders != null) { for (Map.Entry<String, Object> entry : requestHeaders) { headers.add(entry.getKey(), entry.getValue().toString()); } } } return headers; }
@Override public MultiMap params() { if (params == null) { params = new CaseSensitiveMultiMapImpl(); JsonObject queryParams = request.getJsonObject("queryStringParameters"); if (queryParams != null) { for (Map.Entry<String, Object> entry : queryParams) { params.add(entry.getKey(), entry.getValue().toString()); } } } return params; }
@Override public MultiMap formAttributes() { if (attributes == null) { attributes = MultiMap.caseInsensitiveMultiMap(); } return attributes; }
@Override public MultiMap headers() { if (headers == null) { headers = MultiMap.caseInsensitiveMultiMap(); } return headers; }
@Override public MultiMap trailers() { if (trailers == null) { trailers = MultiMap.caseInsensitiveMultiMap(); } return trailers; }
private MultiMap set0(Iterable<Map.Entry<String, String>> map) { clear(); for (Map.Entry<String, String> entry : map) { add(entry.getKey(), entry.getValue()); } return this; }
@Override public MultiMap add(final String name, final String strVal) { int h = hash(name); int i = index(h); add0(h, i, name, strVal); return this; }
@Override public MultiMap add(String name, Iterable<String> values) { int h = hash(name); int i = index(h); for (String vstr : values) { add0(h, i, name, vstr); } return this; }
@Override public MultiMap addAll(MultiMap headers) { for (Map.Entry<String, String> entry : headers.entries()) { add(entry.getKey(), entry.getValue()); } return this; }
@Override public MultiMap addAll(Map<String, String> map) { for (Map.Entry<String, String> entry : map.entrySet()) { add(entry.getKey(), entry.getValue()); } return this; }
@Override public MultiMap remove(final String name) { Objects.requireNonNull(name, "name"); int h = hash(name); int i = index(h); remove0(h, i, name); return this; }