Java Code Examples for org.apache.axiom.soap.SOAPEnvelope#getBody()

The following examples show how to use org.apache.axiom.soap.SOAPEnvelope#getBody() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AMQPConsumer.java    From micro-integrator with Apache License 2.0 5 votes vote down vote up
/**
 * @param payload XML message content came inside the JMS message
 * @throws XMLStreamException on error
 */
private void parseOrder(String payload) throws XMLStreamException {
    InputStream is = new ByteArrayInputStream(payload.getBytes());
    javax.xml.stream.XMLStreamReader parser = XMLInputFactory
            .newInstance().createXMLStreamReader(is);
    StAXSOAPModelBuilder builder = new StAXSOAPModelBuilder(parser,
            null);
    SOAPEnvelope envelope = (SOAPEnvelope) builder.getDocumentElement();
    // retrieve SOAP body
    SOAPBody soapBody = envelope.getBody();
    OMElement messageNode = soapBody.getFirstChildWithName(new QName(
            FIX_MSG));
    Iterator<?> messageElements = (Iterator<?>) messageNode
            .getChildElements();
    while (messageElements.hasNext()) {
        OMElement node = (OMElement) messageElements.next();
        if (node.getQName().getLocalPart().equals(FIX_MSG_BODY)) {
            Iterator<?> bodyElements = (Iterator<?>) node.getChildElements();
            while (bodyElements.hasNext()) {
                OMElement bodyNode = (OMElement) bodyElements.next();
                String tag = bodyNode
                        .getAttributeValue(new QName(FIX_MSG_ID));
                String value = bodyNode.getText();
                if (tag.equals(FIX_MSG_SYMBOL)) {
                    inSymbol = value;
                } else if (tag.equals(FIX_MSG_CLORDID)) {
                    inClOrderID = value;
                } else if (tag.equals(FIX_MSG_ORDQTY)) {
                    inQty = value;
                }
            }
        }
    }
}
 
Example 2
Source File: AMQPConsumer.java    From micro-integrator with Apache License 2.0 5 votes vote down vote up
/**
 * @param payload XML message content came inside the JMS message
 * @throws XMLStreamException on error
 */
private void parseOrder(String payload) throws XMLStreamException {
    InputStream is = new ByteArrayInputStream(payload.getBytes());
    javax.xml.stream.XMLStreamReader parser = XMLInputFactory
            .newInstance().createXMLStreamReader(is);
    StAXSOAPModelBuilder builder = new StAXSOAPModelBuilder(parser,
            null);
    SOAPEnvelope envelope = (SOAPEnvelope) builder.getDocumentElement();
    // retrieve SOAP body
    SOAPBody soapBody = envelope.getBody();
    OMElement messageNode = soapBody.getFirstChildWithName(new QName(
            FIX_MSG));
    Iterator<?> messageElements = (Iterator<?>) messageNode
            .getChildElements();
    while (messageElements.hasNext()) {
        OMElement node = (OMElement) messageElements.next();
        if (node.getQName().getLocalPart().equals(FIX_MSG_BODY)) {
            Iterator<?> bodyElements = (Iterator<?>) node.getChildElements();
            while (bodyElements.hasNext()) {
                OMElement bodyNode = (OMElement) bodyElements.next();
                String tag = bodyNode
                        .getAttributeValue(new QName(FIX_MSG_ID));
                String value = bodyNode.getText();
                if (tag.equals(FIX_MSG_SYMBOL)) {
                    inSymbol = value;
                } else if (tag.equals(FIX_MSG_CLORDID)) {
                    inClOrderID = value;
                } else if (tag.equals(FIX_MSG_ORDQTY)) {
                    inQty = value;
                }
            }
        }
    }
}
 
Example 3
Source File: AMQPConsumer.java    From product-ei with Apache License 2.0 5 votes vote down vote up
/**
 * @param payload XML message content came inside the JMS message
 * @throws XMLStreamException on error
 */
