public static boolean generateHandler(Registry configSystemRegistry, String resourceFullPath) throws RegistryException, XMLStreamException { RegistryContext registryContext = configSystemRegistry.getRegistryContext(); if (registryContext == null) { return false; } Resource resource = configSystemRegistry.get(resourceFullPath); if (resource != null) { String content = null; if (resource.getContent() != null) { content = RegistryUtils.decodeBytes((byte[]) resource.getContent()); } if (content != null) { OMElement handler = AXIOMUtil.stringToOM(content); if (handler != null) { OMElement dummy = OMAbstractFactory.getOMFactory().createOMElement("dummy", null); dummy.addChild(handler); try { configSystemRegistry.beginTransaction(); boolean status = RegistryConfigurationProcessor.updateHandler( dummy, configSystemRegistry.getRegistryContext(), HandlerLifecycleManager.USER_DEFINED_HANDLER_PHASE); configSystemRegistry.commitTransaction(); return status; } catch (Exception e) { configSystemRegistry.rollbackTransaction(); throw new RegistryException("Unable to add handler", e); } } } } return false; }
public static boolean addHandler(Registry configSystemRegistry, String payload) throws RegistryException, XMLStreamException { String name; OMElement element = AXIOMUtil.stringToOM(payload); if (element != null) { name = element.getAttributeValue(new QName("class")); } else return false; if (isHandlerNameInUse(name)) throw new RegistryException("The added handler name is already in use!"); String path = getContextRoot() + name; Resource resource; if (!handlerExists(configSystemRegistry, name)) { resource = new ResourceImpl(); } else { throw new RegistryException("The added handler name is already in use!"); } resource.setContent(payload); try { configSystemRegistry.beginTransaction(); configSystemRegistry.put(path, resource); generateHandler(configSystemRegistry, path); configSystemRegistry.commitTransaction(); } catch (Exception e) { configSystemRegistry.rollbackTransaction(); throw new RegistryException("Unable to generate handler", e); } return true; }
public boolean removeDomainMappingFromRegistry(String actualHost) throws Exception { boolean successfullyRemoved; try { registry.beginTransaction(); String hostResourcePath = CartridgeConstants.DomainMappingInfo.HOSTINFO; if (registry.resourceExists(hostResourcePath)) { Resource hostResource = registry.get(hostResourcePath); Collection hostInfoCollection; if (hostResource instanceof Collection) { hostInfoCollection = (Collection) hostResource; } else { throw new Exception("Resource is not a collection " + hostResourcePath); } String[] paths = hostInfoCollection.getChildren(); for (String path : paths) { Resource domainMapping = registry.get(path); String actualHostInRegistry = domainMapping.getProperty(CartridgeConstants.DomainMappingInfo.ACTUAL_HOST); if (actualHostInRegistry != null && actualHost.equalsIgnoreCase(actualHostInRegistry)) { registry.delete(path); } } } registry.commitTransaction(); successfullyRemoved = true; } catch (RegistryException e) { registry.rollbackTransaction(); log.error("Unable to remove the mapping", e); throw e; } return successfullyRemoved; }
public static boolean putRegistryResource(String path, Resource resource) throws DynamicClientRegistrationException { boolean status; try { Registry governanceRegistry = DynamicClientWebAppRegistrationUtil.getGovernanceRegistry(); governanceRegistry.beginTransaction(); governanceRegistry.put(path, resource); governanceRegistry.commitTransaction(); status = true; } catch (RegistryException e) { throw new DynamicClientRegistrationException( "Error occurred while persisting registry resource : " + e.getMessage(), e); } return status; }
public static boolean removeHandler(Registry configSystemRegistry, String handlerName) throws RegistryException, XMLStreamException { String handlerConfiguration = getHandlerConfiguration(configSystemRegistry, handlerName); if (handlerConfiguration != null) { OMElement element = AXIOMUtil.stringToOM(handlerConfiguration); try { try { configSystemRegistry.beginTransaction(); RegistryConfigurationProcessor.HandlerDefinitionObject handlerDefinitionObject = new RegistryConfigurationProcessor.HandlerDefinitionObject(null, element).invoke(); String[] methods = handlerDefinitionObject.getMethods(); Filter filter = handlerDefinitionObject.getFilter(); Handler handler = handlerDefinitionObject.getHandler(); if (handlerDefinitionObject.getTenantId() != -1) { CurrentSession.setCallerTenantId(handlerDefinitionObject.getTenantId()); // We need to swap the tenant id for this call, if the handler has overriden the // default value. configSystemRegistry .getRegistryContext() .getHandlerManager() .removeHandler( methods, filter, handler, HandlerLifecycleManager.USER_DEFINED_HANDLER_PHASE); CurrentSession.removeCallerTenantId(); } else { configSystemRegistry .getRegistryContext() .getHandlerManager() .removeHandler( methods, filter, handler, HandlerLifecycleManager.USER_DEFINED_HANDLER_PHASE); } configSystemRegistry.commitTransaction(); return true; } catch (Exception e) { configSystemRegistry.rollbackTransaction(); throw e; } } catch (Exception e) { if (e instanceof RegistryException) { throw (RegistryException) e; } else if (e instanceof XMLStreamException) { throw (XMLStreamException) e; } throw new RegistryException("Unable to build handler configuration", e); } } return false; }
public static boolean deleteHandler(Registry configSystemRegistry, String name) throws RegistryException, XMLStreamException { if (isHandlerNameInUse(name)) throw new RegistryException("Handler could not be deleted, since it is already in use!"); String path = getContextRoot() + name; if (configSystemRegistry.resourceExists(path)) { try { configSystemRegistry.beginTransaction(); removeHandler(configSystemRegistry, name); configSystemRegistry.delete(path); configSystemRegistry.commitTransaction(); } catch (Exception e) { configSystemRegistry.rollbackTransaction(); throw new RegistryException("Unable to remove handler", e); } return true; } return false; }
public boolean addDomainMappingToRegistry(String hostName, String actualHost) throws ADCException, RegistryException { boolean successfullyAdded = false; try { registry.beginTransaction(); Resource hostResource = registry.newResource(); hostResource.addProperty(CartridgeConstants.DomainMappingInfo.ACTUAL_HOST, actualHost); if (!registry.resourceExists(CartridgeConstants.DomainMappingInfo.HOSTINFO + hostName)) { registry.put(CartridgeConstants.DomainMappingInfo.HOSTINFO + hostName, hostResource); successfullyAdded = true; } else { registry.rollbackTransaction(); String msg = "Requested domain is already taken!"; log.error(msg); throw new ADCException(msg); } registry.commitTransaction(); } catch (RegistryException e) { registry.rollbackTransaction(); } return successfullyAdded; }
/** * This method is used to load custom security scenarios used inside Identity STS componsnts. * * @throws Exception */ private void loadSecurityScenarios() throws Exception { Registry registry = registryService.getConfigSystemRegistry(); try { // Scenarios are listed in resources/scenario-config.xml URL resource = bundleContext.getBundle().getResource("scenario-config.xml"); XmlConfiguration xmlConfiguration = new XmlConfiguration(resource.openStream(), SecurityConstants.SECURITY_NAMESPACE); OMElement[] elements = xmlConfiguration.getElements("//ns:Scenario"); boolean transactionStarted = Transaction.isStarted(); if (!transactionStarted) { registry.beginTransaction(); } for (OMElement scenarioEle : elements) { SecurityScenario scenario = new SecurityScenario(); String scenarioId = scenarioEle.getAttribute(SecurityConstants.ID_QN).getAttributeValue(); scenario.setScenarioId(scenarioId); scenario.setSummary( scenarioEle.getFirstChildWithName(SecurityConstants.SUMMARY_QN).getText()); scenario.setDescription( scenarioEle.getFirstChildWithName(SecurityConstants.DESCRIPTION_QN).getText()); scenario.setCategory( scenarioEle.getFirstChildWithName(SecurityConstants.CATEGORY_QN).getText()); scenario.setWsuId(scenarioEle.getFirstChildWithName(SecurityConstants.WSUID_QN).getText()); scenario.setType(scenarioEle.getFirstChildWithName(SecurityConstants.TYPE_QN).getText()); OMElement genPolicyElem = scenarioEle.getFirstChildWithName(SecurityConstants.IS_GEN_POLICY_QN); if (genPolicyElem != null && genPolicyElem.getText().equals("false")) { scenario.setGeneralPolicy(false); } String resourceUri = SecurityConstants.SECURITY_POLICY + "/" + scenarioId; for (Iterator modules = scenarioEle.getFirstChildWithName(SecurityConstants.MODULES_QN).getChildElements(); modules.hasNext(); ) { String module = ((OMElement) modules.next()).getText(); scenario.addModule(module); } // Save it in the DB SecurityScenarioDatabase.put(scenarioId, scenario); // Store the scenario in the Registry if (!scenarioId.equals(SecurityConstants.SCENARIO_DISABLE_SECURITY) && !scenarioId.equals(SecurityConstants.POLICY_FROM_REG_SCENARIO)) { Resource scenarioResource = new ResourceImpl(); scenarioResource.setContentStream( bundleContext.getBundle().getResource(scenarioId + "-policy.xml").openStream()); if (!registry.resourceExists(resourceUri)) { registry.put(resourceUri, scenarioResource); } } } if (!transactionStarted) { registry.commitTransaction(); } } catch (Exception e) { registry.rollbackTransaction(); throw e; } }
public static boolean updateHandler(Registry configSystemRegistry, String oldName, String payload) throws RegistryException, XMLStreamException { if (isHandlerNameInUse(oldName)) throw new RegistryException("Could not update handler since it is already in use!"); String newName = null; OMElement element = AXIOMUtil.stringToOM(payload); if (element != null) { newName = element.getAttributeValue(new QName("class")); } if (newName == null || newName.equals("")) return false; // invalid configuration if (oldName == null || oldName.equals("")) { String path = getContextRoot() + newName; Resource resource; if (handlerExists(configSystemRegistry, newName)) { return false; // we are adding a new handler } else { resource = new ResourceImpl(); } resource.setContent(payload); try { configSystemRegistry.beginTransaction(); configSystemRegistry.put(path, resource); generateHandler(configSystemRegistry, path); configSystemRegistry.commitTransaction(); } catch (Exception e) { configSystemRegistry.rollbackTransaction(); throw new RegistryException("Unable to generate handler", e); } return true; } if (newName.equals(oldName)) { // updating the rest of the content String oldPath = getContextRoot() + oldName; Resource resource; if (handlerExists(configSystemRegistry, oldName)) { resource = configSystemRegistry.get(oldPath); } else { resource = new ResourceImpl(); // will this ever happen? } resource.setContent(payload); try { configSystemRegistry.beginTransaction(); removeHandler(configSystemRegistry, oldName); configSystemRegistry.put(oldPath, resource); generateHandler(configSystemRegistry, oldPath); configSystemRegistry.commitTransaction(); } catch (Exception e) { configSystemRegistry.rollbackTransaction(); throw new RegistryException("Unable to generate handler", e); } return true; } else { String oldPath = getContextRoot() + oldName; String newPath = getContextRoot() + newName; if (handlerExists(configSystemRegistry, newName)) { return false; // we are trying to use the name of a existing handler } Resource resource; if (handlerExists(configSystemRegistry, oldName)) { resource = configSystemRegistry.get(oldPath); } else { resource = new ResourceImpl(); // will this ever happen? } resource.setContent(payload); try { configSystemRegistry.beginTransaction(); configSystemRegistry.put(newPath, resource); generateHandler(configSystemRegistry, newPath); removeHandler(configSystemRegistry, oldName); configSystemRegistry.delete(oldPath); configSystemRegistry.commitTransaction(); } catch (Exception e) { configSystemRegistry.rollbackTransaction(); throw new RegistryException("Unable to renew handler", e); } return true; } }
/** * 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); } } } }
/** * 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); } } } }
/** * Updates the given artifact on the registry. * * @param artifact the artifact. * @throws GovernanceException if the operation failed. */ public void updateGovernanceArtifact(GovernanceArtifact artifact) throws GovernanceException { boolean succeeded = false; try { registry.beginTransaction(); validateArtifact(artifact); GovernanceArtifact oldArtifact = getGovernanceArtifact(artifact.getId()); // first check for the old artifact and remove it. String oldPath = null; if (oldArtifact != null) { QName oldName = oldArtifact.getQName(); if (!oldName.equals(artifact.getQName())) { String temp = oldArtifact.getPath(); // then it is analogue to moving the resource for the new location // so just delete the old path registry.delete(temp); String artifactName = artifact.getQName().getLocalPart(); artifact.setAttributes(artifactNameAttribute, new String[] {artifactName}); String namespace = artifact.getQName().getNamespaceURI(); if (artifactNamespaceAttribute != null) { artifact.setAttributes(artifactNamespaceAttribute, new String[] {namespace}); } } else { oldPath = oldArtifact.getPath(); } } else { throw new GovernanceException( "No artifact found for the artifact id :" + artifact.getId() + "."); } String artifactId = artifact.getId(); Resource resource = registry.newResource(); resource.setMediaType(mediaType); setContent(artifact, resource); String path = GovernanceUtils.getPathFromPathExpression(pathExpression, artifact); if (oldPath != null) { path = oldPath; } if (registry.resourceExists(path)) { Resource oldResource = registry.get(path); Properties properties = (Properties) oldResource.getProperties().clone(); resource.setProperties(properties); // persisting resource description at artifact update String description = oldResource.getDescription(); if (description != null) { resource.setDescription(description); } String oldContent; Object content = oldResource.getContent(); if (content instanceof String) { oldContent = (String) content; } else { oldContent = new String((byte[]) content); } String newContent; content = resource.getContent(); if (content instanceof String) { newContent = (String) content; } else { newContent = new String((byte[]) content); } if (newContent.equals(oldContent)) { artifact.setId(oldResource.getUUID()); addRelationships(path, artifact); succeeded = true; return; } } resource.setUUID(artifactId); registry.put(path, resource); // artifact.setId(resource.getUUID()); //This is done to get the UUID of a existing // resource. addRelationships(oldPath, artifact); ((GovernanceArtifactImpl) artifact).updatePath(artifactId); succeeded = true; } catch (RegistryException e) { if (e instanceof GovernanceException) { throw (GovernanceException) e; } String msg; if (artifact.getPath() != null) { msg = "Error in updating the artifact, artifact id: " + artifact.getId() + ", artifact path: " + artifact.getPath() + "." + e.getMessage() + "."; } else { msg = "Error in updating the artifact, artifact id: " + artifact.getId() + "." + e.getMessage() + "."; } log.error(msg, e); throw new GovernanceException(msg, e); } finally { if (succeeded) { try { registry.commitTransaction(); } catch (RegistryException e) { String msg; if (artifact.getPath() != null) { msg = "Error in committing transactions. Update artifact failed: artifact " + "id: " + artifact.getId() + ", path: " + artifact.getPath() + "."; } else { msg = "Error in committing transactions. Update artifact failed: artifact " + "id: " + artifact.getId() + "."; } log.error(msg, e); } } else { try { registry.rollbackTransaction(); } catch (RegistryException e) { String msg = "Error in rolling back transactions. Update artifact failed: " + "artifact id: " + artifact.getId() + ", path: " + artifact.getPath() + "."; log.error(msg, e); } } } }
/** * Adds the given artifact to the registry. Please do not use this method to update an existing * artifact use the update method instead. If this method is used to update an existing artifact, * all existing properties (such as lifecycle details) will be removed from the existing artifact. * * @param artifact the artifact. * @throws GovernanceException if the operation failed. */ public void addGovernanceArtifact(GovernanceArtifact artifact) throws GovernanceException { // adding the attributes for name, namespace + artifact if (artifact.getQName() == null || artifact.getQName().getLocalPart() == null) { String msg = "A valid qualified name was not set for this artifact"; log.error(msg); throw new GovernanceException(msg); } validateArtifact(artifact); String artifactName = artifact.getQName().getLocalPart(); artifact.setAttributes(artifactNameAttribute, new String[] {artifactName}); // namespace can be null String namespace = artifact.getQName().getNamespaceURI(); if (artifactNamespaceAttribute != null) { artifact.setAttributes(artifactNamespaceAttribute, new String[] {namespace}); } ((GovernanceArtifactImpl) artifact).associateRegistry(registry); boolean succeeded = false; try { registry.beginTransaction(); Resource resource = registry.newResource(); resource.setMediaType(mediaType); setContent(artifact, resource); // the artifact will not actually stored in the tmp path. String path = GovernanceUtils.getPathFromPathExpression(pathExpression, artifact); if (registry.resourceExists(path)) { throw new GovernanceException( "Governance artifact " + artifactName + " already exists at " + path); } String artifactId = artifact.getId(); resource.setUUID(artifactId); registry.put(path, resource); if (lifecycle != null) { registry.associateAspect(path, lifecycle); } ((GovernanceArtifactImpl) artifact).updatePath(); // artifact.setId(resource.getUUID()); //This is done to get the UUID of a existing // resource. addRelationships(path, artifact); succeeded = true; } catch (RegistryException e) { String msg; if (artifact.getPath() != null) { msg = "Failed to add artifact: artifact id: " + artifact.getId() + ", path: " + artifact.getPath() + ". " + e.getMessage(); } else { msg = "Failed to add artifact: artifact id: " + artifact.getId() + ". " + e.getMessage(); } log.error(msg, e); throw new GovernanceException(msg, e); } finally { if (succeeded) { try { registry.commitTransaction(); } catch (RegistryException e) { String msg; if (artifact.getPath() != null) { msg = "Error in committing transactions. Failed to add artifact: artifact " + "id: " + artifact.getId() + ", path: " + artifact.getPath() + "."; } else { msg = "Error in committing transactions. Failed to add artifact: artifact " + "id: " + artifact.getId() + "."; } log.error(msg, e); } } else { try { registry.rollbackTransaction(); } catch (RegistryException e) { String msg = "Error in rolling back transactions. Failed to add artifact: " + "artifact id: " + artifact.getId() + ", path: " + artifact.getPath() + "."; log.error(msg, e); } } } }