Search in sources :

Example 26 with OMException

use of org.apache.axiom.om.OMException in project webservices-axiom by apache.

the class DigestGenerator method getDigest.

/**
 * This method is an overloaded method for the digest generation for OMElement
 *
 * @param element
 * @param digestAlgorithm
 * @return Returns a byte array representing the calculated digest value
 */
public byte[] getDigest(OMElement element, String digestAlgorithm) throws OMException {
    byte[] digest = new byte[0];
    try {
        MessageDigest md = MessageDigest.getInstance(digestAlgorithm);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        DataOutputStream dos = new DataOutputStream(baos);
        dos.writeInt(1);
        dos.write(getExpandedName(element).getBytes("UnicodeBigUnmarked"));
        dos.write((byte) 0);
        dos.write((byte) 0);
        Collection<OMAttribute> attrs = getAttributesWithoutNS(element);
        dos.writeInt(attrs.size());
        for (Iterator<OMAttribute> itr = attrs.iterator(); itr.hasNext(); ) {
            dos.write(getDigest(itr.next(), digestAlgorithm));
        }
        OMNode node = element.getFirstOMChild();
        // adjoining Texts are merged,
        // there is  no 0-length Text, and
        // comment nodes are removed.
        int length = 0;
        for (Iterator<OMNode> itr = element.getChildren(); itr.hasNext(); ) {
            OMNode child = itr.next();
            if (child instanceof OMElement || child instanceof OMText || child instanceof OMProcessingInstruction) {
                length++;
            }
        }
        dos.writeInt(length);
        while (node != null) {
            dos.write(getDigest(node, digestAlgorithm));
            node = node.getNextOMSibling();
        }
        dos.close();
        md.update(baos.toByteArray());
        digest = md.digest();
    } catch (NoSuchAlgorithmException e) {
        throw new OMException(e);
    } catch (IOException e) {
        throw new OMException(e);
    }
    return digest;
}
Also used : DataOutputStream(java.io.DataOutputStream) OMElement(org.apache.axiom.om.OMElement) ByteArrayOutputStream(java.io.ByteArrayOutputStream) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) IOException(java.io.IOException) OMProcessingInstruction(org.apache.axiom.om.OMProcessingInstruction) OMNode(org.apache.axiom.om.OMNode) OMText(org.apache.axiom.om.OMText) MessageDigest(java.security.MessageDigest) OMAttribute(org.apache.axiom.om.OMAttribute) OMException(org.apache.axiom.om.OMException)

Example 27 with OMException

use of org.apache.axiom.om.OMException in project webservices-axiom by apache.

the class ParserInputStreamDataSource method getXMLBytes.

@Override
public byte[] getXMLBytes(String encoding) {
    if (log.isDebugEnabled()) {
        log.debug("Entry ParserInputStreamDataSource.getXMLBytes(encoding)");
    }
    try {
        InputStream is = data.readParserInputStream();
        if (is != null) {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            OMOutputFormat format = new OMOutputFormat();
            format.setCharSetEncoding(encoding);
            try {
                BufferUtils.inputStream2OutputStream(is, baos);
                if (log.isDebugEnabled()) {
                    log.debug("Exit ParserInputStreamDataSource.getXMLBytes(encoding)");
                }
                return baos.toByteArray();
            } catch (IOException e) {
                throw new OMException(e);
            }
        } else {
            // via SerializeAndConsume call
            if (log.isDebugEnabled()) {
                log.warn("Parser was already read, recovering by just returning new byte[0]");
                log.debug("Exit ParserInputStreamDataSource.getXMLBytes(encoding)");
            }
            return new byte[0];
        }
    } catch (XMLStreamException e) {
        throw new OMException(e);
    }
}
Also used : XMLStreamException(javax.xml.stream.XMLStreamException) BAAInputStream(org.apache.axiom.attachments.utils.BAAInputStream) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) OMOutputFormat(org.apache.axiom.om.OMOutputFormat) OMException(org.apache.axiom.om.OMException)

Example 28 with OMException

use of org.apache.axiom.om.OMException in project webservices-axiom by apache.

the class MIMEOutputUtils method writeMM7Message.

/**
 * @deprecated Axiom only supports standard SwA messages. However, {@link OMMultipartWriter}
 *             provides a flexible way to build MIME packages for non standard formats such as
 *             MM7.
 */
public static void writeMM7Message(StringWriter writer, OutputStream outputStream, Attachments attachments, OMOutputFormat format, String innerPartCID, String innerBoundary) {
    try {
        OMMultipartWriter mpw = new OMMultipartWriter(outputStream, format);
        Writer rootPartWriter = new OutputStreamWriter(mpw.writeRootPart(), format.getCharSetEncoding());
        rootPartWriter.write(writer.toString());
        rootPartWriter.close();
        if (attachments.getContentIDSet().size() != 0) {
            OMOutputFormat innerFormat = new OMOutputFormat(format);
            innerFormat.setMimeBoundary(innerBoundary);
            OutputStream innerOutputStream = mpw.writePart("multipart/related; boundary=\"" + innerBoundary + "\"", innerPartCID);
            OMMultipartWriter innerMpw = new OMMultipartWriter(innerOutputStream, innerFormat);
            Collection ids = Arrays.asList(attachments.getAllContentIDs());
            for (Iterator it = ids.iterator(); it.hasNext(); ) {
                String id = (String) it.next();
                innerMpw.writePart(attachments.getDataHandler(id), id);
            }
            innerMpw.complete();
            innerOutputStream.close();
        }
        mpw.complete();
    } catch (IOException e) {
        throw new OMException("Error while writing to the OutputStream.", e);
    }
}
Also used : OutputStream(java.io.OutputStream) Iterator(java.util.Iterator) Collection(java.util.Collection) OutputStreamWriter(java.io.OutputStreamWriter) IOException(java.io.IOException) OMOutputFormat(org.apache.axiom.om.OMOutputFormat) OMException(org.apache.axiom.om.OMException) StringWriter(java.io.StringWriter) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter)

