Skip to content

Commit

Permalink
Temporarily work around Vert.x incompatibilities between Camel & Quarkus
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesnetherton committed Jul 7, 2022
1 parent 46a5268 commit f87d9d6
Show file tree
Hide file tree
Showing 5 changed files with 145 additions and 3 deletions.
5 changes: 5 additions & 0 deletions extensions/platform-http/runtime/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@
<groupId>io.quarkus</groupId>
<artifactId>quarkus-vertx-http</artifactId>
</dependency>
<dependency>
<groupId>org.graalvm.nativeimage</groupId>
<artifactId>svm</artifactId>
<scope>provided</scope>
</dependency>

</dependencies>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* 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.platform.http.vertx;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.vertx.core.Handler;
import io.vertx.core.MultiMap;
import io.vertx.core.buffer.Buffer;
import io.vertx.ext.web.RoutingContext;
import org.apache.camel.Exchange;
import org.apache.camel.Message;
import org.apache.camel.Processor;
import org.apache.camel.component.platform.http.PlatformHttpEndpoint;
import org.apache.camel.component.platform.http.spi.Method;
import org.apache.camel.spi.HeaderFilterStrategy;

import static org.apache.camel.component.platform.http.vertx.VertxPlatformHttpSupport.appendHeader;
import static org.apache.camel.component.platform.http.vertx.VertxPlatformHttpSupport.populateCamelHeaders;

// TODO: Remove when Camel / Quarkus Vert.x version is in sync https://github.com/apache/camel-quarkus/issues/3877
public final class CamelQuarkusVertxPlatformHttpConsumer extends VertxPlatformHttpConsumer {

public CamelQuarkusVertxPlatformHttpConsumer(PlatformHttpEndpoint endpoint, Processor processor,
List<Handler<RoutingContext>> handlers) {
super(endpoint, processor, handlers);
}

@Override
protected Message toCamelMessage(RoutingContext ctx, Exchange exchange) {
final Message result = exchange.getIn();

final HeaderFilterStrategy headerFilterStrategy = getEndpoint().getHeaderFilterStrategy();
populateCamelHeaders(ctx, result.getHeaders(), exchange, headerFilterStrategy);
final String mimeType = ctx.parsedHeaders().contentType().value();
final boolean isMultipartFormData = "multipart/form-data".equals(mimeType);
if ("application/x-www-form-urlencoded".equals(mimeType) || isMultipartFormData) {
final MultiMap formData = ctx.request().formAttributes();
final Map<String, Object> body = new HashMap<>();
for (String key : formData.names()) {
for (String value : formData.getAll(key)) {
if (headerFilterStrategy != null
&& !headerFilterStrategy.applyFilterToExternalHeaders(key, value, exchange)) {
appendHeader(result.getHeaders(), key, value);
appendHeader(body, key, value);
}
}
}

if (!body.isEmpty()) {
result.setBody(body);
}

if (isMultipartFormData) {
populateAttachments(new ArrayList<>(ctx.fileUploads()), result);
}
} else {
Method m = Method.valueOf(ctx.request().method().name());
if (m.canHaveBody()) {
final Buffer body = ctx.getBody();
if (body != null) {
result.setBody(body);
} else {
result.setBody(null);
}
} else {
result.setBody(null);
}
}
return result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* 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.graal;

import com.oracle.svm.core.annotate.Substitute;
import com.oracle.svm.core.annotate.TargetClass;
import io.vertx.ext.web.RoutingContext;
import org.apache.camel.Exchange;
import org.apache.camel.Message;
import org.apache.camel.component.platform.http.vertx.VertxPlatformHttpConsumer;

@TargetClass(VertxPlatformHttpConsumer.class)
public final class PlatformHttpVertxSubstitutions {

// Remove incompatible Vert.x API usage. This method is overridden in
// CamelQuarkusVertxPlatformHttpConsumer. So it's safe to effectively replace
// the original method content and return null
@Substitute
protected Message toCamelMessage(RoutingContext ctx, Exchange exchange) {
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,19 @@
import io.vertx.core.Vertx;
import io.vertx.ext.web.Router;
import io.vertx.ext.web.RoutingContext;
import org.apache.camel.Consumer;
import org.apache.camel.Processor;
import org.apache.camel.component.platform.http.PlatformHttpComponent;
import org.apache.camel.component.platform.http.PlatformHttpEndpoint;
import org.apache.camel.component.platform.http.spi.PlatformHttpEngine;
import org.apache.camel.component.platform.http.vertx.CamelQuarkusVertxPlatformHttpConsumer;
import org.apache.camel.component.platform.http.vertx.VertxPlatformHttpEngine;
import org.apache.camel.component.platform.http.vertx.VertxPlatformHttpRouter;

@Recorder
public class PlatformHttpRecorder {
public RuntimeValue<PlatformHttpEngine> createEngine() {
return new RuntimeValue<>(new VertxPlatformHttpEngine());
return new RuntimeValue<>(new CamelQuarkusVertxPlatformHttpEngine());
}

public RuntimeValue<PlatformHttpComponent> createComponent(RuntimeValue<PlatformHttpEngine> engine) {
Expand All @@ -50,4 +54,12 @@ public Handler<RoutingContext> bodyHandler() {
};
return new RuntimeValue<>(vertxPlatformHttpRouter);
}

// TODO: Remove when Camel / Quarkus Vert.x version is in sync https://github.com/apache/camel-quarkus/issues/3877
static final class CamelQuarkusVertxPlatformHttpEngine extends VertxPlatformHttpEngine {
@Override
public Consumer createConsumer(PlatformHttpEndpoint endpoint, Processor processor) {
return new CamelQuarkusVertxPlatformHttpConsumer(endpoint, processor, getHandlers());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@
import io.restassured.http.ContentType;
import io.restassured.http.Method;
import org.apache.camel.component.platform.http.PlatformHttpComponent;
import org.apache.camel.component.platform.http.vertx.VertxPlatformHttpEngine;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;

import static org.hamcrest.CoreMatchers.endsWith;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.Matchers.notNullValue;
Expand Down Expand Up @@ -357,7 +357,7 @@ public void registrySetUp() {
.then()
.statusCode(200)
.body(
"engine", is(VertxPlatformHttpEngine.class.getName()),
"engine", endsWith("CamelQuarkusVertxPlatformHttpEngine"),
"component", is(PlatformHttpComponent.class.getName()));
}

Expand Down

0 comments on commit f87d9d6

Please sign in to comment.