2
* JBoss, Home of Professional Open Source
3
* Copyright 2005, JBoss Inc., and individual contributors as indicated
4
* by the @authors tag. See the copyright.txt in the distribution for a
5
* full listing of individual contributors.
7
* This is free software; you can redistribute it and/or modify it
8
* under the terms of the GNU Lesser General Public License as
9
* published by the Free Software Foundation; either version 2.1 of
10
* the License, or (at your option) any later version.
12
* This software is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15
* Lesser General Public License for more details.
17
* You should have received a copy of the GNU Lesser General Public
18
* License along with this software; if not, write to the Free
19
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
23
package org.jboss.remoting.transport.web;
25
import java.io.ByteArrayOutputStream;
26
import java.io.IOException;
27
import java.io.ObjectOutputStream;
28
import java.security.AccessController;
29
import java.security.PrivilegedAction;
30
import java.util.HashMap;
32
import org.jboss.remoting.InvocationRequest;
33
import org.jboss.remoting.InvokerLocator;
34
import org.jboss.remoting.ServerInvoker;
35
import org.jboss.remoting.transport.http.HTTPMetadataConstants;
36
import org.jboss.remoting.util.SecurityUtility;
37
import org.jboss.remoting.marshal.MarshalFactory;
38
import org.jboss.remoting.marshal.Marshaller;
39
import org.jboss.remoting.marshal.UnMarshaller;
40
import org.jboss.remoting.marshal.http.HTTPMarshaller;
43
* @author <a href="mailto:tom.elrod@jboss.com">Tom Elrod</a>
45
public abstract class WebServerInvoker extends ServerInvoker
48
public static String HEADER_SESSION_ID = "sessionId";
49
public static String HEADER_SUBSYSTEM = "subsystem";
52
public WebServerInvoker(InvokerLocator locator)
57
public WebServerInvoker(InvokerLocator locator, Map configuration)
59
super(locator, configuration);
63
* returns true if the transport is bi-directional in nature, for example, HTTP in unidirectional and SOCKETs are
64
* bi-directional (unless behind a firewall for example).
66
* @return false (HTTP is unidirrectional)
68
public boolean isTransportBiDirectional()
73
protected String getDefaultDataType()
75
return HTTPMarshaller.DATATYPE;
78
protected InvocationRequest getInvocationRequest(Map metadata, Object obj)
80
InvocationRequest request = null;
82
if(obj instanceof InvocationRequest)
84
request = (InvocationRequest) obj;
85
if(request.getRequestPayload() == null)
87
request.setRequestPayload(metadata);
91
request.getRequestPayload().putAll(metadata);
96
request = createNewInvocationRequest(metadata, obj);
101
public UnMarshaller getUnMarshaller()
103
ClassLoader classLoader = getClassLoader(WebServerInvoker.class);
104
Map map = passConfigMapToMarshalFactory ? configuration : null;
105
UnMarshaller unmarshaller = MarshalFactory.getUnMarshaller(getLocator(), classLoader, map);
106
if(unmarshaller == null)
108
unmarshaller = MarshalFactory.getUnMarshaller(getDataType(), getSerializationType());
113
public Marshaller getMarshaller()
115
ClassLoader classLoader = getClassLoader(WebServerInvoker.class);
116
Map map = passConfigMapToMarshalFactory ? configuration : null;
117
Marshaller marshaller = MarshalFactory.getMarshaller(getLocator(), classLoader, map);
118
if(marshaller == null)
120
marshaller = MarshalFactory.getMarshaller(getDataType(), getSerializationType());
126
protected InvocationRequest createNewInvocationRequest(Map metadata, Object payload)
128
// will try to use the same session id if possible to track
129
String sessionId = getSessionId(metadata);
130
String subSystem = (String) metadata.get(HEADER_SUBSYSTEM);
132
InvocationRequest request = null;
133
Map responseMap = new HashMap();
134
boolean isLeasQuery = checkForLeaseQuery(metadata);
137
addLeaseInfo(responseMap);
138
request = new CreatedInvocationRequest(sessionId, subSystem, "$PING$", null, responseMap, null);
142
request = new CreatedInvocationRequest(sessionId, subSystem, payload, metadata, null, null);
144
request.setReturnPayload(responseMap);
148
private boolean checkForLeaseQuery(Map headers)
150
boolean isLeaseQuery = false;
154
Object val = headers.get(HTTPMetadataConstants.REMOTING_LEASE_QUERY);
155
if(val != null && val instanceof String)
157
isLeaseQuery = Boolean.valueOf((String)val).booleanValue();
161
val = headers.get(HTTPMetadataConstants.REMOTING_LEASE_QUERY_LOWER_CASE);
162
if(val != null && val instanceof String)
164
isLeaseQuery = Boolean.valueOf((String)val).booleanValue();
171
private void addLeaseInfo(Map response)
173
boolean leaseManagement = isLeaseActivated();
174
response.put("LEASING_ENABLED", new Boolean(leaseManagement));
178
long leasePeriod = getLeasePeriod();
179
response.put("LEASE_PERIOD", new Long(leasePeriod));
185
protected String getSessionId(Map metadata)
187
String sessionId = (String) metadata.get(HEADER_SESSION_ID);
189
if(sessionId == null || sessionId.length() == 0)
191
String userAgent = (String) metadata.get("User-Agent");
192
String host = (String) metadata.get("Host");
193
String idSeed = userAgent + ":" + host;
194
sessionId = Integer.toString(idSeed.hashCode());
201
* Will write out the object to byte array and check size of byte array. This is VERY expensive, but need for the
207
protected int getContentLength(Object response) throws IOException
213
* Am checking to see if type String because:
214
* 1. faster to just get the length compared to doing serialization
215
* 2. doing serialization adds extra bytes, so the value calculated is larger
216
* than the actual number of characters (and causes the client to wait for the
217
* extra characters that it will never get).
219
if(response instanceof String)
221
return ((String) response).length();
225
ByteArrayOutputStream bos = new ByteArrayOutputStream();
226
ObjectOutputStream oos = new ObjectOutputStream(bos);
227
oos.writeObject(response);
230
byte buffer[] = bos.toByteArray();
231
return buffer.length;
240
static protected class CreatedInvocationRequest extends InvocationRequest
242
public CreatedInvocationRequest(String sessionId, String subsystem, Object arg, Map requestPayload, Map returnPayload, InvokerLocator locator)
244
super(sessionId, subsystem, arg, requestPayload, returnPayload, locator);
248
static private ClassLoader getClassLoader(final Class c)
250
if (SecurityUtility.skipAccessControl())
252
return c.getClassLoader();
255
return (ClassLoader)AccessController.doPrivileged( new PrivilegedAction()
259
return c.getClassLoader();
b'\\ No newline at end of file'