1 7 package org.jboss.remoting.transport.web; 8 9 import java.io.ByteArrayOutputStream ; 10 import java.io.IOException ; 11 import java.io.ObjectOutputStream ; 12 import java.util.Map ; 13 import org.jboss.remoting.InvocationRequest; 14 import org.jboss.remoting.InvokerLocator; 15 import org.jboss.remoting.ServerInvoker; 16 17 20 public abstract class WebServerInvoker extends ServerInvoker 21 { 22 public static String BINARY = "application/octet-stream"; 23 24 public static String HEADER_SESSION_ID = "sessionId"; 26 public static String HEADER_SUBSYSTEM = "subsystem"; 27 28 29 public WebServerInvoker(InvokerLocator locator) 30 { 31 super(locator); 32 } 33 34 public WebServerInvoker(InvokerLocator locator, Map configuration) 35 { 36 super(locator, configuration); 37 } 38 39 45 public boolean isTransportBiDirectional() 46 { 47 return false; 48 } 49 50 51 protected boolean isBinary(String requestContentType) 52 { 53 if(BINARY.equalsIgnoreCase(requestContentType)) 54 { 55 return true; 56 } 57 else 58 { 59 return false; 60 } 61 } 62 63 protected InvocationRequest getInvocationRequest(Map metadata, Object obj) 64 { 65 InvocationRequest request = null; 66 67 if(obj instanceof InvocationRequest) 68 { 69 request = (InvocationRequest) obj; 70 if(request.getRequestPayload() == null) 71 { 72 request.setRequestPayload(metadata); 73 } 74 else 75 { 76 request.getRequestPayload().putAll(metadata); 77 } 78 } 79 else 80 { 81 request = createNewInvocationRequest(metadata, obj); 82 } 83 return request; 84 } 85 86 protected InvocationRequest createNewInvocationRequest(Map metadata, Object payload) 87 { 88 String sessionId = getSessionId(metadata); 90 String subSystem = (String ) metadata.get(HEADER_SUBSYSTEM); 91 92 InvocationRequest request = new InvocationRequest(sessionId, subSystem, payload, metadata, null, null); 93 94 return request; 95 } 96 97 protected String getSessionId(Map metadata) 98 { 99 String sessionId = (String ) metadata.get(HEADER_SESSION_ID); 100 101 if(sessionId == null || sessionId.length() == 0) 102 { 103 String userAgent = (String ) metadata.get("User-Agent"); 104 String host = (String ) metadata.get("Host"); 105 String idSeed = userAgent + ":" + host; 106 sessionId = Integer.toString(idSeed.hashCode()); 107 } 108 109 return sessionId; 110 } 111 112 119 protected int getContentLength(Object response) throws IOException 120 121 { 122 ByteArrayOutputStream bos = new ByteArrayOutputStream (); 123 ObjectOutputStream oos = new ObjectOutputStream (bos); 124 oos.writeObject(response); 125 oos.flush(); 126 bos.flush(); 127 byte buffer[] = bos.toByteArray(); 128 return buffer.length; 129 } 130 131 } | Popular Tags |