Search in sources :

Example 1 with WsdlGenerator

use of nl.nn.adapterframework.soap.WsdlGenerator in project iaf by ibissource.

the class WsdlGeneratorPipe method doPipe.

@Override
public PipeRunResult doPipe(Message message, PipeLineSession session) throws PipeRunException {
    Message fileInSession = session.getMessage(getSessionKey());
    if (fileInSession == null) {
        throw new PipeRunException(this, getLogPrefix(session) + "got null value from session under key [" + getSessionKey() + "]");
    }
    File tempDir;
    String fileName;
    try (InputStream inputStream = fileInSession.asInputStream()) {
        tempDir = FileUtils.createTempDir(null, "WEB-INF" + File.separator + "classes");
        fileName = session.getMessage(getFilenameSessionKey()).asString();
        if (FileUtils.extensionEqualsIgnoreCase(fileName, "zip")) {
            FileUtils.unzipStream(inputStream, tempDir);
        } else {
            File file = new File(tempDir, fileName);
            Misc.streamToFile(inputStream, file);
            file.deleteOnExit();
        }
    } catch (IOException e) {
        throw new PipeRunException(this, getLogPrefix(session) + " Exception on uploading and unzipping/writing file", e);
    }
    File propertiesFile = new File(tempDir, getPropertiesFileName());
    PipeLine pipeLine;
    ClassLoader originalClassLoader = Thread.currentThread().getContextClassLoader();
    try {
        // A DirectoryClassloader is used to create a new 'dummy' pipeline, see createPipeLineFromPropertiesFile(String)
        // This method reads a properties file and xsd's (when present) to programmatically 'create' a pipeline.
        // The pipeline will then be used to generate a new WSDL file.
        DirectoryClassLoader directoryClassLoader = new DirectoryClassLoader(originalClassLoader);
        directoryClassLoader.setDirectory(tempDir.getPath());
        directoryClassLoader.setBasePath(".");
        directoryClassLoader.configure(getAdapter().getConfiguration().getIbisManager().getIbisContext(), "dummy");
        Thread.currentThread().setContextClassLoader(directoryClassLoader);
        if (propertiesFile.exists()) {
            pipeLine = createPipeLineFromPropertiesFile(propertiesFile);
        } else {
            File xsdFile = FileUtils.getFirstFile(tempDir);
            pipeLine = createPipeLineFromXsdFile(xsdFile);
        }
    } catch (Exception e) {
        throw new PipeRunException(this, getLogPrefix(session) + " Exception on generating wsdl", e);
    } finally {
        if (originalClassLoader != null) {
            Thread.currentThread().setContextClassLoader(originalClassLoader);
        }
    }
    Object result = null;
    OutputStream zipOut = null;
    OutputStream fullWsdlOut = null;
    try {
        Adapter adapter = new Adapter();
        Configuration configuration = new Configuration();
        configuration.setClassLoader(getConfigurationClassLoader());
        adapter.setConfiguration(configuration);
        String fileBaseName = FileUtils.getBaseName(fileName).replaceAll(" ", "_");
        adapter.setName(fileBaseName);
        Receiver receiver = new Receiver();
        EsbJmsListener esbJmsListener = new EsbJmsListener();
        esbJmsListener.setQueueConnectionFactoryName("jms/qcf_" + fileBaseName);
        esbJmsListener.setDestinationName("jms/dest_" + fileBaseName);
        receiver.setListener(esbJmsListener);
        adapter.registerReceiver(receiver);
        adapter.setPipeLine(pipeLine);
        WsdlGenerator wsdl = null;
        String generationInfo = "at " + RestListenerUtils.retrieveRequestURL(session);
        wsdl = new WsdlGenerator(pipeLine, generationInfo);
        wsdl.setIndent(true);
        wsdl.init();
        File wsdlDir = FileUtils.createTempDir(tempDir);
        // zip (with includes)
        File zipOutFile = new File(wsdlDir, wsdl.getFilename() + ".zip");
        zipOutFile.deleteOnExit();
        zipOut = new FileOutputStream(zipOutFile);
        wsdl.setUseIncludes(true);
        wsdl.zip(zipOut, null);
        // full wsdl (without includes)
        File fullWsdlOutFile = new File(wsdlDir, wsdl.getFilename() + ".wsdl");
        fullWsdlOutFile.deleteOnExit();
        fullWsdlOut = new FileOutputStream(fullWsdlOutFile);
        wsdl.setUseIncludes(false);
        wsdl.wsdl(fullWsdlOut, null);
        Dir2Xml dx = new Dir2Xml();
        dx.setPath(wsdlDir.getPath());
        result = dx.getDirList();
    } catch (Exception e) {
        throw new PipeRunException(this, getLogPrefix(session) + " Exception on generating wsdl", e);
    } finally {
        try {
            if (zipOut != null) {
                zipOut.close();
            }
            if (fullWsdlOut != null) {
                fullWsdlOut.close();
            }
        } catch (IOException e1) {
            log.warn("exception closing outputstream", e1);
        }
    }
    return new PipeRunResult(getSuccessForward(), result);
}
Also used : Dir2Xml(nl.nn.adapterframework.util.Dir2Xml) DirectoryClassLoader(nl.nn.adapterframework.configuration.classloaders.DirectoryClassLoader) Message(nl.nn.adapterframework.stream.Message) Configuration(nl.nn.adapterframework.configuration.Configuration) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) Receiver(nl.nn.adapterframework.receivers.Receiver) Adapter(nl.nn.adapterframework.core.Adapter) IOException(java.io.IOException) PipeRunException(nl.nn.adapterframework.core.PipeRunException) IOException(java.io.IOException) ConfigurationException(nl.nn.adapterframework.configuration.ConfigurationException) PipeRunResult(nl.nn.adapterframework.core.PipeRunResult) FileOutputStream(java.io.FileOutputStream) PipeRunException(nl.nn.adapterframework.core.PipeRunException) DirectoryClassLoader(nl.nn.adapterframework.configuration.classloaders.DirectoryClassLoader) PipeLine(nl.nn.adapterframework.core.PipeLine) File(java.io.File) WsdlGenerator(nl.nn.adapterframework.soap.WsdlGenerator)

