public class Response implements HttpServletResponse
{
protected Socket Connect; //
Socket der Verbindung
protected Context Env;
protected OutStream Stream; //
ServletOutputStream
protected int StatusCode = 500; //
Status-Code, default "Server Error"
protected String StatusMsg = "";//
Status-Code
protected int Length = 0; //
Content-Length
protected String Type = "text/plain"; //
Content-Type
protected Hashtable Header; //
Header-Felder
protected boolean HeaderSent
= false; // Status und Header wurde gesendet
public Response(Socket connect, Request req, Context env)
throws IOException
{
Connect = connect;
Env = env;
Stream = new OutStream(Connect);
Header = new Hashtable(2);
if (req.getProtocol().startsWith("HTTP/0"))
HeaderSent = true; // bei altem
HTTP kein Header senden
}
public void setContentLength(int len)
{ Length = len; }
public void setContentType(String type)
{ Type = type; }
public ServletOutputStream getOutputStream() throws IOException
{ // Stream zum Schreiben; Senden des
Header
SendHeader();
return Stream;
}
public boolean containsHeader(String name)
{ return Header.containsKey(name.toLowerCase());
}
public void setStatus(int sc, String sm)
{
StatusCode = sc;
StatusMsg = sm;
}
public void setStatus(int sc)
{
StatusCode = sc;
StatusMsg = "";
}
public void setHeader(String name, String value)
{
Header.put(name,value);
}
public void setIntHeader(String name, int value)
{
Header.put(name,Integer.toString(value));
}
public void setDateHeader(String name, long date)
{
Header.put(name, new HttpDate(date).toString());
}
public void sendError(int sc, String msg) throws IOException
{ // Senden einer Fehlermeldung
setStatus(sc,msg);
setContentLength(0); // damit das
Header-Feld nicht gesendet wird
setContentType("text/html");
SendHeader();
Stream.println("<HTML><HEAD><TITLE>Error: " + msg);
Stream.println("</TITLE></HEAD><BODY>" + "<H1>Error:
" + sc);
Stream.println(" " + msg + "</H1>" + "</BODY></HTML>");
}
public void sendError(int sc) throws IOException
{
sendError(sc, "");
}
public void sendRedirect(String location) throws IOException
{ // Senden eines Redirect nach location
if (StatusCode == 500)
setStatus(300,"Multiple Choices");
setContentLength(0); // damit das
Header-Feld nicht gesendet wird
setContentType("text/html");
setHeader("location",location);
SendHeader();
Stream.println("<HTML><HEAD><TITLE>Redirect</TITLE></HEAD>");
Stream.println("<BODY><H1>Redirect: </H1><a
href=");
Stream.println(location + "><H4>" + location + "</H4></a>");
Stream.println("</BODY></HTML>");
}
public void SendHeader()
{ // HTTP-Header senden, wenn er noch
nicht gesendet wurde
if (HeaderSent) return;
try
{
Stream.println("HTTP/1.0 " + StatusCode + " " + StatusMsg);
if (Length > 0)
Stream.println("Content-length: " + Length);
Stream.println("Content-type: " + Type);
Enumeration keys = Header.keys();
String name;
while (keys.hasMoreElements())
{ // weitere Header-Felder
name = (String)keys.nextElement();
Stream.println(name + ": " + Header.get(name));
}
Stream.println();
}
catch (IOException e)
{
Env.log("Response: " + e);
}
finally
{
HeaderSent = true;
}
}
}