1
2
3
4
5
6
7
8
9
10
11
12
13 package com.eviware.soapui.impl.rest;
14
15 import com.eviware.soapui.impl.rest.support.XmlBeansRestParamsTestPropertyHolder;
16 import com.eviware.soapui.impl.rest.support.XmlBeansRestParamsTestPropertyHolder.RestParamProperty;
17 import com.eviware.soapui.model.ModelItem;
18 import com.eviware.soapui.support.StringUtils;
19 import com.sun.research.wadl.x2006.x10.ApplicationDocument;
20 import com.sun.research.wadl.x2006.x10.ApplicationDocument.Application;
21 import com.sun.research.wadl.x2006.x10.DocDocument.Doc;
22 import com.sun.research.wadl.x2006.x10.MethodDocument.Method;
23 import com.sun.research.wadl.x2006.x10.ParamDocument.Param;
24 import com.sun.research.wadl.x2006.x10.ParamStyle;
25 import com.sun.research.wadl.x2006.x10.RepresentationType;
26 import com.sun.research.wadl.x2006.x10.RequestDocument.Request;
27 import com.sun.research.wadl.x2006.x10.ResourceDocument.Resource;
28 import com.sun.research.wadl.x2006.x10.ResourcesDocument.Resources;
29 import com.sun.research.wadl.x2006.x10.ResponseDocument.Response;
30 import org.apache.xmlbeans.XmlObject;
31
32 import java.util.HashMap;
33 import java.util.List;
34 import java.util.Map;
35
36 public class WadlGenerator
37 {
38 private RestService restService;
39
40 public WadlGenerator(RestService restService)
41 {
42 this.restService = restService;
43 }
44
45 public ApplicationDocument generateWadl()
46 {
47 ApplicationDocument applicationDocument = ApplicationDocument.Factory.newInstance();
48 Application application = applicationDocument.addNewApplication();
49
50 createDoc(application.addNewDoc(), restService);
51
52 Resources resources = application.addNewResources();
53
54
55 String basePath = restService.getBasePath();
56 String[] endpoints = restService.getEndpoints();
57 if (endpoints.length > 0)
58 basePath = endpoints[0] + basePath;
59
60 resources.setBase(basePath);
61
62 for (int c = 0; c < restService.getOperationCount(); c++)
63 {
64 resources.addNewResource().set(generateWadlResource(restService.getOperationAt(c)));
65 }
66
67 return applicationDocument;
68 }
69
70 private XmlObject generateWadlResource(RestResource resource)
71 {
72 Resource resourceConfig = Resource.Factory.newInstance();
73 createDoc(resourceConfig.addNewDoc(), resource);
74 String path = resource.getPath();
75 if (path.startsWith("/"))
76 path = path.length() > 1 ? path.substring(1) : "";
77
78 resourceConfig.setPath(path);
79 resourceConfig.setId(resource.getName());
80
81 XmlBeansRestParamsTestPropertyHolder params = resource.getParams();
82 for (int c = 0; c < params.size(); c++)
83 {
84 generateParam(resourceConfig.addNewParam(), params.getPropertyAt(c));
85 }
86
87 for (int c = 0; c < resource.getChildResourceCount(); c++)
88 {
89 resourceConfig.addNewResource().set(generateWadlResource(resource.getChildResourceAt(c)));
90 }
91
92 for (int c = 0; c < resource.getRequestCount(); c++)
93 {
94 RestRequest request = resource.getRequestAt(c);
95 generateWadlMethod(resourceConfig, request);
96 }
97
98 return resourceConfig;
99 }
100
101 private void generateParam(Param paramConfig, RestParamProperty param)
102 {
103 paramConfig.setName(param.getName());
104
105 if (StringUtils.hasContent(param.getDefaultValue()))
106 paramConfig.setDefault(param.getDefaultValue());
107
108 paramConfig.setType(param.getType());
109 paramConfig.setRequired(param.getRequired());
110 paramConfig.setDefault(param.getDefaultValue());
111
112 if (StringUtils.hasContent(param.getDescription()))
113 createDoc(paramConfig.addNewDoc(), param.getName() + " Parameter", param.getDescription());
114
115 String[] options = param.getOptions();
116 for (String option : options)
117 paramConfig.addNewOption().setValue(option);
118
119 ParamStyle.Enum style = ParamStyle.QUERY;
120 switch (param.getStyle())
121 {
122 case HEADER:
123 style = ParamStyle.HEADER;
124 break;
125 case MATRIX:
126 style = ParamStyle.MATRIX;
127 break;
128 case PLAIN:
129 style = ParamStyle.PLAIN;
130 break;
131 case TEMPLATE:
132 style = ParamStyle.TEMPLATE;
133 break;
134 }
135
136 paramConfig.setStyle(style);
137 }
138
139 private void createDoc(Doc docConfig, ModelItem modelItem)
140 {
141 createDoc(docConfig, modelItem.getName(), modelItem.getDescription());
142 }
143
144 private void createDoc(Doc docConfig, String name, String description)
145 {
146 docConfig.setLang("en");
147 docConfig.setTitle(name);
148 docConfig.getDomNode().appendChild(docConfig.getDomNode().getOwnerDocument().createTextNode(description));
149 }
150
151 private void generateWadlMethod(Resource resourceConfig, RestRequest request)
152 {
153 Method methodConfig = resourceConfig.addNewMethod();
154 createDoc(methodConfig.addNewDoc(), request);
155 methodConfig.setName(request.getMethod().toString());
156 methodConfig.setId(request.getName());
157 Request requestConfig = methodConfig.addNewRequest();
158
159 Map<String, RestParamProperty> defaultParams = new HashMap<String, RestParamProperty>();
160 for (RestParamProperty defaultParam : request.getResource().getDefaultParams())
161 defaultParams.put(defaultParam.getName(), defaultParam);
162
163 XmlBeansRestParamsTestPropertyHolder params = request.getParams();
164 for (int c = 0; c < params.size(); c++)
165 {
166 RestParamProperty param = params.getPropertyAt(c);
167 if (!defaultParams.containsKey(param.getName()) || !param.equals(defaultParams.get(param.getName())))
168 generateParam(requestConfig.addNewParam(), param);
169 }
170
171 if (request.hasRequestBody())
172 {
173 for (RestRepresentation representation : request.getRepresentations(RestRepresentation.Type.REQUEST, null))
174 {
175 generateRepresentation(requestConfig.addNewRepresentation(), representation);
176 }
177 }
178
179 Response responseConfig = methodConfig.addNewResponse();
180 for (RestRepresentation representation : request.getRepresentations(RestRepresentation.Type.RESPONSE, null))
181 {
182 generateRepresentation(responseConfig.addNewRepresentation(), representation);
183 }
184
185 for (RestRepresentation representation : request.getRepresentations(RestRepresentation.Type.FAULT, null))
186 {
187 generateRepresentation(responseConfig.addNewFault(), representation);
188 }
189 }
190
191 private void generateRepresentation(RepresentationType representationConfig, RestRepresentation representation)
192 {
193 representationConfig.setMediaType(representation.getMediaType());
194
195 if (StringUtils.hasContent(representation.getId()))
196 representationConfig.setId(representation.getId());
197
198 List status = representation.getStatus();
199 if (status != null && status.size() > 0)
200 {
201 representationConfig.setStatus(status);
202 }
203 }
204
205 }