Example 2 with WsdlGenerator

use of nl.nn.adapterframework.soap.WsdlGenerator in project iaf by ibissource.

the class Webservices method getWebServices.

@GET
@RolesAllowed({ "IbisObserver", "IbisDataAdmin", "IbisAdmin", "IbisTester" })
@Path("/webservices")
@Relation("webservices")
@Produces(MediaType.APPLICATION_JSON)
public Response getWebServices() throws ApiException {
    Map<String, Object> returnMap = new HashMap<String, Object>();
    List<Map<String, Object>> webServices = new ArrayList<Map<String, Object>>();
    for (Adapter adapter : getIbisManager().getRegisteredAdapters()) {
        for (Receiver receiver : adapter.getReceivers()) {
            IListener listener = receiver.getListener();
            if (listener instanceof RestListener) {
                RestListener rl = (RestListener) listener;
                Map<String, Object> service = new HashMap<String, Object>();
                service.put("name", adapter.getName() + " " + receiver.getName());
                service.put("method", rl.getMethod());
                service.put("view", rl.isView());
                service.put("uriPattern", rl.getUriPattern());
                webServices.add(service);
            }
        }
    }
    returnMap.put("services", webServices);
    List<Map<String, Object>> wsdls = new ArrayList<Map<String, Object>>();
    for (Adapter adapter : getIbisManager().getRegisteredAdapters()) {
        Map<String, Object> wsdlMap = null;
        try {
            if (WsdlGeneratorUtils.canProvideWSDL(adapter)) {
                // check eligibility
                wsdlMap = new HashMap<String, Object>(2);
                WsdlGenerator wsdl = new WsdlGenerator(adapter.getPipeLine());
                wsdlMap.put("name", wsdl.getName());
                wsdlMap.put("extension", getWsdlExtension());
            }
        } catch (Exception e) {
            wsdlMap.put("name", adapter.getName());
            if (e.getMessage() != null) {
                wsdlMap.put("error", e.getMessage());
            } else {
                wsdlMap.put("error", e.toString());
            }
        }
        if (wsdlMap != null) {
            wsdls.add(wsdlMap);
        }
    }
    returnMap.put("wsdls", wsdls);
    // ApiListeners
    List<Map<String, Object>> apiListeners = new LinkedList<Map<String, Object>>();
    SortedMap<String, ApiDispatchConfig> patternClients = ApiServiceDispatcher.getInstance().getPatternClients();
    for (Entry<String, ApiDispatchConfig> client : patternClients.entrySet()) {
        ApiDispatchConfig config = client.getValue();
        Set<HttpMethod> methods = config.getMethods();
        for (HttpMethod method : methods) {
            ApiListener listener = config.getApiListener(method);
            Receiver receiver = listener.getReceiver();
            IAdapter adapter = receiver == null ? null : receiver.getAdapter();
            Map<String, Object> endpoint = new HashMap<>();
            String uriPattern = listener.getUriPattern();
            endpoint.put("uriPattern", uriPattern);
            endpoint.put("method", method);
            if (adapter != null)
                endpoint.put("adapter", adapter.getName());
            if (receiver != null)
                endpoint.put("receiver", receiver.getName());
            String schemaResource = uriPattern.substring(1).replace("/", "_") + "_" + method + "_" + "openapi.json";
            endpoint.put("schemaResource", schemaResource);
            apiListeners.add(endpoint);
        }
    }
    returnMap.put("apiListeners", apiListeners);
    return Response.status(Response.Status.OK).entity(returnMap).build();
}
Also used : HashMap(java.util.HashMap) IListener(nl.nn.adapterframework.core.IListener) ArrayList(java.util.ArrayList) Receiver(nl.nn.adapterframework.receivers.Receiver) Adapter(nl.nn.adapterframework.core.Adapter) IAdapter(nl.nn.adapterframework.core.IAdapter) NamingException(javax.naming.NamingException) XMLStreamException(javax.xml.stream.XMLStreamException) IOException(java.io.IOException) ConfigurationException(nl.nn.adapterframework.configuration.ConfigurationException) WebApplicationException(javax.ws.rs.WebApplicationException) LinkedList(java.util.LinkedList) RestListener(nl.nn.adapterframework.http.RestListener) ApiListener(nl.nn.adapterframework.http.rest.ApiListener) ApiDispatchConfig(nl.nn.adapterframework.http.rest.ApiDispatchConfig) HashMap(java.util.HashMap) Map(java.util.Map) SortedMap(java.util.SortedMap) WsdlGenerator(nl.nn.adapterframework.soap.WsdlGenerator) IAdapter(nl.nn.adapterframework.core.IAdapter) HttpMethod(nl.nn.adapterframework.http.rest.ApiListener.HttpMethod) Path(javax.ws.rs.Path) RolesAllowed(javax.annotation.security.RolesAllowed) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 3 with WsdlGenerator

