Example usage for javax.xml.stream XMLStreamException XMLStreamException

List of usage examples for javax.xml.stream XMLStreamException XMLStreamException

Introduction

In this page you can find the example usage for javax.xml.stream XMLStreamException XMLStreamException.

Prototype

public XMLStreamException(Throwable th) 

Source Link

Document

Construct an exception with the assocated exception

Usage

From source file:org.ut.biolab.medsavant.client.plugin.PluginIndex.java

public PluginIndex(URL url) throws IOException {
    urls = new HashMap<String, URL>();
    try {//from w w  w  .  j  av a 2  s . c  om

        XMLStreamReader reader = XMLInputFactory.newInstance().createXMLStreamReader(
                ClientNetworkUtils.openStream(url, ClientNetworkUtils.NONCRITICAL_CONNECT_TIMEOUT,
                        ClientNetworkUtils.NONCRITICAL_READ_TIMEOUT));
        if (reader.getVersion() == null) {
            throw new XMLStreamException("Invalid XML at URL " + url);
        }
        boolean done = false;
        String id = null;
        do {
            if (reader.hasNext()) {
                int t = reader.next();
                switch (t) {
                case XMLStreamConstants.START_ELEMENT:
                    String elemName = reader.getLocalName();
                    if (elemName.equals("leaf")) {
                        id = reader.getAttributeValue(null, "id");
                    } else if (elemName.equals("url")) {
                        if (id != null) {
                            try {
                                urls.put(id, new URL(reader.getElementText()));
                            } catch (MalformedURLException x) {
                                LOG.warn(String.format("Unable to parse \"%s\" as a plugin URL.",
                                        reader.getElementText()));
                            }
                            id = null;
                        }
                    }
                    break;
                case XMLStreamConstants.END_DOCUMENT:
                    reader.close();
                    done = true;
                    break;
                }
            } else {
                throw new XMLStreamException("Malformed XML at " + url);
            }
        } while (!done);
    } catch (XMLStreamException x) {
        throw new IOException("Unable to get version number from web-site.", x);
    }
}

From source file:org.wso2.am.admin.clients.proxy.admin.ProxyServiceAdminClient.java

/**
 * Method used to create proxy data/*from w w  w .  j  ava  2 s. c o  m*/
 *
 * @param proxyString proxy configuration
 * @return proxy data
 */
