Skip to content

Commit

Permalink
Property substitution doesn't happen in from URI #392
Browse files Browse the repository at this point in the history
  • Loading branch information
lburgazzoli committed Jul 14, 2020
1 parent 4cdf93e commit 3b07d3d
Show file tree
Hide file tree
Showing 16 changed files with 277 additions and 130 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,23 @@
*/
package org.apache.camel.k.loader.yaml.parser;

import java.io.UnsupportedEncodingException;
import java.net.URISyntaxException;
import java.util.List;
import java.util.Map;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import org.apache.camel.k.annotation.yaml.YAMLNodeDefinition;
import org.apache.camel.k.annotation.yaml.YAMLStepParser;
import org.apache.camel.k.loader.yaml.model.Step;
import org.apache.camel.k.loader.yaml.spi.StartStepParser;
import org.apache.camel.k.loader.yaml.spi.StepParserSupport;
import org.apache.camel.model.RouteDefinition;
import org.apache.camel.util.URISupport;

@YAMLStepParser(id = "from", definition = FromStepParser.Definition.class)
public class FromStepParser implements StartStepParser {
@Override
public Object process(Context context) {
final Definition definition = context.node(Definition.class);
final String uri = definition.getEndpointUri();
final String uri = StepParserSupport.createEndpointUri(definition.uri, definition.parameters);
final RouteDefinition route = context.builder().from(uri);

// as this is a start converter, steps are mandatory
Expand All @@ -57,21 +53,6 @@ public static final class Definition {
public Map<String, Object> parameters;
@JsonProperty(required = true)
public List<Step> steps;

@JsonIgnore
public String getEndpointUri() {
String answer = uri;

if (parameters != null) {
try {
answer = URISupport.appendParametersToURI(answer, parameters);
} catch (URISyntaxException | UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
}

return answer;
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,9 @@
*/
package org.apache.camel.k.loader.yaml.parser;

import java.io.UnsupportedEncodingException;
import java.net.URISyntaxException;
import java.util.List;
import java.util.Map;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import org.apache.camel.k.annotation.yaml.YAMLNodeDefinition;
import org.apache.camel.k.annotation.yaml.YAMLStepParser;
Expand All @@ -30,14 +27,13 @@
import org.apache.camel.k.loader.yaml.spi.StepParserSupport;
import org.apache.camel.model.RouteDefinition;
import org.apache.camel.util.ObjectHelper;
import org.apache.camel.util.URISupport;

@YAMLStepParser(id = "route", definition = RouteStepParser.Definition.class)
public class RouteStepParser implements StartStepParser {
@Override
public Object process(Context context) {
final Definition definition = context.node(Definition.class);
final String uri = definition.from.getEndpointUri();
final String uri = StepParserSupport.createEndpointUri(definition.from.uri, definition.from.parameters);
final RouteDefinition route = context.builder().from(uri);

ObjectHelper.ifNotEmpty(definition.id, route::routeId);
Expand Down Expand Up @@ -78,21 +74,6 @@ public From() {
public From(String uri) {
this.uri = uri;
}

@JsonIgnore
public String getEndpointUri() {
String answer = uri;

if (parameters != null) {
try {
answer = URISupport.appendParametersToURI(answer, parameters);
} catch (URISyntaxException | UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
}

return answer;
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,23 @@
*/
package org.apache.camel.k.loader.yaml.parser;

import java.io.UnsupportedEncodingException;
import java.net.URISyntaxException;
import java.util.Map;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import org.apache.camel.k.annotation.yaml.YAMLNodeDefinition;
import org.apache.camel.k.annotation.yaml.YAMLStepParser;
import org.apache.camel.k.loader.yaml.spi.ProcessorStepParser;
import org.apache.camel.k.loader.yaml.spi.StepParserSupport;
import org.apache.camel.model.ProcessorDefinition;
import org.apache.camel.model.ToDynamicDefinition;
import org.apache.camel.util.URISupport;

@YAMLStepParser(id = "tod", definition = ToDynamicStepParser.Definition.class)
public class ToDynamicStepParser implements ProcessorStepParser {
@Override
public ProcessorDefinition<?> toProcessor(Context context) {
final Definition definition = context.node(Definition.class);
final ToDynamicDefinition answer = new ToDynamicDefinition(definition.getEndpointUri());
final String uri = StepParserSupport.createEndpointUri(definition.getUri(), definition.parameters);
final ToDynamicDefinition answer = new ToDynamicDefinition(uri);

return answer;
}
Expand All @@ -50,21 +48,6 @@ public Definition() {
public Definition(String uri) {
super(uri);
}

@JsonIgnore
public String getEndpointUri() {
String answer = getUri();

if (parameters != null) {
try {
answer = URISupport.appendParametersToURI(answer, parameters);
} catch (URISyntaxException | UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
}

return answer;
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,23 @@
*/
package org.apache.camel.k.loader.yaml.parser;

import java.io.UnsupportedEncodingException;
import java.net.URISyntaxException;
import java.util.Map;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import org.apache.camel.k.annotation.yaml.YAMLNodeDefinition;
import org.apache.camel.k.annotation.yaml.YAMLStepParser;
import org.apache.camel.k.loader.yaml.spi.ProcessorStepParser;
import org.apache.camel.k.loader.yaml.spi.StepParserSupport;
import org.apache.camel.model.ProcessorDefinition;
import org.apache.camel.model.ToDefinition;
import org.apache.camel.util.URISupport;

@YAMLStepParser(id = "to", definition = ToStepParser.Definition.class)
public class ToStepParser implements ProcessorStepParser {
@Override
public ProcessorDefinition<?> toProcessor(Context context) {
final Definition definition = context.node(Definition.class);
final ToDefinition answer = new ToDefinition(definition.getEndpointUri());
final String uri = StepParserSupport.createEndpointUri(definition.getUri(), definition.parameters);
final ToDefinition answer = new ToDefinition(uri);

return answer;
}
Expand All @@ -50,21 +48,6 @@ public Definition() {
public Definition(String uri) {
super(uri);
}

@JsonIgnore
public String getEndpointUri() {
String answer = uri;

if (parameters != null) {
try {
answer = URISupport.appendParametersToURI(answer, parameters);
} catch (URISyntaxException | UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
}

return answer;
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,13 @@
*/
package org.apache.camel.k.loader.yaml.parser;

import java.io.UnsupportedEncodingException;
import java.net.URISyntaxException;
import java.util.List;
import java.util.Map;

import com.fasterxml.jackson.annotation.JsonIgnore;
import org.apache.camel.k.annotation.yaml.YAMLNodeDefinition;
import org.apache.camel.k.annotation.yaml.YAMLStepParser;
import org.apache.camel.k.loader.yaml.spi.ProcessorStepParser;
import org.apache.camel.k.loader.yaml.spi.StepParserSupport;
import org.apache.camel.model.ExpressionSubElementDefinition;
import org.apache.camel.model.ProcessorDefinition;
import org.apache.camel.model.SetHeaderDefinition;
Expand All @@ -33,7 +31,6 @@
import org.apache.camel.model.language.ExpressionDefinition;
import org.apache.camel.reifier.WireTapReifier;
import org.apache.camel.util.ObjectHelper;
import org.apache.camel.util.URISupport;

@YAMLStepParser(id = "wiretap", definition = WireTapStepParser.Definition.class)
public class WireTapStepParser implements ProcessorStepParser {
Expand All @@ -56,7 +53,9 @@ public ProcessorDefinition<?> toProcessor(Context context) {
}
}

answer.setUri(definition.getEndpointUri());
answer.setUri(
StepParserSupport.createEndpointUri(definition.getUri(), definition.parameters)
);

return answer;
}
Expand All @@ -70,21 +69,6 @@ public static final class Definition extends ToDynamicDefinition {
public Boolean dynamicUri;
public NewExchangeDefinition newExchange;
public Map<String, Object> parameters;

@JsonIgnore
public String getEndpointUri() {
String answer = getUri();

if (parameters != null) {
try {
answer = URISupport.appendParametersToURI(answer, parameters);
} catch (URISyntaxException | UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
}

return answer;
}
}

@YAMLNodeDefinition
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
package org.apache.camel.k.loader.yaml.spi;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.apache.camel.k.loader.yaml.model.Step;
import org.apache.camel.model.OutputNode;
Expand Down Expand Up @@ -60,4 +62,21 @@ public static ProcessorDefinition<?> convertSteps(ProcessorStepParser.Context co

return parent;
}

public static String createEndpointUri(String uri, Map<String, Object> parameters) {
String answer = uri;

if (parameters != null) {
String queryString = parameters.entrySet().stream()
.filter(entry -> entry.getValue() != null)
.map(entry -> String.format("%s=%s", entry.getKey(), entry.getValue()))
.collect(Collectors.joining("&"));

if (ObjectHelper.isNotEmpty(queryString)) {
answer += "?" + queryString;
}
}

return answer;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
* 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.k.loader.yaml

import org.apache.camel.component.direct.DirectEndpoint

class RouteWithPlaceholdersTest extends TestSupport {
def 'route'() {
setup:
def parameters = [
'direct.id': 'myDirect',
'direct.timeout': 1234,
'direct.result': UUID.randomUUID().toString()
]
def context = startContext {
propertiesComponent.initialProperties = parameters as Properties
}
when:
def uri = context.resolvePropertyPlaceholders('direct://{{direct.id}}?timeout={{direct.timeout}}')
def out = template(context).to(uri).request(String.class)
then:
out == parameters['direct.result']
cleanup:
context?.stop()
}
def 'from'() {
setup:
def parameters = [
'direct.id': 'myDirect',
'direct.timeout': 1234,
'direct.result': UUID.randomUUID().toString()
]
def context = startContext {
propertiesComponent.initialProperties = parameters as Properties
}
when:
def uri = context.resolvePropertyPlaceholders('direct://{{direct.id}}?timeout={{direct.timeout}}')
def out = template(context).to(uri).request(String.class)
def eps = context.getEndpoints().find { it instanceof DirectEndpoint }
then:
out == parameters['direct.result']
with (eps, DirectEndpoint) {
timeout == parameters['direct.timeout']
}
cleanup:
context?.stop()
}

def 'to'() {
setup:
def parameters = [
'direct.id': 'myDirect',
'direct.timeout': 1234,
'direct.result': UUID.randomUUID().toString()
]
def context = startContext {
propertiesComponent.initialProperties = parameters as Properties
}
when:
def out = template(context).to('direct:start').request(String.class)
then:
out == parameters['direct.result']
cleanup:
context?.stop()
}

def 'tod'() {
setup:
def parameters = [
'direct.id': 'myDirect',
'direct.timeout': 1234,
'direct.result': UUID.randomUUID().toString()
]
def context = startContext {
propertiesComponent.initialProperties = parameters as Properties
}
when:
def out = template(context).to('direct:start').request(String.class)
then:
out == parameters['direct.result']
cleanup:
context?.stop()
}
}
Loading

0 comments on commit 3b07d3d

Please sign in to comment.