Example 29 with OMException

use of org.apache.axiom.om.OMException in project webservices-axiom by apache.

the class OMSerializerUtil method isAssociated.

/**
 * @param prefix
 * @param namespace
 * @param writer
 * @return true if the prefix is associated with the namespace in the current context
 */
public static boolean isAssociated(String prefix, String namespace, XMLStreamWriter writer) throws XMLStreamException {
    // of this issue.
    if ("xml".equals(prefix)) {
        return true;
    }
    // NOTE: Calling getNamespaceContext() on many XMLStreamWriter implementations is expensive.
    // Please use other writer methods first.
    // For consistency, convert null arguments.
    // This helps get around the parser implementation differences.
    // In addition, the getPrefix/getNamespace methods cannot be called with null parameters.
    prefix = (prefix == null) ? "" : prefix;
    namespace = (namespace == null) ? "" : namespace;
    if (namespace.length() > 0) {
        // QUALIFIED NAMESPACE
        // Get the namespace associated with the prefix
        String writerPrefix = writer.getPrefix(namespace);
        if (prefix.equals(writerPrefix)) {
            return true;
        }
        // So try getting the namespace as a second step.
        if (writerPrefix != null) {
            NamespaceContext nsContext = writer.getNamespaceContext();
            if (nsContext != null) {
                String writerNS = nsContext.getNamespaceURI(prefix);
                return namespace.equals(writerNS);
            }
        }
        return false;
    } else {
        // Neither XML 1.0 nor XML 1.1 allow to associate a prefix with an unqualified name (see also AXIOM-372).
        if (prefix.length() > 0) {
            throw new OMException("Invalid namespace declaration: Prefixed namespace bindings may not be empty.");
        }
        // protected
        try {
            String writerPrefix = writer.getPrefix("");
            if (writerPrefix != null && writerPrefix.length() == 0) {
                return true;
            }
        } catch (Throwable t) {
            if (log.isDebugEnabled()) {
                log.debug("Caught exception from getPrefix(\"\"). Processing continues: " + t);
            }
        }
        // Fallback to using the namespace context
        NamespaceContext nsContext = writer.getNamespaceContext();
        if (nsContext != null) {
            String writerNS = nsContext.getNamespaceURI("");
            if (writerNS != null && writerNS.length() > 0) {
                return false;
            }
        }
        return true;
    }
}
Also used : NamespaceContext(javax.xml.namespace.NamespaceContext) OMException(org.apache.axiom.om.OMException)

Example 30 with OMException

use of org.apache.axiom.om.OMException in project carbon-apimgt by wso2.

the class ApplicationThrottleControllerTest method testCreatingThrottleContextThrowsSynapseExceptionWhenThrottlingPolicyFailedDueToOMException.

@Test(expected = SynapseException.class)
public void testCreatingThrottleContextThrowsSynapseExceptionWhenThrottlingPolicyFailedDueToOMException() throws UserStoreException, RegistryException, XMLStreamException {
    Mockito.when(throttleDataHolder.getThrottleContext(applicationId)).thenReturn(null);
    PowerMockito.when(tenantManager.getTenantId(tenantDomain)).thenReturn(tenantID);
    PowerMockito.when(registryService.getGovernanceSystemRegistry(tenantID)).thenReturn(registry);
    PowerMockito.when(registry.resourceExists(RESOURCE_PATH)).thenReturn(true);
    PowerMockito.when(registry.get(RESOURCE_PATH)).thenReturn(throttlingPolicyResource);
    PowerMockito.when(throttlingPolicyResource.getContent()).thenReturn(THROTTLING_POLICY_DEFINITION.getBytes());
    PowerMockito.mockStatic(XMLInputFactory.class);
    XMLInputFactory factory = Mockito.mock(XMLInputFactory.class);
    PowerMockito.when(XMLInputFactory.newInstance()).thenReturn(factory);
    PowerMockito.doThrow(new OMException()).when(factory).createXMLStreamReader((ByteArrayInputStream) Mockito.anyObject());
    ApplicationThrottleController.getApplicationThrottleContext(messageContext, throttleDataHolder, applicationId, THROTTLE_POLICY_KEY);
}
Also used : OMException(org.apache.axiom.om.OMException) XMLInputFactory(javax.xml.stream.XMLInputFactory) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

OMException (org.apache.axiom.om.OMException)89 OMElement (org.apache.axiom.om.OMElement)35 XMLStreamException (javax.xml.stream.XMLStreamException)31 IOException (java.io.IOException)30 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)21 UserRegistry (org.wso2.carbon.registry.core.session.UserRegistry)18 InputStream (java.io.InputStream)16 QName (javax.xml.namespace.QName)13 OMFactory (org.apache.axiom.om.OMFactory)12 ArrayList (java.util.ArrayList)11 MalformedURLException (java.net.MalformedURLException)10 Test (org.junit.Test)10 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)10 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)9 ParseException (org.json.simple.parser.ParseException)9 APIMgtResourceAlreadyExistsException (org.wso2.carbon.apimgt.api.APIMgtResourceAlreadyExistsException)9 APIMgtResourceNotFoundException (org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException)9 FaultGatewaysException (org.wso2.carbon.apimgt.api.FaultGatewaysException)9 MonetizationException (org.wso2.carbon.apimgt.api.MonetizationException)9 UnsupportedPolicyTypeException (org.wso2.carbon.apimgt.api.UnsupportedPolicyTypeException)9