<commit_before>package gov.nist.registry.ws.serviceclasses;

import gov.nist.registry.common2.exception.XdsInternalException;
import gov.nist.registry.common2.exception.XdsWSException;
import gov.nist.registry.common2.registry.AdhocQueryResponse;
import gov.nist.registry.common2.registry.MetadataSupport;
import gov.nist.registry.common2.registry.Properties;
import gov.nist.registry.common2.registry.RegistryErrorList;
import gov.nist.registry.common2.registry.RegistryResponse;
import gov.nist.registry.common2.registry.RetrieveMultipleResponse;
import gov.nist.registry.ws.log.Fields;
import gov.nist.registry.xdslog.Log;
import gov.nist.registry.xdslog.LoggerException;
import gov.nist.registry.xdslog.Message;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Vector;

import javax.xml.namespace.QName;
import javax.xml.stream.XMLStreamException;

import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMException;
import org.apache.axiom.om.OMNamespace;
import org.apache.axiom.soap.SOAPEnvelope;
import org.apache.axiom.soap.SOAPHeader;
import org.apache.axis2.Constants;
import org.apache.axis2.context.MessageContext;
import org.apache.axis2.transport.http.TransportHeaders;
import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Logger;

public class XdsService {
	protected Log log = null;
	Message log_message;
	public static short registry_actor = 1;
	public static short repository_actor = 2;
	private final static Logger logger = Logger.getLogger(XdsService.class);
	String service_name;
	static Properties properties = null;
	boolean is_secure;
	MessageContext return_message_context = null;
	static public String technicalFramework = null;

	static {
//		BasicConfigurator.configure();
		properties = Properties.loader();
		technicalFramework = properties.getString("tf");
	}

	protected boolean isSecure() { return is_secure; }

	MessageContext getMessageContext() {
		return MessageContext.getCurrentMessageContext();
	}

	public void setReturnMessageContext(MessageContext return_context) {
		this.return_message_context = return_context;
	}

	public void useXop() {
		this.return_message_context = MessageContext.getCurrentMessageContext();
		if (return_message_context != null)
			return_message_context.getOptions().setProperty(Constants.Configuration.ENABLE_MTOM, Constants.VALUE_TRUE);
	}

	protected OMElement beginTransaction(String service_name, OMElement request, short actor) {

		// This gets around a bug in Leopard (MacOS X 10.5) on Macs
		// Though removing this property fixes a Mac problem, but it disables transactions via proxy server in a production environment
		//System.setProperty("http.nonProxyHosts", "");


		this.service_name = service_name;

//TODO: need re-work as it is not efficient to block IPs		
//		String incoming_ip_address = getMessageContext().getFrom().getAddress();
//		for (int i=1; i<100; i++) {
//			String entry = this.properties.getString("BlockIp" + i);
//			if (entry == null || entry.equals(""))
//				break;
//			if (entry.equals(incoming_ip_address))
//				return start_up_error(request, null, actor, "Continuous jamming from IP " + incoming_ip_address + " - access blocked");
//		}


		logger.info("Start " + service_name + " : " + getMessageContext().getFrom().getAddress()  + " : " + getMessageContext().getTo().toString());
		try {
			startTestLog();
			if (log != null && log_message == null) {
				log_message = log.createMessage(getMessageContext().getFrom().getAddress());
			}
			log_message.addOtherParam(Fields.service, service_name);
			is_secure = getMessageContext().getTo().toString().indexOf("https://") != -1;
			log_message.addHTTPParam(Fields.isSecure, (is_secure) ? "true" : "false");
			log_message.addHTTPParam(Fields.date, getDateTime());
			log_message.setSecure(is_secure);
			if (request != null)
				log_message.addOtherParam("Request", request.toString());
			else {
				log_message.addErrorParam("Error", "Cannot access request body in XdsService.begin_service()");
				return start_up_error(request, null, actor, "Request body is null");
			}

            HashMap transportHeaders = (HashMap)getMessageContext().getProperty("TRANSPORT_HEADERS");
			for (Object o_key : transportHeaders.keySet()) {
				String key = (String) o_key;
				String value = (String) transportHeaders.get(key);

				Vector<String> thdrs = new Vector<String>();
				thdrs.add(key + " : " + value);
				addHttp( "HTTP Header", thdrs ) ;
			}


			if ( getMessageContext().getEnvelope().getHeader() != null )
			{

				try {
					addSoap ( "Soap Header", getMessageContext().getEnvelope().getHeader().toStringWithConsume() ) ;
				}  catch (OMException e) {} catch (XMLStreamException e) {}
			}

			if ( getMessageContext().getEnvelope().getBody() != null )
			{
				try
				{
					addSoap( "Soap Envelope", getMessageContext().getEnvelope().toStringWithConsume() );
				}  catch (OMException e) {} catch (XMLStreamException e) {}
			}
			log_message.addHTTPParam(Fields.fromIpAddress , getMessageContext().getFrom().getAddress() ) ;  
			log_message.addHTTPParam(Fields.endpoint , getMessageContext().getTo().toString() ) ; 

			return null;  // no error
		} catch (LoggerException e) {
			System.out.println("LoggerException: new Log: " + e.getMessage());
			e.printStackTrace();
			stopTestLog();
			return start_up_error(request, e, actor, "Internal Error:Cannot access Test Log Facility");
		}
	}

