WadlImporter { public void initFromWadl(String wadlUrl) { try { XmlObject xmlObject = XmlUtils.createXmlObject(new URL(wadlUrl)); String content = Tools.removePropertyExpansions(wadlUrl, xmlObject.xmlText()); Element element = ((Document) xmlObject.getDomNode()).getDocumentElement(); if (element.getLocalName().equals("application") && element.getNamespaceURI().startsWith("http: isWADL11 = false; content = content.replaceAll("\"" + element.getNamespaceURI() + "\"", "\"" + Constants.WADL11_NS + "\""); } else if (!element.getLocalName().equals("application") || !element.getNamespaceURI().equals(Constants.WADL11_NS)) { throw new Exception("Document is not a WADL application with " + Constants.WADL11_NS + " namespace"); } ApplicationDocument applicationDocument = ApplicationDocument.Factory.parse(content); application = applicationDocument.getApplication(); resourcesList = application.getResourcesList(); service.setName(getFirstTitle(application.getDocList(), service.getName())); String base = resourcesList.size() == 1 ? resourcesList.get(0).getBase() : ""; try { URL baseUrl = new URL(base); service.setBasePath(baseUrl.getPath()); service.addEndpoint(Tools.getEndpointFromUrl(baseUrl)); } catch (Exception e) { service.setBasePath(base); } service.setWadlUrl(wadlUrl); service.getConfig().setWadlVersion(isWADL11 ? Constants.WADL11_NS : Constants.WADL10_NS); for (Resources resources : resourcesList) { RestResource baseResource = null; if (resourcesList.size() > 1) { String path = resources.getBase(); baseResource = service.addNewResource(path, path); } for (Resource resource : resources.getResourceList()) { String name = getFirstTitle(resource.getDocList(), resource.getPath()); String path = resource.getPath(); RestResource newResource = null; if (baseResource != null && path != null) { for (RestResource res : baseResource.getChildResourceList()) { if (path.equals(res.getPath())) { newResource = res; break; } } if (newResource == null) { newResource = baseResource.addNewChildResource(name, path); } } else if (path != null) { for (RestResource res : service.getResourceList()) { if (path.equals(res.getPath())) { newResource = res; break; } } if (newResource == null) { newResource = service.addNewResource(name, path); } } else { newResource = service.addNewResource(name, ""); } initResourceFromWadlResource(newResource, resource); addSubResources(newResource, resource); } } } catch (InvalidDefinitionException ex) { ex.show(); } catch (Exception e) { UISupport.showErrorMessage(e); } } WadlImporter(RestService service); void initFromWadl(String wadlUrl); static Map<String, XmlObject> getDefinitionParts(String wadlUrl); static String extractParams(URL param, RestParamsPropertyHolder params);  }
@Test public void importsWadl() throws Exception { WsdlProject project = new WsdlProject(); RestService service = (RestService) project.addNewInterface("Test", RestServiceFactory.REST_TYPE); new WadlImporter(service).initFromWadl(RestUtilsTest.class.getResource("/wadl/YahooSearch.wadl").toURI().toString()); assertEquals(1, service.getOperationCount()); assertEquals("/NewsSearchService/V1/", service.getBasePath()); RestResource resource = service.getOperationAt(0); assertEquals(1, resource.getPropertyCount()); assertEquals("appid", resource.getPropertyAt(0).getName()); assertNotNull(resource.getProperty("appid")); assertEquals(1, resource.getRequestCount()); RestRequest request = resource.getRequestAt(0); assertEquals(RestRequestInterface.HttpMethod.GET, request.getMethod()); assertEquals(9, request.getPropertyCount()); }
@Test public void removesPropertyExpansions() throws Exception { WsdlProject project = new WsdlProject(); RestService service = (RestService) project.addNewInterface("Test", RestServiceFactory.REST_TYPE); new WadlImporter(service).initFromWadl(RestUtilsTest.class.getResource( "/wadl/YahooSearchWithExpansions.wadl").toURI().toString()); RestResource operation = (RestResource) service.getAllOperations()[0]; RestMethod restMethod = operation.getRestMethodAt(0); RestRequest request = restMethod.getRequestAt(0); assertThat(request.getParams().getProperty("language").getDefaultValue(), is(anEmptyString())); }