//this function from org.wso2.carbon.org.wso2.carbon.org.wso2.carbon.proxyadmin.service in proxy-admin modules
public ProxyData getProxyData(String proxyString) {
    final String BUNDLE = "org.wso2.carbon.proxyadmin.Resources";
    ProxyData pd = new ProxyData();
    ResourceBundle bundle = ResourceBundle.getBundle(BUNDLE, Locale.US);
    try {
        byte[] bytes = null;
        try {
            bytes = proxyString.getBytes("UTF8");
        } catch (UnsupportedEncodingException e) {
            log.error("Unable to extract bytes in UTF-8 encoding. "
                    + "Extracting bytes in the system default encoding" + e.getMessage());
            bytes = proxyString.getBytes();
        }
        OMElement elem = new StAXOMBuilder(new ByteArrayInputStream(bytes)).getDocumentElement();

        // check whether synapse namespace is present in the configuration.
        Iterator itr = elem.getAllDeclaredNamespaces();
        OMNamespace ns;
        boolean synapseNSPresent = false;
        while (itr.hasNext()) {
            ns = (OMNamespace) itr.next();
            if (XMLConfigConstants.SYNAPSE_NAMESPACE.equals(ns.getNamespaceURI())) {
                synapseNSPresent = true;
                break;
            }
        }

        OMAttribute name = elem.getAttribute(new QName("name"));
        if (name != null) {
            pd.setName(name.getAttributeValue());
        }

        OMAttribute statistics = elem.getAttribute(new QName(XMLConfigConstants.STATISTICS_ATTRIB_NAME));
        if (statistics != null) {
            String statisticsValue = statistics.getAttributeValue();
            if (statisticsValue != null) {
                if (XMLConfigConstants.STATISTICS_ENABLE.equals(statisticsValue)) {
                    pd.setEnableStatistics(true);
                } else if (XMLConfigConstants.STATISTICS_DISABLE.equals(statisticsValue)) {
                    pd.setEnableStatistics(false);
                }
            }
        }

        OMAttribute trans = elem.getAttribute(new QName("transports"));
        if (trans != null) {
            String transports = trans.getAttributeValue();
            if (transports == null || "".equals(transports) || ProxyService.ALL_TRANSPORTS.equals(transports)) {
                // default to all transports using service name as destination
            } else {
                String[] arr = transports.split(",");
                if (arr != null && arr.length != 0) {
                    pd.setTransports(arr);
                }
            }
        }

        OMAttribute pinnedServers = elem.getAttribute(new QName("pinnedServers"));
        if (pinnedServers != null) {
            String pinnedServersValue = pinnedServers.getAttributeValue();
            if (pinnedServersValue == null || "".equals(pinnedServersValue)) {
                // default to all servers
            } else {
                String[] arr = pinnedServersValue.split(",");
                if (arr != null && arr.length != 0) {
                    pd.setPinnedServers(arr);
                }
            }
        }

        OMAttribute trace = elem.getAttribute(new QName(XMLConfigConstants.TRACE_ATTRIB_NAME));
        if (trace != null) {
            String traceValue = trace.getAttributeValue();
            if (traceValue != null) {
                if (traceValue.equals(XMLConfigConstants.TRACE_ENABLE)) {
                    pd.setEnableTracing(true);
                } else if (traceValue.equals(XMLConfigConstants.TRACE_DISABLE)) {
                    pd.setEnableTracing(false);
                }
            }
        }

        OMAttribute startOnLoad = elem.getAttribute(new QName("startOnLoad"));
        String val;
        if (startOnLoad != null && (val = startOnLoad.getAttributeValue()) != null && !"".equals(val)) {
            pd.setStartOnLoad(Boolean.valueOf(val));
        } else {
            pd.setStartOnLoad(true);
        }

        // read definition of the target of this proxy service. The target could be an 'endpoint'
        // or a named sequence. If none of these are specified, the messages would be mediated
        // by the Synapse main mediator
        OMElement target = elem
                .getFirstChildWithName(new QName(XMLConfigConstants.SYNAPSE_NAMESPACE, "target"));
        if (target != null) {
            OMAttribute inSequence = target.getAttribute(new QName("inSequence"));
            if (inSequence != null) {
                pd.setInSeqKey(inSequence.getAttributeValue());
            } else {
                OMElement inSequenceElement = target
                        .getFirstChildWithName(new QName(XMLConfigConstants.SYNAPSE_NAMESPACE, "inSequence"));
                if (inSequenceElement != null) {
                    pd.setInSeqXML(inSequenceElement.toString());
                }
            }
            OMAttribute outSequence = target.getAttribute(new QName("outSequence"));
            if (outSequence != null) {
                pd.setOutSeqKey(outSequence.getAttributeValue());
            } else {
                OMElement outSequenceElement = target
                        .getFirstChildWithName(new QName(XMLConfigConstants.SYNAPSE_NAMESPACE, "outSequence"));
                if (outSequenceElement != null) {
                    pd.setOutSeqXML(outSequenceElement.toString());
                }
            }
            OMAttribute faultSequence = target.getAttribute(new QName("faultSequence"));
            if (faultSequence != null) {
                pd.setFaultSeqKey(faultSequence.getAttributeValue());
            } else {
                OMElement faultSequenceElement = target.getFirstChildWithName(
                        new QName(XMLConfigConstants.SYNAPSE_NAMESPACE, "faultSequence"));
                if (faultSequenceElement != null) {
                    pd.setFaultSeqXML(faultSequenceElement.toString());
                }
            }
            OMAttribute tgtEndpt = target.getAttribute(new QName("endpoint"));
            if (tgtEndpt != null) {
                pd.setEndpointKey(tgtEndpt.getAttributeValue());
            } else {
                OMElement endpointElement = target
                        .getFirstChildWithName(new QName(XMLConfigConstants.SYNAPSE_NAMESPACE, "endpoint"));
                if (endpointElement != null) {
                    pd.setEndpointXML(endpointElement.toString());
                }
            }
        }

        Iterator props = elem.getChildrenWithName(new QName(XMLConfigConstants.SYNAPSE_NAMESPACE, "parameter"));
        ArrayList<Entry> params = new ArrayList<Entry>();
        Entry entry = null;
        while (props.hasNext()) {
            Object o = props.next();
            if (o instanceof OMElement) {
                OMElement prop = (OMElement) o;
                OMAttribute pname = prop.getAttribute(new QName("name"));
                OMElement propertyValue = prop.getFirstElement();
                if (pname != null) {
                    if (propertyValue != null) {
                        entry = new Entry();
                        entry.setKey(pname.getAttributeValue());
                        entry.setValue(propertyValue.toString());
                        params.add(entry);
                    } else {
                        entry = new Entry();
                        entry.setKey(pname.getAttributeValue());
                        entry.setValue(prop.getText().trim());
                        params.add(entry);
                    }
                }
            }
        }
        pd.setServiceParams(params.toArray(new Entry[params.size()]));

        OMElement wsdl = elem
                .getFirstChildWithName(new QName(XMLConfigConstants.SYNAPSE_NAMESPACE, "publishWSDL"));
        if (wsdl != null) {
            OMAttribute wsdlkey = wsdl.getAttribute(new QName(XMLConfigConstants.NULL_NAMESPACE, "key"));
            OMAttribute wsdlEP = wsdl.getAttribute(new QName(XMLConfigConstants.NULL_NAMESPACE, "endpoint"));
            if (wsdlEP != null) {
                //Commenting out this line because of the possible API change in stub
                // pd.setPublishWSDLEndpoint(wsdlEP.getAttributeValue());
            } else if (wsdlkey != null) {
                pd.setWsdlKey(wsdlkey.getAttributeValue());
            } else {
                OMAttribute wsdlURI = wsdl.getAttribute(new QName(XMLConfigConstants.NULL_NAMESPACE, "uri"));
                if (wsdlURI != null) {
                    pd.setWsdlURI(wsdlURI.getAttributeValue());
                } else {
                    OMElement wsdl11 = wsdl
                            .getFirstChildWithName(new QName(WSDLConstants.WSDL1_1_NAMESPACE, "definitions"));
                    String wsdlDef;
                    if (wsdl11 != null) {
                        wsdlDef = wsdl11.toString().replaceAll("\n|\\r|\\f|\\t", "");
                        wsdlDef = wsdlDef.replaceAll("> +<", "><");
                        pd.setWsdlDef(wsdlDef);
                    } else {
                        OMElement wsdl20 = wsdl
                                .getFirstChildWithName(new QName(WSDL2Constants.WSDL_NAMESPACE, "description"));
                        if (wsdl20 != null) {
                            wsdlDef = wsdl20.toString().replaceAll("\n|\\r|\\f|\\t", "");
                            wsdlDef = wsdlDef.replaceAll("> +<", "><");
                            pd.setWsdlDef(wsdlDef);
                        }
                    }
                }
            }

            Iterator it = wsdl.getChildrenWithName(new QName(XMLConfigConstants.SYNAPSE_NAMESPACE, "resource"));
            ArrayList<Entry> resources = new ArrayList<Entry>();
            Entry resource = null;
            while (it.hasNext()) {
                OMElement resourceElem = (OMElement) it.next();
                OMAttribute location = resourceElem
                        .getAttribute(new QName(XMLConfigConstants.NULL_NAMESPACE, "location"));
                if (location == null) {
                    throw new XMLStreamException("location element not found in xml file");
                }
                OMAttribute key = resourceElem
                        .getAttribute(new QName(XMLConfigConstants.NULL_NAMESPACE, "key"));
                if (key == null) {
                    throw new XMLStreamException("key element not found in xml file");
                }
                resource = new Entry();
                resource.setKey(location.getAttributeValue());
                resource.setValue(key.getAttributeValue());
                resources.add(resource);
            }
            pd.setWsdlResources(resources.toArray(new Entry[resources.size()]));
        }

        OMElement enableSec = elem
                .getFirstChildWithName(new QName(XMLConfigConstants.SYNAPSE_NAMESPACE, "enableSec"));
        if (enableSec != null) {
            pd.setEnableSecurity(true);
        }

        OMElement description = elem
                .getFirstChildWithName(new QName(XMLConfigConstants.SYNAPSE_NAMESPACE, "description"));
        if (description != null) {
            pd.setDescription(description.getText());
        }

        Iterator policies = elem.getChildrenWithName(new QName(XMLConfigConstants.SYNAPSE_NAMESPACE, "policy"));
        while (policies.hasNext()) {
            OMElement policyElement = (OMElement) policies.next();
            String policyKey = policyElement.getAttributeValue(new QName("key"));
            ProxyServicePolicyInfo policyInfo = new ProxyServicePolicyInfo();
            policyInfo.setKey(policyKey);
            pd.addPolicies(policyInfo);
        }

    } catch (XMLStreamException e) {
        log.error(bundle.getString("unable.to.build.the.design.view.from.the.given.xml"), e);
        Assert.fail(bundle.getString("unable.to.build.the.design.view.from.the.given.xml"));
    }
    return pd;

}

