ppalaga commented on a change in pull request #201: Fix #184 Leverage platform 
http service
URL: https://github.com/apache/camel-quarkus/pull/201#discussion_r330016874
 
 

 ##########
 File path: 
extensions/platform-http/runtime/src/main/java/org/apache/camel/quarkus/component/platform/http/runtime/QuarkusPlatformHttpConsumer.java
 ##########
 @@ -0,0 +1,376 @@
+/*
+ * 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.quarkus.component.platform.http.runtime;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.PrintWriter;
+import java.io.StringWriter;
+import java.nio.ByteBuffer;
+import java.nio.charset.StandardCharsets;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import io.vertx.core.MultiMap;
+import io.vertx.core.buffer.Buffer;
+import io.vertx.core.http.HttpMethod;
+import io.vertx.core.http.HttpServerRequest;
+import io.vertx.core.http.HttpServerResponse;
+import io.vertx.ext.web.Route;
+import io.vertx.ext.web.Router;
+import io.vertx.ext.web.RoutingContext;
+import io.vertx.ext.web.handler.BodyHandler;
+
+import org.apache.camel.Consumer;
+import org.apache.camel.Endpoint;
+import org.apache.camel.Exchange;
+import org.apache.camel.Message;
+import org.apache.camel.NoTypeConversionAvailableException;
+import org.apache.camel.Processor;
+import org.apache.camel.TypeConversionException;
+import org.apache.camel.TypeConverter;
+import org.apache.camel.component.platform.http.PlatformHttpComponent;
+import org.apache.camel.component.platform.http.PlatformHttpEndpoint;
+import org.apache.camel.component.platform.http.spi.Method;
+import org.apache.camel.spi.HeaderFilterStrategy;
+import org.apache.camel.support.DefaultConsumer;
+import org.apache.camel.support.DefaultMessage;
+import org.apache.camel.support.ExchangeHelper;
+import org.apache.camel.support.MessageHelper;
+import org.apache.camel.support.ObjectHelper;
+import org.eclipse.microprofile.config.Config;
+import org.eclipse.microprofile.config.ConfigProvider;
+import org.jboss.logging.Logger;
+
+/**
+ * A Quarkus specific {@link Consumer} for the {@link PlatformHttpComponent}.
+ */
+public class QuarkusPlatformHttpConsumer extends DefaultConsumer {
+    private static final Logger LOG = 
Logger.getLogger("org.apache.camel.quarkus.component.platform.http");
+
+    private final Router router;
+    private Route route;
+
+    public QuarkusPlatformHttpConsumer(Endpoint endpoint, Processor processor, 
Router router) {
+        super(endpoint, processor);
+        this.router = router;
+    }
+
+    @Override
+    public PlatformHttpEndpoint getEndpoint() {
+        return (PlatformHttpEndpoint) super.getEndpoint();
+    }
+
+    @Override
+    protected void doStart() throws Exception {
+        super.doStart();
+
+        final String path = getEndpoint().getPath();
+        final Route r = router.route(path);
+        final Set<Method> methods = getEndpoint().getEffectiveMethods();
+        if (!methods.equals(Method.getAll())) {
+            methods.stream().forEach(m -> 
r.method(HttpMethod.valueOf(m.name())));
+        }
+
+        Config cfg = ConfigProvider.getConfig();
+        final BodyHandler bodyHandler = BodyHandler.create();
+        /* Keep in sync with how the BodyHandler is configured in 
io.quarkus.vertx.web.runtime.VertxWebRecorder
+         * Eventually, VertxWebRecorder should have a method to do this for 
us. */
+        cfg.getOptionalValue("quarkus.http.body.handle-file-uploads", 
boolean.class).ifPresent(bodyHandler::setHandleFileUploads);
+        cfg.getOptionalValue("quarkus.http.body.uploads-directory", 
String.class).ifPresent(bodyHandler::setUploadsDirectory);
+        cfg.getOptionalValue("quarkus.http.body.delete-uploaded-files-on-end", 
boolean.class).ifPresent(bodyHandler::setDeleteUploadedFilesOnEnd);
+        cfg.getOptionalValue("quarkus.http.body.merge-form-attributes", 
boolean.class).ifPresent(bodyHandler::setMergeFormAttributes);
+        cfg.getOptionalValue("quarkus.http.body.preallocate-body-buffer", 
boolean.class).ifPresent(bodyHandler::setPreallocateBodyBuffer);
+
+        r
+            .handler(bodyHandler)
+            .handler(ctx -> {
+                try {
+                    final PlatformHttpEndpoint endpoint = getEndpoint();
+                    final HeaderFilterStrategy headerFilterStrategy = 
endpoint.getHeaderFilterStrategy();
+                    final Exchange e = toExchange(ctx, 
endpoint.createExchange(), headerFilterStrategy);
+                    getProcessor().process(e);
+                    writeResponse(ctx, e, headerFilterStrategy);
+                } catch (Exception e1) {
+                    LOG.debugf(e1, "Could not handle '%s'", path);
+                    ctx.fail(e1);
+                }
+            });
+
+        this.route = r;
+    }
+
+    @Override
+    protected void doStop() throws Exception {
+        if (route != null) {
+            route.remove();
+            route = null;
+        }
+        super.doStop();
+    }
+
+    @Override
+    protected void doSuspend() throws Exception {
+        if (route != null) {
+            route.disable();
+        }
+        super.doSuspend();
+    }
+
+    @Override
+    protected void doResume() throws Exception {
+        if (route != null) {
+            route.enable();
+        }
+        super.doResume();
+    }
+
+    Object toHttpResponse(HttpServerResponse response, Message message, 
HeaderFilterStrategy headerFilterStrategy) {
+        final Exchange exchange = message.getExchange();
+        final boolean failed = exchange.isFailed();
+        final int defaultCode = failed ? 500 : 200;
+
+        final int code = message.getHeader(Exchange.HTTP_RESPONSE_CODE, 
defaultCode, int.class);
+
+        response.setStatusCode(code);
+
+        final TypeConverter tc = exchange.getContext().getTypeConverter();
+
+        //copy headers from Message to Response
+        if (headerFilterStrategy != null) {
+            for (Map.Entry<String, Object> entry : 
message.getHeaders().entrySet()) {
+                final String key = entry.getKey();
+                final Object value = entry.getValue();
+                // use an iterator as there can be multiple values. (must not 
use a delimiter)
+                final Iterator<?> it = ObjectHelper.createIterator(value, 
null);
+                String firstValue = null;
+                List<String> values = null;
+                while (it.hasNext()) {
+                    final String headerValue = tc.convertTo(String.class, 
it.next());
+                    if (headerValue != null
+                            && 
!headerFilterStrategy.applyFilterToCamelHeaders(key, headerValue, exchange)) {
+                        if (firstValue == null) {
+                            firstValue = headerValue;
+                        } else {
+                            if (values == null) {
+                                values = new ArrayList<String>();
+                                values.add(firstValue);
+                            }
+                            values.add(headerValue);
+                        }
+                    }
+                }
+                if (values != null) {
+                    response.putHeader(key, values);
+                } else if (firstValue != null) {
+                    response.putHeader(key, firstValue);
+                }
+            }
+        }
+
+        Object body = message.getBody();
+        final Exception exception = exchange.getException();
+
+        if (exception != null) {
+            // we failed due an exception so print it as plain text
+            final StringWriter sw = new StringWriter();
+            final PrintWriter pw = new PrintWriter(sw);
+            exception.printStackTrace(pw);
+
+            // the body should then be the stacktrace
+            body = 
ByteBuffer.wrap(sw.toString().getBytes(StandardCharsets.UTF_8));
+            // force content type to be text/plain as that is what the 
stacktrace is
+            message.setHeader(Exchange.CONTENT_TYPE, "text/plain; 
charset=utf-8");
+
+            // and mark the exception as failure handled, as we handled it by 
returning it as the response
+            ExchangeHelper.setFailureHandled(exchange);
+        }
+
+        // set the content type in the response.
+        final String contentType = MessageHelper.getContentType(message);
+        if (contentType != null) {
+            // set content-type
+            response.putHeader("Content-Type", contentType);
+        }
+        return body;
+    }
+
+    void writeResponse(RoutingContext ctx, Exchange camelExchange, 
HeaderFilterStrategy headerFilterStrategy) {
+        final Object body = toHttpResponse(ctx.response(), 
camelExchange.getMessage(), headerFilterStrategy);
+
+        final HttpServerResponse response = ctx.response();
+        if (body == null) {
+            QuarkusPlatformHttpEngine.LOG.tracef("No payload to send as reply 
for exchange: %s", camelExchange);
+            response.putHeader("Content-Type", "text/plain; charset=utf-8");
+            response.end("No response available");
 
 Review comment:
   I personally find the current code good enough, but adding a note to my TODO 
list that this case should be covered by a test. BTW, which of the existing 
HTTP components is the most up to date? - i.e. where to look for the desired 
behavior?

----------------------------------------------------------------
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.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to