Search in sources :

Example 6 with WSDLException

use of javax.wsdl.WSDLException in project ofbiz-framework by apache.

the class SOAPEventHandler method invoke.

/**
 * @see org.apache.ofbiz.webapp.event.EventHandler#invoke(ConfigXMLReader.Event, ConfigXMLReader.RequestMap, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
 */
public String invoke(Event event, RequestMap requestMap, HttpServletRequest request, HttpServletResponse response) throws EventHandlerException {
    LocalDispatcher dispatcher = (LocalDispatcher) request.getAttribute("dispatcher");
    Delegator delegator = (Delegator) request.getAttribute("delegator");
    // first check for WSDL request
    String wsdlReq = request.getParameter("wsdl");
    if (wsdlReq == null) {
        wsdlReq = request.getParameter("WSDL");
    }
    if (wsdlReq != null) {
        String serviceName = RequestHandler.getOverrideViewUri(request.getPathInfo());
        DispatchContext dctx = dispatcher.getDispatchContext();
        String locationUri = this.getLocationURI(request);
        if (serviceName != null) {
            Document wsdl = null;
            try {
                wsdl = dctx.getWSDL(serviceName, locationUri);
            } catch (GenericServiceException e) {
                serviceName = null;
            } catch (WSDLException e) {
                sendError(response, "Unable to obtain WSDL", serviceName);
                throw new EventHandlerException("Unable to obtain WSDL", e);
            }
            if (wsdl != null) {
                try {
                    OutputStream os = response.getOutputStream();
                    response.setContentType("text/xml");
                    UtilXml.writeXmlDocument(os, wsdl);
                    response.flushBuffer();
                } catch (IOException e) {
                    throw new EventHandlerException(e);
                }
                return null;
            } else {
                sendError(response, "Unable to obtain WSDL", serviceName);
                throw new EventHandlerException("Unable to obtain WSDL");
            }
        }
        if (serviceName == null) {
            try {
                Writer writer = response.getWriter();
                StringBuilder sb = new StringBuilder();
                sb.append("<html><head><title>OFBiz SOAP/1.1 Services</title></head>");
                sb.append("<body>No such service.").append("<p>Services:<ul>");
                for (String scvName : dctx.getAllServiceNames()) {
                    ModelService model = dctx.getModelService(scvName);
                    if (model.export) {
                        sb.append("<li><a href=\"").append(locationUri).append("/").append(model.name).append("?wsdl\">");
                        sb.append(model.name).append("</a></li>");
                    }
                }
                sb.append("</ul></p></body></html>");
                writer.write(sb.toString());
                writer.flush();
                return null;
            } catch (Exception e) {
                sendError(response, "Unable to obtain WSDL", null);
                throw new EventHandlerException("Unable to obtain WSDL");
            }
        }
    }
    // not a wsdl request; invoke the service
    response.setContentType("text/xml");
    // request envelope
    SOAPEnvelope reqEnv = null;
    // get the service name and parameters
    try {
        InputStream inputStream = (InputStream) request.getInputStream();
        SOAPModelBuilder builder = (SOAPModelBuilder) OMXMLBuilderFactory.createSOAPModelBuilder(inputStream, "UTF-8");
        reqEnv = (SOAPEnvelope) builder.getDocumentElement();
        // log the request message
        if (Debug.verboseOn()) {
            try {
                Debug.logInfo("Request Message:\n" + reqEnv + "\n", module);
            } catch (Throwable t) {
            }
        }
    } catch (Exception e) {
        sendError(response, "Problem processing the service", null);
        throw new EventHandlerException("Cannot get the envelope", e);
    }
    if (Debug.verboseOn())
        Debug.logVerbose("[Processing]: SOAP Event", module);
    String serviceName = null;
    try {
        SOAPBody reqBody = reqEnv.getBody();
        validateSOAPBody(reqBody);
        OMElement serviceElement = reqBody.getFirstElement();
        serviceName = serviceElement.getLocalName();
        Map<String, Object> parameters = UtilGenerics.cast(SoapSerializer.deserialize(serviceElement.toString(), delegator));
        try {
            // verify the service is exported for remote execution and invoke it
            ModelService model = dispatcher.getDispatchContext().getModelService(serviceName);
            if (model == null) {
                sendError(response, "Problem processing the service", serviceName);
                Debug.logError("Could not find Service [" + serviceName + "].", module);
                return null;
            }
            if (!model.export) {
                sendError(response, "Problem processing the service", serviceName);
                Debug.logError("Trying to call Service [" + serviceName + "] that is not exported.", module);
                return null;
            }
            Map<String, Object> serviceResults = dispatcher.runSync(serviceName, parameters);
            if (Debug.verboseOn())
                Debug.logVerbose("[EventHandler] : Service invoked", module);
            createAndSendSOAPResponse(serviceResults, serviceName, response);
        } catch (GenericServiceException e) {
            Debug.logError(e, module);
            if (UtilProperties.getPropertyAsBoolean("service", "secureSoapAnswer", true)) {
                sendError(response, "Problem processing the service, check your parameters.", serviceName);
            } else {
                if (e.getMessageList() == null) {
                    sendError(response, e.getMessage(), serviceName);
                } else {
                    sendError(response, e.getMessageList(), serviceName);
                }
                return null;
            }
        }
    } catch (Exception e) {
        sendError(response, e.getMessage(), serviceName);
        Debug.logError(e, module);
        return null;
    }
    return null;
}
Also used : LocalDispatcher(org.apache.ofbiz.service.LocalDispatcher) WSDLException(javax.wsdl.WSDLException) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) OMElement(org.apache.axiom.om.OMElement) IOException(java.io.IOException) SOAPEnvelope(org.apache.axiom.soap.SOAPEnvelope) Document(org.w3c.dom.Document) GenericServiceException(org.apache.ofbiz.service.GenericServiceException) IOException(java.io.IOException) WSDLException(javax.wsdl.WSDLException) ModelService(org.apache.ofbiz.service.ModelService) DispatchContext(org.apache.ofbiz.service.DispatchContext) SOAPBody(org.apache.axiom.soap.SOAPBody) Delegator(org.apache.ofbiz.entity.Delegator) GenericServiceException(org.apache.ofbiz.service.GenericServiceException) SOAPModelBuilder(org.apache.axiom.soap.SOAPModelBuilder) Writer(java.io.Writer)

