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;
}
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);
}
}
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);
}
}
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;
}
}
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);
}
Aggregations