private void parseOrder(String payload) throws XMLStreamException {
    InputStream is = new ByteArrayInputStream(payload.getBytes());
    javax.xml.stream.XMLStreamReader parser = XMLInputFactory
            .newInstance().createXMLStreamReader(is);
    StAXSOAPModelBuilder builder = new StAXSOAPModelBuilder(parser,
            null);
    SOAPEnvelope envelope = (SOAPEnvelope) builder.getDocumentElement();
    // retrieve SOAP body
    SOAPBody soapBody = envelope.getBody();
    OMElement messageNode = soapBody.getFirstChildWithName(new QName(
            FIX_MSG));
    Iterator<?> messageElements = (Iterator<?>) messageNode
            .getChildElements();
    while (messageElements.hasNext()) {
        OMElement node = (OMElement) messageElements.next();
        if (node.getQName().getLocalPart().equals(FIX_MSG_BODY)) {
            Iterator<?> bodyElements = (Iterator<?>) node.getChildElements();
            while (bodyElements.hasNext()) {
                OMElement bodyNode = (OMElement) bodyElements.next();
                String tag = bodyNode
                        .getAttributeValue(new QName(FIX_MSG_ID));
                String value = bodyNode.getText();
                if (tag.equals(FIX_MSG_SYMBOL)) {
                    inSymbol = value;
                } else if (tag.equals(FIX_MSG_CLORDID)) {
                    inClOrderID = value;
                } else if (tag.equals(FIX_MSG_ORDQTY)) {
                    inQty = value;
                }
            }
        }
    }
}
 
Example 4
Source File: RegularExpressionProtector.java    From carbon-apimgt with Apache License 2.0 5 votes vote down vote up
/**
 * This method returns true if the request payload size exceeds the system property
 * 'payloadSizeLimitForRegexThreatProtector' value (in KB) defined. If this system property is not defined, this
 * check won't be done.
 *
 * @param messageContext contains the message properties of the relevant API request which was
 *                       enabled the regexValidator message mediation in flow.
 * @return true if the payload size has exceeded the defined value in system property
 */
private boolean isPayloadSizeExceeded(MessageContext messageContext) {
    // payloadSizeLimit is in KB
    Integer payloadSizeLimit = Integer.getInteger(APIMgtGatewayConstants.PAYLOAD_SIZE_LIMIT_FOR_REGEX_TREAT_PROTECTOR);
    if (payloadSizeLimit == null) {
        return false;
    }
    long requestPayloadSize = 0;
    org.apache.axis2.context.MessageContext axis2MC = ((Axis2MessageContext) messageContext)
            .getAxis2MessageContext();
    Map headers = (Map) axis2MC.getProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS);
    String contentLength = (String) headers.get(HttpHeaders.CONTENT_LENGTH);
    if (contentLength != null) {
        requestPayloadSize = Integer.parseInt(contentLength);
    } else {  //When chunking is enabled
        SOAPEnvelope env = messageContext.getEnvelope();
        if (env != null) {
            SOAPBody soapbody = env.getBody();
            if (soapbody != null) {
                byte[] size = soapbody.toString().getBytes(Charset.defaultCharset());
                requestPayloadSize = size.length;
            }
        }
    }
    if (requestPayloadSize > payloadSizeLimit * 1024) {
        GatewayUtils.handleThreat(messageContext, APIMgtGatewayConstants.HTTP_SC_CODE, "Exceeded Request Payload " +
                "size limit allowed to be used with the enabledCheckBody option of Regular Expression Threat " +
                "Protector mediator");
        return true;
    }
    return false;
}
 
Example 5
Source File: RegularExpressionProtector.java    From carbon-apimgt with Apache License 2.0 5 votes vote down vote up
/**
 * This method checks whether the request body contains matching vulnerable key words.
 *
 * @param messageContext contains the message properties of the relevant API request which was
 *                       enabled the regexValidator message mediation in flow.
 */
private boolean isRequestBodyVulnerable(MessageContext messageContext) {
    SOAPEnvelope soapEnvelope;
    SOAPBody soapBody;
    OMElement omElement;
    org.apache.axis2.context.MessageContext axis2MC = ((Axis2MessageContext)
            messageContext).getAxis2MessageContext();
    if (enabledCheckBody) {
        soapEnvelope = axis2MC.getEnvelope();
        if (soapEnvelope == null) {
            return false;
        }
        soapBody = soapEnvelope.getBody();
        if (soapBody == null) {
            return false;
        }
        omElement = soapBody.getFirstElement();
        if (omElement == null) {
            return false;
        }
        String payload = omElement.toString();
        if (pattern != null && payload != null && pattern.matcher(payload).find()) {
            if (logger.isDebugEnabled()) {
                logger.debug(String.format("Threat detected in request payload [ %s ] by regex [ %s ]))",
                        payload, pattern));
            }
            GatewayUtils.handleThreat(messageContext, APIMgtGatewayConstants.HTTP_SC_CODE,
                    threatType + " " + APIMgtGatewayConstants.PAYLOAD_THREAT_MSG);
            return true;
        }
    }
    return false;
}
 