From source file:org.wso2.appserver.integration.tests.javaee.WebappDeploymentTestCase.java

protected OMElement runAndGetResultAsOM(String webAppURL) throws Exception {
    log.info("Endpoint : " + webAppURL);
    HttpURLConnection httpCon = null;
    String xmlContent = null;/*from w ww . j a v  a 2 s .c  o  m*/
    boolean responseCode = true;

    int responseCode1;
    try {
        URL e = new URL(webAppURL);
        httpCon = (HttpURLConnection) e.openConnection();
        httpCon.setConnectTimeout(30000);
        httpCon.setRequestProperty("Accept", "application/xml");
        InputStream in = httpCon.getInputStream();
        xmlContent = getStringFromInputStream(in);
        responseCode1 = httpCon.getResponseCode();
        in.close();
    } catch (Exception var12) {
        log.error("Failed to get the response " + var12);
        throw new Exception("Failed to get the response :" + var12);
    } finally {
        if (httpCon != null) {
            httpCon.disconnect();
        }

    }

    Assert.assertEquals(responseCode1, 200, "Response code not 200");
    if (xmlContent != null) {
        try {
            return AXIOMUtil.stringToOM(xmlContent);
        } catch (XMLStreamException var11) {
            log.error("Error while processing response to OMElement" + var11);
            throw new XMLStreamException("Error while processing response to OMElement" + var11);
        }
    } else {
        return null;
    }
}

