// ** This class was generated with DemFGen (vers:06/04/2009) package http.gen; import edu.neu.ccs.demeterf.demfgen.lib.*; import java.net.Socket; import java.net.ServerSocket; import java.io.IOException; import java.io.BufferedReader; import java.io.PrintWriter; import java.io.InputStreamReader; import java.io.InputStream; /** Representation of HTTPResp */ public class HTTPResp{ public final HTTPVer ver; public final int resp; public final ident label; public final List keys; public final ident body; /** Construct a(n) HTTPResp Instance */ public HTTPResp(HTTPVer ver, int resp, ident label, List keys, ident body){ this.ver = ver; this.resp = resp; this.label = label; this.keys = keys; this.body = body; } /** Is the given object Equal to this HTTPResp? */ public boolean equals(Object o){ if(!(o instanceof HTTPResp))return false; if(o == this)return true; HTTPResp oo = (HTTPResp)o; return (((Object)ver).equals(oo.ver))&&(((Object)resp).equals(oo.resp))&&(((Object)label).equals(oo.label))&&(((Object)keys).equals(oo.keys))&&(((Object)body).equals(oo.body)); } /** Field Class for HTTPResp.ver */ public static class ver extends edu.neu.ccs.demeterf.control.Fields.any{} /** Field Class for HTTPResp.resp */ public static class resp extends edu.neu.ccs.demeterf.control.Fields.any{} /** Field Class for HTTPResp.label */ public static class label extends edu.neu.ccs.demeterf.control.Fields.any{} /** Field Class for HTTPResp.keys */ public static class keys extends edu.neu.ccs.demeterf.control.Fields.any{} /** Field Class for HTTPResp.body */ public static class body extends edu.neu.ccs.demeterf.control.Fields.any{} /** Basic Response Numbers */ public static final int OK = 200, ERROR = 404; /** Typical HTTP Version */ public static final HTTPVer VER = new HTTPVer(1.0); /** Create a response with the given number, description, headers, and body */ public static HTTPResp create(int resp, String desc, List hds, String body){ return new HTTPResp(VER, resp, new ident(desc), hds.append(new MsgHead("Content-Length",""+body.length())), new ident(body)); } /** Create an OK Response with the given Content-Type/Body */ public static HTTPResp ok(String type, String body){ return create(OK, "OK", commonHeaders(type), body); } /** Common Headers: Date, Content-Type */ private static List commonHeaders(String type){ return List.create( new MsgHead("Date",""+new java.util.Date()), new MsgHead("Content-Type",type)); } /** Create an (empty) Error Response */ public static HTTPResp error(){ return error(""); } /** Create an Error Response with a Body */ public static HTTPResp error(String body){ return create(ERROR, "Not Found", commonHeaders("text/plain"), body); } /** Create a Plain Text Response */ public static HTTPResp textResponse(String text){ return ok("text/plain", text); } /** Create an HTML Response */ public static HTTPResp htmlResponse(String text){ return ok("text/html", text); } /** Is this Response OK? */ public boolean isOK(){ return resp == OK; } /** Is this Response an Error? */ public boolean isError(){ return resp == ERROR; } /** Write a Response to a Socket */ public void toSocket(Socket s){ try{ PrintWriter out = new PrintWriter(s.getOutputStream()); out.print(this.toString()); out.flush(); s.shutdownOutput(); }catch(Exception e){ throw new RuntimeException(e); } } /** Read a Response from a Socket */ public static HTTPResp fromSocket(Socket s){ try{ HTTPResp resp = fromInputStream(s.getInputStream()); s.shutdownInput(); return resp; }catch(Exception e){ throw new RuntimeException(e); } } /** Read a Response from an Input Stream */ public static HTTPResp fromInputStream(InputStream inpt){ try{ BufferedReader in = new BufferedReader(new InputStreamReader(inpt)); String first = in.readLine(); HTTPVer v = HTTPVer.parse(first); first = first.substring(first.indexOf(' ')+1); int b = first.indexOf(' '), rnum = Integer.parseInt(first.substring(0,b)); return new HTTPResp(v, rnum, new ident(first.substring(b+1)), HTTPReq.parseMsgHeads(in), HTTPReq.parseBody(in)); }catch(Exception e){ throw new RuntimeException(e); } } /** Get the body of this Response */ public String getBody(){ return ""+body; } /** Return the headers (key/value Map) of this Response */ public Map getHeaders(){ return HTTPReq.getHeaders(keys); } /** DGP method from Class PrintToString */ public String toString(){ return http.gen.PrintToString.PrintToStringM(this); } }