Search in sources :

Example 61 with OMException

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

the class InputStreamDataSource method getXMLBytes.

@Override
public byte[] getXMLBytes(String encoding) throws UnsupportedEncodingException {
    // Return the byte array directly if it is the same encoding
    // Otherwise convert the bytes to the proper encoding
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    OMOutputFormat format = new OMOutputFormat();
    format.setCharSetEncoding(encoding);
    try {
        serialize(baos, format);
    } catch (XMLStreamException e) {
        throw new OMException(e);
    }
    return baos.toByteArray();
}
Also used : XMLStreamException(javax.xml.stream.XMLStreamException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OMOutputFormat(org.apache.axiom.om.OMOutputFormat) OMException(org.apache.axiom.om.OMException)

Example 62 with OMException

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

the class FirstElementNameWithParserTestCase method runTest.

@Override
protected final void runTest() throws Throwable {
    SOAPEnvelope orgEnvelope = soapFactory.getDefaultEnvelope();
    orgEnvelope.getBody().addChild(soapFactory.createOMElement(qname.getLocalPart(), qname.getNamespaceURI(), qname.getPrefix()));
    SOAPModelBuilder builder = OMXMLBuilderFactory.createSOAPModelBuilder(metaFactory, new StringReader(orgEnvelope.toString()));
    SOAPBody body = builder.getSOAPEnvelope().getBody();
    runTest(body);
    if (supportsOptimization) {
        // The expectation is that even after looking at the payload element name, registering
        // a custom builder still transforms the element.
        ((CustomBuilderSupport) builder).registerCustomBuilder(CustomBuilder.Selector.PAYLOAD, new CustomBuilder() {

            @Override
            public OMDataSource create(OMElement element) throws OMException {
                try {
                    element.getXMLStreamReaderWithoutCaching().close();
                } catch (XMLStreamException ex) {
                    throw new OMException(ex);
                }
                return new AbstractPushOMDataSource() {

                    @Override
                    public void serialize(XMLStreamWriter xmlWriter) throws XMLStreamException {
                        xmlWriter.writeEmptyElement(qname.getPrefix(), qname.getLocalPart(), qname.getNamespaceURI());
                    }

                    @Override
                    public boolean isDestructiveWrite() {
                        return false;
                    }
                };
            }
        });
        assertThat(body.getFirstElement()).isInstanceOf(OMSourcedElement.class);
    }
}
Also used : OMDataSource(org.apache.axiom.om.OMDataSource) AbstractPushOMDataSource(org.apache.axiom.om.ds.AbstractPushOMDataSource) CustomBuilderSupport(org.apache.axiom.om.ds.custombuilder.CustomBuilderSupport) OMElement(org.apache.axiom.om.OMElement) SOAPEnvelope(org.apache.axiom.soap.SOAPEnvelope) CustomBuilder(org.apache.axiom.om.ds.custombuilder.CustomBuilder) SOAPBody(org.apache.axiom.soap.SOAPBody) XMLStreamException(javax.xml.stream.XMLStreamException) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) StringReader(java.io.StringReader) AbstractPushOMDataSource(org.apache.axiom.om.ds.AbstractPushOMDataSource) SOAPModelBuilder(org.apache.axiom.soap.SOAPModelBuilder) OMException(org.apache.axiom.om.OMException)

Example 63 with OMException

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

the class HTCoordinationContextHandler method invoke.

