KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > remoting > transport > web > WebServerInvoker


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.remoting.transport.web;
8
9 import java.io.ByteArrayOutputStream JavaDoc;
10 import java.io.IOException JavaDoc;
11 import java.io.ObjectOutputStream JavaDoc;
12 import java.util.Map JavaDoc;
13 import org.jboss.remoting.InvocationRequest;
14 import org.jboss.remoting.InvokerLocator;
15 import org.jboss.remoting.ServerInvoker;
16
17 /**
18  * @author <a HREF="mailto:tom.elrod@jboss.com">Tom Elrod</a>
19  */

20 public abstract class WebServerInvoker extends ServerInvoker
21 {
22    public static String JavaDoc BINARY = "application/octet-stream";
23
24    // header constants
25
public static String JavaDoc HEADER_SESSION_ID = "sessionId";
26    public static String JavaDoc HEADER_SUBSYSTEM = "subsystem";
27
28
29    public WebServerInvoker(InvokerLocator locator)
30    {
31       super(locator);
32    }
33
34    public WebServerInvoker(InvokerLocator locator, Map JavaDoc configuration)
35    {
36       super(locator, configuration);
37    }
38
39    /**
40     * returns true if the transport is bi-directional in nature, for example, HTTP in unidirectional and SOCKETs are
41     * bi-directional (unless behind a firewall for example).
42     *
43     * @return false (HTTP is unidirrectional)
44     */

45    public boolean isTransportBiDirectional()
46    {
47       return false;
48    }
49
50
51    protected boolean isBinary(String JavaDoc 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 JavaDoc metadata, Object JavaDoc 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 JavaDoc metadata, Object JavaDoc payload)
87    {
88       // will try to use the same session id if possible to track
89
String JavaDoc sessionId = getSessionId(metadata);
90       String JavaDoc subSystem = (String JavaDoc) 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 JavaDoc getSessionId(Map JavaDoc metadata)
98    {
99       String JavaDoc sessionId = (String JavaDoc) metadata.get(HEADER_SESSION_ID);
100
101       if(sessionId == null || sessionId.length() == 0)
102       {
103          String JavaDoc userAgent = (String JavaDoc) metadata.get("User-Agent");
104          String JavaDoc host = (String JavaDoc) metadata.get("Host");
105          String JavaDoc idSeed = userAgent + ":" + host;
106          sessionId = Integer.toString(idSeed.hashCode());
107       }
108
109       return sessionId;
110    }
111
112    /**
113     * Will write out the object to byte array and check size of byte array. This is VERY expensive, but need for the
114     * content length.
115     *
116     * @param response
117     * @return
118     */

119    protected int getContentLength(Object JavaDoc response) throws IOException JavaDoc
120
121    {
122       ByteArrayOutputStream JavaDoc bos = new ByteArrayOutputStream JavaDoc();
123       ObjectOutputStream JavaDoc oos = new ObjectOutputStream JavaDoc(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