Search in sources :

Example 1 with WSDLLoader

use of org.talend.utils.wsdl.WSDLLoader in project tesb-studio-se by Talend.

the class OpenWSDLPage method finish.

public boolean finish() {
    // changed by hqzhang for TDI-19527, label=displayName
    final String label = item.getProperty().getDisplayName();
    item.getProperty().setLabel(label);
    IWorkspaceRunnable runnable = new IWorkspaceRunnable() {

        public void run(final IProgressMonitor monitor) throws CoreException {
            IProxyRepositoryFactory factory = ProxyRepositoryFactory.getInstance();
            try {
                item.setConnection(ServicesFactory.eINSTANCE.createServiceConnection());
                if (creation) {
                    item.getProperty().setId(factory.getNextId());
                    factory.create(item, getDestinationPath());
                    repositoryNode = new RepositoryNode(new RepositoryViewObject(item.getProperty()), repositoryNode.getParent(), ENodeType.REPOSITORY_ELEMENT);
                }
                ((ServiceConnection) item.getConnection()).setWSDLPath(path);
                ((ServiceConnection) item.getConnection()).getServicePort().clear();
                final IFile fileWsdl = WSDLUtils.getWsdlFile(item);
                final InputStream is;
                if (null == path) {
                    ByteArrayOutputStream baos = new ByteArrayOutputStream();
                    // create new WSDL file from template
                    TemplateProcessor.processTemplate("DATA_SERVICE_WSDL", Collections.singletonMap("serviceName", (Object) label), baos, getClass().getResourceAsStream(TEMPLATE_SERVICE_WSDL));
                    is = new ByteArrayInputStream(baos.toByteArray());
                } else {
                    //$NON-NLS-1$
                    String filenameTemplate = item.getProperty().getLabel() + '_' + item.getProperty().getVersion() + ".%d.wsdl";
                    Map<String, InputStream> wsdls = new WSDLLoader().load(path, filenameTemplate);
                    is = wsdls.remove(WSDLLoader.DEFAULT_FILENAME);
                    for (Map.Entry<String, InputStream> wsdl : wsdls.entrySet()) {
                        String filename = wsdl.getKey();
                        IFile importedWsdl = fileWsdl.getParent().getFile(new Path(filename));
                        if (!importedWsdl.exists()) {
                            importedWsdl.create(wsdl.getValue(), true, monitor);
                        } else {
                            importedWsdl.setContents(wsdl.getValue(), 0, monitor);
                        }
                        createReferenceResources(filename.substring(filename.lastIndexOf('.', filename.lastIndexOf('.') - 1) + 1));
                    }
                }
                // store WSDL in service
                if (!fileWsdl.exists()) {
                    fileWsdl.create(is, true, monitor);
                } else {
                    fileWsdl.setContents(is, 0, monitor);
                }
                // create reference to wsdl
                //$NON-NLS-1$
                createReferenceResources("wsdl");
                // path
                definition = WSDLUtils.getDefinition(fileWsdl);
                populateModelFromWsdl(factory, definition, item, repositoryNode);
                factory.save(item);
                ProxyRepositoryFactory.getInstance().saveProject(ProjectManager.getInstance().getCurrentProject());
            } catch (Exception e) {
                //delete the node if any exception during the creation
                if (creation) {
                    try {
                        factory.save(item);
                        factory.deleteObjectPhysical(repositoryNode.getObject());
                    } catch (PersistenceException e1) {
                        throw getCoreException("WDSL creation failed", e1);
                    }
                }
                //throw the exception
                if (e instanceof CoreException) {
                    throw (CoreException) e;
                }
                if (e instanceof InvocationTargetException) {
                    throw getCoreException("WDSL creation failed", e.getCause());
                }
                throw getCoreException("WDSL creation failed", e);
            }
        }
    };
    IWorkspace workspace = ResourcesPlugin.getWorkspace();
    try {
        // we use the workspace scheduling rule to lock all
        ISchedulingRule schedulingRule = workspace.getRoot();
        // workspace modifications during the run.
        // the update of the project files need to be done in the workspace runnable to avoid all notification
        // of changes before the end of the modifications.
        workspace.run(runnable, schedulingRule, IWorkspace.AVOID_UPDATE, null);
    } catch (CoreException e) {
        MessageBoxExceptionHandler.process(e);
        return false;
    }
    // open wsdl editor
    OpenWSDLEditorAction action = new OpenWSDLEditorAction();
    action.setServiceItem(item);
    action.run();
    // import schemas if required
    if (checkImport.isVisible() && checkImport.getSelection() && null != definition) {
        PublishMetadataRunnable publishMetadataRunnable = new PublishMetadataRunnable(definition, getShell());
        try {
            getContainer().run(true, true, publishMetadataRunnable);
        } catch (InvocationTargetException e) {
            Throwable cause = e.getCause();
            String message = (null != cause.getMessage()) ? cause.getMessage() : cause.getClass().getName();
            setErrorMessage("Populate schema to repository: " + message);
            return false;
        } catch (InterruptedException e) {
            return false;
        }
    }
    return true;
}
Also used : ServiceConnection(org.talend.repository.services.model.services.ServiceConnection) IFile(org.eclipse.core.resources.IFile) OpenWSDLEditorAction(org.talend.repository.services.action.OpenWSDLEditorAction) IProxyRepositoryFactory(org.talend.repository.model.IProxyRepositoryFactory) WSDLLoader(org.talend.utils.wsdl.WSDLLoader) RepositoryViewObject(org.talend.core.model.repository.RepositoryViewObject) IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) IWorkspaceRunnable(org.eclipse.core.resources.IWorkspaceRunnable) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) RepositoryNode(org.talend.repository.model.RepositoryNode) CoreException(org.eclipse.core.runtime.CoreException) InvocationTargetException(java.lang.reflect.InvocationTargetException) PersistenceException(org.talend.commons.exception.PersistenceException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ISchedulingRule(org.eclipse.core.runtime.jobs.ISchedulingRule) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) CoreException(org.eclipse.core.runtime.CoreException) ByteArrayInputStream(java.io.ByteArrayInputStream) IWorkspace(org.eclipse.core.resources.IWorkspace) PersistenceException(org.talend.commons.exception.PersistenceException) RepositoryViewObject(org.talend.core.model.repository.RepositoryViewObject) PublishMetadataRunnable(org.talend.repository.services.action.PublishMetadataRunnable) Map(java.util.Map)

