// Path: src/main/java/org/jembi/openhim/RestfulHttpRequest.java
// public static enum Scheme { HTTP, HTTPS }
// 
// Path: src/main/java/org/jembi/openhim/exception/DefaultChannelInvalidConfigException.java
// public class DefaultChannelInvalidConfigException extends Exception {
// 
// 	private static final long serialVersionUID = 1L;
// 
// 	public DefaultChannelInvalidConfigException() {}
// 
// 	public DefaultChannelInvalidConfigException(String message) {
// 		super(message);
// 	}
// 
// 	public DefaultChannelInvalidConfigException(Throwable cause) {
// 		super(cause);
// 	}
// 
// 	public DefaultChannelInvalidConfigException(String message, Throwable cause) {
// 		super(message, cause);
// 	}
// }


import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.jembi.openhim.RestfulHttpRequest.Scheme;
import org.jembi.openhim.exception.DefaultChannelInvalidConfigException;
import org.jembi.openhim.exception.URLMappingNotFoundException;
import org.mule.api.MuleEventContext;
import org.mule.api.MuleMessage;
import org.mule.api.lifecycle.Callable;
import org.mule.api.transport.PropertyScope;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.codec.binary.Base64;
package org.jembi.openhim;




public class DefaultChannelComponent implements Callable {

	protected static List<URLMapping> mappings = new ArrayList<URLMapping>();
	
	private static final String MAPPING_FILE = "/defaultchannel-mapping.json";
	private static final String HTTP_AUTH_TYPE_BASIC = "basic";

	@Override
	public Object onCall(MuleEventContext eventContext) throws Exception {

		if (mappings.size() < 1) {
			readMappings();
		}

		MuleMessage msg = eventContext.getMessage();
		RestfulHttpRequest req = (RestfulHttpRequest) msg.getPayload();
		String actualPath = req.getPath();

		URLMapping mapping = findURLMapping(req.getScheme(), actualPath);
		
		if (mapping == null) {
			throw new URLMappingNotFoundException("A URL mapping was not found for the URL: " + req.getPath());
		}
		
		setMessagePropertiesFromMapping(req, msg, mapping);
		
		return msg;
	}
	
// Path: src/main/java/org/jembi/openhim/RestfulHttpRequest.java
// public static enum Scheme { HTTP, HTTPS }
// 
// Path: src/main/java/org/jembi/openhim/exception/DefaultChannelInvalidConfigException.java
// public class DefaultChannelInvalidConfigException extends Exception {
// 
// 	private static final long serialVersionUID = 1L;
// 
// 	public DefaultChannelInvalidConfigException() {}
// 
// 	public DefaultChannelInvalidConfigException(String message) {
// 		super(message);
// 	}
// 
// 	public DefaultChannelInvalidConfigException(Throwable cause) {
// 		super(cause);
// 	}
// 
// 	public DefaultChannelInvalidConfigException(String message, Throwable cause) {
// 		super(message, cause);
// 	}
// }


// Path: src/main/java/org/jembi/openhim/DefaultChannelComponent.java
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.jembi.openhim.RestfulHttpRequest.Scheme;
import org.jembi.openhim.exception.DefaultChannelInvalidConfigException;
import org.jembi.openhim.exception.URLMappingNotFoundException;
import org.mule.api.MuleEventContext;
import org.mule.api.MuleMessage;
import org.mule.api.lifecycle.Callable;
import org.mule.api.transport.PropertyScope;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.codec.binary.Base64;

package org.jembi.openhim;




public class DefaultChannelComponent implements Callable {

	protected static List<URLMapping> mappings = new ArrayList<URLMapping>();
	
	private static final String MAPPING_FILE = "/defaultchannel-mapping.json";
	private static final String HTTP_AUTH_TYPE_BASIC = "basic";

	@Override
	public Object onCall(MuleEventContext eventContext) throws Exception {

		if (mappings.size() < 1) {
			readMappings();
		}

		MuleMessage msg = eventContext.getMessage();
		RestfulHttpRequest req = (RestfulHttpRequest) msg.getPayload();
		String actualPath = req.getPath();

		URLMapping mapping = findURLMapping(req.getScheme(), actualPath);
		
		if (mapping == null) {
			throw new URLMappingNotFoundException("A URL mapping was not found for the URL: " + req.getPath());
		}
		
		setMessagePropertiesFromMapping(req, msg, mapping);
		
		return msg;
	}
	
	protected void setMessagePropertiesFromMapping(RestfulHttpRequest request, MuleMessage msg, URLMapping mapping) throws DefaultChannelInvalidConfigException {