From source file:org.wso2.bps.integration.common.utils.RequestSender.java

public void assertRequest(String eprUrl, String operation, String payload, int numberOfInstances,
        String expectedException, boolean twoWay) throws XMLStreamException, AxisFault {

    for (int i = 0; i < numberOfInstances; i++) {
        OMElement result = null;//from  w ww .  ja v a 2 s  . c  o m
        try {
            EndpointReference epr = new EndpointReference(eprUrl + "/" + operation);
            if (twoWay) {
                result = this.sendRequest(payload, epr);
                //  Assert.fail("Exception expected!!! : " + result.toString());

            } else {
                this.sendRequestOneWay(payload, epr);
            }
        } catch (XMLStreamException e) {
            if (!e.getClass().getSimpleName().equals(expectedException)) {
                throw new XMLStreamException(e);
            }
        } catch (AxisFault axisFault) {
            log.error("Error occurred while sending request.", axisFault);
            if (!axisFault.getClass().getSimpleName().equals(expectedException)) {
                throw new AxisFault(axisFault.getMessage());
            }
        }

        assert result != null;
        if (!(result.toString().contains(expectedException))) {
            Assert.fail("Expected response not found");
        }
    }
}

From source file:org.wso2.brs.integration.common.utils.RequestSender.java

public void assertRequest(String eprUrl, String operation, String payload, int numberOfInstances,
        String expectedException, boolean twoWay) throws XMLStreamException, AxisFault {

    for (int i = 0; i < numberOfInstances; i++) {
        OMElement result = null;//  w  ww  .j  a va  2s .co m
        try {
            EndpointReference epr = new EndpointReference(eprUrl + "/" + operation);
            if (twoWay) {
                result = this.sendRequest(payload, epr);
                //  Assert.fail("Exception expected!!! : " + result.toString());

            } else {
                this.sendRequestOneWay(payload, epr);
            }
        } catch (XMLStreamException e) {
            if (!e.getClass().getSimpleName().equals(expectedException)) {
                throw new XMLStreamException(e);
            }
        } catch (AxisFault axisFault) {
            axisFault.printStackTrace();
            if (!axisFault.getClass().getSimpleName().equals(expectedException)) {
                throw new AxisFault(axisFault.getMessage());
            }
        }

        assert result != null;
        if (!(result.toString().contains(expectedException))) {
            Assert.fail("Expected response not found");
        }
    }
}