Example 2 with WSDLLoader

use of org.talend.utils.wsdl.WSDLLoader in project tesb-studio-se by Talend.

the class PublishMetadataRunnable method process.

@SuppressWarnings("unchecked")
private void process(Definition wsdlDefinition, Collection<XmlFileConnectionItem> selectTables) throws Exception, CoreException {
    File tempFile = null;
    try {
        File wsdlFile = null;
        String baseUri = wsdlDefinition.getDocumentBaseURI();
        URI uri = new URI(baseUri);
        if ("file".equals(uri.getScheme())) {
            wsdlFile = new File(uri.toURL().getFile());
        } else {
            Map<String, InputStream> load = new WSDLLoader().load(baseUri, "tempWsdl" + "%d.wsdl");
            InputStream inputStream = load.get(WSDLLoader.DEFAULT_FILENAME);
            tempFile = getTempFile(inputStream);
            wsdlFile = tempFile;
        }
        if (populationUtil == null) {
            populationUtil = new WSDLPopulationUtil();
            populationUtil.loadWSDL("file://" + wsdlFile.getAbsolutePath());
        }
        final Set<QName> portTypes = new HashSet<QName>();
        final Set<QName> alreadyCreated = new HashSet<QName>();
        for (Binding binding : (Collection<Binding>) wsdlDefinition.getAllBindings().values()) {
            final QName portType = binding.getPortType().getQName();
            if (portTypes.add(portType)) {
                for (BindingOperation operation : (Collection<BindingOperation>) binding.getBindingOperations()) {
                    Operation oper = operation.getOperation();
                    Input inDef = oper.getInput();
                    if (inDef != null) {
                        Message inMsg = inDef.getMessage();
                        if (inMsg != null) {
                            // fix for TDI-20699
                            QName parameterFromMessage = getParameterFromMessage(inMsg);
                            if (parameterFromMessage == null) {
                                continue;
                            }
                            if (alreadyCreated.add(parameterFromMessage)) {
                                XsdMetadataUtils.createMetadataFromXSD(parameterFromMessage, portType.getLocalPart(), oper.getName(), selectTables, wsdlFile, populationUtil);
                            }
                        }
                    }
                    Output outDef = oper.getOutput();
                    if (outDef != null) {
                        Message outMsg = outDef.getMessage();
                        if (outMsg != null) {
                            QName parameterFromMessage = getParameterFromMessage(outMsg);
                            if (parameterFromMessage == null) {
                                continue;
                            }
                            if (alreadyCreated.add(parameterFromMessage)) {
                                XsdMetadataUtils.createMetadataFromXSD(parameterFromMessage, portType.getLocalPart(), oper.getName(), selectTables, wsdlFile, populationUtil);
                            }
                        }
                    }
                    for (Fault fault : (Collection<Fault>) oper.getFaults().values()) {
                        Message faultMsg = fault.getMessage();
                        if (faultMsg != null) {
                            QName parameterFromMessage = getParameterFromMessage(faultMsg);
                            if (parameterFromMessage == null) {
                                continue;
                            }
                            if (alreadyCreated.add(parameterFromMessage)) {
                                XsdMetadataUtils.createMetadataFromXSD(parameterFromMessage, portType.getLocalPart(), oper.getName(), selectTables, wsdlFile, populationUtil);
                            }
                        }
                    }
                }
            }
        }
    } catch (Exception e) {
        throw e;
    } finally {
        if (tempFile != null) {
            tempFile.delete();
        }
    }
}
Also used : Binding(javax.wsdl.Binding) Message(javax.wsdl.Message) InputStream(java.io.InputStream) QName(javax.xml.namespace.QName) Fault(javax.wsdl.Fault) Operation(javax.wsdl.Operation) BindingOperation(javax.wsdl.BindingOperation) URI(java.net.URI) URISyntaxException(java.net.URISyntaxException) CoreException(org.eclipse.core.runtime.CoreException) InvocationTargetException(java.lang.reflect.InvocationTargetException) IOException(java.io.IOException) PersistenceException(org.talend.commons.exception.PersistenceException) BindingOperation(javax.wsdl.BindingOperation) Input(javax.wsdl.Input) WSDLPopulationUtil(org.talend.repository.services.utils.WSDLPopulationUtil) Output(javax.wsdl.Output) Collection(java.util.Collection) IFile(org.eclipse.core.resources.IFile) File(java.io.File) WSDLLoader(org.talend.utils.wsdl.WSDLLoader) HashSet(java.util.HashSet)

