You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nifi.apache.org by pv...@apache.org on 2022/06/07 13:34:06 UTC
[nifi] branch main updated: NIFI-10094 Removed unused test HttpServer classes
This is an automated email from the ASF dual-hosted git repository.
pvillard pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/nifi.git
The following commit(s) were added to refs/heads/main by this push:
new 2455ba2c45 NIFI-10094 Removed unused test HttpServer classes
2455ba2c45 is described below
commit 2455ba2c459f3ba963a94a2046f73849dc76352f
Author: exceptionfactory <ex...@apache.org>
AuthorDate: Mon Jun 6 14:03:13 2022 -0500
NIFI-10094 Removed unused test HttpServer classes
Signed-off-by: Pierre Villard <pi...@gmail.com>
This closes #6102.
---
.../cluster/manager/testutils/HttpRequest.java | 237 --------------------
.../cluster/manager/testutils/HttpResponse.java | 92 --------
.../manager/testutils/HttpResponseAction.java | 57 -----
.../nifi/cluster/manager/testutils/HttpServer.java | 239 ---------------------
4 files changed, 625 deletions(-)
diff --git a/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-cluster/src/test/java/org/apache/nifi/cluster/manager/testutils/HttpRequest.java b/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-cluster/src/test/java/org/apache/nifi/cluster/manager/testutils/HttpRequest.java
deleted file mode 100644
index 89c9a0ab63..0000000000
--- a/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-cluster/src/test/java/org/apache/nifi/cluster/manager/testutils/HttpRequest.java
+++ /dev/null
@@ -1,237 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.nifi.cluster.manager.testutils;
-
-import java.io.IOException;
-import java.io.Reader;
-import java.io.UnsupportedEncodingException;
-import java.net.URLDecoder;
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-
-import javax.ws.rs.HttpMethod;
-import javax.ws.rs.core.MediaType;
-
-import org.apache.commons.lang3.StringUtils;
-
-/**
- * Encapsulates an HTTP request. The toString method returns the specification-compliant request.
- *
- */
-public class HttpRequest {
-
- private String method;
- private String uri;
- private String rawUri;
- private String version;
- private String body;
- private String rawRequest;
- private Map<String, String> headers = new HashMap<>();
- private Map<String, List<String>> parameters = new HashMap<>();
-
- public static HttpRequestBuilder createFromRequestLine(final String requestLine) {
- return new HttpRequestBuilder(requestLine);
- }
-
- public String getBody() {
- return body;
- }
-
- public Map<String, String> getHeaders() {
- return Collections.unmodifiableMap(headers);
- }
-
- public String getHeaderValue(final String header) {
- for (final Map.Entry<String, String> entry : getHeaders().entrySet()) {
- if (entry.getKey().equalsIgnoreCase(header)) {
- return entry.getValue();
- }
- }
- return null;
- }
-
- public String getMethod() {
- return method;
- }
-
- public Map<String, List<String>> getParameters() {
- final Map<String, List<String>> result = new HashMap<>();
- for (final Map.Entry<String, List<String>> entry : parameters.entrySet()) {
- result.put(entry.getKey(), Collections.unmodifiableList(entry.getValue()));
- }
- return Collections.unmodifiableMap(result);
- }
-
- public String getUri() {
- return uri;
- }
-
- public String getRawUri() {
- return rawUri;
- }
-
- public String getVersion() {
- return version;
- }
-
- @Override
- public String toString() {
- return rawRequest;
- }
-
- /**
- * A builder for constructing basic HTTP requests. It handles only enough of the HTTP specification to support basic unit testing, and it should not be used otherwise.
- */
- public static class HttpRequestBuilder {
-
- private String method;
- private String uri;
- private String rawUri;
- private String version;
- private Map<String, String> headers = new HashMap<>();
- private Map<String, List<String>> parameters = new HashMap<>();
- private int contentLength = 0;
- private String contentType;
- private String body = "";
- private StringBuilder rawRequest = new StringBuilder();
-
- private HttpRequestBuilder(final String requestLine) {
-
- final String[] tokens = requestLine.split(" ");
- if (tokens.length != 3) {
- throw new IllegalArgumentException("Invalid HTTP Request Line: " + requestLine);
- }
-
- method = tokens[0];
- if (HttpMethod.GET.equalsIgnoreCase(method) || HttpMethod.HEAD.equalsIgnoreCase(method) || HttpMethod.DELETE.equalsIgnoreCase(method) || HttpMethod.OPTIONS.equalsIgnoreCase(method)) {
- final int queryIndex = tokens[1].indexOf("?");
- if (queryIndex > -1) {
- uri = tokens[1].substring(0, queryIndex);
- addParameters(tokens[1].substring(queryIndex + 1));
- } else {
- uri = tokens[1];
- }
- }
- rawUri = tokens[1];
- version = tokens[2];
- rawRequest.append(requestLine).append("\n");
- }
-
- private void addHeader(final String key, final String value) {
- if (key.contains(" ")) {
- throw new IllegalArgumentException("Header key may not contain spaces.");
- } else if ("content-length".equalsIgnoreCase(key)) {
- contentLength = (StringUtils.isBlank(value.trim())) ? 0 : Integer.parseInt(value.trim());
- } else if ("content-type".equalsIgnoreCase(key)) {
- contentType = value.trim();
- }
- headers.put(key, value);
- }
-
- public void addHeader(final String header) {
- final int firstColonIndex = header.indexOf(":");
- if (firstColonIndex < 0) {
- throw new IllegalArgumentException("Invalid HTTP Header line: " + header);
- }
- addHeader(header.substring(0, firstColonIndex), header.substring(firstColonIndex + 1));
- rawRequest.append(header).append("\n");
- }
-
- // final because constructor calls it
- public final void addParameters(final String queryString) {
-
- if (StringUtils.isBlank(queryString)) {
- return;
- }
-
- final String normQueryString;
- if (queryString.startsWith("?")) {
- normQueryString = queryString.substring(1);
- } else {
- normQueryString = queryString;
- }
- final String[] keyValuePairs = normQueryString.split("&");
- for (final String keyValuePair : keyValuePairs) {
- final String[] keyValueTokens = keyValuePair.split("=");
- try {
- addParameter(
- URLDecoder.decode(keyValueTokens[0], "utf-8"),
- URLDecoder.decode(keyValueTokens[1], "utf-8")
- );
- } catch (UnsupportedEncodingException use) {
- throw new RuntimeException(use);
- }
- }
- }
-
- public void addParameter(final String key, final String value) {
-
- if (key.contains(" ")) {
- throw new IllegalArgumentException("Parameter key may not contain spaces: " + key);
- }
-
- final List<String> values;
- if (parameters.containsKey(key)) {
- values = parameters.get(key);
- } else {
- values = new ArrayList<>();
- parameters.put(key, values);
- }
- values.add(value);
- }
-
- public void addBody(final Reader reader) throws IOException {
-
- if (contentLength <= 0) {
- return;
- }
-
- final char[] buf = new char[contentLength];
- int offset = 0;
- int numRead = 0;
- while (offset < buf.length && (numRead = reader.read(buf, offset, buf.length - offset)) >= 0) {
- offset += numRead;
- }
- body = new String(buf);
- rawRequest.append("\n");
- rawRequest.append(body);
- }
-
- public HttpRequest build() throws UnsupportedEncodingException {
-
- if (HttpMethod.GET.equalsIgnoreCase(method) == false && HttpMethod.HEAD.equalsIgnoreCase(method) == false && contentType.equalsIgnoreCase(MediaType.APPLICATION_FORM_URLENCODED)) {
- addParameters(body);
- }
-
- final HttpRequest request = new HttpRequest();
- request.method = this.method;
- request.uri = this.uri;
- request.rawUri = this.rawUri;
- request.version = this.version;
- request.headers.putAll(this.headers);
- request.parameters.putAll(this.parameters);
- request.body = this.body;
- request.rawRequest = this.rawRequest.toString();
-
- return request;
- }
-
- }
-}
diff --git a/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-cluster/src/test/java/org/apache/nifi/cluster/manager/testutils/HttpResponse.java b/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-cluster/src/test/java/org/apache/nifi/cluster/manager/testutils/HttpResponse.java
deleted file mode 100644
index 26c892d649..0000000000
--- a/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-cluster/src/test/java/org/apache/nifi/cluster/manager/testutils/HttpResponse.java
+++ /dev/null
@@ -1,92 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.nifi.cluster.manager.testutils;
-
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.Map;
-
-import javax.ws.rs.core.Response.Status;
-
-/**
- * Encapsulates an HTTP response. The toString method returns the specification-compliant response.
- *
- */
-public class HttpResponse {
-
- private final Status status;
- private final String entity;
- private final Map<String, String> headers = new HashMap<>();
-
- public HttpResponse(final Status status, final String entity) {
- this.status = status;
- this.entity = entity;
- headers.put("content-length", String.valueOf(entity.getBytes().length));
- }
-
- public String getEntity() {
- return entity;
- }
-
- public Status getStatus() {
- return status;
- }
-
- public Map<String, String> getHeaders() {
- return Collections.unmodifiableMap(headers);
- }
-
- public void addHeader(final String key, final String value) {
- if (key.contains(" ")) {
- throw new IllegalArgumentException("Header key may not contain spaces.");
- } else if ("content-length".equalsIgnoreCase(key)) {
- throw new IllegalArgumentException("Content-Length header is set automatically based on length of content.");
- }
- headers.put(key, value);
- }
-
- public void addHeaders(final Map<String, String> headers) {
- for (final Map.Entry<String, String> entry : headers.entrySet()) {
- addHeader(entry.getKey(), entry.getValue());
- }
- }
-
- @Override
- public String toString() {
-
- final StringBuilder strb = new StringBuilder();
-
- // response line
- strb.append("HTTP/1.1 ")
- .append(status.getStatusCode())
- .append(" ")
- .append(status.getReasonPhrase())
- .append("\n");
-
- // headers
- for (final Map.Entry<String, String> entry : headers.entrySet()) {
- strb.append(entry.getKey()).append(": ").append(entry.getValue()).append("\n");
- }
-
- strb.append("\n");
-
- // body
- strb.append(entity);
-
- return strb.toString();
- }
-}
diff --git a/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-cluster/src/test/java/org/apache/nifi/cluster/manager/testutils/HttpResponseAction.java b/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-cluster/src/test/java/org/apache/nifi/cluster/manager/testutils/HttpResponseAction.java
deleted file mode 100644
index a2899cfe6e..0000000000
--- a/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-cluster/src/test/java/org/apache/nifi/cluster/manager/testutils/HttpResponseAction.java
+++ /dev/null
@@ -1,57 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.nifi.cluster.manager.testutils;
-
-/**
- * Wraps a HttpResponse with a time-delay. When the action is applied, the currently executing thread sleeps for the given delay before returning the response to the caller.
- *
- * This class is good for simulating network latency.
- *
- */
-public class HttpResponseAction {
-
- private final HttpResponse response;
-
- private final int waitTimeMs;
-
- public HttpResponseAction(final HttpResponse response) {
- this(response, 0);
- }
-
- public HttpResponseAction(final HttpResponse response, final int waitTimeMs) {
- this.response = response;
- this.waitTimeMs = waitTimeMs;
- }
-
- public HttpResponse apply() {
- try {
- Thread.sleep(waitTimeMs);
- } catch (final InterruptedException ie) {
- throw new RuntimeException(ie);
- }
-
- return response;
- }
-
- public HttpResponse getResponse() {
- return response;
- }
-
- public int getWaitTimeMs() {
- return waitTimeMs;
- }
-}
diff --git a/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-cluster/src/test/java/org/apache/nifi/cluster/manager/testutils/HttpServer.java b/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-cluster/src/test/java/org/apache/nifi/cluster/manager/testutils/HttpServer.java
deleted file mode 100644
index e9cbd1d492..0000000000
--- a/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-cluster/src/test/java/org/apache/nifi/cluster/manager/testutils/HttpServer.java
+++ /dev/null
@@ -1,239 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.nifi.cluster.manager.testutils;
-
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.InputStreamReader;
-import java.io.PrintWriter;
-import java.io.Reader;
-import java.net.ServerSocket;
-import java.net.Socket;
-import java.net.SocketException;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-import java.util.Queue;
-import java.util.concurrent.ConcurrentLinkedQueue;
-import java.util.concurrent.ExecutorService;
-import java.util.concurrent.Executors;
-import java.util.concurrent.TimeUnit;
-
-import org.apache.nifi.cluster.manager.testutils.HttpRequest.HttpRequestBuilder;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-/**
- * A simple HTTP web server that allows clients to register canned-responses to respond to received requests.
- *
- */
-public class HttpServer {
-
- private static final Logger logger = LoggerFactory.getLogger(HttpServer.class);
-
- private final ExecutorService executorService;
- private final ServerSocket serverSocket;
- private final Queue<HttpResponseAction> responseQueue = new ConcurrentLinkedQueue<>();
- private final Map<String, String> checkedHeaders = new HashMap<>();
- private final Map<String, List<String>> checkedParameters = new HashMap<>();
- private final int port;
-
- public HttpServer(int numThreads, int port) throws IOException {
- this.port = port;
- executorService = Executors.newFixedThreadPool(numThreads);
- serverSocket = new ServerSocket(port);
- }
-
- public void start() {
-
- new Thread() {
- @Override
- public void run() {
- while (isRunning()) {
- try {
- final Socket conn = serverSocket.accept();
- executorService.execute(new Runnable() {
- @Override
- public void run() {
- handleRequest(conn);
- if (conn.isClosed() == false) {
- try {
- conn.close();
- } catch (IOException ioe) {
- }
- }
- }
- });
- } catch (final SocketException se) {
- /* ignored */
- } catch (final IOException ioe) {
- if (logger.isDebugEnabled()) {
- logger.warn("", ioe);
- }
- }
- }
- }
- ;
- }
-
- .start();
- }
-
- public boolean isRunning() {
- return executorService.isShutdown() == false;
- }
-
- public void stop() {
- // shutdown server socket
- try {
- if (serverSocket.isClosed() == false) {
- serverSocket.close();
- }
- } catch (final Exception ex) {
- throw new RuntimeException(ex);
- }
-
- // shutdown executor service
- try {
- executorService.shutdown();
- executorService.awaitTermination(3, TimeUnit.SECONDS);
- } catch (final Exception ex) {
- throw new RuntimeException(ex);
- }
- }
-
- public int getPort() {
- if (isRunning()) {
- return serverSocket.getLocalPort();
- } else {
- return port;
- }
- }
-
- public Queue<HttpResponseAction> addResponseAction(final HttpResponseAction response) {
- responseQueue.add(response);
- return responseQueue;
- }
-
- public void addCheckedHeaders(final Map<String, String> headers) {
- checkedHeaders.putAll(headers);
- }
-
- public void addCheckedParameters(final Map<String, List<String>> parameters) {
- checkedParameters.putAll(parameters);
- }
-
- private void handleRequest(final Socket conn) {
- try {
-
- final HttpRequest httpRequest = buildRequest(conn.getInputStream());
-
- if (logger.isDebugEnabled()) {
- logger.debug("\n" + httpRequest);
- }
-
- // check headers
- final Map<String, String> reqHeaders = httpRequest.getHeaders();
- for (final Map.Entry<String, String> entry : checkedHeaders.entrySet()) {
- if (reqHeaders.containsKey(entry.getKey())) {
- if (entry.getValue().equals(reqHeaders.get(entry.getKey()))) {
- logger.error("Incorrect HTTP request header value received for checked header: " + entry.getKey());
- conn.close();
- return;
- }
- } else {
- logger.error("Missing checked header: " + entry.getKey());
- conn.close();
- return;
- }
- }
-
- // check parameters
- final Map<String, List<String>> reqParams = httpRequest.getParameters();
- for (final Map.Entry<String, List<String>> entry : checkedParameters.entrySet()) {
- if (reqParams.containsKey(entry.getKey())) {
- if (entry.getValue().equals(reqParams.get(entry.getKey())) == false) {
- logger.error("Incorrect HTTP request parameter values received for checked parameter: " + entry.getKey());
- conn.close();
- return;
- }
- } else {
- logger.error("Missing checked parameter: " + entry.getKey());
- conn.close();
- return;
- }
- }
-
- // apply the next response
- final HttpResponseAction response = responseQueue.remove();
- response.apply();
-
- // send the response to client
- final PrintWriter pw = new PrintWriter(conn.getOutputStream(), true);
-
- if (logger.isDebugEnabled()) {
- logger.debug("\n" + response.getResponse());
- }
-
- pw.print(response.getResponse());
- pw.flush();
-
- } catch (IOException ioe) { /* ignored */ }
- }
-
- private HttpRequest buildRequest(final InputStream requestIs) throws IOException {
- return new HttpRequestReader().read(new InputStreamReader(requestIs));
- }
-
- // reads an HTTP request from the given reader
- private class HttpRequestReader {
-
- public HttpRequest read(final Reader reader) throws IOException {
-
- HttpRequestBuilder builder = null;
- String line = "";
- boolean isRequestLine = true;
- while ((line = readLine(reader)).isEmpty() == false) {
- if (isRequestLine) {
- builder = HttpRequest.createFromRequestLine(line);
- isRequestLine = false;
- } else {
- builder.addHeader(line);
- }
- }
-
- if (builder != null) {
- builder.addBody(reader);
- }
-
- return builder.build();
- }
-
- private String readLine(final Reader reader) throws IOException {
-
- /* read character at time to prevent blocking */
- final StringBuilder strb = new StringBuilder();
- char c;
- while ((c = (char) reader.read()) != '\n') {
- if (c != '\r') {
- strb.append(c);
- }
- }
- return strb.toString();
- }
- }
-}