1 20 package org.apache.mina.example.httpserver.codec; 21 22 import java.io.ByteArrayOutputStream ; 23 import java.io.IOException ; 24 import java.text.SimpleDateFormat ; 25 import java.util.Date ; 26 import java.util.HashMap ; 27 import java.util.Map ; 28 29 import org.apache.mina.common.ByteBuffer; 30 31 37 public class HttpResponseMessage { 38 39 public static final int HTTP_STATUS_SUCCESS = 200; 40 41 public static final int HTTP_STATUS_NOT_FOUND = 404; 42 43 44 private Map <String , String > headers = new HashMap <String , String >(); 45 46 47 private ByteArrayOutputStream body = new ByteArrayOutputStream (1024); 48 49 private int responseCode = HTTP_STATUS_SUCCESS; 50 51 public HttpResponseMessage() { 52 headers.put("Server", "HttpServer (" + Server.VERSION_STRING + ')'); 53 headers.put("Cache-Control", "private"); 54 headers.put("Content-Type", "text/html; charset=iso-8859-1"); 55 headers.put("Connection", "keep-alive"); 56 headers.put("Keep-Alive", "200"); 57 headers.put("Date", new SimpleDateFormat ( 58 "EEE, dd MMM yyyy HH:mm:ss zzz").format(new Date ())); 59 headers.put("Last-Modified", new SimpleDateFormat ( 60 "EEE, dd MMM yyyy HH:mm:ss zzz").format(new Date ())); 61 } 62 63 public Map getHeaders() { 64 return headers; 65 } 66 67 public void setContentType(String contentType) { 68 headers.put("Content-Type", contentType); 69 } 70 71 public void setResponseCode(int responseCode) { 72 this.responseCode = responseCode; 73 } 74 75 public int getResponseCode() { 76 return this.responseCode; 77 } 78 79 public void appendBody(byte[] b) { 80 try { 81 body.write(b); 82 } catch (IOException ex) { 83 ex.printStackTrace(); 84 } 85 } 86 87 public void appendBody(String s) { 88 try { 89 body.write(s.getBytes()); 90 } catch (IOException ex) { 91 ex.printStackTrace(); 92 } 93 } 94 95 public ByteBuffer getBody() { 96 return ByteBuffer.wrap(body.toByteArray()); 97 } 98 99 public int getBodyLength() { 100 return body.size(); 101 } 102 } 103 | Popular Tags |