package com.eactive.eai.adapter.socket2.protocol.http.codec;

import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.mina.common.IoSession;


public class HttpRequest {
    private Map headers = null;
    private IoSession session = null;

    public void setHeaders(Map headers) {
        this.headers = headers;
    }

    public Map getHeaders() {
        return headers;
    }
    
    public String getURI() {
    	String[] uris = (String[]) headers.get("URI");
    	return uris == null ? "" : uris[0];
    }

    public String getContext() {
        String[] context = (String[]) headers.get("Context");
        return context == null ? "" : context[0];
    }

    public String getParameter(String name) {
        String[] param = (String[]) headers.get("@".concat(name));
        return param == null ? "" : param[0];
    }
    	
    public String[] getParameters(String name) {
        String[] param = (String[]) headers.get("@".concat(name));
        return param == null ? new String[] {} : param;
    }

    public String[] getHeader(String name) {
        return (String[]) headers.get(name);
    }
    
    public void setSession(IoSession session) {
    	this.session = session;
    }
    
    public IoSession getSession() {
    	return session;
    }

    public String toString() {
        StringBuilder str = new StringBuilder();

        Iterator it = headers.entrySet().iterator();
        while (it.hasNext()) {
            Entry e = (Entry) it.next();
            str.append(e.getKey() + " : "
                    + arrayToString((String[]) e.getValue(), ',') + "\n");
        }
        return str.toString();
    }

    public static String arrayToString(String[] s, char sep) {
        if (s == null || s.length == 0)
            return "";
        StringBuilder buf = new StringBuilder();
        for (int i = 0; i < s.length; i++) {
            if (i > 0)
                buf.append(sep);
            buf.append(s[i]);
        }
        return buf.toString();
    }
}
