Skip to content

Commit c975754

Browse files
Add support for Http outbound component (#95)
1 parent 265236a commit c975754

File tree

4 files changed

+125
-53
lines changed

4 files changed

+125
-53
lines changed

components/sbm-recipes-mule-to-boot/src/main/java/org/springframework/sbm/mule/actions/javadsl/translators/http/HttpRequestTranslator.java

+43-50
Original file line numberDiff line numberDiff line change
@@ -15,26 +15,17 @@
1515
*/
1616
package org.springframework.sbm.mule.actions.javadsl.translators.http;
1717

18-
import freemarker.template.Configuration;
19-
import freemarker.template.Template;
20-
import freemarker.template.TemplateException;
2118
import org.mulesoft.schema.mule.http.RequestConfigType;
2219
import org.mulesoft.schema.mule.http.RequestType;
23-
import org.springframework.beans.factory.annotation.Autowired;
2420
import org.springframework.sbm.mule.actions.javadsl.translators.DslSnippet;
2521
import org.springframework.sbm.mule.actions.javadsl.translators.MuleComponentToSpringIntegrationDslTranslator;
2622
import org.springframework.sbm.mule.api.toplevel.configuration.ConfigurationTypeAdapter;
2723
import org.springframework.sbm.mule.api.toplevel.configuration.MuleConfigurations;
2824
import org.springframework.stereotype.Component;
2925

3026
import javax.xml.namespace.QName;
31-
import java.io.IOException;
32-
import java.io.StringReader;
33-
import java.io.StringWriter;
34-
import java.util.Collections;
35-
import java.util.HashMap;
36-
import java.util.Map;
37-
import java.util.Optional;
27+
import java.util.*;
28+
3829

3930
/**
4031
* Translator for {@code <http:request> } elements.spring integration
@@ -46,59 +37,61 @@
4637
@Component
4738
public class HttpRequestTranslator implements MuleComponentToSpringIntegrationDslTranslator<RequestType> {
4839

49-
@Autowired
50-
private Configuration configuration;
51-
5240
@Override
5341
public Class getSupportedMuleType() {
5442
return RequestType.class;
5543
}
5644

45+
private static final String template = " .headerFilter(\"accept-encoding\", false)\n" +
46+
" .handle(\n" +
47+
" Http.outboundGateway(\"$PROTOCOL://$HOST:$PORT$PATH\")\n" +
48+
" .httpMethod(HttpMethod.$METHOD)\n" +
49+
" //FIXME: Use appropriate response class type here instead of String.class\n" +
50+
" .expectedResponseType(String.class)\n" +
51+
" )";
52+
5753
@Override
5854
public DslSnippet translate(RequestType component,
5955
QName name,
6056
MuleConfigurations muleConfigurations,
6157
String flowName) {
6258

59+
RequestConfigType config = getRequestConfiguration(component, muleConfigurations);
60+
return new DslSnippet(
61+
template
62+
.replace("$PATH", emptyStringIfNull(component.getPath()))
63+
.replace("$METHOD", defaultToValueIfNull(component.getMethod(), "GET"))
64+
.replace("$HOST", emptyStringIfNull(config.getHost()))
65+
.replace("$PORT", emptyStringIfNull(config.getPort()))
66+
.replace("$PROTOCOL", defaultToValueIfNull(config.getProtocol(), "http").toLowerCase())
67+
,
68+
Set.of("org.springframework.http.HttpMethod")
69+
);
70+
}
71+
72+
private RequestConfigType getRequestConfiguration(RequestType component, MuleConfigurations muleConfigurations) {
73+
RequestConfigType emptyRequestConfig = new RequestConfigType();
74+
75+
ConfigurationTypeAdapter<RequestConfigType> configurationTypeAdapter =
76+
muleConfigurations.getConfigurations().get(component.getConfigRef());
6377

64-
String templateStr = "return IntegrationFlows\n" +
65-
" .from\n" +
66-
" (\n" +
67-
" Http.inboundChannelAdapter(\"${host}<#if port?has_content>:${port}</#if>/${basePath}\")\n" +
68-
" .requestMapping(m -> m.methods(HttpMethod.GET))\n" +
69-
"<#if responseTimeout?has_content>" +
70-
" .replyTimeout(${responseTimeout})\n" +
71-
"</#if>" +
72-
" )\n" +
73-
" .channel(INBOUND_DEMO_CHANNEL)\n" +
74-
" .get();";
75-
76-
try {
77-
Map<String, Object> data = new HashMap<>();
78-
79-
// TODO: requires access to config, e.g. muleMigrationContext.getConfigRef("...")
80-
String configRef = component.getConfigRef();
81-
Optional<? extends ConfigurationTypeAdapter> configurationTypeAdapter = muleConfigurations.find(configRef);
82-
if(configurationTypeAdapter.isPresent()) {
83-
RequestConfigType cast = (RequestConfigType) configurationTypeAdapter.get().getMuleConfiguration();
84-
data.put("host", cast.getHost());
85-
data.put("port", cast.getPort());
86-
}
87-
88-
89-
data.put("basePath", component.getPath());
90-
data.put("method", component.getMethod());
91-
data.put("responseTimeout", component.getResponseTimeout());
92-
Template t = new Template("name", new StringReader(templateStr), configuration);
93-
StringWriter stringWriter = new StringWriter();
94-
t.process(data,
95-
stringWriter);
96-
return new DslSnippet(stringWriter.toString(), Collections.emptySet());
97-
} catch (IOException | TemplateException e) {
98-
e.printStackTrace();
78+
if (configurationTypeAdapter == null) {
79+
80+
return emptyRequestConfig;
9981
}
10082

83+
RequestConfigType requestConfig = configurationTypeAdapter
84+
.getMuleConfiguration();
85+
86+
return requestConfig != null ? requestConfig : emptyRequestConfig;
87+
}
88+
89+
private String defaultToValueIfNull(String originalValue, String defaultValue) {
90+
91+
return originalValue == null ? defaultValue : originalValue;
92+
}
10193

102-
return null;
94+
private String emptyStringIfNull(String value) {
95+
return value == null ? "" : value;
10396
}
10497
}

components/sbm-recipes-mule-to-boot/src/main/java/org/springframework/sbm/mule/api/toplevel/configuration/MuleConfigurationsExtractor.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,11 @@ public Map<String, ConfigurationTypeAdapter<?>> extractAllConfigurations(List<Mu
5151

5252
public static boolean isConfigType(JAXBElement jaxbElement) {
5353
Class<?> aClass = jaxbElement.getValue().getClass();
54-
Set<Class> configTypes = Set.of(RequestConfigType.class, ConfigurationType.class, AbstractConnectorType.class, ListenerConfigType.class);
54+
Set<Class> configTypes = Set.of(RequestConfigType.class,
55+
ConfigurationType.class,
56+
AbstractConnectorType.class,
57+
ListenerConfigType.class
58+
);
5559
return configTypes.stream()
5660
.anyMatch(ct -> ct.isAssignableFrom(aClass));
5761
}

components/sbm-recipes-mule-to-boot/src/test/java/org/springframework/sbm/mule/actions/JavaDSLActionBaseTest.java

+6-2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
import org.springframework.sbm.mule.actions.javadsl.translators.dwl.DwlTransformTranslator;
3131
import org.springframework.sbm.mule.actions.javadsl.translators.http.HttpListenerConfigTypeAdapter;
3232
import org.springframework.sbm.mule.actions.javadsl.translators.http.HttpListenerTranslator;
33+
import org.springframework.sbm.mule.actions.javadsl.translators.http.HttpRequestTranslator;
34+
import org.springframework.sbm.mule.actions.javadsl.translators.http.RequestConfigTypeAdapter;
3335
import org.springframework.sbm.mule.actions.javadsl.translators.logging.LoggingTranslator;
3436
import org.springframework.sbm.mule.actions.javadsl.translators.wmq.WmqConnectorTypeAdapter;
3537
import org.springframework.sbm.mule.actions.javadsl.translators.wmq.WmqInboundEndpointTranslator;
@@ -73,7 +75,8 @@ public void setup() {
7375
new TransformerTranslator(),
7476
new WmqOutboundEndpointTranslator(),
7577
new WmqInboundEndpointTranslator(),
76-
new DwlTransformTranslator()
78+
new DwlTransformTranslator(),
79+
new HttpRequestTranslator()
7780
);
7881
List<TopLevelElementFactory> topLevelTypeFactories = List.of(
7982
new FlowTopLevelElementFactory(translators),
@@ -84,7 +87,8 @@ public void setup() {
8487
List.of(
8588
new AmqpConfigTypeAdapter(),
8689
new HttpListenerConfigTypeAdapter(),
87-
new WmqConnectorTypeAdapter()
90+
new WmqConnectorTypeAdapter(),
91+
new RequestConfigTypeAdapter()
8892
)
8993
);
9094
MuleMigrationContextFactory muleMigrationContextFactory = new MuleMigrationContextFactory(new MuleConfigurationsExtractor(configurationTypeAdapterFactory));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* Copyright 2021 - 2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.sbm.mule.actions;
17+
18+
import org.junit.jupiter.api.Test;
19+
20+
import static org.assertj.core.api.Assertions.assertThat;
21+
22+
public class MuleToJavaDSLHttpOutbound extends JavaDSLActionBaseTest {
23+
private static final String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
24+
"\n" +
25+
"<mule xmlns:json=\"http://www.mulesoft.org/schema/mule/json\" xmlns:http=\"http://www.mulesoft.org/schema/mule/http\" xmlns=\"http://www.mulesoft.org/schema/mule/core\" xmlns:doc=\"http://www.mulesoft.org/schema/mule/documentation\"\n" +
26+
" xmlns:spring=\"http://www.springframework.org/schema/beans\" \n" +
27+
" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n" +
28+
" xsi:schemaLocation=\"http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd\n" +
29+
"http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd\n" +
30+
"http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd\n" +
31+
"http://www.mulesoft.org/schema/mule/json http://www.mulesoft.org/schema/mule/json/current/mule-json.xsd\">\n" +
32+
" <http:listener-config name=\"HTTP_Listener_Configuration1\" host=\"0.0.0.0\" port=\"9082\" doc:name=\"HTTP Listener Configuration\"/>\n" +
33+
" <http:request-config name=\"HTTP_Request_Configuration\" host=\"catfact.ninja\" port=\"443\" doc:name=\"HTTP Request Configuration\" protocol=\"HTTPS\"/>\n" +
34+
" <flow name=\"httpFlow\">\n" +
35+
" <http:listener config-ref=\"HTTP_Listener_Configuration1\" path=\"/gimme-a-cat-fact\" doc:name=\"HTTP\"/>\n" +
36+
" <http:request config-ref=\"HTTP_Request_Configuration\" path=\"/fact\" method=\"GET\" doc:name=\"HTTP\"/>\n" +
37+
" <set-payload doc:name=\"Set Payload\" value=\"#[payload]\"/>\n" +
38+
" </flow>\n" +
39+
"</mule>";
40+
41+
@Test
42+
public void supportForHttpOutboundRequest() {
43+
addXMLFileToResource(xml);
44+
runAction();
45+
assertThat(projectContext.getProjectJavaSources().list()).hasSize(1);
46+
assertThat(projectContext.getProjectJavaSources().list().get(0).print())
47+
.isEqualTo("package com.example.javadsl;\n" +
48+
"import org.springframework.context.annotation.Bean;\n" +
49+
"import org.springframework.context.annotation.Configuration;\n" +
50+
"import org.springframework.http.HttpMethod;\n" +
51+
"import org.springframework.integration.dsl.IntegrationFlow;\n" +
52+
"import org.springframework.integration.dsl.IntegrationFlows;\n" +
53+
"import org.springframework.integration.http.dsl.Http;\n" +
54+
"\n" +
55+
"@Configuration\n" +
56+
"public class FlowConfigurations {\n" +
57+
" @Bean\n" +
58+
" IntegrationFlow httpFlow() {\n" +
59+
" return IntegrationFlows.from(Http.inboundChannelAdapter(\"/gimme-a-cat-fact\")).handle((p, h) -> p)\n" +
60+
" .headerFilter(\"accept-encoding\", false)\n" +
61+
" .handle(\n" +
62+
" Http.outboundGateway(\"https://catfact.ninja:443/fact\")\n" +
63+
" .httpMethod(HttpMethod.GET)\n" +
64+
" //FIXME: Use appropriate response class type here instead of String.class\n" +
65+
" .expectedResponseType(String.class)\n" +
66+
" )\n" +
67+
" .handle((p, h) -> \"#[payload]\")\n" +
68+
" .get();\n" +
69+
" }}");
70+
}
71+
}

0 commit comments

Comments
 (0)