package dev.peerat.framework;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

public class TomcatHttpReader extends HttpReader{

	private HttpServletRequest request;
	
	public TomcatHttpReader(HttpServletRequest request) throws Exception{
		super(request.getInputStream());
		this.request = request;
	}

	@Override
	public boolean isClosed(){
		return false;
	}
	
	@Override
	void readHeaders() throws Exception{}
	
	@Override
	public String getHeader(String key){
		String result;
		result = request.getHeader(key);
		if(result != null) return result;
		result = request.getHeader(key.toLowerCase());
		if(result != null) return result;
		result = request.getHeader(key.toUpperCase());
		if(result != null) return result;
		result = request.getHeader(firstUpper(key.toLowerCase()));
		if(result != null) return result;
		return null;
	}
	
	private String firstUpper(String value){
		boolean beUpper = true;
		String result = "";
		for(char c : value.toCharArray()){
			if(beUpper){
				result+=Character.toUpperCase(c);
				beUpper = false;
			}else if(c == '-'){
				beUpper = true;
				result+='-';
			}else result+=c;
		}
		return result;
	}
}
