Skip to content

Commit

Permalink
Cannot use kamelets in yaml integrations #581
Browse files Browse the repository at this point in the history
  • Loading branch information
lburgazzoli committed Jan 11, 2021
1 parent f9d9ba3 commit aa70150
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,54 +76,65 @@ public static String createEndpointUri(CamelContext context, String uri, Map<Str
String answer = uri;

if (parameters == null || parameters.isEmpty()) {
//
// nothing to do here, there are no parameters so we can return the
// uri as it is.
//
return answer;
}
if (uri.indexOf('?') != -1) {
//
// we support URIs defined as scheme only or scheme and path params,
// query params are not supported so a definition like:
//
// - from:
// uri: "foo:bar?option1=value1"
// parameters:
// option2: value2
//
// is not supported and leads to the an IllegalArgumentException being
// thrown.
//
throw new IllegalArgumentException("Uri should not contains query params (uri: " + uri + ")");
}

final String scheme = uri.contains(":") ? StringHelper.before(uri, ":") : uri;
final EndpointUriFactory factory = context.adapt(ExtendedCamelContext.class).getEndpointUriFactory(scheme);

if (uri.contains(":")) {
final String scheme = StringHelper.before(uri, ":");
final EndpointUriFactory factory = getEndpointUriFactory(context, scheme);

// we want sorted parameters
Map<String, Object> map = new TreeMap<>(parameters);
for (String secretParameter : factory.secretPropertyNames()) {
Object val = map.get(secretParameter);
if (val instanceof String) {
String newVal = (String) val;
if (!newVal.startsWith("#") && !newVal.startsWith("RAW(")) {
map.put(secretParameter, "RAW(" + val + ")");
try {
if (factory != null && factory.isEnabled(scheme)) {
if (scheme.equals(uri)) {
//
// if the uri is expressed as simple scheme, then we can use the
// discovered EndpointUriFactory to build the uri
//
answer = factory.buildUri(scheme, parameters, false);
} else {
//
// otherwise we have to compose it but we can still leverage the
// discovered EndpointUriFactory to properly handle secrets
//
Map<String, Object> options = new TreeMap<>(parameters);

for (String secretParameter : factory.secretPropertyNames()) {
Object val = options.get(secretParameter);
if (val instanceof String) {
String newVal = (String) val;
if (!newVal.startsWith("#") && !newVal.startsWith("RAW(")) {
options.put(secretParameter, "RAW(" + val + ")");
}
}
}
}
}

try {
String queryString = URISupport.createQueryString(map, false);
if (ObjectHelper.isNotEmpty(queryString)) {
answer += "?" + queryString;
answer += "?" + URISupport.createQueryString(options, false);
}
} catch (URISyntaxException e) {
throw new IllegalArgumentException(e);
}
} else {
try {
answer = getEndpointUriFactory(context, uri).buildUri(uri, parameters);
} catch (URISyntaxException e) {
throw new IllegalArgumentException(e);
} else {
answer += "?" + URISupport.createQueryString(parameters, false);
}
} catch (URISyntaxException e) {
throw new IllegalArgumentException(e);
}

return answer;
}

public static EndpointUriFactory getEndpointUriFactory(CamelContext context, String scheme) {
final EndpointUriFactory factory = context.adapt(ExtendedCamelContext.class).getEndpointUriFactory(scheme);

if (factory == null) {
throw new IllegalArgumentException("Cannot compute endpoint URI: unable to find an EndpointUriFactory for scheme " + scheme);
}
if (!factory.isEnabled(scheme)) {
throw new IllegalArgumentException("Cannot compute endpoint URI: scheme " + scheme + " is not enabled");
}

return factory;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"supportLevel": "Preview",
"groupId": "org.apache.camel.k",
"artifactId": "camel-kamelet-reify",
"version": "1.6.0-SNAPSHOT",
"version": "1.7.0-SNAPSHOT",
"scheme": "kamelet-reify",
"extendsScheme": "",
"syntax": "kamelet-reify:delegateUri",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"supportLevel": "Preview",
"groupId": "org.apache.camel.k",
"artifactId": "camel-kamelet",
"version": "1.6.0-SNAPSHOT",
"version": "1.7.0-SNAPSHOT",
"scheme": "kamelet",
"extendsScheme": "",
"syntax": "kamelet:templateId\/routeId",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"supportLevel": "Preview",
"groupId": "org.apache.camel.k",
"artifactId": "camel-knative",
"version": "1.6.0-SNAPSHOT",
"version": "1.7.0-SNAPSHOT",
"scheme": "knative",
"extendsScheme": "",
"syntax": "knative:type\/typeId",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ public JsonObject inspect() {
}

@GET
@Path("/execute")
@Path("/execute/{id}")
@Produces(MediaType.TEXT_PLAIN)
public String execute() {
return template.to("direct:process").request(String.class);
public String execute(@PathParam("id") String id) {
return template.to("direct:" + id).request(String.class);
}

@POST
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,20 @@ public void inspect() {
}

@Test
public void execute() {
public void invokeProcess() {
RestAssured.given()
.accept(MediaType.TEXT_PLAIN)
.get("/test/execute")
.get("/test/execute/process")
.then()
.statusCode(200)
.body(is("template"));
}

@Test
public void invokeProcessWithParams() {
RestAssured.given()
.accept(MediaType.TEXT_PLAIN)
.get("/test/execute/process-params")
.then()
.statusCode(200)
.body(is("template"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,6 @@ camel.k.sources[1].property-names[0] = message
# camel-k - sources (routes)
#
camel.k.sources[2].location = file:{{env:ROUTES_DIR}}/process.yaml
camel.k.sources[2].type = source
camel.k.sources[2].type = source
camel.k.sources[3].location = file:{{env:ROUTES_DIR}}/process-params.yaml
camel.k.sources[3].type = source
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#
# 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.
#

- from:
uri: "direct:process-params"
steps:
- to:
uri: "kamelet:set-body"
parameters:
bodyValue: "template"

0 comments on commit aa70150

Please sign in to comment.