/*
* Copyright (c) 2005-2010, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.wso2.carbon.registry.extensions.handlers;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.impl.builder.StAXOMBuilder;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.registry.core.*;
import org.wso2.carbon.registry.core.exceptions.RegistryException;
import org.wso2.carbon.registry.core.jdbc.handlers.Handler;
import org.wso2.carbon.registry.core.jdbc.handlers.RequestContext;
import org.wso2.carbon.registry.core.utils.RegistryUtils;
import org.wso2.carbon.registry.extensions.beans.BusinessServiceInfo;
import org.wso2.carbon.registry.extensions.handlers.utils.EndpointUtils;
import org.wso2.carbon.registry.extensions.handlers.utils.UDDIPublisher;
import org.wso2.carbon.registry.extensions.handlers.utils.WSDLInfo;
import org.wso2.carbon.registry.extensions.handlers.utils.WSDLProcessor;
import org.wso2.carbon.registry.extensions.utils.CommonConstants;
import org.wso2.carbon.registry.extensions.utils.CommonUtil;
import javax.xml.namespace.QName;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamReader;
import java.io.StringReader;
import java.util.*;
/**
* Handler to process the service.
*/
public class ServiceMediaTypeHandler extends Handler {
private static final Log log = LogFactory.getLog(ServiceMediaTypeHandler.class);
private static final String TRUNK = "trunk";
private boolean disableWSDLValidation = false;
private List<String> smartLifecycleLinks = new LinkedList<String>();
public void setSmartLifecycleLinks(OMElement locationConfiguration) throws RegistryException {
Iterator confElements = locationConfiguration.getChildElements();
while (confElements.hasNext()) {
OMElement confElement = (OMElement)confElements.next();
if (confElement.getQName().equals(new QName("key"))) {
smartLifecycleLinks.add(confElement.getText());
}
}
}
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);
publisher.publishBusinessService();
}
} finally {
CommonUtil.releaseUpdateLock();
}
}
private void persistServiceResource(Registry registry, Resource resource,
String servicePath) throws RegistryException {
registry.put(servicePath, resource);
}
public void setDisableWSDLValidation(String disableWSDLValidation) {
this.disableWSDLValidation = Boolean.toString(true).equals(disableWSDLValidation);
}
public String mergeServiceContent(String newContent, String oldContent) {
return newContent;
}
}