	protected void endTransaction(boolean status) {<commit_msg>Fixed logs and from IP address.<commit_after>package gov.nist.registry.ws.serviceclasses;

import gov.nist.registry.common2.exception.XdsInternalException;
import gov.nist.registry.common2.exception.XdsWSException;
import gov.nist.registry.common2.registry.AdhocQueryResponse;
import gov.nist.registry.common2.registry.MetadataSupport;
import gov.nist.registry.common2.registry.Properties;
import gov.nist.registry.common2.registry.RegistryErrorList;
import gov.nist.registry.common2.registry.RegistryResponse;
import gov.nist.registry.common2.registry.RetrieveMultipleResponse;
import gov.nist.registry.ws.log.Fields;
import gov.nist.registry.xdslog.Log;
import gov.nist.registry.xdslog.LoggerException;
import gov.nist.registry.xdslog.Message;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Vector;

import javax.xml.namespace.QName;
import javax.xml.stream.XMLStreamException;

import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMException;
import org.apache.axiom.om.OMNamespace;
import org.apache.axiom.soap.SOAPEnvelope;
import org.apache.axiom.soap.SOAPHeader;
import org.apache.axis2.Constants;
import org.apache.axis2.context.MessageContext;
import org.apache.commons.logging.LogFactory;

public class XdsService {
	protected Log log = null;
	Message log_message;
	public static short registry_actor = 1;
	public static short repository_actor = 2;
	private final static org.apache.commons.logging.Log logger = LogFactory.getLog(XdsService.class);
	String service_name;
	static Properties properties = null;
	boolean is_secure;
	MessageContext return_message_context = null;
	static public String technicalFramework = null;
	
	static {
//		BasicConfigurator.configure();
		properties = Properties.loader();
		technicalFramework = properties.getString("tf");
	}

	protected boolean isSecure() { return is_secure; }

	MessageContext getMessageContext() {
		return MessageContext.getCurrentMessageContext();
	}

	public void setReturnMessageContext(MessageContext return_context) {
		this.return_message_context = return_context;
	}

	public void useXop() {
		this.return_message_context = MessageContext.getCurrentMessageContext();
		if (return_message_context != null)
			return_message_context.getOptions().setProperty(Constants.Configuration.ENABLE_MTOM, Constants.VALUE_TRUE);
	}

	protected OMElement beginTransaction(String service_name, OMElement request, short actor) {

		// This gets around a bug in Leopard (MacOS X 10.5) on Macs
		// Though removing this property fixes a Mac problem, but it disables transactions via proxy server in a production environment
		//System.setProperty("http.nonProxyHosts", "");


		this.service_name = service_name;

		String incoming_ip_address = null;
		if (getMessageContext().getFrom() != null) {
			incoming_ip_address = getMessageContext().getFrom().getAddress();
		} else {
			incoming_ip_address = (String)getMessageContext().getProperty(MessageContext.REMOTE_ADDR);
		}

//		String incoming_ip_address = getMessageContext().getFrom().getAddress();
//		for (int i=1; i<100; i++) {
//			String entry = this.properties.getString("BlockIp" + i);
//			if (entry == null || entry.equals(""))
//				break;
//			if (entry.equals(incoming_ip_address))
//				return start_up_error(request, null, actor, "Continuous jamming from IP " + incoming_ip_address + " - access blocked");
//		}

		logger.info("Start " + service_name + " : " + incoming_ip_address  + " : " + getMessageContext().getTo().toString());
		try {
			startTestLog();
			if (log != null && log_message == null) {
//				log_message = log.createMessage(getMessageContext().getFrom().getAddress());
				log_message = log.createMessage(incoming_ip_address);
			}
			log_message.addOtherParam(Fields.service, service_name);
			is_secure = getMessageContext().getTo().toString().indexOf("https://") != -1;
			log_message.addHTTPParam(Fields.isSecure, (is_secure) ? "true" : "false");
			log_message.addHTTPParam(Fields.date, getDateTime());
			log_message.setSecure(is_secure);
			if (request != null)
				log_message.addOtherParam("Request", request.toString());
			else {
				log_message.addErrorParam("Error", "Cannot access request body in XdsService.begin_service()");
				return start_up_error(request, null, actor, "Request body is null");
			}

            HashMap transportHeaders = (HashMap)getMessageContext().getProperty("TRANSPORT_HEADERS");
			for (Object o_key : transportHeaders.keySet()) {
				String key = (String) o_key;
				String value = (String) transportHeaders.get(key);
                
				if (logger.isDebugEnabled()) {
					logger.debug("key=" + key +", value=" + value );
				}
				Vector<String> thdrs = new Vector<String>();
				thdrs.add(key + " : " + value);
				addHttp( "HTTP Header", thdrs ) ;
			}


			if ( getMessageContext().getEnvelope().getHeader() != null )
			{

				try {
					addSoap ( "Soap Header", getMessageContext().getEnvelope().getHeader().toStringWithConsume() ) ;
				}  catch (OMException e) {} catch (XMLStreamException e) {}
			}

			if ( getMessageContext().getEnvelope().getBody() != null )
			{
				try
				{
					addSoap( "Soap Envelope", getMessageContext().getEnvelope().toStringWithConsume() );
				}  catch (OMException e) {} catch (XMLStreamException e) {}
			}
//			log_message.addHTTPParam(Fields.fromIpAddress , getMessageContext().getFrom().getAddress() ) ;  
			log_message.addHTTPParam(Fields.fromIpAddress , incoming_ip_address ) ;  			
			log_message.addHTTPParam(Fields.endpoint , getMessageContext().getTo().toString() ) ; 

			return null;  // no error
		} catch (LoggerException e) {
			System.out.println("LoggerException: new Log: " + e.getMessage());
			e.printStackTrace();
			stopTestLog();
			return start_up_error(request, e, actor, "Internal Error:Cannot access Test Log Facility");
		}
	}

	protected void endTransaction(boolean status) {
{'fraction_non_alphanumeric': 0.12503231365790607, 'fraction_numerical': 0.003705299439896596, 'mean_word_length': 4.800099950024988, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 7, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, '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': '2280061', 'n_tokens_mistral': 3838, 'n_tokens_neox': 3529, 'n_words': 711}