Example 6
Source File: LogsHandler.java    From carbon-apimgt with Apache License 2.0 5 votes vote down vote up
private long buildRequestMessage(org.apache.synapse.MessageContext messageContext) {
    long requestSize = 0;
    org.apache.axis2.context.MessageContext axis2MC = ((Axis2MessageContext) messageContext)
            .getAxis2MessageContext();
    Map headers = (Map) axis2MC.getProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS);
    String contentLength = (String) headers.get(HttpHeaders.CONTENT_LENGTH);
    if (contentLength != null) {
        requestSize = Integer.parseInt(contentLength);
    } else {
        // When chunking is enabled
        try {
            RelayUtils.buildMessage(axis2MC);
        } catch (IOException | XMLStreamException ex) {
            // In case of an exception, it won't be propagated up,and set response size to 0
            log.error(REQUEST_BODY_SIZE_ERROR, ex);
        }
        SOAPEnvelope env = messageContext.getEnvelope();
        if (env != null) {
            SOAPBody soapbody = env.getBody();
            if (soapbody != null) {
                byte[] size = soapbody.toString().getBytes(Charset.defaultCharset());
                requestSize = size.length;
            }

        }
    }
    return requestSize;
}
 
Example 7
Source File: LogsHandler.java    From carbon-apimgt with Apache License 2.0 5 votes vote down vote up
private long buildResponseMessage(org.apache.synapse.MessageContext messageContext) {
    long responseSize = 0;
    org.apache.axis2.context.MessageContext axis2MC = ((Axis2MessageContext) messageContext)
            .getAxis2MessageContext();
    Map headers = (Map) axis2MC.getProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS);
    String contentLength = (String) headers.get(HttpHeaders.CONTENT_LENGTH);
    if (contentLength != null) {
        responseSize = Integer.parseInt(contentLength);
    } else {
        // When chunking is enabled
        try {
            RelayUtils.buildMessage(axis2MC);
        } catch (IOException | XMLStreamException ex) {
            // In case of an exception, it won't be propagated up,and set response size to 0
            log.error(REQUEST_BODY_SIZE_ERROR, ex);
        }
    }
    SOAPEnvelope env = messageContext.getEnvelope();
    if (env != null) {
        SOAPBody soapbody = env.getBody();
        if (soapbody != null) {
            byte[] size = soapbody.toString().getBytes(Charset.defaultCharset());
            responseSize = size.length;
        }

    }
    return responseSize;

}
 
Example 8
Source File: BotDetectionMediator.java    From carbon-apimgt with Apache License 2.0 4 votes vote down vote up
@Override
public boolean mediate(MessageContext messageContext) {
    org.apache.axis2.context.MessageContext msgContext = ((Axis2MessageContext) messageContext).
            getAxis2MessageContext();

    String clientIP = DataPublisherUtil.getClientIp(msgContext);
    if (isThrottledOut(clientIP)) {
        messageContext.setProperty("BOT_THROTTLED_OUT", true);
        messageContext.setProperty("BOT_IP", clientIP);
        return true;
    }
    String messageBody;
    long currentTime = System.currentTimeMillis();
    String messageId = messageContext.getMessageID();
    String apiMethod = (String) msgContext.getProperty("HTTP_METHOD");
    try {
        RelayUtils.buildMessage(msgContext);
        SOAPEnvelope messageEnvelop = msgContext.getEnvelope();
        if (messageEnvelop != null && messageEnvelop.getBody() != null) {
            messageBody = String.valueOf(messageEnvelop.getBody());
        } else {
            messageBody = "Empty Message";
        }
    } catch (Exception e) {
        messageBody = "Malformed Message";
    }

    String headerSet = getPassedHeaderSet(msgContext);

    log.info(String.format("MessageId : %s | Request Method : %s | Message Body : %s | client Ip : %s | " +
            "Headers set : %s", messageId, apiMethod, messageBody, clientIP, headerSet));

    /**
     * check whether analytics enabled or not
     */
    if (!enabled) {
        return true;
    }

    BotDataDTO botDataDTO = new BotDataDTO();
    botDataDTO.setCurrentTime(currentTime);
    botDataDTO.setMessageID(messageId);
    botDataDTO.setApiMethod(apiMethod);
    botDataDTO.setHeaderSet(headerSet);
    botDataDTO.setMessageBody(messageBody);
    botDataDTO.setClientIp(clientIP);
    publisher.publishEvent(botDataDTO);
    return true;
}