<commit_before>package com.dreamoval.motech.omp.manager.orserve;

import com.dreamoval.motech.core.manager.CoreManager;
import com.dreamoval.motech.core.model.GatewayRequest;
import com.dreamoval.motech.core.model.GatewayResponse;
import com.dreamoval.motech.core.model.MStatus;
import com.dreamoval.motech.core.service.MotechContext;
import com.dreamoval.motech.omp.manager.GatewayMessageHandler;
import java.util.Date;
import java.util.Set;
import java.util.HashSet;
import java.util.Map;
import java.util.Map.Entry;
import org.apache.log4j.Logger;

/**
 * Handles preparation and parsing of messages and responses from the OutReach Server message gateway
 *
 * @author <NAME> (<EMAIL>)
 * @date Jul 15, 2009
 */
public class ORServeGatewayMessageHandlerImpl implements GatewayMessageHandler {

    private CoreManager coreManager;
    private static Logger logger = Logger.getLogger(ORServeGatewayMessageHandlerImpl.class);
    private Map<MStatus, String> codeStatusMap;
    private Map<MStatus, String> codeResponseMap;

    /**
     *
     * @see GatewayMessageHandler.parseResponse
     */
    public Set<GatewayResponse> parseMessageResponse(GatewayRequest message, String gatewayResponse, MotechContext context) {
        logger.info("Parsing message gateway response");
        logger.debug(gatewayResponse);

        if(message == null)
            return null;
        
        if(gatewayResponse.isEmpty())
            return null;

        Set<GatewayResponse> responses = new HashSet<GatewayResponse>();
        if(gatewayResponse.contains("error:") && gatewayResponse.contains("param:")){
            gatewayResponse = gatewayResponse.substring(0, 15).replace("\n", "");
        }
        String[] responseLines = gatewayResponse.split("\n");

        for(String line : responseLines){
            String[] responseParts = line.split(" ");            

            if(responseParts[0].equalsIgnoreCase("ID:")){
                GatewayResponse response = getCoreManager().createGatewayResponse(context);
                
                response.setGatewayMessageId(responseParts[1]);
                response.setRequestId(message.getRequestId());
                response.setMessageStatus(MStatus.PENDING);
                response.setGatewayRequest(message);
                response.setResponseText(gatewayResponse);
                response.setDateCreated(new Date());

                if(responseParts.length == 4)
                    response.setRecipientNumber(responseParts[3]);
                else
                    response.setRecipientNumber(message.getRecipientsNumber());

                
                responses.add(response);
            }
            else{
                logger.error("Gateway returned error: " + gatewayResponse);
                
                String errorCode = responseParts[1];
                errorCode.replaceAll(",", "");<commit_msg>Changed use of substring on response string "error: param:" to trim to prevent indexOutOfBoundsException<commit_after>package com.dreamoval.motech.omp.manager.orserve;

import com.dreamoval.motech.core.manager.CoreManager;
import com.dreamoval.motech.core.model.GatewayRequest;
import com.dreamoval.motech.core.model.GatewayResponse;
import com.dreamoval.motech.core.model.MStatus;
import com.dreamoval.motech.core.service.MotechContext;
import com.dreamoval.motech.omp.manager.GatewayMessageHandler;
import java.util.Date;
import java.util.Set;
import java.util.HashSet;
import java.util.Map;
import java.util.Map.Entry;
import org.apache.log4j.Logger;

/**
 * Handles preparation and parsing of messages and responses from the OutReach Server message gateway
 *
 * @author <NAME> (<EMAIL>)
 * @date Jul 15, 2009
 */
public class ORServeGatewayMessageHandlerImpl implements GatewayMessageHandler {

    private CoreManager coreManager;
    private static Logger logger = Logger.getLogger(ORServeGatewayMessageHandlerImpl.class);
    private Map<MStatus, String> codeStatusMap;
    private Map<MStatus, String> codeResponseMap;

    /**
     *
     * @see GatewayMessageHandler.parseResponse
     */
    public Set<GatewayResponse> parseMessageResponse(GatewayRequest message, String gatewayResponse, MotechContext context) {
        logger.info("Parsing message gateway response");
        logger.debug(gatewayResponse);

        if(message == null)
            return null;
        
        if(gatewayResponse.isEmpty())
            return null;

        Set<GatewayResponse> responses = new HashSet<GatewayResponse>();
        if(gatewayResponse.contains("error:") && gatewayResponse.contains("param:")){
            gatewayResponse = gatewayResponse.trim().replace("\n", "");
        }
        String[] responseLines = gatewayResponse.split("\n");

        for(String line : responseLines){
            String[] responseParts = line.split(" ");            

            if(responseParts[0].equalsIgnoreCase("ID:")){
                GatewayResponse response = getCoreManager().createGatewayResponse(context);
                
                response.setGatewayMessageId(responseParts[1]);
                response.setRequestId(message.getRequestId());
                response.setMessageStatus(MStatus.PENDING);
                response.setGatewayRequest(message);
                response.setResponseText(gatewayResponse);
                response.setDateCreated(new Date());

                if(responseParts.length == 4)
                    response.setRecipientNumber(responseParts[3]);
                else
                    response.setRecipientNumber(message.getRecipientsNumber());

                
                responses.add(response);
            }
            else{
                logger.error("Gateway returned error: " + gatewayResponse);
                
                String errorCode = responseParts[1];
                errorCode.replaceAll(",", "");
{'fraction_non_alphanumeric': 0.10124213033860813, 'fraction_numerical': 0.004594180704441042, 'mean_word_length': 2.7511167836630506, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '836643', 'n_tokens_mistral': 1554, 'n_tokens_neox': 1463, 'n_words': 280}