|
15 | 15 | */
|
16 | 16 | package org.springframework.sbm.mule.actions.javadsl.translators.http;
|
17 | 17 |
|
18 |
| -import freemarker.template.Configuration; |
19 |
| -import freemarker.template.Template; |
20 |
| -import freemarker.template.TemplateException; |
21 | 18 | import org.mulesoft.schema.mule.http.RequestConfigType;
|
22 | 19 | import org.mulesoft.schema.mule.http.RequestType;
|
23 |
| -import org.springframework.beans.factory.annotation.Autowired; |
24 | 20 | import org.springframework.sbm.mule.actions.javadsl.translators.DslSnippet;
|
25 | 21 | import org.springframework.sbm.mule.actions.javadsl.translators.MuleComponentToSpringIntegrationDslTranslator;
|
26 | 22 | import org.springframework.sbm.mule.api.toplevel.configuration.ConfigurationTypeAdapter;
|
27 | 23 | import org.springframework.sbm.mule.api.toplevel.configuration.MuleConfigurations;
|
28 | 24 | import org.springframework.stereotype.Component;
|
29 | 25 |
|
30 | 26 | 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 | + |
38 | 29 |
|
39 | 30 | /**
|
40 | 31 | * Translator for {@code <http:request> } elements.spring integration
|
|
46 | 37 | @Component
|
47 | 38 | public class HttpRequestTranslator implements MuleComponentToSpringIntegrationDslTranslator<RequestType> {
|
48 | 39 |
|
49 |
| - @Autowired |
50 |
| - private Configuration configuration; |
51 |
| - |
52 | 40 | @Override
|
53 | 41 | public Class getSupportedMuleType() {
|
54 | 42 | return RequestType.class;
|
55 | 43 | }
|
56 | 44 |
|
| 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 | + |
57 | 53 | @Override
|
58 | 54 | public DslSnippet translate(RequestType component,
|
59 | 55 | QName name,
|
60 | 56 | MuleConfigurations muleConfigurations,
|
61 | 57 | String flowName) {
|
62 | 58 |
|
| 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()); |
63 | 77 |
|
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; |
99 | 81 | }
|
100 | 82 |
|
| 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 | + } |
101 | 93 |
|
102 |
| - return null; |
| 94 | + private String emptyStringIfNull(String value) { |
| 95 | + return value == null ? "" : value; |
103 | 96 | }
|
104 | 97 | }
|
0 commit comments