use of nl.nn.adapterframework.soap.WsdlGenerator in project iaf by ibissource.

the class Webservices method getWsdl.

@GET
@RolesAllowed({ "IbisObserver", "IbisDataAdmin", "IbisAdmin", "IbisTester" })
@Path("/webservices/{resourceName}")
@Relation("webservices")
@Produces(MediaType.APPLICATION_XML)
public Response getWsdl(@PathParam("resourceName") String resourceName, @DefaultValue("true") @QueryParam("indent") boolean indent, @DefaultValue("false") @QueryParam("useIncludes") boolean useIncludes) throws ApiException {
    String adapterName;
    boolean zip;
    int dotPos = resourceName.lastIndexOf('.');
    if (dotPos >= 0) {
        adapterName = resourceName.substring(0, dotPos);
        zip = resourceName.substring(dotPos).equals(".zip");
    } else {
        adapterName = resourceName;
        zip = false;
    }
    if (StringUtils.isEmpty(adapterName)) {
        return Response.status(Response.Status.BAD_REQUEST).entity("<error>no adapter specified</error>").build();
    }
    IAdapter adapter = getIbisManager().getRegisteredAdapter(adapterName);
    if (adapter == null) {
        return Response.status(Response.Status.BAD_REQUEST).entity("<error>adapter not found</error>").build();
    }
    try {
        String servletName = getServiceEndpoint(adapter);
        String generationInfo = "by FrankConsole";
        WsdlGenerator wsdl = new WsdlGenerator(adapter.getPipeLine(), generationInfo);
        wsdl.setIndent(indent);
        wsdl.setUseIncludes(useIncludes || zip);
        wsdl.init();
        StreamingOutput stream = new StreamingOutput() {

            @Override
            public void write(OutputStream out) throws IOException, WebApplicationException {
                try {
                    if (zip) {
                        wsdl.zip(out, servletName);
                    } else {
                        wsdl.wsdl(out, servletName);
                    }
                } catch (ConfigurationException | XMLStreamException | NamingException e) {
                    throw new WebApplicationException(e);
                }
            }
        };
        ResponseBuilder responseBuilder = Response.ok(stream);
        if (zip) {
            responseBuilder.type(MediaType.APPLICATION_OCTET_STREAM);
            responseBuilder.header("Content-Disposition", "attachment; filename=\"" + adapterName + ".zip\"");
        }
        return responseBuilder.build();
    } catch (Exception e) {
        throw new ApiException("exception on retrieving wsdl", e);
    }
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) OutputStream(java.io.OutputStream) StreamingOutput(javax.ws.rs.core.StreamingOutput) NamingException(javax.naming.NamingException) XMLStreamException(javax.xml.stream.XMLStreamException) IOException(java.io.IOException) ConfigurationException(nl.nn.adapterframework.configuration.ConfigurationException) WebApplicationException(javax.ws.rs.WebApplicationException) XMLStreamException(javax.xml.stream.XMLStreamException) ConfigurationException(nl.nn.adapterframework.configuration.ConfigurationException) NamingException(javax.naming.NamingException) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) IAdapter(nl.nn.adapterframework.core.IAdapter) WsdlGenerator(nl.nn.adapterframework.soap.WsdlGenerator) Path(javax.ws.rs.Path) RolesAllowed(javax.annotation.security.RolesAllowed) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 4 with WsdlGenerator

