Examples of WSDLProcessor


Examples of org.wso2.carbon.registry.extensions.handlers.utils.WSDLProcessor

            } else {
                wsdlName = "/" + wsdlName;
            }
            path = path + wsdlName;
            requestContext.setResourcePath(new ResourcePath(path));
            WSDLProcessor wsdlProcessor = new WSDLProcessor(requestContext);
            return wsdlProcessor.addWSDLToRegistry(requestContext, uri, local, false, true,
                    disableWSDLValidation);
        }
        return null;
    }
View Full Code Here

Examples of org.wso2.carbon.registry.extensions.handlers.utils.WSDLProcessor

            }
        }
    }

    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);
View Full Code Here

Examples of org.wso2.carbon.registry.extensions.handlers.utils.WSDLProcessor

    public void put(RequestContext requestContext) throws RegistryException {
        if (!CommonUtil.isUpdateLockAvailable()) {
            return;
        }
        CommonUtil.acquireUpdateLock();
                WSDLProcessor wsdlProcessor=null;
        try {
            Resource metadata = requestContext.getResource();
            String path = requestContext.getResourcePath().getPath();
            try {
                // If the WSDL is already there, we don't need to re-run this handler unless the content is changed.
                // Re-running this handler causes issues with downstream handlers and other behaviour (ex:- lifecycles).
                // If you need to do a replace pragmatically, delete-then-replace.
                if (metadata == null) {
                    // will go with the default processing
                    return;
                }
                Registry registry = requestContext.getRegistry();

                // This is to distinguish operations on xsd and wsdl on remote mounting.
                String remotePut = metadata.getProperty(RegistryConstants.REMOTE_MOUNT_OPERATION);
                if (remotePut != null) {
                    metadata.removeProperty(RegistryConstants.REMOTE_MOUNT_OPERATION);
                    registry.put(path, metadata);
                    requestContext.setProcessingComplete(true);
                    return;
                }

                if (registry.resourceExists(path)) {
                    // logic to compare content, and return only if the content didn't change.
                    Object newContent = metadata.getContent();

                    Resource oldResource = registry.get(path);
                    Object oldContent = oldResource.getContent();
                    String newContentString = null;
                    String oldContentString = null;
                    if (newContent != null) {
                        if (newContent instanceof String) {
                            newContentString = (String) newContent;
                        } else {
                            newContentString = new String((byte[]) newContent);
                        }
                    }
                    if (oldContent != null) {
                        if (oldContent instanceof String) {
                            oldContentString = (String) oldContent;
                        } else {
                            oldContentString = new String((byte[]) oldContent);
                        }
                    }
                    if ((newContent == null && oldContent == null) ||
                            (newContentString != null && newContentString.equals(oldContentString))) {
                        // this will continue adding from the default path.
                        return;
                    }

                    // so we creating temp files for the wsdl and all the dependencies.
                    Set<String> registryPaths = new LinkedHashSet<String>();
                    // the first path is the current resource path.
                    registryPaths.add(path);
                    // get the associations.
                    Association[] dependencies = registry.getAssociations(path, CommonConstants.DEPENDS);
                    if (dependencies != null) {
                        for (Association dependency: dependencies) {
                            String targetPath = dependency.getDestinationPath();
                            if (targetPath.startsWith(RegistryConstants.ROOT_PATH)) {
                                registryPaths.add(targetPath);
                            }
                        }
                    }

                    File referenceTempFile = File.createTempFile("wsdl", ".ref");
                    File tempDir = new File(referenceTempFile.getAbsolutePath().substring(0,
                            referenceTempFile.getAbsolutePath().length() - ".ref".length()));
                    String tempDirPath = tempDir.getAbsolutePath();
                    // now add each of the registry paths to the the tempDir
                    List<File> tempFiles = new ArrayList<File>();
                    for (String registryPath: registryPaths) {
                        if (!registryPath.startsWith(RegistryConstants.ROOT_PATH)) {
                            continue;
                        }
                        String filePath = tempDirPath + registryPath;
                        File tempFile = new File(filePath);
                        makeDirs(tempFile.getParentFile());

                        Object resourceContent;
                        if (registryPath.equals(path)) {
                            // this is the wsdl we want to update.
                            resourceContent = metadata.getContent();
                        } else {
                            Resource r = registry.get(registryPath);
                            if (r == null) {
                                continue;
                            }
                            resourceContent = r.getContent();
                        }
                        byte[] resourceContentBytes;

                        if (resourceContent == null) {
                            resourceContentBytes = new byte[0];
                        } else if (resourceContent instanceof byte[]) {
                            resourceContentBytes = (byte[])resourceContent;
                        } else if (resourceContent instanceof String) {
                            resourceContentBytes = ((String)resourceContent).getBytes();
                        } else {
                            String msg = "Unknown type for the content path: " + path + ", content type: " +
                                    resourceContent.getClass().getName() + ".";
                            log.error(msg);
                            throw new RegistryException(msg);
                        }
                        InputStream in = new ByteArrayInputStream(resourceContentBytes);

                        BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(tempFile));
                        byte[] contentChunk = new byte[1024];
                        int byteCount;
                        while ((byteCount = in.read(contentChunk)) != -1) {
                            out.write(contentChunk, 0, byteCount);
                        }
                        out.flush();
                        out.close();
                        tempFiles.add(tempFile);
                    }

                    if (tempFiles.size() == 0) {
                        // unreachable state, anyway better log and return.
                        String msg = "Temporary files count is zero, when updating a wsdl. " +
                                "wsdl path: " + path + ".";
                        log.error(msg);
                        // we are just returning, as the put operation will continue in its default path.
                        return;
                    }

                    File tempFile = tempFiles.get(0);
                    String uri = tempFile.toURI().toString();
                    if (uri.startsWith("file:")) {
                        uri = uri.substring(5);
                    }
                    while (uri.startsWith("/")) {
                        uri = uri.substring(1);
                    }
                    uri = "file:///" + uri;
                    String wsdlPath = null;
                    if (uri != null) {
                        requestContext.setSourceURL(uri);
                        requestContext.setResource(metadata);

                        wsdlProcessor = new WSDLProcessor(requestContext);
                        wsdlPath = processWSDLImport(requestContext, wsdlProcessor, metadata, uri);
                    }

                    // now we will delete each temp files, ref file and the temp directory.
                    for (File temp : tempFiles) {
                        FileUtils.forceDelete(temp);
                    }
                    FileUtils.deleteDirectory(tempDir);
                    FileUtils.forceDelete(referenceTempFile);

                    if (wsdlPath != null) {
                        onPutCompleted(path, Collections.singletonMap(uri, wsdlPath),
                                Collections.<String>emptyList(), requestContext);
                    }
                    requestContext.setProcessingComplete(true);
                    return;
                }
            } catch (IOException e) {
                String msg = "Error in updating the wsdl. wsdl path: " + path + ".";
                log.error(msg, e);
                throw new RegistryException(msg, e);
            }
            try {
                Object resourceContent = metadata.getContent();
                byte[] resourceContentBytes;

                if (resourceContent == null) {
                    resourceContentBytes = new byte[0];
                } else if (resourceContent instanceof byte[]) {
                    resourceContentBytes = (byte[])resourceContent;
                } else if (resourceContent instanceof String) {
                    resourceContentBytes = ((String)resourceContent).getBytes();
                } else {
                    String msg = "Unknown type for the content path: " + path + ", content type: " +
                            resourceContent.getClass().getName() + ".";
                    log.error(msg);
                    throw new RegistryException(msg);
                }
                InputStream in = new ByteArrayInputStream(resourceContentBytes);

                File tempFile = File.createTempFile("wsdl", ".wsdl");

                BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(tempFile));
                byte[] contentChunk = new byte[1024];
                int byteCount;
                while ((byteCount = in.read(contentChunk)) != -1) {
                    out.write(contentChunk, 0, byteCount);
                }
                out.flush();
                out.close();
                String uri = tempFile.toURI().toString();
                if (uri.startsWith("file:")) {
                    uri = uri.substring(5);
                }
                while (uri.startsWith("/")) {
                    uri = uri.substring(1);
                }
                uri = "file:///" + uri;
                String wsdlPath = null;
                if (uri != null) {
                    requestContext.setSourceURL(uri);
                    requestContext.setResource(metadata);

                    wsdlProcessor = new WSDLProcessor(requestContext);
                    wsdlPath = processWSDLImport(requestContext, wsdlProcessor, metadata, uri);
                }
                delete(tempFile);
                if (wsdlPath != null) {
                    onPutCompleted(path, Collections.singletonMap(uri, wsdlPath),
                            Collections.<String>emptyList(), requestContext);
                }
                /*WSDLProcessor wsdlProcessor = new WSDLProcessor(requestContext.getRegistry());
                wsdlProcessor
                       .addWSDLToRegistry(
                               requestContext, null,
                               metadata, true, true);*/
            } catch (IOException e) {
                throw new RegistryException("An error occurred while uploading WSDL file", e);
            }

            requestContext.setProcessingComplete(true);

            if (wsdlProcessor != null && CommonConstants.ENABLE.equals(System.getProperty(CommonConstants.UDDI_SYSTEM_PROPERTY))) {
                BusinessServiceInfo businessServiceInfo = new BusinessServiceInfo();
                WSDLInfo wsdlInfo = wsdlProcessor.getMasterWSDLInfo();
                businessServiceInfo.setServiceWSDLInfo(wsdlInfo);
                UDDIPublisher publisher = new UDDIPublisher(businessServiceInfo);
                publisher.publishBusinessService();
            }
        } finally {
View Full Code Here

Examples of org.wso2.carbon.registry.extensions.handlers.utils.WSDLProcessor

    public void importResource(RequestContext requestContext) throws RegistryException {
        if (!CommonUtil.isUpdateLockAvailable()) {
            return;
        }
        CommonUtil.acquireUpdateLock();
        WSDLProcessor wsdlProcessor=null;
        try {
            Resource metadata = requestContext.getResource();
            String sourceURL = requestContext.getSourceURL();
            if (requestContext.getSourceURL() != null &&
                    requestContext.getSourceURL().toLowerCase().startsWith("file:")) {
                String msg = "The source URL must not be file in the server's local file system";
                throw new RegistryException(msg);
            }
            try {
                wsdlProcessor = new WSDLProcessor(requestContext);
                String wsdlPath =
                        processWSDLImport(requestContext, wsdlProcessor, metadata, sourceURL);
                ResourcePath resourcePath = requestContext.getResourcePath();
                String path = null;
                if (resourcePath != null) {
                    path = resourcePath.getPath();
                }
                onPutCompleted(path,
                        Collections.singletonMap(sourceURL, wsdlPath),
                        Collections.<String>emptyList(), requestContext);
            } catch (Exception e) {
                throw new RegistryException(e.getMessage(), e);
            }

            requestContext.setProcessingComplete(true);

            if (wsdlProcessor != null && CommonConstants.ENABLE.equals(System.getProperty(CommonConstants.UDDI_SYSTEM_PROPERTY))) {
                BusinessServiceInfo businessServiceInfo = new BusinessServiceInfo();
                businessServiceInfo.setServiceWSDLInfo(wsdlProcessor.getMasterWSDLInfo());
                UDDIPublisher publisher = new UDDIPublisher(businessServiceInfo);
                publisher.publishBusinessService();
            }
        } finally {
            CommonUtil.releaseUpdateLock();
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.