<commit_before>/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
package org.jembi.rhea.transformers;

import java.io.PrintWriter;
import java.io.StringWriter;

import org.jembi.rhea.RestfulHttpResponse;
import org.jembi.rhea.orchestration.exceptions.ClientValidationException;
import org.jembi.rhea.orchestration.exceptions.EncounterEnrichmentException;
import org.jembi.rhea.orchestration.exceptions.LocationValidationException;
import org.jembi.rhea.orchestration.exceptions.ProviderValidationException;
import org.mule.api.MuleMessage;
import org.mule.api.transformer.TransformerException;
import org.mule.api.transport.PropertyScope;
import org.mule.transformer.AbstractMessageTransformer;

public class RestfulHttpResponseToHttpResponseTransformer extends
		AbstractMessageTransformer {

	@Override
	public Object transformMessage(MuleMessage msg, String enc) throws TransformerException {
		
		Object payload = msg.getPayload();
		
		if (payload instanceof RestfulHttpResponse) {
			RestfulHttpResponse restRes = (RestfulHttpResponse) payload;
			msg.setOutboundProperty("http.status", restRes.getHttpStatus());
			msg.setPayload(restRes.getBody());
			
			return msg;
			
		} else {
			Throwable exception = msg.getExceptionPayload().getException();
			StringWriter sw = new StringWriter();
			
			if (exception != null) {
				exception.printStackTrace(new PrintWriter(sw));
				
				Throwable cause = exception.getCause();
				if (cause != null) {
					if (cause instanceof ClientValidationException) {
						msg.setProperty("http.status", 400, PropertyScope.OUTBOUND);
						msg.setPayload("Message orchestration failed, the client ID failed validation: " + sw.toString());
					} else if (cause instanceof EncounterEnrichmentException) {
						msg.setProperty("http.status", 400, PropertyScope.OUTBOUND);
						msg.setPayload("Message orchestration failed, encounter enrichment failed: " + sw.toString());
					} else if (cause instanceof LocationValidationException) {
						msg.setProperty("http.status", 400, PropertyScope.OUTBOUND);
						msg.setPayload("Message orchestration failed, the location ID failed validation: " + sw.toString());
					} else if (cause instanceof ProviderValidationException) {
						msg.setProperty("http.status", 400, PropertyScope.OUTBOUND);
						msg.setPayload("Message orchestration failed, the provider ID failed validation: " + sw.toString());
					} else {
						msg.setProperty("http.status", 500, PropertyScope.OUTBOUND);
						msg.setPayload("A server error has occured, the responce was not of type RestfulHttpResponse, caused by: " + sw.toString());
					}
				}
				
			} else {
				msg.setProperty("http.status", 500, PropertyScope.OUTBOUND);
				msg.setPayload("A server error has occured, the responce was not of type RestfulHttpResponse");
			}
			
			return msg;
		}

	}

}<commit_msg>Fixed spelling, added payload type to error response<commit_after>/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
package org.jembi.rhea.transformers;

import java.io.PrintWriter;
import java.io.StringWriter;

import org.jembi.rhea.RestfulHttpResponse;
import org.jembi.rhea.orchestration.exceptions.ClientValidationException;
import org.jembi.rhea.orchestration.exceptions.EncounterEnrichmentException;
import org.jembi.rhea.orchestration.exceptions.LocationValidationException;
import org.jembi.rhea.orchestration.exceptions.ProviderValidationException;
import org.mule.api.MuleMessage;
import org.mule.api.transformer.TransformerException;
import org.mule.api.transport.PropertyScope;
import org.mule.transformer.AbstractMessageTransformer;

public class RestfulHttpResponseToHttpResponseTransformer extends
		AbstractMessageTransformer {

	@Override
	public Object transformMessage(MuleMessage msg, String enc) throws TransformerException {
		
		Object payload = msg.getPayload();
		
		if (payload instanceof RestfulHttpResponse) {
			RestfulHttpResponse restRes = (RestfulHttpResponse) payload;
			msg.setOutboundProperty("http.status", restRes.getHttpStatus());
			msg.setPayload(restRes.getBody());
			
			return msg;
			
		} else {
			Throwable exception = msg.getExceptionPayload().getException();
			StringWriter sw = new StringWriter();
			
			if (exception != null) {
				exception.printStackTrace(new PrintWriter(sw));
				
				Throwable cause = exception.getCause();
				if (cause != null) {
					if (cause instanceof ClientValidationException) {
						msg.setProperty("http.status", 400, PropertyScope.OUTBOUND);
						msg.setPayload("Message orchestration failed, the client ID failed validation: " + sw.toString());
					} else if (cause instanceof EncounterEnrichmentException) {
						msg.setProperty("http.status", 400, PropertyScope.OUTBOUND);
						msg.setPayload("Message orchestration failed, encounter enrichment failed: " + sw.toString());
					} else if (cause instanceof LocationValidationException) {
						msg.setProperty("http.status", 400, PropertyScope.OUTBOUND);
						msg.setPayload("Message orchestration failed, the location ID failed validation: " + sw.toString());
					} else if (cause instanceof ProviderValidationException) {
						msg.setProperty("http.status", 400, PropertyScope.OUTBOUND);
						msg.setPayload("Message orchestration failed, the provider ID failed validation: " + sw.toString());
					} else {
						msg.setProperty("http.status", 500, PropertyScope.OUTBOUND);
						msg.setPayload("A server error has occured, the response was not of type RestfulHttpResponse, caused by: " + sw.toString());
					}
				}
				
			} else {
				msg.setProperty("http.status", 500, PropertyScope.OUTBOUND);
				msg.setPayload("A server error has occured, the response was of type " + payload.getClass().getName() + " instead of the expected type RestfulHttpResponse");
			}
			
			return msg;
		}

	}

}
{'fraction_non_alphanumeric': 0.10175323611338685, 'fraction_numerical': 0.00720956906439456, 'mean_word_length': 4.494149414941494, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '2658611', 'n_tokens_mistral': 2030, 'n_tokens_neox': 1666, 'n_words': 432}