use of nl.nn.adapterframework.soap.WsdlGenerator in project iaf by ibissource.

the class WsdlGeneratorPipe method doPipe.

@Override
public PipeRunResult doPipe(Message message, PipeLineSession session) throws PipeRunException {
    String result = null;
    Adapter adapter;
    try {
        if ("input".equals(getFrom())) {
            String adapterName = message.asString();
            adapter = getAdapter().getConfiguration().getIbisManager().getRegisteredAdapter(adapterName);
            if (adapter == null) {
                throw new PipeRunException(this, "Could not find adapter: " + adapterName);
            }
        } else {
            adapter = getPipeLine().getAdapter();
        }
    } catch (IOException e) {
        throw new PipeRunException(this, "Could not determine adapter name", e);
    }
    try {
        String generationInfo = "at " + RestListenerUtils.retrieveRequestURL(session);
        WsdlGenerator wsdl = new WsdlGenerator(adapter.getPipeLine(), generationInfo);
        wsdl.init();
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        wsdl.wsdl(outputStream, null);
        result = outputStream.toString(StreamUtil.DEFAULT_INPUT_STREAM_ENCODING);
    } catch (Exception e) {
        throw new PipeRunException(this, "Could not generate WSDL for adapter [" + adapter.getName() + "]", e);
    }
    return new PipeRunResult(getSuccessForward(), result);
}
Also used : PipeRunResult(nl.nn.adapterframework.core.PipeRunResult) PipeRunException(nl.nn.adapterframework.core.PipeRunException) Adapter(nl.nn.adapterframework.core.Adapter) IOException(java.io.IOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) WsdlGenerator(nl.nn.adapterframework.soap.WsdlGenerator) PipeRunException(nl.nn.adapterframework.core.PipeRunException) IOException(java.io.IOException) ConfigurationException(nl.nn.adapterframework.configuration.ConfigurationException)

Aggregations

IOException (java.io.IOException)4 ConfigurationException (nl.nn.adapterframework.configuration.ConfigurationException)4 WsdlGenerator (nl.nn.adapterframework.soap.WsdlGenerator)4 Adapter (nl.nn.adapterframework.core.Adapter)3 OutputStream (java.io.OutputStream)2 RolesAllowed (javax.annotation.security.RolesAllowed)2 NamingException (javax.naming.NamingException)2 GET (javax.ws.rs.GET)2 Path (javax.ws.rs.Path)2 Produces (javax.ws.rs.Produces)2 WebApplicationException (javax.ws.rs.WebApplicationException)2 XMLStreamException (javax.xml.stream.XMLStreamException)2 IAdapter (nl.nn.adapterframework.core.IAdapter)2 PipeRunException (nl.nn.adapterframework.core.PipeRunException)2 PipeRunResult (nl.nn.adapterframework.core.PipeRunResult)2 Receiver (nl.nn.adapterframework.receivers.Receiver)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1