davsclaus commented on code in PR #30603:
URL: https://github.com/apache/camel/pull/30603#discussion_r4659195157


##########
components/camel-cxf/camel-cxf-platform-http/src/main/java/org/apache/camel/component/cxf/platform/http/VertxServletInputStream.java:
##########
@@ -0,0 +1,201 @@
+/*
+ * 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.camel.component.cxf.platform.http;
+
+import java.io.IOException;
+import java.io.InterruptedIOException;
+import java.util.ArrayDeque;
+import java.util.Deque;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.locks.Condition;
+import java.util.concurrent.locks.ReentrantLock;
+
+import jakarta.servlet.ReadListener;
+import jakarta.servlet.ServletInputStream;
+
+import io.vertx.core.Context;
+import io.vertx.core.buffer.Buffer;
+import io.vertx.core.http.HttpServerRequest;
+
+/**
+ * A blocking stream of the body of a Vert.x request, read on a worker thread. 
The request is paused, and each read that
+ * finds no data fetches the next buffer from the event loop, so no more than 
one buffer of the body is held at a time.
+ * <p/>
+ * Created on the event loop, before the request is given to the worker thread.
+ */
+final class VertxServletInputStream extends ServletInputStream {
+
+    private final Context context;
+    private final HttpServerRequest request;
+    private final ReentrantLock lock = new ReentrantLock();
+    private final Condition changed = lock.newCondition();
+    private final Deque<Buffer> queue = new ArrayDeque<>();
+    private boolean ended;
+    private boolean fetching;
+    private Throwable failure;
+    private Buffer current;
+    private int position;
+    private boolean closed;
+
+    VertxServletInputStream(Context context, HttpServerRequest request) {
+        this.context = context;
+        this.request = request;
+        request.pause();
+        if (request.isEnded()) {
+            // no body (or it has been read already)
+            ended = true;
+        } else {
+            request.handler(this::onData);
+            request.endHandler(v -> onEnd());
+            request.exceptionHandler(this::onFailure);
+        }
+    }
+
+    private void onData(Buffer buffer) {
+        lock.lock();
+        try {
+            if (buffer.length() > 0) {
+                queue.add(buffer);
+            }
+            fetching = false;
+            changed.signalAll();
+        } finally {
+            lock.unlock();
+        }
+    }
+
+    private void onEnd() {
+        lock.lock();
+        try {
+            ended = true;
+            changed.signalAll();
+        } finally {
+            lock.unlock();
+        }
+    }
+
+    private void onFailure(Throwable cause) {
+        lock.lock();
+        try {
+            failure = cause;
+            changed.signalAll();
+        } finally {
+            lock.unlock();
+        }
+    }
+
+    /**
+     * The next buffer of the body, waiting for it, or null at the end of the 
body.
+     */
+    private boolean nextBuffer() throws IOException {
+        lock.lock();
+        try {
+            while (queue.isEmpty()) {
+                if (failure != null) {
+                    throw new IOException("Error reading the request body: " + 
failure.getMessage(), failure);
+                }
+                if (ended || closed) {
+                    return false;
+                }
+                if (!fetching) {
+                    fetching = true;
+                    context.runOnContext(v -> request.fetch(1));
+                }
+                try {
+                    // the connection closing (or the idle timeout of the 
server) fails the request and wakes us up
+                    changed.await(1, TimeUnit.SECONDS);
+                } catch (InterruptedException e) {
+                    Thread.currentThread().interrupt();
+                    throw new InterruptedIOException("Interrupted reading the 
request body");
+                }
+            }
+            current = queue.poll();
+            position = 0;
+            return true;
+        } finally {
+            lock.unlock();
+        }
+    }
+
+    private boolean ensureData() throws IOException {
+        while (current == null || position >= current.length()) {
+            if (!nextBuffer()) {
+                return false;
+            }
+        }
+        return true;
+    }
+
+    @Override
+    public int read() throws IOException {
+        if (!ensureData()) {
+            return -1;
+        }
+        return current.getByte(position++) & 0xff;
+    }
+
+    @Override
+    public int read(byte[] b, int off, int len) throws IOException {
+        if (len == 0) {
+            return 0;
+        }
+        if (!ensureData()) {
+            return -1;
+        }
+        int n = Math.min(len, current.length() - position);
+        current.getBytes(position, position + n, b, off);
+        position += n;
+        return n;
+    }
+
+    @Override
+    public int available() {
+        return current != null ? current.length() - position : 0;
+    }
+
+    @Override
+    public boolean isFinished() {
+        lock.lock();
+        try {
+            return ended && queue.isEmpty() && (current == null || position >= 
current.length());
+        } finally {
+            lock.unlock();
+        }
+    }
+
+    @Override
+    public boolean isReady() {
+        return available() > 0 || isFinished();
+    }
+
+    @Override
+    public void setReadListener(ReadListener readListener) {
+        throw new UnsupportedOperationException("Asynchronous reads are not 
supported");
+    }
+
+    @Override
+    public void close() {
+        lock.lock();
+        try {
+            closed = true;
+            queue.clear();
+            current = null;
+        } finally {
+            lock.unlock();

Review Comment:
   Fixed in 1eab135b: `close()` now signals the condition, so a blocked reader 
returns at once.
   
   _Claude Code on behalf of Claus Ibsen (davsclaus)_



##########
components/camel-cxf/camel-cxf-platform-http/src/main/java/org/apache/camel/component/cxf/platform/http/VertxServletRequest.java:
##########
@@ -0,0 +1,619 @@
+/*
+ * 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.camel.component.cxf.platform.http;
+
+import java.io.BufferedReader;
+import java.io.InputStreamReader;
+import java.io.UnsupportedEncodingException;
+import java.nio.charset.Charset;
+import java.nio.charset.StandardCharsets;
+import java.security.Principal;
+import java.time.ZonedDateTime;
+import java.time.format.DateTimeFormatter;
+import java.time.format.DateTimeParseException;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Enumeration;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.atomic.AtomicLong;
+
+import jakarta.servlet.AsyncContext;
+import jakarta.servlet.DispatcherType;
+import jakarta.servlet.RequestDispatcher;
+import jakarta.servlet.ServletConnection;
+import jakarta.servlet.ServletContext;
+import jakarta.servlet.ServletInputStream;
+import jakarta.servlet.ServletRequest;
+import jakarta.servlet.ServletResponse;
+import jakarta.servlet.http.Cookie;
+import jakarta.servlet.http.HttpServletRequest;
+import jakarta.servlet.http.HttpServletResponse;
+import jakarta.servlet.http.HttpSession;
+import jakarta.servlet.http.HttpUpgradeHandler;
+import jakarta.servlet.http.Part;
+
+import io.vertx.core.MultiMap;
+import io.vertx.core.http.HttpHeaders;
+import io.vertx.core.http.HttpServerRequest;
+import io.vertx.core.http.HttpVersion;
+import io.vertx.core.net.HostAndPort;
+import io.vertx.core.net.SocketAddress;
+import io.vertx.ext.auth.User;
+import io.vertx.ext.web.RoutingContext;
+
+/**
+ * The servlet request that CXF reads, over a Vert.x request. Only what CXF 
uses is supported: the request line,
+ * headers, query parameters, the body as a stream, the addresses, and the 
user that the authentication of the
+ * platform-http server has set. There are no sessions, no dispatchers, no 
form parameters and no parts.
+ */
+final class VertxServletRequest implements HttpServletRequest {
+
+    private static final AtomicLong REQUEST_IDS = new AtomicLong();
+
+    private final RoutingContext rc;
+    private final HttpServerRequest request;
+    private final ServletInputStream in;
+    private final String contextPath;
+    private final String servletPath;
+    private final String pathInfo;
+    private final boolean useXForwardedHeaders;
+    private final Map<String, Object> attributes = new ConcurrentHashMap<>();
+    private final String requestId = 
Long.toString(REQUEST_IDS.incrementAndGet());
+    private String characterEncoding;
+    private BufferedReader reader;
+
+    VertxServletRequest(RoutingContext rc, ServletInputStream in, String 
contextPath, String servletPath,
+                        String pathInfo, boolean useXForwardedHeaders) {
+        this.rc = rc;
+        this.request = rc.request();
+        this.in = in;
+        this.contextPath = contextPath;
+        this.servletPath = servletPath;
+        this.pathInfo = pathInfo;
+        this.useXForwardedHeaders = useXForwardedHeaders;
+    }
+
+    // attributes
+
+    @Override
+    public Object getAttribute(String name) {
+        return attributes.get(name);
+    }
+
+    @Override
+    public Enumeration<String> getAttributeNames() {
+        return Collections.enumeration(attributes.keySet());
+    }
+
+    @Override
+    public void setAttribute(String name, Object o) {
+        if (o == null) {
+            attributes.remove(name);
+        } else {
+            attributes.put(name, o);
+        }
+    }
+
+    @Override
+    public void removeAttribute(String name) {
+        attributes.remove(name);
+    }
+
+    // body
+
+    @Override
+    public String getCharacterEncoding() {
+        if (characterEncoding == null) {
+            String type = getContentType();
+            if (type != null) {
+                int pos = type.toLowerCase(Locale.ROOT).indexOf("charset=");
+                if (pos >= 0) {
+                    String charset = type.substring(pos + 8).trim();
+                    int end = charset.indexOf(';');
+                    characterEncoding = (end >= 0 ? charset.substring(0, end) 
: charset).trim().replace("\"", "");
+                }
+            }
+        }
+        return characterEncoding;
+    }
+
+    @Override
+    public void setCharacterEncoding(String env) throws 
UnsupportedEncodingException {
+        if (reader == null) {
+            if (!Charset.isSupported(env)) {
+                throw new UnsupportedEncodingException(env);
+            }
+            this.characterEncoding = env;
+        }
+    }
+
+    @Override
+    public int getContentLength() {
+        long len = getContentLengthLong();
+        return len > Integer.MAX_VALUE ? -1 : (int) len;
+    }
+
+    @Override
+    public long getContentLengthLong() {
+        String value = request.getHeader(HttpHeaders.CONTENT_LENGTH);
+        if (value == null) {
+            return -1;
+        }
+        try {
+            return Long.parseLong(value.trim());
+        } catch (NumberFormatException e) {
+            return -1;
+        }
+    }
+
+    @Override
+    public String getContentType() {
+        return request.getHeader(HttpHeaders.CONTENT_TYPE);
+    }
+
+    @Override
+    public ServletInputStream getInputStream() {
+        return in;
+    }
+
+    @Override
+    public BufferedReader getReader() {
+        if (reader == null) {
+            String charset = getCharacterEncoding();
+            reader = new BufferedReader(
+                    new InputStreamReader(in, charset != null ? 
Charset.forName(charset) : StandardCharsets.ISO_8859_1));
+        }
+        return reader;
+    }
+
+    // parameters: only those of the query, as a form body is the SOAP message
+
+    @Override
+    public String getParameter(String name) {
+        return rc.queryParams().get(name);
+    }
+
+    @Override
+    public Enumeration<String> getParameterNames() {
+        return Collections.enumeration(rc.queryParams().names());
+    }
+
+    @Override
+    public String[] getParameterValues(String name) {
+        List<String> values = rc.queryParams().getAll(name);
+        return values.isEmpty() ? null : values.toArray(new String[0]);
+    }
+
+    @Override
+    public Map<String, String[]> getParameterMap() {
+        Map<String, String[]> answer = new LinkedHashMap<>();
+        MultiMap params = rc.queryParams();
+        for (String name : params.names()) {
+            answer.put(name, params.getAll(name).toArray(new String[0]));
+        }
+        return answer;
+    }
+
+    // the request line and the addresses
+
+    @Override
+    public String getProtocol() {
+        HttpVersion version = request.version();
+        if (version == HttpVersion.HTTP_2) {
+            return "HTTP/2.0";
+        }
+        return version == HttpVersion.HTTP_1_0 ? "HTTP/1.0" : "HTTP/1.1";
+    }
+
+    @Override
+    public String getScheme() {
+        String proto = forwarded("X-Forwarded-Proto");
+        return proto != null ? proto.toLowerCase(Locale.ROOT) : 
request.scheme();
+    }
+
+    @Override
+    public String getServerName() {
+        String host = forwarded("X-Forwarded-Host");
+        if (host != null) {
+            return HostAndPort.parseAuthority(host, -1) != null ? 
HostAndPort.parseAuthority(host, -1).host() : host;
+        }

Review Comment:
   Fixed in 1eab135b: the authority is parsed once into a local variable.
   
   _Claude Code on behalf of Claus Ibsen (davsclaus)_



##########
components/camel-cxf/camel-cxf-soap/src/main/java/org/apache/camel/component/cxf/jaxws/CxfEndpoint.java:
##########
@@ -595,6 +597,17 @@ protected void setupServerFactoryBean(ServerFactoryBean 
sfb, Class<?> cls) {
         for (var factory : factories) {
             sfb.getBus().setExtension(factory, HttpDestinationFactory.class);
         }
+        // add custom server lifecycle listeners (such as 
camel-cxf-platform-http, which serves the endpoint on the
+        // platform-http server when the server starts)
+        var listeners = 
getCamelContext().getRegistry().findByType(ServerLifeCycleListener.class);
+        if (!listeners.isEmpty()) {
+            ServerLifeCycleManager manager = 
sfb.getBus().getExtension(ServerLifeCycleManager.class);
+            if (manager != null) {
+                for (var listener : listeners) {
+                    manager.registerListener(listener);

Review Comment:
   No change needed: CXF's `ServerLifeCycleManagerImpl.registerListener` adds 
with `CopyOnWriteArrayList.addIfAbsent`, so a listener already registered on a 
shared bus is not added again. I added a comment in 1eab135b that says so. 
Also, `startServer` of the transport returns early for a server that is already 
mounted.
   
   _Claude Code on behalf of Claus Ibsen (davsclaus)_



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to