package com.wangyin.ak47.pipes.http;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.wangyin.ak47.common.ByteUtil;


/**
 * 简单的HttpRequest实现
 * 
 * @author wyhanyu
 *
 */
public class SimpleHttpMessage {
    

    
    // HttpMessage = startLine + headers + content;
    // first line
    private String startLine;
    // many headers
    private Map<String, List<String>> headers = new HashMap<String, List<String>>();;
    // any content
    private byte[] content;

    public String getStartLine(){
        return startLine;
    }
    public void setStartLine(String startLine){
        this.startLine = startLine;
    }
    
    public Map<String, List<String>> getHeaders(){
        return headers;
    }
    public void setHeaders(Map<String, List<String>> headers){
        this.headers = headers;
    }
    
    public byte[] getContent(){
        return content;
    }
    public void setContent(byte[] content){
        this.content = content;
    }
    
    /**
     * 添加一个Header
     * 
     * @param key
     * @param value
     */
    public void addHeader(String key, String value){
//        key = StringUtil2.asciiToLowerCase(key);
        List<String> vl = headers.get(key);
        if( null == vl ){
            vl = new ArrayList<String>(1);
            vl.add(value);
            headers.put(key, vl);
        }else{
            vl.add(value);
        }
    }
    
    /**
     * 获取该key的第一个header
     * 若不存在，则返回null
     * 
     * @param key
     * @return 
     */
    public String getHeaderFirst(String key){
//        key = StringUtil2.asciiToLowerCase(key);
        List<String> vl = headers.get(key);
        if( null == vl ){
            return null;
        }else {
            return vl.get(0);
        }
    }
    
    /**
     * 修改key的header
     * @param key
     * @param value
     */
    public void setOrAddFirstHeader(String key, String value){
//        key = StringUtil2.asciiToLowerCase(key);
        List<String> vl = headers.get(key);
        if( null == vl ){
            addHeader(key, value);
        }else {
            vl.set(0, value);
        }
    }
    
    /**
     * build HTTP header
     * 
     * @return
     */
    public String buildHeaderString(){
        StringBuilder sb = new StringBuilder();
        sb.append(startLine).append(SimpleHttpParser.CRLF_STRING);
        for(String key : headers.keySet()){
            List<String> vl = headers.get(key);
            for(String v : vl){
                if( key.equals("content-length") ){
                    key = "Content-Length";
                }
                if( key.equals("server") ){
                    key = "Server";
                }
                sb.append(key).append(": ").append(v).append(SimpleHttpParser.CRLF_STRING);
                
            }
        }
        sb.append(SimpleHttpParser.CRLF_STRING);
        
        return sb.toString();
    }
    
    /**
     * 完整打印
     * 
     * @return
     */
    @Override
    public String toString(){
        if( null != content ){
            StringBuilder sb = new StringBuilder();
            sb.append( buildHeaderString() );
            sb.append( "content("+content.length+")" );
            return sb.toString();
        }else{
            return buildHeaderString();
        }
    }
    
    
    /**
     * 导出为bytearray
     * @return
     */
    public byte[] buildFullBytes(){
        byte[] half = buildHeaderString().getBytes();
        if( null != content ){
            byte[] full = ByteUtil.merge(half,content);
            return full;
        }else{
            return half;
        }
    }
}

@@ -1074,36 +1074,77 @@
 aram key
-%0A     * @param value
+       Key in header%0A     * @param value     Value of the key
 %0A     */
@@ -1566,24 +1566,69 @@
  key
-%0A     * @return 
+       Key in header%0A     * @return          Value of the key
 %0A   
@@ -1915,24 +1915,32 @@
 %E6%94%B9key%E7%9A%84header%0A
+     * %0A
      * @para
@@ -1944,36 +1944,77 @@
 aram key
-%0A     * @param value
+       Key in header%0A     * @param value     Value of the Key
 %0A     */
@@ -2347,32 +2347,52 @@
  %0A     * @return
+      Text of header
 %0A     */%0A    pub
@@ -3140,32 +3140,70 @@
  %0A     * @return
+      Text of header, and size of body
 %0A     */%0A    @Ov
@@ -3567,16 +3567,24 @@
 tearray%0A
+     * %0A
      * @
@@ -3589,16 +3589,50 @@
  @return
+      Bytes of all header and body
 %0A     */

add more javadocs