}
}
}
public void put(RequestContext requestContext) throws RegistryException {
WSDLProcessor wsdl = null;
if (!CommonUtil.isUpdateLockAvailable()) {
return;
}
CommonUtil.acquireUpdateLock();
try {
Registry registry = requestContext.getRegistry();
Resource resource = requestContext.getResource();
if (resource == null) {
throw new RegistryException("The resource is not available.");
}
String originalServicePath = requestContext.getResourcePath().getPath();
String resourceName = RegistryUtils.getResourceName(originalServicePath);
OMElement serviceInfoElement;
Object resourceContent = resource.getContent();
String serviceInfo;
if (resourceContent instanceof String) {
serviceInfo = (String) resourceContent;
} else {
serviceInfo = new String((byte[]) resourceContent);
}
try {
XMLStreamReader reader = XMLInputFactory.newInstance().
createXMLStreamReader(new StringReader(serviceInfo));
StAXOMBuilder builder = new StAXOMBuilder(reader);
serviceInfoElement = builder.getDocumentElement();
} catch (Exception e) {
String msg = "Error in parsing the service content of the service. " +
"The requested path to store the service: " + originalServicePath + ".";
log.error(msg);
throw new RegistryException(msg, e);
}
// derive the service path that the service should be saved.
String serviceName = CommonUtil.getServiceName(serviceInfoElement);
String serviceNamespace = CommonUtil.getServiceNamespace(serviceInfoElement);
String servicePath = "";
if(serviceInfoElement.getChildrenWithLocalName("newServicePath").hasNext()){
Iterator OmElementIterator = serviceInfoElement.getChildrenWithLocalName("newServicePath");
while (OmElementIterator.hasNext()) {
OMElement next = (OMElement) OmElementIterator.next();
servicePath = next.getText();
break;
}
}
else{
servicePath = RegistryUtils.getAbsolutePath(registry.getRegistryContext(),
registry.getRegistryContext().getServicePath() +
(serviceNamespace == null ? "" :
CommonUtil.derivePathFragmentFromNamespace(serviceNamespace)) +
serviceName);
}
String serviceVersion =
org.wso2.carbon.registry.common.utils.CommonUtil.getServiceVersion(
serviceInfoElement);
if (serviceVersion.length() == 0) {
serviceVersion = CommonConstants.SERVICE_VERSION_DEFAULT_VALUE;
CommonUtil.setServiceVersion(serviceInfoElement, serviceVersion);
resource.setContent(serviceInfoElement.toString());
}
if (!servicePath.startsWith(RegistryConstants.GOVERNANCE_REGISTRY_BASE_PATH + RegistryConstants.PATH_SEPARATOR+ TRUNK)) {
servicePath = CommonUtil.computeServicePathWithVersion(servicePath, serviceVersion);
}
// saving the artifact id.
String serviceId = resource.getProperty(CommonConstants.ARTIFACT_ID_PROP_KEY);
if (serviceId == null) {
// generate a service id
serviceId = UUID.randomUUID().toString();
resource.setProperty(CommonConstants.ARTIFACT_ID_PROP_KEY, serviceId);
}
if (registry.resourceExists(servicePath)) {
Resource oldResource = registry.get(servicePath);
String oldContent;
Object content = oldResource.getContent();
if (content instanceof String) {
oldContent = (String) content;
} else {
oldContent = new String((byte[]) content);
}
OMElement oldServiceInfoElement = null;
if (serviceInfo.equals(oldContent)) {
/* if user is not changing anything in service we skip the processing done in this handler */
return;
}
if ("true".equals(resource.getProperty("registry.WSDLImport"))) {
resource.removeProperty("registry.WSDLImport");
try {
XMLStreamReader reader = XMLInputFactory.newInstance().
createXMLStreamReader(new StringReader(oldContent));
StAXOMBuilder builder = new StAXOMBuilder(reader);
oldServiceInfoElement = builder.getDocumentElement();
CommonUtil.setServiceName(oldServiceInfoElement, CommonUtil.getServiceName(serviceInfoElement));
CommonUtil.setServiceNamespace(oldServiceInfoElement,
CommonUtil.getServiceNamespace(serviceInfoElement));
CommonUtil.setWSDLURL(oldServiceInfoElement,
CommonUtil.getWSDLURL(serviceInfoElement));
CommonUtil.setEndpointEntries(oldServiceInfoElement,
CommonUtil.getEndpointEntries(serviceInfoElement));
CommonUtil.setServiceVersion(oldServiceInfoElement,
org.wso2.carbon.registry.common.utils.CommonUtil.getServiceVersion(
serviceInfoElement));
serviceInfoElement = oldServiceInfoElement;
resource.setContent(serviceInfoElement.toString());
} catch (Exception e) {
String msg = "Error in parsing the service content of the service. " +
"The requested path to store the service: " + originalServicePath + ".";
log.error(msg);
throw new RegistryException(msg, e);
}
}
}
CommonUtil.addGovernanceArtifactEntryWithAbsoluteValues(
CommonUtil.getUnchrootedSystemRegistry(requestContext),
serviceId, servicePath);
if(serviceInfoElement.getChildrenWithLocalName("newServicePath").hasNext()){
if (registry.resourceExists(servicePath)) {
Resource r = registry.get(servicePath);
resource.setProperties(r.getProperties());
}
persistServiceResource(registry, resource, servicePath);
requestContext.setProcessingComplete(true);
return;
}
boolean alreadyAdded = false;
String wsdlURL = CommonUtil.getWSDLURL(serviceInfoElement);
if (wsdlURL != null && (wsdlURL.startsWith("http://") || wsdlURL.startsWith("https://"))) {
wsdl = new WSDLProcessor(requestContext);
RequestContext context = new RequestContext(registry, requestContext.getRepository(),
requestContext.getVersionRepository());
context.setResourcePath(new ResourcePath(RegistryConstants.PATH_SEPARATOR + serviceName + ".wsdl"));
context.setSourceURL(wsdlURL);
context.setResource(new ResourceImpl());
String wsdlPath = wsdl.addWSDLToRegistry(context, wsdlURL, null, false, false,
disableWSDLValidation);
if (wsdlPath == null) {
// we will get the null value, if this is called within addWSDLToRegistry
return;
}
wsdlURL = RegistryUtils.getRelativePath(requestContext.getRegistryContext(), wsdlPath);
CommonUtil.setWSDLURL(serviceInfoElement, wsdlURL);
resource.setContent(serviceInfoElement.toString().getBytes());
// updating the wsdl url
((ResourceImpl) resource).prepareContentForPut();
persistServiceResource(registry, resource, servicePath);
alreadyAdded = true;
// and make the associations
registry.addAssociation(servicePath, wsdlPath, CommonConstants.DEPENDS);
registry.addAssociation(wsdlPath, servicePath, CommonConstants.USED_BY);
} else if (wsdlURL != null && wsdlURL.startsWith(RegistryConstants.ROOT_PATH)) {
// it seems wsdlUrl is a registry path..
String wsdlPath = RegistryUtils.getAbsolutePath(requestContext.getRegistryContext(), wsdlURL);
boolean addItHere = false;
if (!registry.resourceExists(wsdlPath)) {
String msg = "Associating service to a non-existing WSDL. wsdl url: " + wsdlPath + ", " +
"service path: " + servicePath + ".";
log.error(msg);
throw new RegistryException(msg);
}
if (!registry.resourceExists(servicePath)) {
addItHere = true;
} else {
Association[] dependencies = registry.getAssociations(servicePath, CommonConstants.DEPENDS);
boolean dependencyFound = false;
if (dependencies != null) {
for (Association dependency : dependencies) {
if (wsdlPath.equals(dependency.getDestinationPath())) {
dependencyFound = true;
}
}
}
if (!dependencyFound) {
addItHere = true;
}
}
if (addItHere) { // add the service right here..
((ResourceImpl) resource).prepareContentForPut();
persistServiceResource(registry, resource, servicePath);
alreadyAdded = true;
// and make the associations
registry.addAssociation(servicePath, wsdlPath, CommonConstants.DEPENDS);
registry.addAssociation(wsdlPath, servicePath, CommonConstants.USED_BY);
}
}
if (!alreadyAdded) {
// we are adding the resource anyway.
((ResourceImpl) resource).prepareContentForPut();
persistServiceResource(registry, resource, servicePath);
}
EndpointUtils.saveEndpointsFromServices(servicePath, serviceInfoElement, registry,
CommonUtil.getUnchrootedSystemRegistry(requestContext));
String symlinkLocation = RegistryUtils.getAbsolutePath(requestContext.getRegistryContext(),
requestContext.getResource().getProperty(RegistryConstants.SYMLINK_PROPERTY_NAME));
if (!servicePath.equals(originalServicePath)) {
// we are creating a sym link from service path to original service path.
Resource serviceResource = requestContext.getRegistry().get(
RegistryUtils.getParentPath(originalServicePath));
String isLink = serviceResource.getProperty("registry.link");
String mountPoint = serviceResource.getProperty("registry.mountpoint");
String targetPoint = serviceResource.getProperty("registry.targetpoint");
String actualPath = serviceResource.getProperty("registry.actualpath");
if (isLink != null && mountPoint != null && targetPoint != null) {
symlinkLocation = actualPath + RegistryConstants.PATH_SEPARATOR;
}
if (symlinkLocation != null) {
registry.createLink(symlinkLocation + resourceName, servicePath);
}
}
// in this flow the resource is already added. marking the process completed..
requestContext.setProcessingComplete(true);
if (wsdl != null && CommonConstants.ENABLE.equals(System.getProperty(CommonConstants.UDDI_SYSTEM_PROPERTY))) {
//creating the business service info bean
BusinessServiceInfo businessServiceInfo = new BusinessServiceInfo();
businessServiceInfo.setServiceName(serviceName.trim());
businessServiceInfo.setServiceNamespace(serviceNamespace.trim());
businessServiceInfo.setServiceDescription(CommonUtil.getServiceDescription(serviceInfoElement));
WSDLInfo wsdlInfo = wsdl.getMasterWSDLInfo();
businessServiceInfo.setServiceWSDLInfo(wsdlInfo);
businessServiceInfo.setServiceEndpoints(CommonUtil.getEndpointEntries(serviceInfoElement));
businessServiceInfo.setDocuments(CommonUtil.getDocLinks(serviceInfoElement));
UDDIPublisher publisher = new UDDIPublisher(businessServiceInfo);