From source file:org.wso2.carbon.automation.test.utils.axis2client.AxisServiceClientUtils.java

public static void sendRequest(String eprUrl, String operation, String payload, int numberOfInstances,
        List<String> expectedStrings, boolean twoWay) throws Exception {
    waitForServiceDeployment(eprUrl);/*  w  ww  .  ja va 2  s.c  o  m*/
    assertFalse(!AxisServiceClientUtils.isServiceAvailable(eprUrl));
    for (int i = 0; i < numberOfInstances; i++) {
        try {
            EndpointReference epr = new EndpointReference(eprUrl + "/" + operation);
            if (twoWay) {
                OMElement result = AxisServiceClientUtils.sendRequest(payload, epr);
                if (expectedStrings != null) {
                    for (String expectedString : expectedStrings) {
                        assertFalse(!result.toString().contains(expectedString));
                    }
                }
            } else {
                AxisServiceClientUtils.sendRequestOneWay(payload, epr);
            }
        } catch (XMLStreamException e) {
            log.error(e);
            throw new XMLStreamException("cannot read xml stream " + e);
        } catch (AxisFault axisFault) {
            log.error(axisFault.getMessage());
            throw new AxisFault("cannot read xml stream " + axisFault.getMessage());
        }
    }
}

From source file:org.wso2.carbon.automation.test.utils.axis2client.AxisServiceClientUtils.java

public static void sendRequest(String eprUrl, String operation, String payload, int numberOfInstances,
        String expectedException, boolean twoWay) throws XMLStreamException, IOException {
    assertFalse(!AxisServiceClientUtils.isServiceAvailable(eprUrl));
    for (int i = 0; i < numberOfInstances; i++) {
        try {//from w  w w.  ja va  2s.  co  m
            EndpointReference epr = new EndpointReference(operation);
            if (twoWay) {
                OMElement result = AxisServiceClientUtils.sendRequest(payload, epr);
                fail("Exception expected!!! : " + result.toString());
            } else {
                AxisServiceClientUtils.sendRequestOneWay(payload, epr);
            }
        } catch (XMLStreamException e) {
            if (!e.getClass().getSimpleName().equals(expectedException)) {
                throw new XMLStreamException(e);
            }
        } catch (AxisFault axisFault) {
            if (!axisFault.getClass().getSimpleName().equals(expectedException)) {
                throw new AxisFault(axisFault.getMessage());
            }
        }
        if (expectedException != null) {
            fail("Exception expected. But not found!!");
        }
    }
}

From source file:org.wso2.carbon.automation.test.utils.http.client.HttpClientUtil.java

public OMElement get(String endpoint) throws Exception {
    log.info("Endpoint : " + endpoint);
    HttpURLConnection httpCon = null;
    String xmlContent = null;//w  w w .  java2 s .c  om
    int responseCode = -1;
    try {
        URL url = new URL(endpoint);
        httpCon = (HttpURLConnection) url.openConnection();
        httpCon.setConnectTimeout(connectionTimeOut);
        InputStream in = httpCon.getInputStream();
        xmlContent = getStringFromInputStream(in);
        responseCode = httpCon.getResponseCode();
        in.close();
    } catch (Exception e) {
        log.error("Failed to get the response " + e);
        throw new Exception("Failed to get the response :" + e);
    } finally {
        if (httpCon != null) {
            httpCon.disconnect();
        }
    }
    Assert.assertEquals(responseCode, 200, "Response code not 200");
    /*if (xmlContent != null) {*/
    try {
        return AXIOMUtil.stringToOM(xmlContent);
    } catch (XMLStreamException e) {
        log.error("Error while processing response to OMElement" + e);
        throw new XMLStreamException("Error while processing response to OMElement" + e);
    }
    /*} else {
    return null;
    }*/
}