@Override
public InvocationResponse invoke(MessageContext messageContext) throws AxisFault {
    if (serverConfig == null || !serverConfig.isTaskRegistrationEnabled()) {
        return InvocationResponse.CONTINUE;
    }
    SOAPHeader soapHeader;
    try {
        soapHeader = messageContext.getEnvelope().getHeader();
    } catch (OMException ex) {
        throw new AxisFault("Error while extracting SOAP header", ex);
    }
    if (soapHeader == null) {
        if (log.isDebugEnabled()) {
            log.debug("No SOAP Header received. Continuing as an uncoordinated HumanTask.");
        }
        return InvocationResponse.CONTINUE;
    }
    Iterator headers = soapHeader.getChildElements();
    SOAPHeaderBlock coordinationHeaderBlock = null;
    // Searching for WS-Coor Coordination Context
    while (headers.hasNext()) {
        SOAPHeaderBlock hb = (SOAPHeaderBlock) headers.next();
        if (hb.getLocalName().equals(Constants.WS_COOR_COORDINATION_CONTEXT) && hb.getNamespace().getNamespaceURI().equals(Constants.WS_COOR_NAMESPACE)) {
            coordinationHeaderBlock = hb;
            break;
        }
    }
    if (coordinationHeaderBlock == null) {
        if (log.isDebugEnabled()) {
            log.debug("No coordination context received. Processing as an uncoordinated HumanTask.");
        }
        return InvocationResponse.CONTINUE;
    }
    // We have received a ws-coordination context. Now validate it for HT coordination type
    String coordinationType = SOAPUtils.getCoordinationType(coordinationHeaderBlock);
    if (!Constants.WS_HT_COORDINATION_TYPE.equals(coordinationType)) {
        // Found wrong coordination Type. We Only support http://docs.oasis-open.org/ns/bpel4people/ws-humantask/protocol/200803.
        // So we cannot allow message to go forward.
        String errorMsg = "Message aborted ! Invalid coordination type" + coordinationType + " . Support only " + Constants.WS_HT_COORDINATION_TYPE;
        log.error(errorMsg);
        return InvocationResponse.ABORT;
    }
    if (log.isDebugEnabled()) {
        log.debug("HT coordination context received.");
    }
    String identifier = SOAPUtils.getCoordinationIdentifier(coordinationHeaderBlock);
    String registrationService = SOAPUtils.getRegistrationService(coordinationHeaderBlock);
    // validating values. These values cannot be empty
    if (identifier == null || identifier.isEmpty() || registrationService == null || registrationService.isEmpty()) {
        String errorMsg = "Message aborted ! Invalid coordination context parameters.";
        log.error(errorMsg);
        return InvocationResponse.ABORT;
    }
    // Service URL of the HumanTask Coordination Protocol Handler AdminService
    String humanTaskProtocolHandlerServiceURL;
    try {
        humanTaskProtocolHandlerServiceURL = ServiceUtils.getTaskProtocolHandlerURL(messageContext.getConfigurationContext());
    } catch (HumanTaskCoordinationException e) {
        String errorMsg = "Error while generating HumanTask engine's protocol Handler Service URL.";
        log.error(errorMsg);
        throw new AxisFault(e.getLocalizedMessage(), e);
    }
    // We are OK to invokeRegistrationService Registration service
    try {
        OMElement response = invokeRegistrationServiceUsingServiceClient(identifier, humanTaskProtocolHandlerServiceURL, registrationService);
        // But we are validating it for successful completion.
        if (!SOAPUtils.validateResponse(response, identifier)) {
            String errorMsg = "Message aborted ! registration response validation failed.";
            log.error(errorMsg);
            return InvocationResponse.ABORT;
        }
        // successful coordination
        if (log.isDebugEnabled()) {
            log.debug("RegistrationResponse received. Task is successfully coordinated with Task parent.");
        }
    } catch (AxisFault e) {
        String errorMsg = "Error while invoking registration service";
        log.error(errorMsg);
        throw new AxisFault(e.getLocalizedMessage(), e);
    }
    return InvocationResponse.CONTINUE;
}
Also used : AxisFault(org.apache.axis2.AxisFault) Iterator(java.util.Iterator) HumanTaskCoordinationException(org.wso2.carbon.humantask.coordination.module.HumanTaskCoordinationException) SOAPHeaderBlock(org.apache.axiom.soap.SOAPHeaderBlock) OMElement(org.apache.axiom.om.OMElement) OMException(org.apache.axiom.om.OMException) SOAPHeader(org.apache.axiom.soap.SOAPHeader)

Example 64 with OMException

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

the class APIDescriptionGenUtil method getTimeDuration.

/**
 * This method is used to get time duration of a tier
 *
 * @param policy tier policy
 * @return time duration for requests
 * @throws APIManagementException if error occurs when processing XML
 */
public static long getTimeDuration(OMElement policy) throws APIManagementException {
    try {
        OMElement duration = policy.getFirstChildWithName(APIConstants.POLICY_ELEMENT).getFirstChildWithName(APIConstants.THROTTLE_CONTROL_ELEMENT).getFirstChildWithName(APIConstants.POLICY_ELEMENT).getFirstChildWithName(APIConstants.THROTTLE_UNIT_TIME_ELEMENT);
        if (duration.getText().isEmpty()) {
            String message = APIConstants.THROTTLE_UNIT_TIME_ELEMENT.getLocalPart() + " element data not found empty in the policy.";
            log.warn(message);
            throw new APIManagementException(message);
        }
        // Reason - We need the ability to do fine grained throttling configurations.
        return Long.parseLong(duration.getText().trim());
    } catch (OMException e) {
        String errorMessage = "Policy could not be parsed correctly based on " + "http://schemas.xmlsoap.org/ws/2004/09/policy specification";
        log.error(errorMessage, e);
        throw new APIManagementException(errorMessage, e);
    } catch (NumberFormatException e) {
        String message = "Error in retrieving time duration from the tiers xml";
        log.error(message, e);
        throw new APIManagementException(message, e);
    }
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) OMElement(org.apache.axiom.om.OMElement) OMException(org.apache.axiom.om.OMException)

Example 65 with OMException

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

the class APIDescriptionGenUtil method getAllowedRequestCount.

/**
 * This method is used to get Allowed Requests count for a tier
 *
 * @param policy tier policy
 * @return Allowed Requests Count
 * @throws APIManagementException if error occurs when processing XML
 */
public static long getAllowedRequestCount(OMElement policy) throws APIManagementException {
    try {
        OMElement maxCount = policy.getFirstChildWithName(APIConstants.POLICY_ELEMENT).getFirstChildWithName(APIConstants.THROTTLE_CONTROL_ELEMENT).getFirstChildWithName(APIConstants.POLICY_ELEMENT).getFirstChildWithName(APIConstants.THROTTLE_MAXIMUM_COUNT_ELEMENT);
        if (maxCount.getText().isEmpty()) {
            String message = APIConstants.THROTTLE_MAXIMUM_COUNT_ELEMENT.getLocalPart() + " element data not found empty in the policy.";
            log.warn(message);
            throw new APIManagementException(message);
        }
        return Long.parseLong(maxCount.getText().trim());
    } catch (OMException e) {
        // We capture the runtime exception here.
        String errorMessage = "Policy could not be parsed correctly based on " + "http://schemas.xmlsoap.org/ws/2004/09/policy specification";
        log.error(errorMessage, e);
        throw new APIManagementException(errorMessage + e.getMessage());
    } catch (NumberFormatException e) {
        log.error("Error in retrieving request count in tier xml.", e);
        throw new APIManagementException("Error in retrieving request count in tier xml." + e.getMessage());
    }
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) OMElement(org.apache.axiom.om.OMElement) OMException(org.apache.axiom.om.OMException)

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