private void addWSDL() throws IOException, RegistryException { WsdlManager wsdlManager = new WsdlManager(governance); Wsdl wsdl; String wsdlFilePath = ProductConstant.getResourceLocations(ProductConstant.GREG_SERVER_NAME) + File.separator + "wsdl" + File.separator; wsdl = wsdlManager.newWsdl( FileManager.readFile(wsdlFilePath + "echo.wsdl").getBytes(), "echo.wsdl"); wsdlManager.addWsdl(wsdl); wsdl = wsdlManager.getWsdl(wsdl.getId()); Resource resource = governance.get(wsdl.getPath()); resource.addProperty("y", "20"); governance.put(wsdl.getPath(), resource); }
/** adding a encoded URL wsdl */ @Test(groups = "wso2.greg", description = "Add Encoded WSDL") public void testAddEncodedURLWSDL() throws RemoteException, ResourceAdminServiceExceptionException, GovernanceException, MalformedURLException { wsdl = wsdlManager.newWsdl( "https://raw.githubusercontent.com/wso2/wso2-qa-artifacts/master/automation-artifacts/greg" + "/wsdl/StockQuote.wsdl"); wsdl.addAttribute("version", "1.0.0"); wsdl.addAttribute("author", "Aparna"); wsdl.addAttribute("description", "added encoded url wsdl"); wsdlManager.addWsdl(wsdl); assertFalse(wsdl.getId().isEmpty()); assertNotNull(wsdl); assertTrue(wsdl.getAttribute("author").contentEquals("Aparna")); // encoded url wsdl // addition verification }
/** * Adds the given WSDL artifact to the registry. * * @param wsdl the WSDL artifact. * @throws GovernanceException if the operation failed. */ public void addWsdl(Wsdl wsdl) throws GovernanceException { boolean succeeded = false; try { registry.beginTransaction(); String url = wsdl.getUrl(); Resource wsdlResource = registry.newResource(); wsdlResource.setMediaType(GovernanceConstants.WSDL_MEDIA_TYPE); // setting the wsdl content setContent(wsdl, wsdlResource); wsdlResource.setUUID(wsdl.getId()); String tmpPath; if (wsdl.getQName() != null) { tmpPath = "/" + wsdl.getQName().getLocalPart(); } else if (url != null && !url.startsWith("name://")) { tmpPath = RegistryUtils.getResourceName(new URL(url).getFile().replace("~", "")); } else if (url != null) { tmpPath = url.substring("name://".length()); } else { tmpPath = wsdl.getId() + ".wsdl"; } // OK this is a hack to get the UUID of the newly added artifact. This needs to be fixed // properly with the fix for UUID support at Kernel-level - Janaka. // Resource resource; if (url == null || url.startsWith("name://")) { // resource = registry.get(registry.put("/" + tmpPath, wsdlResource)); registry.put("/" + tmpPath, wsdlResource); } else { // resource = registry.get(registry.importResource(tmpPath, url, // wsdlResource)); registry.importResource(tmpPath, url, wsdlResource); } // wsdl.setId(resource.getUUID()); wsdl.updatePath(); // wsdl.loadWsdlDetails(); succeeded = true; } catch (RegistryException e) { String msg = "Error in adding the wsdl. wsdl id: " + wsdl.getId() + "."; log.error(msg, e); throw new GovernanceException(msg, e); } catch (MalformedURLException e) { String msg = "Malformed policy url provided. url: " + wsdl.getUrl() + "."; log.error(msg, e); throw new GovernanceException(msg, e); } finally { if (succeeded) { try { registry.commitTransaction(); } catch (RegistryException e) { String msg = "Error in committing transactions. Add wsdl failed: wsdl id: " + wsdl.getId() + ", path: " + wsdl.getPath() + "."; log.error(msg, e); } } else { try { registry.rollbackTransaction(); } catch (RegistryException e) { String msg = "Error in rolling back transactions. Add wsdl failed: wsdl id: " + wsdl.getId() + ", path: " + wsdl.getPath() + "."; log.error(msg, e); } } } }
/** * Sets content of the given WSDL artifact to the given resource on the registry. * * @param wsdl the WSDL artifact. * @param wsdlResource the content resource. * @throws GovernanceException if the operation failed. */ protected void setContent(Wsdl wsdl, Resource wsdlResource) throws GovernanceException { if (wsdl.getWsdlElement() != null) { OMElement contentElement = wsdl.getWsdlElement().cloneOMElement(); try { for (String importType : new String[] {"import", "include"}) { List<OMElement> wsdlImports = GovernanceUtils.evaluateXPathToElements("//wsdl:" + importType, contentElement); for (OMElement wsdlImport : wsdlImports) { OMAttribute location = wsdlImport.getAttribute(new QName("location")); if (location != null) { String path = location.getAttributeValue(); if (path.indexOf(";version:") > 0) { location.setAttributeValue(path.substring(0, path.lastIndexOf(";version:"))); } } } } for (String importType : new String[] {"import", "include", "redefine"}) { List<OMElement> schemaImports = GovernanceUtils.evaluateXPathToElements("//xsd:" + importType, contentElement); for (OMElement schemaImport : schemaImports) { OMAttribute location = schemaImport.getAttribute(new QName("schemaLocation")); if (location != null) { String path = location.getAttributeValue(); if (path.indexOf(";version:") > 0) { location.setAttributeValue(path.substring(0, path.lastIndexOf(";version:"))); } } } } } catch (JaxenException ignore) { } String wsdlContent = contentElement.toString(); try { wsdlResource.setContent(wsdlContent); } catch (RegistryException e) { String msg = "Error in setting the content from wsdl, wsdl id: " + wsdl.getId() + ", wsdl path: " + wsdl.getPath() + "."; log.error(msg, e); throw new GovernanceException(msg, e); } } // and set all the attributes as properties. String[] attributeKeys = wsdl.getAttributeKeys(); if (attributeKeys != null) { Properties properties = new Properties(); for (String attributeKey : attributeKeys) { String[] attributeValues = wsdl.getAttributes(attributeKey); if (attributeValues != null) { // The list obtained from the Arrays#asList method is // immutable. Therefore, // we create a mutable object out of it before adding it as // a property. properties.put(attributeKey, new ArrayList<String>(Arrays.asList(attributeValues))); } } wsdlResource.setProperties(properties); } wsdlResource.setUUID(wsdl.getId()); }
/** * Updates the given WSDL artifact on the registry. * * @param wsdl the WSDL artifact. * @throws GovernanceException if the operation failed. */ public void updateWsdl(Wsdl wsdl) throws GovernanceException { if (wsdl.getWsdlElement() == null) { // there won't be any updates String msg = "Updates are only accepted if the wsdlElement available. " + "So no updates will be done. " + "wsdl id: " + wsdl.getId() + ", wsdl path: " + wsdl.getPath() + "."; log.error(msg); throw new GovernanceException(msg); } boolean succeeded = false; try { registry.beginTransaction(); // getting the old wsdl. Wsdl oldWsdl = getWsdl(wsdl.getId()); if (oldWsdl == null) { addWsdl(wsdl); return; } // we are expecting only the OMElement to be different. Resource wsdlResource = registry.newResource(); wsdlResource.setMediaType(GovernanceConstants.WSDL_MEDIA_TYPE); // setting the wsdl content setContent(wsdl, wsdlResource); // remove the old wsdl resource. String tmpPath = oldWsdl.getPath(); wsdlResource.setUUID(wsdl.getId()); registry.put(tmpPath, wsdlResource); // wsdl.setId(wsdlResource.getUUID()); wsdl.updatePath(); // wsdl.loadWsdlDetails(); succeeded = true; } catch (RegistryException e) { String msg = "Error in updating the wsdl, wsdl id: " + wsdl.getId() + ", wsdl path: " + wsdl.getPath() + "."; log.error(msg, e); throw new GovernanceException(msg, e); } finally { if (succeeded) { try { registry.commitTransaction(); } catch (RegistryException e) { String msg = "Error in committing transactions. Update wsdl failed: wsdl id: " + wsdl.getId() + ", path: " + wsdl.getPath() + "."; log.error(msg, e); } } else { try { registry.rollbackTransaction(); } catch (RegistryException e) { String msg = "Error in rolling back transactions. Update wsdl failed: wsdl id: " + wsdl.getId() + ", path: " + wsdl.getPath() + "."; log.error(msg, e); } } } }