From source file:org.wso2.carbon.automation.test.utils.http.client.HttpClientUtil.java

public OMElement getWithContentType(String endpoint, String params, String contentType) throws Exception {
    log.info("Endpoint : " + endpoint);
    HttpURLConnection httpCon = null;
    String xmlContent = null;/*w ww.j a v a 2  s .  c o  m*/
    int responseCode = -1;
    try {
        URL url = new URL(endpoint);
        httpCon = (HttpURLConnection) url.openConnection();
        httpCon.setConnectTimeout(connectionTimeOut);
        httpCon.setRequestProperty("Content-type", contentType);
        httpCon.setRequestMethod("GET");
        httpCon.setDoOutput(true);
        OutputStreamWriter out = new OutputStreamWriter(httpCon.getOutputStream(), Charset.defaultCharset());
        out.write(params);
        out.close();
        InputStream in = httpCon.getInputStream();
        xmlContent = getStringFromInputStream(in);
        responseCode = httpCon.getResponseCode();
        in.close();
    } catch (Exception e) {
        log.error("Failed to get the response " + e);
        throw new Exception("Failed to get the response :" + e);
    } finally {
        if (httpCon != null) {
            httpCon.disconnect();
        }
    }
    Assert.assertEquals(responseCode, 200, "Response code not 200");
    // idea warning fixed
    /*if (xmlContent != null) {*/
    try {
        return AXIOMUtil.stringToOM(xmlContent);
    } catch (XMLStreamException e) {
        log.error("Error while processing response to OMElement" + e);
        throw new XMLStreamException("Error while processing response to OMElement" + e);
    }
    /*} else {
    return null;
    }*/
}

From source file:org.wso2.carbon.registry.jira.issues.test.Carbon12213.java

@Test(groups = { "wso2.greg" }, description = "change registry.xml and restart server")
@SetEnvironment(executionEnvironments = { ExecutionEnvironment.integration_user })
public void editRegistryXML() throws Exception {

    String registryXmlPath = CarbonUtils.getCarbonHome() + File.separator + "repository" + File.separator
            + "conf" + File.separator + "registry.xml";

    FileOutputStream fileOutputStream = null;
    XMLStreamWriter writer = null;
    File srcFile = new File(registryXmlPath);
    try {//  w  ww  .  jav a  2  s.  c o  m
        OMElement handlerConfig = AXIOMUtil.stringToOM("<property name=\"useOriginalSchema\">true</property>");
        OMElement registryXML = getRegistryXmlOmElement();

        OMElement om1;
        for (Iterator iterator = registryXML.getChildrenWithName(new QName("handler")); iterator.hasNext();) {
            OMElement om = (OMElement) iterator.next();

            if (om.getAttribute(new QName("class")).getAttributeValue()
                    .equals("org.wso2.carbon.registry.extensions.handlers" + ".ZipWSDLMediaTypeHandler")) {
                om1 = om;
                om1.addChild(handlerConfig);
                registryXML.addChild(om1);
                registryXML.build();
                break;
            }

        }

        fileOutputStream = new FileOutputStream(srcFile);
        writer = XMLOutputFactory.newInstance().createXMLStreamWriter(fileOutputStream);
        registryXML.serialize(writer);

    } catch (FileNotFoundException e) {
        throw new FileNotFoundException("Registry.xml file not found" + e);

    } catch (XMLStreamException e) {
        throw new XMLStreamException("XML stream exception" + e);

    } catch (IOException e) {
        throw new IOException("IO exception" + e);

    } finally {
        if (writer != null) {
            writer.close();
        }
        if (fileOutputStream != null) {
            fileOutputStream.close();
        }
    }

    restartServer();

    EnvironmentBuilder builder = new EnvironmentBuilder().greg(userId);
    ManageEnvironment environment = builder.build();

    //reinitialize environment after server restart
    resourceAdminServiceClient = new ResourceAdminServiceClient(environment.getGreg().getBackEndUrl(),
            environment.getGreg().getSessionCookie());
    serverAdminClient = new ServerAdminClient(environment.getGreg().getProductVariables().getBackendUrl(),
            environment.getGreg().getSessionCookie());
}