Example 3 with WSDLLoader

use of org.talend.utils.wsdl.WSDLLoader in project tesb-studio-se by Talend.

the class WSDLHelper method load.

public static Definition load(String wsdlLocation, String filenamePrefix) throws InvocationTargetException, WSDLException {
    WSDLFactory wsdlFactory = WSDLFactory.newInstance();
    WSDLReader newWSDLReader = wsdlFactory.newWSDLReader();
    newWSDLReader.setExtensionRegistry(wsdlFactory.newPopulatedExtensionRegistry());
    newWSDLReader.setFeature(com.ibm.wsdl.Constants.FEATURE_VERBOSE, false);
    return newWSDLReader.readWSDL(new InMemoryWSDLLocator(wsdlLocation, new WSDLLoader().load(wsdlLocation, filenamePrefix + "%d.wsdl")));
}
Also used : WSDLFactory(javax.wsdl.factory.WSDLFactory) WSDLReader(javax.wsdl.xml.WSDLReader) WSDLLoader(org.talend.utils.wsdl.WSDLLoader)

Aggregations

WSDLLoader (org.talend.utils.wsdl.WSDLLoader)3 InputStream (java.io.InputStream)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 IFile (org.eclipse.core.resources.IFile)2 CoreException (org.eclipse.core.runtime.CoreException)2 PersistenceException (org.talend.commons.exception.PersistenceException)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 File (java.io.File)1 IOException (java.io.IOException)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 Collection (java.util.Collection)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 Binding (javax.wsdl.Binding)1 BindingOperation (javax.wsdl.BindingOperation)1 Fault (javax.wsdl.Fault)1 Input (javax.wsdl.Input)1 Message (javax.wsdl.Message)1