1 4 package nl.justobjects.pushlet.core; 5 6 import nl.justobjects.pushlet.util.PushletException; 7 import nl.justobjects.pushlet.util.Servlets; 8 9 import javax.servlet.http.HttpServletRequest ; 10 import javax.servlet.http.HttpServletResponse ; 11 12 18 public class Command implements Protocol { 19 20 21 public final Event reqEvent; 22 23 24 private Event rspEvent; 25 26 27 public final HttpServletRequest httpReq; 28 29 30 public final HttpServletResponse httpRsp; 31 32 33 public final Session session; 34 35 36 private ClientAdapter clientAdapter; 37 38 39 private Command(Session aSession, Event aRequestEvent, HttpServletRequest aHTTPReq, HttpServletResponse aHTTPRsp) { 40 session = aSession; 41 reqEvent = aRequestEvent; 42 httpReq = aHTTPReq; 43 httpRsp = aHTTPRsp; 44 } 45 46 47 public static Command create(Session aSession, Event aReqEvent, HttpServletRequest aHTTPReq, HttpServletResponse aHTTPRsp) { 48 return new Command(aSession, aReqEvent, aHTTPReq, aHTTPRsp); 49 } 50 51 52 public void setResponseEvent(Event aResponseEvent) { 53 rspEvent = aResponseEvent; 54 } 55 56 57 public Event getResponseEvent() { 58 return rspEvent; 59 } 60 61 62 public ClientAdapter getClientAdapter() throws PushletException { 63 if (clientAdapter == null) { 64 clientAdapter = createClientAdapter(); 66 } 67 return clientAdapter; 68 } 69 70 71 protected ClientAdapter createClientAdapter() throws PushletException { 72 73 String outputFormat = session.getFormat(); 75 76 if (outputFormat.equals(FORMAT_JAVASCRIPT)) { 78 return new BrowserAdapter(httpRsp); 80 } else if (outputFormat.equals(FORMAT_SERIALIZED_JAVA_OBJECT)) { 81 return new SerializedAdapter(httpRsp); 83 } else if (outputFormat.equals(FORMAT_XML)) { 84 return new XMLAdapter(httpRsp); 86 } else { 87 throw new PushletException("Null or invalid output format: " + outputFormat); 88 } 89 } 90 91 92 protected void sendResponseHeaders() { 93 Servlets.setNoCacheHeaders(httpRsp); 95 96 if (session.getUserAgent().indexOf("java") > 0) { 98 httpRsp.setHeader("Connection", "close"); 108 } 109 } 110 111 112 } 113 114 124 | Popular Tags |