Example 7 with WSDLException

use of javax.wsdl.WSDLException in project carbon-apimgt by wso2.

the class WSDL11ProcessorImpl method init.

@Override
public boolean init(byte[] wsdlContent) throws APIMgtWSDLException {
    setMode(Mode.SINGLE);
    WSDLReader wsdlReader = getWsdlFactoryInstance().newWSDLReader();
    // switch off the verbose mode
    wsdlReader.setFeature(JAVAX_WSDL_VERBOSE_MODE, false);
    wsdlReader.setFeature(JAVAX_WSDL_IMPORT_DOCUMENTS, false);
    try {
        wsdlDefinition = wsdlReader.readWSDL(null, getSecuredParsedDocumentFromContent(wsdlContent));
        if (log.isDebugEnabled()) {
            log.debug("Successfully initialized an instance of " + this.getClass().getSimpleName() + " with a single WSDL.");
        }
    } catch (WSDLException | APIManagementException e) {
        // This implementation class cannot process the WSDL.
        log.debug("Cannot process the WSDL by " + this.getClass().getName(), e);
        setError(new ErrorItem(ExceptionCodes.CANNOT_PROCESS_WSDL_CONTENT.getErrorMessage(), e.getMessage(), ExceptionCodes.CANNOT_PROCESS_WSDL_CONTENT.getErrorCode(), ExceptionCodes.CANNOT_PROCESS_WSDL_CONTENT.getHttpStatusCode()));
    }
    return !hasError;
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) APIMgtWSDLException(org.wso2.carbon.apimgt.impl.wsdl.exceptions.APIMgtWSDLException) WSDLException(javax.wsdl.WSDLException) ErrorItem(org.wso2.carbon.apimgt.api.ErrorItem) WSDLReader(javax.wsdl.xml.WSDLReader) APIMWSDLReader(org.wso2.carbon.apimgt.impl.utils.APIMWSDLReader)

Example 8 with WSDLException

use of javax.wsdl.WSDLException in project carbon-apimgt by wso2.

the class WSDL11ProcessorImpl method initPath.

@Override
public boolean initPath(String path) throws APIMgtWSDLException {
    setMode(Mode.ARCHIVE);
    pathToDefinitionMap = new HashMap<>();
    wsdlArchiveExtractedPath = path;
    WSDLReader wsdlReader = getWsdlFactoryInstance().newWSDLReader();
    try {
        // switch off the verbose mode
        wsdlReader.setFeature(JAVAX_WSDL_VERBOSE_MODE, false);
        wsdlReader.setFeature(JAVAX_WSDL_IMPORT_DOCUMENTS, false);
        File folderToImport = new File(path);
        Collection<File> foundWSDLFiles = APIFileUtil.searchFilesWithMatchingExtension(folderToImport, "wsdl");
        if (log.isDebugEnabled()) {
            log.debug("Found " + foundWSDLFiles.size() + " WSDL file(s) in path " + path);
        }
        for (File file : foundWSDLFiles) {
            String absWSDLPath = file.getAbsolutePath();
            if (log.isDebugEnabled()) {
                log.debug("Processing WSDL file: " + absWSDLPath);
            }
            Definition definition = wsdlReader.readWSDL(path, getSecuredParsedDocumentFromPath(absWSDLPath));
            pathToDefinitionMap.put(absWSDLPath, definition);
            // set the first found WSDL as wsdlDefinition variable assuming that it is the root WSDL
            if (wsdlDefinition == null) {
                wsdlDefinition = definition;
            }
        }
        if (foundWSDLFiles.isEmpty()) {
            setError(ExceptionCodes.NO_WSDL_FOUND_IN_WSDL_ARCHIVE);
        }
        if (log.isDebugEnabled()) {
            log.debug("Successfully processed all WSDL files in path " + path);
        }
    } catch (WSDLException | APIManagementException e) {
        // This implementation class cannot process the WSDL. Continuing after setting canProcess = false
        log.debug(this.getClass().getName() + " was unable to process the WSDL Files for the path: " + path, e);
        setError(new ErrorItem(ExceptionCodes.CANNOT_PROCESS_WSDL_CONTENT.getErrorMessage(), e.getMessage(), ExceptionCodes.CANNOT_PROCESS_WSDL_CONTENT.getErrorCode(), ExceptionCodes.CANNOT_PROCESS_WSDL_CONTENT.getHttpStatusCode()));
    }
    return !hasError;
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) APIMgtWSDLException(org.wso2.carbon.apimgt.impl.wsdl.exceptions.APIMgtWSDLException) WSDLException(javax.wsdl.WSDLException) Definition(javax.wsdl.Definition) ErrorItem(org.wso2.carbon.apimgt.api.ErrorItem) File(java.io.File) WSDLReader(javax.wsdl.xml.WSDLReader) APIMWSDLReader(org.wso2.carbon.apimgt.impl.utils.APIMWSDLReader)

