<commit_before>/*
 * Copyright (c) 2005-2014, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
 *
 * WSO2 Inc. licenses this file to you under the Apache License,
 * Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package org.apache.axis2.transport.rabbitmq;

import org.apache.axis2.AxisFault;
import org.apache.axis2.Constants;
import org.apache.axis2.context.MessageContext;
import org.apache.axis2.transport.rabbitmq.utils.RabbitMQUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * This is the RabbitMQ AMQP message receiver which is invoked when a message is received. This processes
 * the message through the axis2 engine
 */
public class RabbitMQMessageReceiver {
    private static final Log log = LogFactory.getLog(RabbitMQMessageReceiver.class);
    private final RabbitMQEndpoint endpoint;
    private final RabbitMQListener listener;
    private final ConnectionFactory connectionFactory;

    /**
     * Create a new RabbitMQMessage receiver
     *
     * @param listener            the AMQP transport Listener
     * @param connectionFactory   the AMQP connection factory we are associated with
     * @param endpoint            the RabbitMQEndpoint definition to be used
     */
    public RabbitMQMessageReceiver(RabbitMQListener listener,
                                   ConnectionFactory connectionFactory, RabbitMQEndpoint endpoint) {
        this.endpoint = endpoint;
        this.connectionFactory = connectionFactory;
        this.listener = listener;
    }

    /**
     * Process a new message received
     *
     * @param message the RabbitMQ AMQP message received
     */
    public boolean onMessage(RabbitMQMessage message) {
        boolean successful = false;
        try {
            successful = processThroughAxisEngine(message);
        } catch (AxisFault axisFault) {
            log.error("Error while processing message", axisFault);
        }
        return successful;
    }

