You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by or...@apache.org on 2023/11/30 21:02:48 UTC
(camel) 03/08: (chores) break large methods in HTTP components
This is an automated email from the ASF dual-hosted git repository.
orpiske pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git
commit 7422ff355b016997b68ee5a7026297902bfa28ee
Author: Otavio Rodolfo Piske <an...@gmail.com>
AuthorDate: Thu Nov 30 14:50:56 2023 -0300
(chores) break large methods in HTTP components
---
.../camel/http/base/HttpSendDynamicAware.java | 29 +++++++-----
.../netty/http/DefaultNettyHttpBinding.java | 23 ++++++----
.../http/handlers/HttpServerChannelHandler.java | 30 ++++++------
.../platform/http/PlatformHttpConsole.java | 35 ++++++++------
.../undertow/DefaultUndertowHttpBinding.java | 27 ++++++-----
.../component/undertow/UndertowClientCallback.java | 23 ++++++----
.../camel/component/undertow/UndertowConsumer.java | 53 ++++++++++++----------
.../undertow/handlers/CamelRootHandler.java | 30 +++++++-----
8 files changed, 145 insertions(+), 105 deletions(-)
diff --git a/components/camel-http-base/src/main/java/org/apache/camel/http/base/HttpSendDynamicAware.java b/components/camel-http-base/src/main/java/org/apache/camel/http/base/HttpSendDynamicAware.java
index 78173505162..1f6658ab5d6 100644
--- a/components/camel-http-base/src/main/java/org/apache/camel/http/base/HttpSendDynamicAware.java
+++ b/components/camel-http-base/src/main/java/org/apache/camel/http/base/HttpSendDynamicAware.java
@@ -61,18 +61,7 @@ public class HttpSendDynamicAware extends SendDynamicAwareSupport {
if (path != null || !entry.getLenientProperties().isEmpty()) {
// the context path can be dynamic or any lenient properties
// and therefore build a new static uri without path or lenient options
- Map<String, Object> params = entry.getProperties();
- for (String k : entry.getLenientProperties().keySet()) {
- params.remove(k);
- }
- if (path != null) {
- params.remove("httpUri");
- params.remove("httpURI");
- if ("netty-http".equals(getScheme())) {
- // the netty-http stores host,port etc in other fields than httpURI so we can just remove the path parameter
- params.remove("path");
- }
- }
+ final Map<String, Object> params = getParams(entry, path);
// build static url with the known parameters
String url;
@@ -92,6 +81,22 @@ public class HttpSendDynamicAware extends SendDynamicAwareSupport {
}
}
+ private Map<String, Object> getParams(DynamicAwareEntry entry, String path) {
+ Map<String, Object> params = entry.getProperties();
+ for (String k : entry.getLenientProperties().keySet()) {
+ params.remove(k);
+ }
+ if (path != null) {
+ params.remove("httpUri");
+ params.remove("httpURI");
+ if ("netty-http".equals(getScheme())) {
+ // the netty-http stores host,port etc in other fields than httpURI so we can just remove the path parameter
+ params.remove("path");
+ }
+ }
+ return params;
+ }
+
@Override
public Processor createPreProcessor(Exchange exchange, DynamicAwareEntry entry) throws Exception {
String[] hostAndPath = parseUri(entry);
diff --git a/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/DefaultNettyHttpBinding.java b/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/DefaultNettyHttpBinding.java
index 5d29fc06242..b2cd67bfd84 100644
--- a/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/DefaultNettyHttpBinding.java
+++ b/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/DefaultNettyHttpBinding.java
@@ -179,15 +179,7 @@ public class DefaultNettyHttpBinding implements NettyHttpBinding, Cloneable {
headers.put(Exchange.HTTP_PORT, port > 0 ? port : configuration.isSsl() || "https".equals(uri.getScheme()) ? 443 : 80);
// strip the starting endpoint path so the path is relative to the endpoint uri
- String path = uri.getRawPath();
- if (configuration.getPath() != null) {
- // need to match by lower case as we want to ignore case on context-path
- String matchPath = path.toLowerCase(Locale.US);
- String match = configuration.getPath() != null ? configuration.getPath().toLowerCase(Locale.US) : null;
- if (match != null && matchPath.startsWith(match)) {
- path = path.substring(configuration.getPath().length());
- }
- }
+ final String path = stripPath(configuration, uri);
// keep the path uri using the case the request provided (do not convert to lower case)
headers.put(NettyHttpConstants.HTTP_PATH, path);
@@ -288,6 +280,19 @@ public class DefaultNettyHttpBinding implements NettyHttpBinding, Cloneable {
}
+ private static String stripPath(NettyHttpConfiguration configuration, URI uri) {
+ String path = uri.getRawPath();
+ if (configuration.getPath() != null) {
+ // need to match by lower case as we want to ignore case on context-path
+ String matchPath = path.toLowerCase(Locale.US);
+ String match = configuration.getPath() != null ? configuration.getPath().toLowerCase(Locale.US) : null;
+ if (match != null && matchPath.startsWith(match)) {
+ path = path.substring(configuration.getPath().length());
+ }
+ }
+ return path;
+ }
+
/**
* Copy camel header from exchange to headers map.
*
diff --git a/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/handlers/HttpServerChannelHandler.java b/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/handlers/HttpServerChannelHandler.java
index e641b1ad9ba..d9df6e10d02 100644
--- a/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/handlers/HttpServerChannelHandler.java
+++ b/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/handlers/HttpServerChannelHandler.java
@@ -47,7 +47,6 @@ import org.apache.camel.component.netty.http.InboundStreamHttpRequest;
import org.apache.camel.component.netty.http.NettyHttpConfiguration;
import org.apache.camel.component.netty.http.NettyHttpConstants;
import org.apache.camel.component.netty.http.NettyHttpConsumer;
-import org.apache.camel.component.netty.http.NettyHttpHelper;
import org.apache.camel.component.netty.http.NettyHttpSecurityConfiguration;
import org.apache.camel.component.netty.http.SecurityAuthenticator;
import org.apache.camel.spi.CamelLogger;
@@ -159,18 +158,7 @@ public class HttpServerChannelHandler extends ServerChannelHandler {
// we need the relative path without the hostname and port
URI uri = new URI(request.uri());
- String target = uri.getPath();
-
- // strip the starting endpoint path so the target is relative to the endpoint uri
- String path = consumer.getConfiguration().getPath();
- if (path != null && target.startsWith(path)) {
- // need to match by lower case as we want to ignore case on context-path
- path = path.toLowerCase(Locale.US);
- String match = target.toLowerCase(Locale.US);
- if (match.startsWith(path)) {
- target = target.substring(path.length());
- }
- }
+ final String target = extractTarget(uri);
// is it a restricted resource?
String roles;
@@ -224,6 +212,22 @@ public class HttpServerChannelHandler extends ServerChannelHandler {
super.channelRead0(ctx, msg);
}
+ private String extractTarget(URI uri) {
+ String target = uri.getPath();
+
+ // strip the starting endpoint path so the target is relative to the endpoint uri
+ String path = consumer.getConfiguration().getPath();
+ if (path != null && target.startsWith(path)) {
+ // need to match by lower case as we want to ignore case on context-path
+ path = path.toLowerCase(Locale.US);
+ String match = target.toLowerCase(Locale.US);
+ if (match.startsWith(path)) {
+ target = target.substring(path.length());
+ }
+ }
+ return target;
+ }
+
protected boolean matchesRoles(String roles, String userRoles) {
// matches if no role restrictions or any role is accepted
if (roles.equals("*")) {
diff --git a/components/camel-platform-http/src/main/java/org/apache/camel/component/platform/http/PlatformHttpConsole.java b/components/camel-platform-http/src/main/java/org/apache/camel/component/platform/http/PlatformHttpConsole.java
index 1879ecb051f..e4c47ba15b7 100644
--- a/components/camel-platform-http/src/main/java/org/apache/camel/component/platform/http/PlatformHttpConsole.java
+++ b/components/camel-platform-http/src/main/java/org/apache/camel/component/platform/http/PlatformHttpConsole.java
@@ -69,21 +69,7 @@ public class PlatformHttpConsole extends AbstractDevConsole {
}
root.put("server", server);
- Set<HttpEndpointModel> models = http.getHttpEndpoints();
- List<JsonObject> list = new ArrayList<>();
- for (HttpEndpointModel model : models) {
- JsonObject jo = new JsonObject();
- String uri = model.getUri();
- if (!uri.startsWith("/")) {
- uri = "/" + uri;
- }
- jo.put("url", server + uri);
- jo.put("path", model.getUri());
- if (model.getVerbs() != null) {
- jo.put("verbs", model.getVerbs());
- }
- list.add(jo);
- }
+ final List<JsonObject> list = buildEndpointList(http, server);
if (!list.isEmpty()) {
root.put("endpoints", list);
}
@@ -91,4 +77,23 @@ public class PlatformHttpConsole extends AbstractDevConsole {
return root;
}
+
+ private static List<JsonObject> buildEndpointList(PlatformHttpComponent http, String server) {
+ Set<HttpEndpointModel> models = http.getHttpEndpoints();
+ List<JsonObject> list = new ArrayList<>();
+ for (HttpEndpointModel model : models) {
+ JsonObject jo = new JsonObject();
+ String uri = model.getUri();
+ if (!uri.startsWith("/")) {
+ uri = "/" + uri;
+ }
+ jo.put("url", server + uri);
+ jo.put("path", model.getUri());
+ if (model.getVerbs() != null) {
+ jo.put("verbs", model.getVerbs());
+ }
+ list.add(jo);
+ }
+ return list;
+ }
}
diff --git a/components/camel-undertow/src/main/java/org/apache/camel/component/undertow/DefaultUndertowHttpBinding.java b/components/camel-undertow/src/main/java/org/apache/camel/component/undertow/DefaultUndertowHttpBinding.java
index d023121e1d9..1c411f399f3 100644
--- a/components/camel-undertow/src/main/java/org/apache/camel/component/undertow/DefaultUndertowHttpBinding.java
+++ b/components/camel-undertow/src/main/java/org/apache/camel/component/undertow/DefaultUndertowHttpBinding.java
@@ -198,17 +198,7 @@ public class DefaultUndertowHttpBinding implements UndertowHttpBinding {
throws Exception {
LOG.trace("populateCamelHeaders: {}", exchange.getMessage().getHeaders());
- String path = httpExchange.getRequestPath();
- UndertowEndpoint endpoint = (UndertowEndpoint) exchange.getFromEndpoint();
- if (endpoint.getHttpURI() != null) {
- // need to match by lower case as we want to ignore case on context-path
- String endpointPath = endpoint.getHttpURI().getPath();
- String matchPath = path.toLowerCase(Locale.US);
- String match = endpointPath.toLowerCase(Locale.US);
- if (matchPath.startsWith(match)) {
- path = path.substring(endpointPath.length());
- }
- }
+ final String path = stripPath(httpExchange, exchange);
headersMap.put(UndertowConstants.HTTP_PATH, path);
if (LOG.isTraceEnabled()) {
@@ -290,6 +280,21 @@ public class DefaultUndertowHttpBinding implements UndertowHttpBinding {
headersMap.put(Exchange.HTTP_RAW_QUERY, httpExchange.getQueryString());
}
+ private static String stripPath(HttpServerExchange httpExchange, Exchange exchange) {
+ String path = httpExchange.getRequestPath();
+ UndertowEndpoint endpoint = (UndertowEndpoint) exchange.getFromEndpoint();
+ if (endpoint.getHttpURI() != null) {
+ // need to match by lower case as we want to ignore case on context-path
+ String endpointPath = endpoint.getHttpURI().getPath();
+ String matchPath = path.toLowerCase(Locale.US);
+ String match = endpointPath.toLowerCase(Locale.US);
+ if (matchPath.startsWith(match)) {
+ path = path.substring(endpointPath.length());
+ }
+ }
+ return path;
+ }
+
@Override
public void populateCamelHeaders(ClientResponse response, Map<String, Object> headersMap, Exchange exchange) {
LOG.trace("populateCamelHeaders: {}", exchange.getMessage().getHeaders());
diff --git a/components/camel-undertow/src/main/java/org/apache/camel/component/undertow/UndertowClientCallback.java b/components/camel-undertow/src/main/java/org/apache/camel/component/undertow/UndertowClientCallback.java
index d7f1463c5aa..0b8eedafae9 100644
--- a/components/camel-undertow/src/main/java/org/apache/camel/component/undertow/UndertowClientCallback.java
+++ b/components/camel-undertow/src/main/java/org/apache/camel/component/undertow/UndertowClientCallback.java
@@ -243,19 +243,24 @@ class UndertowClientCallback implements ClientCallback<ClientConnection> {
// creating the url to use takes 2-steps
final String url = UndertowHelper.createURL(exchange, endpoint);
final URI uri = UndertowHelper.createURI(exchange, url, endpoint);
- final HeaderMap headerMap = clientExchange.getResponse().getResponseHeaders();
- final Map<String, List<String>> m = new HashMap<>();
- for (final HttpString headerName : headerMap.getHeaderNames()) {
- final List<String> headerValue = new LinkedList<>();
- for (int i = 0; i < headerMap.count(headerName); i++) {
- headerValue.add(headerMap.get(headerName, i));
- }
- m.put(headerName.toString(), headerValue);
- }
+ final Map<String, List<String>> m = extractHeaders(clientExchange);
endpoint.getCookieHandler().storeCookies(exchange, uri, m);
}
}
+ private static Map<String, List<String>> extractHeaders(ClientExchange clientExchange) {
+ final HeaderMap headerMap = clientExchange.getResponse().getResponseHeaders();
+ final Map<String, List<String>> m = new HashMap<>();
+ for (final HttpString headerName : headerMap.getHeaderNames()) {
+ final List<String> headerValue = new LinkedList<>();
+ for (int i = 0; i < headerMap.count(headerName); i++) {
+ headerValue.add(headerMap.get(headerName, i));
+ }
+ m.put(headerName.toString(), headerValue);
+ }
+ return m;
+ }
+
protected void writeRequest(final ClientExchange clientExchange) {
final StreamSinkChannel requestChannel = clientExchange.getRequestChannel();
if (body != null) {
diff --git a/components/camel-undertow/src/main/java/org/apache/camel/component/undertow/UndertowConsumer.java b/components/camel-undertow/src/main/java/org/apache/camel/component/undertow/UndertowConsumer.java
index d3041cb5783..02f9556df87 100644
--- a/components/camel-undertow/src/main/java/org/apache/camel/component/undertow/UndertowConsumer.java
+++ b/components/camel-undertow/src/main/java/org/apache/camel/component/undertow/UndertowConsumer.java
@@ -165,30 +165,7 @@ public class UndertowConsumer extends DefaultConsumer implements HttpHandler, Su
public void handleRequest(HttpServerExchange httpExchange) throws Exception {
HttpString requestMethod = httpExchange.getRequestMethod();
if (Methods.OPTIONS.equals(requestMethod) && !getEndpoint().isOptionsEnabled()) {
- StringJoiner methodsBuilder = new StringJoiner(",");
-
- Collection<HttpHandlerRegistrationInfo> handlers = getEndpoint().getComponent().getHandlers();
- for (HttpHandlerRegistrationInfo reg : handlers) {
- URI uri = reg.getUri();
- // what other HTTP methods may exists for the same path
- if (reg.getMethodRestrict() != null && getEndpoint().getHttpURI().equals(uri)) {
- String restrict = reg.getMethodRestrict();
- if (restrict.endsWith(",OPTIONS")) {
- restrict = restrict.substring(0, restrict.length() - 8);
- }
- methodsBuilder.add(restrict);
- }
- }
- String allowedMethods = methodsBuilder.toString();
- if (ObjectHelper.isEmpty(allowedMethods)) {
- allowedMethods = getEndpoint().getHttpMethodRestrict();
- }
- if (ObjectHelper.isEmpty(allowedMethods)) {
- allowedMethods = "GET,HEAD,POST,PUT,DELETE,TRACE,OPTIONS,CONNECT,PATCH";
- }
- if (!allowedMethods.contains("OPTIONS")) {
- allowedMethods = allowedMethods + ",OPTIONS";
- }
+ final String allowedMethods = evalAllowedMethods();
//return list of allowed methods in response headers
httpExchange.setStatusCode(StatusCodes.OK);
httpExchange.getResponseHeaders().put(ExchangeHeaders.CONTENT_LENGTH, 0);
@@ -244,6 +221,34 @@ public class UndertowConsumer extends DefaultConsumer implements HttpHandler, Su
}
}
+ private String evalAllowedMethods() {
+ StringJoiner methodsBuilder = new StringJoiner(",");
+
+ Collection<HttpHandlerRegistrationInfo> handlers = getEndpoint().getComponent().getHandlers();
+ for (HttpHandlerRegistrationInfo reg : handlers) {
+ URI uri = reg.getUri();
+ // what other HTTP methods may exists for the same path
+ if (reg.getMethodRestrict() != null && getEndpoint().getHttpURI().equals(uri)) {
+ String restrict = reg.getMethodRestrict();
+ if (restrict.endsWith(",OPTIONS")) {
+ restrict = restrict.substring(0, restrict.length() - 8);
+ }
+ methodsBuilder.add(restrict);
+ }
+ }
+ String allowedMethods = methodsBuilder.toString();
+ if (ObjectHelper.isEmpty(allowedMethods)) {
+ allowedMethods = getEndpoint().getHttpMethodRestrict();
+ }
+ if (ObjectHelper.isEmpty(allowedMethods)) {
+ allowedMethods = "GET,HEAD,POST,PUT,DELETE,TRACE,OPTIONS,CONNECT,PATCH";
+ }
+ if (!allowedMethods.contains("OPTIONS")) {
+ allowedMethods = allowedMethods + ",OPTIONS";
+ }
+ return allowedMethods;
+ }
+
private void sendResponse(HttpServerExchange httpExchange, Exchange camelExchange)
throws IOException, NoTypeConversionAvailableException {
Object body = getResponseBody(httpExchange, camelExchange);
diff --git a/components/camel-undertow/src/main/java/org/apache/camel/component/undertow/handlers/CamelRootHandler.java b/components/camel-undertow/src/main/java/org/apache/camel/component/undertow/handlers/CamelRootHandler.java
index af0dc3abeff..96b0bd54cc6 100644
--- a/components/camel-undertow/src/main/java/org/apache/camel/component/undertow/handlers/CamelRootHandler.java
+++ b/components/camel-undertow/src/main/java/org/apache/camel/component/undertow/handlers/CamelRootHandler.java
@@ -54,18 +54,7 @@ public class CamelRootHandler implements HttpHandler {
templateHandler.add(relativePath, targetHandler);
}
} else {
- CamelPathTemplateHandler templateHandler;
- if (basePathHandler instanceof CamelMethodHandler) {
- // A static path handler is already set for the base path. Use it as a default handler
- templateHandler = new CamelPathTemplateHandler((CamelMethodHandler) basePathHandler);
- } else if (basePathHandler == null) {
- templateHandler = new CamelPathTemplateHandler(new CamelMethodHandler());
- } else {
- throw new IllegalArgumentException(String.format("Unsupported handler '%s' was found", basePathHandler));
- }
- targetHandler = new CamelMethodHandler();
- templateHandler.add(relativePath, targetHandler);
- pathHandler.addPrefixPath(basePath, templateHandler);
+ targetHandler = add(basePathHandler, relativePath, basePath);
}
} else {
@@ -95,6 +84,23 @@ public class CamelRootHandler implements HttpHandler {
return targetHandler.add(methods, handler);
}
+ private CamelMethodHandler add(HttpHandler basePathHandler, String relativePath, String basePath) {
+ CamelMethodHandler targetHandler;
+ CamelPathTemplateHandler templateHandler;
+ if (basePathHandler instanceof CamelMethodHandler) {
+ // A static path handler is already set for the base path. Use it as a default handler
+ templateHandler = new CamelPathTemplateHandler((CamelMethodHandler) basePathHandler);
+ } else if (basePathHandler == null) {
+ templateHandler = new CamelPathTemplateHandler(new CamelMethodHandler());
+ } else {
+ throw new IllegalArgumentException(String.format("Unsupported handler '%s' was found", basePathHandler));
+ }
+ targetHandler = new CamelMethodHandler();
+ templateHandler.add(relativePath, targetHandler);
+ pathHandler.addPrefixPath(basePath, templateHandler);
+ return targetHandler;
+ }
+
public synchronized void remove(String path, String methods, boolean prefixMatch) {
String basePath = getBasePath(path);
HttpHandler basePathHandler = pathHandler.getHandler(basePath);