Example 9 with WSDLException

use of javax.wsdl.WSDLException in project carbon-apimgt by wso2.

the class APIMWSDLReader method validateBaseURI.

/**
 * Validate the base URI of the WSDL reader
 *
 * @throws APIManagementException When error occurred while parsing the content from the URL
 */
@Deprecated
public void validateBaseURI() throws APIManagementException {
    if (baseURI.startsWith(APIConstants.WSDL_REGISTRY_LOCATION_PREFIX)) {
        baseURI = APIUtil.getServerURL() + baseURI;
    }
    boolean isWsdl20 = false;
    boolean isWsdl11 = false;
    BufferedReader in = null;
    try {
        String inputLine;
        StringBuilder urlContent = new StringBuilder();
        URL wsdl = new URL(baseURI);
        in = new BufferedReader(new InputStreamReader(wsdl.openStream(), Charset.defaultCharset()));
        while ((inputLine = in.readLine()) != null) {
            urlContent.append(inputLine);
            isWsdl20 = urlContent.indexOf(WSDL20_NAMESPACE) > 0;
            isWsdl11 = urlContent.indexOf(WSDL11_NAMESPACE) > 0;
        }
    } catch (IOException e) {
        throw new APIManagementException("Error while reading WSDL from base URI " + baseURI, e);
    } finally {
        IOUtils.closeQuietly(in);
    }
    try {
        if (isWsdl11) {
            readAndValidateWSDL11();
        } else if (isWsdl20) {
            readAndValidateWSDL20();
        } else {
            throw new APIManagementException("URL is not in format of wsdl1.1 or wsdl2.0");
        }
    } catch (WSDLException e) {
        throw new APIManagementException("Error while parsing WSDL content", e);
    } catch (org.apache.woden.WSDLException e) {
        throw new APIManagementException("Error while parsing WSDL content", e);
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) APIMgtWSDLException(org.wso2.carbon.apimgt.impl.wsdl.exceptions.APIMgtWSDLException) WSDLException(javax.wsdl.WSDLException) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) URL(java.net.URL)

Example 10 with WSDLException

use of javax.wsdl.WSDLException in project carbon-apimgt by wso2.

the class APIMWSDLReader method getWSDL.

/**
 * Gets WSDL definition as a byte array given the WSDL definition
 *
 * @param wsdlDefinition generated WSDL definition
 * @return converted WSDL definition as byte array
 * @throws APIManagementException
 */
public byte[] getWSDL(Definition wsdlDefinition) throws APIManagementException {
    try {
        WSDLWriter writer = getWsdlFactoryInstance().newWSDLWriter();
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        writer.writeWSDL(wsdlDefinition, byteArrayOutputStream);
        return byteArrayOutputStream.toByteArray();
    } catch (WSDLException e) {
        throw new APIManagementException("Error occurs when change the address URL of the WSDL", e);
    }
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) APIMgtWSDLException(org.wso2.carbon.apimgt.impl.wsdl.exceptions.APIMgtWSDLException) WSDLException(javax.wsdl.WSDLException) WSDLWriter(javax.wsdl.xml.WSDLWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Aggregations

WSDLException (javax.wsdl.WSDLException)56 ToolException (org.apache.cxf.tools.common.ToolException)18 WSDLReader (javax.wsdl.xml.WSDLReader)14 IOException (java.io.IOException)13 Definition (javax.wsdl.Definition)12 Message (org.apache.cxf.common.i18n.Message)12 QName (javax.xml.namespace.QName)11 WSDLWriter (javax.wsdl.xml.WSDLWriter)10 APIMgtWSDLException (org.wso2.carbon.apimgt.impl.wsdl.exceptions.APIMgtWSDLException)7 File (java.io.File)6 Writer (java.io.Writer)6 HashMap (java.util.HashMap)6 Map (java.util.Map)6 ExtensibilityElement (javax.wsdl.extensions.ExtensibilityElement)6 Document (org.w3c.dom.Document)6 URL (java.net.URL)5 WSDLManager (org.apache.cxf.wsdl.WSDLManager)5 XMLStreamException (javax.xml.stream.XMLStreamException)4 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3