public String createXmlRepresentation(TypedContent typedContent) { String content = typedContent == null ? null : typedContent.getContentAsString(); if (!StringUtils.hasContent(content)) { return "<xml/>"; } try { // XmlObject.Factory.parse( new ByteArrayInputStream( // content.getBytes() ) ); XmlUtils.createXmlObject(new ByteArrayInputStream(content.getBytes())); return content; } catch (Exception e) { // fall through, this wasn't xml } try { Tidy tidy = new Tidy(); tidy.setXmlOut(true); tidy.setShowWarnings(false); tidy.setErrout(new PrintWriter(new StringWriter())); // tidy.setQuiet(true); tidy.setNumEntities(true); tidy.setQuoteNbsp(true); tidy.setFixUri(false); Document document = tidy.parseDOM(new ByteArrayInputStream(content.getBytes()), null); StringWriter writer = new StringWriter(); XmlUtils.serializePretty(document, writer); return writer.toString(); } catch (Throwable e) { SoapUI.logError(e); } return null; }
public XmlObject getRequestXmlObject() throws XmlException { if (requestXmlObject == null && StringUtils.hasContent(getRequestContent())) // requestXmlObject = XmlObject.Factory.parse( getRequestContent() ); requestXmlObject = XmlUtils.createXmlObject(getRequestContent(), XmlUtils.createDefaultXmlOptions()); return requestXmlObject; }
public void initFromWadl(String wadlUrl) { try { // XmlObject xmlObject = XmlObject.Factory.parse( new URL( wadlUrl ) ); XmlObject xmlObject = XmlUtils.createXmlObject(new URL(wadlUrl)); String content = xmlObject.xmlText(); Element element = ((Document) xmlObject.getDomNode()).getDocumentElement(); // try to allow older namespaces if (element.getLocalName().equals("application") && element.getNamespaceURI().startsWith("http://research.sun.com/wadl")) { 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"); } content = PropertyExpansionRemover.removeExpansions(content); 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); } }
private RestMethod initMethod(RestResource newResource, Method method) { // build name String name = getFirstTitle(method.getDocList(), method.getName()); String id = method.getId(); if (StringUtils.hasContent(id) && !id.trim().equals(name.trim())) { name += " - " + method.getId(); } // ensure unique name if (newResource.getRestMethodByName(name) != null) { int cnt = 0; String orgName = name; while (newResource.getRestMethodByName(name) != null) { cnt++; name = orgName + "-" + cnt; } } // add to resource RestMethod restMethod = newResource.addNewMethod(name); restMethod.setMethod(RestRequestInterface.HttpMethod.valueOf(method.getName())); if (method.getRequest() != null) { for (Param param : method.getRequest().getParamList()) { param = resolveParameter(param); if (param != null) { RestParamProperty p = restMethod.addProperty(param.getName()); initParam(param, p); } } for (Representation representation : method.getRequest().getRepresentationList()) { representation = resolveRepresentation(representation); addRepresentationFromConfig( restMethod, representation, RestRepresentation.Type.REQUEST, null); } } for (Response response : method.getResponseList()) { for (Representation representation : response.getRepresentationList()) { addRepresentation(response, restMethod, representation); } if (!isWADL11) { NodeList children = response.getDomNode().getChildNodes(); for (int i = 0; i < children.getLength(); i++) { Node n = children.item(i); if ("fault".equals(n.getNodeName())) { String content = XmlUtils.serialize(n, false); try { Map<Object, Object> map = new HashMap<Object, Object>(); XmlCursor cursor = response.newCursor(); cursor.getAllNamespaces(map); cursor.dispose(); XmlOptions options = new XmlOptions(); options.setLoadAdditionalNamespaces(map); // XmlObject obj = XmlObject.Factory.parse( // content.replaceFirst( "<(([a-z]+:)?)fault ", // "<$1representation " ), options ); XmlObject obj = XmlUtils.createXmlObject( content.replaceFirst("<(([a-z]+:)?)fault ", "<$1representation "), options); RepresentationDocument representation = (RepresentationDocument) obj.changeType(RepresentationDocument.type); addRepresentation(response, restMethod, representation.getRepresentation()); } catch (XmlException e) { } } } } } restMethod.addNewRequest("Request 1"); return restMethod; }