    /**
     * Process the new message through Axis2
     *
     * @param message the RabbitMQMessage
     * @return true if the caller should commit
     * @throws AxisFault     on Axis2 errors
     */
    private boolean processThroughAxisEngine(RabbitMQMessage message) throws AxisFault {

        MessageContext msgContext = endpoint.createMessageContext();


        msgContext.setMessageID(message.getMessageId());
        String amqpCorrelationID = message.getCorrelationId();
        if (amqpCorrelationID != null && amqpCorrelationID.length() > 0) {
            msgContext.setProperty(RabbitMQConstants.CORRELATION_ID, amqpCorrelationID);
        } else {
            msgContext.setProperty(RabbitMQConstants.CORRELATION_ID, message.getMessageId());
        }

        String contentType = message.getContentType();
        if (contentType == null) {
            contentType = RabbitMQConstants.DEFAULT_CONTENT_TYPE;
            log.warn("Unable to determine content type for message " +
                    msgContext.getMessageID() + " setting to default " + RabbitMQConstants.DEFAULT_CONTENT_TYPE);
        }
        msgContext.setProperty(RabbitMQConstants.CONTENT_TYPE, contentType);

        msgContext.setProperty(RabbitMQConstants.CONTENT_TYPE, contentType);
        if (message.getContentEncoding() != null) {
            msgContext.setProperty(RabbitMQConstants.CONTENT_ENCODING, message.getContentEncoding());
        }
        String soapAction = message.getSoapAction();

        if (soapAction == null) {
            soapAction = RabbitMQUtils.getSOAPActionHeader(message);
        }
        String replyTo = message.getReplyTo();
        if (replyTo != null) {
            msgContext.setProperty(Constants.OUT_TRANSPORT_INFO,
                                   new RabbitMQOutTransportInfo(connectionFactory, replyTo, contentType));

        }

        RabbitMQUtils.setSOAPEnvelope(message, msgContext, contentType);

        try {
            listener.handleIncomingMessage(
                    msgContext,
                    RabbitMQUtils.getTransportHeaders(message),
                    soapAction,
                    contentType);<commit_msg>fix for NPE in content type in mqtt messages<commit_after>/*
 * Copyright (c) 2005-2014, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
 *
 * WSO2 Inc. licenses this file to you under the Apache License,
 * Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package org.apache.axis2.transport.rabbitmq;

import org.apache.axis2.AxisFault;
import org.apache.axis2.Constants;
import org.apache.axis2.context.MessageContext;
import org.apache.axis2.transport.rabbitmq.utils.RabbitMQUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * This is the RabbitMQ AMQP message receiver which is invoked when a message is received. This processes
 * the message through the axis2 engine
 */
public class RabbitMQMessageReceiver {
    private static final Log log = LogFactory.getLog(RabbitMQMessageReceiver.class);
    private final RabbitMQEndpoint endpoint;
    private final RabbitMQListener listener;
    private final ConnectionFactory connectionFactory;

    /**
     * Create a new RabbitMQMessage receiver
     *
     * @param listener            the AMQP transport Listener
     * @param connectionFactory   the AMQP connection factory we are associated with
     * @param endpoint            the RabbitMQEndpoint definition to be used
     */
    public RabbitMQMessageReceiver(RabbitMQListener listener,
                                   ConnectionFactory connectionFactory, RabbitMQEndpoint endpoint) {
        this.endpoint = endpoint;
        this.connectionFactory = connectionFactory;
        this.listener = listener;
    }

    /**
     * Process a new message received
     *
     * @param message the RabbitMQ AMQP message received
     */
    public boolean onMessage(RabbitMQMessage message) {
        boolean successful = false;
        try {
            successful = processThroughAxisEngine(message);
        } catch (AxisFault axisFault) {
            log.error("Error while processing message", axisFault);
        }
        return successful;
    }

    /**
     * Process the new message through Axis2
     *
     * @param message the RabbitMQMessage
     * @return true if the caller should commit
     * @throws AxisFault     on Axis2 errors
     */
    private boolean processThroughAxisEngine(RabbitMQMessage message) throws AxisFault {

        MessageContext msgContext = endpoint.createMessageContext();


        msgContext.setMessageID(message.getMessageId());
        String amqpCorrelationID = message.getCorrelationId();
        if (amqpCorrelationID != null && amqpCorrelationID.length() > 0) {
            msgContext.setProperty(RabbitMQConstants.CORRELATION_ID, amqpCorrelationID);
        } else {
            msgContext.setProperty(RabbitMQConstants.CORRELATION_ID, message.getMessageId());
        }

        String contentType = message.getContentType();
        if (contentType == null) {
            contentType = RabbitMQConstants.DEFAULT_CONTENT_TYPE;
            log.warn("Unable to determine content type for message " +
                    msgContext.getMessageID() + " setting to default " + RabbitMQConstants.DEFAULT_CONTENT_TYPE);
        }
        msgContext.setProperty(RabbitMQConstants.CONTENT_TYPE, contentType);

        if (message.getContentEncoding() != null) {
            msgContext.setProperty(RabbitMQConstants.CONTENT_ENCODING, message.getContentEncoding());
        }
        String soapAction = message.getSoapAction();

        if (soapAction == null) {
            soapAction = RabbitMQUtils.getSOAPActionHeader(message);
        }
        String replyTo = message.getReplyTo();
        if (replyTo != null) {
            msgContext.setProperty(Constants.OUT_TRANSPORT_INFO,
                                   new RabbitMQOutTransportInfo(connectionFactory, replyTo, contentType));

        }

        RabbitMQUtils.setSOAPEnvelope(message, msgContext, contentType);

        try {
            listener.handleIncomingMessage(
                    msgContext,
                    RabbitMQUtils.getTransportHeaders(message),
                    soapAction,
                    contentType);
{'fraction_non_alphanumeric': 0.07520104325146708, 'fraction_numerical': 0.005216257335361878, 'mean_word_length': 2.7578603511637403, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3878042', 'n_tokens_mistral': 2445, 'n_tokens_neox': 2290, 'n_words': 661}