forked from jboss-fuse/wsdl2rest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CamelContextGenerator.java
126 lines (106 loc) · 5.31 KB
/
CamelContextGenerator.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package org.jboss.fuse.wsdl2rest.impl.codegen;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import org.apache.cxf.tools.common.model.JavaInterface;
import org.apache.cxf.tools.common.model.JavaMethod;
import org.apache.cxf.tools.common.model.JavaModel;
import org.apache.cxf.tools.common.model.JavaParameter;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.runtime.RuntimeConstants;
import org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader;
import org.jboss.fuse.wsdl2rest.EndpointInfo;
import org.jboss.fuse.wsdl2rest.MethodInfo;
import org.jboss.fuse.wsdl2rest.ParamInfo;
import org.jboss.fuse.wsdl2rest.impl.service.MethodInfoImpl;
import org.jboss.fuse.wsdl2rest.impl.service.ParamImpl;
import org.jboss.fuse.wsdl2rest.util.IllegalArgumentAssertion;
import org.jboss.fuse.wsdl2rest.util.IllegalStateAssertion;
public class CamelContextGenerator {
private Path camelContext;
private URL jaxrsAddress;
private URL jaxwsAddress;
public CamelContextGenerator(Path outpath) {
}
public void setCamelContext(Path camelContext) {
this.camelContext = camelContext;
}
public void setJaxrsAddress(URL jaxrsAddress) {
this.jaxrsAddress = jaxrsAddress;
}
public void setJaxwsAddress(URL jaxwsAddress) {
this.jaxwsAddress = jaxwsAddress;
}
public void process(List<EndpointInfo> clazzDefs, JavaModel javaModel) throws IOException {
IllegalArgumentAssertion.assertNotNull(clazzDefs, "clazzDefs");
IllegalArgumentAssertion.assertNotNull(javaModel, "javaModel");
IllegalStateAssertion.assertNotNull(camelContext, "Camel context file name not set");
IllegalArgumentAssertion.assertTrue(clazzDefs.size() == 1, "Multiple endpoints not supported");
EndpointInfo epinfo = clazzDefs.get(0);
VelocityEngine ve = new VelocityEngine();
ve.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
ve.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());
ve.init();
String tmplPath = "templates/jaxrs-camel-context.vm";
try (InputStreamReader reader = new InputStreamReader(getClass().getClassLoader().getResourceAsStream(tmplPath))) {
jaxrsAddress = jaxrsAddress != null ? jaxrsAddress : new URL("http://localhost:8081/jaxrs");
String jaxrsHost = jaxrsAddress.getHost();
String jaxrsPort = "" + jaxrsAddress.getPort();
String jaxrsPath = jaxrsAddress.getPath();
VelocityContext context = new VelocityContext();
context.put("jaxwsAddress", jaxwsAddress != null ? jaxwsAddress : "http://localhost:8080/somepath");
context.put("jaxrsHost", jaxrsHost);
context.put("jaxrsPort", jaxrsPort);
context.put("jaxrsPath", jaxrsPath);
context.put("serviceClass", epinfo.getFQN());
context.put("allMethods", epinfo.getMethods());
addTypeMapping(epinfo, javaModel);
File outfile = camelContext.toFile();
outfile.getParentFile().mkdirs();
try (BufferedWriter writer = new BufferedWriter(new FileWriter(outfile))) {
ve.evaluate(context, writer, tmplPath, reader);
}
}
}
private void addTypeMapping(EndpointInfo epinfo, JavaModel javaModel) {
IllegalArgumentAssertion.assertTrue(javaModel.getInterfaces().size() == 1, "Multiple interfaces not supported");
JavaInterface javaIntrf = javaModel.getInterfaces().get(epinfo.getClassName());
for (MethodInfo method : epinfo.getMethods()) {
if ("document".equals(method.getStyle())) {
JavaMethod javaMethod = getJavaMethod(javaIntrf, method.getMethodName());
List<ParamInfo> wrappedParams = new ArrayList<>();
for (ParamInfo param : method.getParams()) {
String paramName = param.getParamName();
JavaParameter javaParam = javaMethod.getParameter(paramName);
wrappedParams.add(new ParamImpl(paramName, normalize(javaParam.getClassName())));
}
((MethodInfoImpl) method).setWrappedParams(wrappedParams);
((MethodInfoImpl) method).setWrappedReturnType(normalize(javaMethod.getReturnValue()));
}
}
}
private String normalize(String typeName) {
if (typeName.contains("List<") && typeName.endsWith(">")) {
typeName = typeName.substring(typeName.indexOf("<") + 1);
typeName = typeName.substring(0, typeName.indexOf(">"));
typeName = typeName + "[]";
}
return typeName;
}
private JavaMethod getJavaMethod(JavaInterface intrf, String methodName) {
JavaMethod result = null;
for (JavaMethod method : intrf.getMethods()) {
if (method.getName().equals(methodName))
result = method;
}
IllegalStateAssertion.assertNotNull(result, "Cannot obtain java method for: " + methodName);
return result;
}
}