~ubuntu-branches/ubuntu/raring/libjboss-remoting-java/raring

« back to all changes in this revision

Viewing changes to src/org/jboss/remoting/transport/web/WebServerInvoker.java

  • Committer: Package Import Robot
  • Author(s): Torsten Werner
  • Date: 2011-09-09 14:01:03 UTC
  • mfrom: (1.1.6 upstream)
  • Revision ID: package-import@ubuntu.com-20110909140103-hqokx61534tas9rg
Tags: 2.5.3.SP1-1
* Newer but not newest upstream release. Do not build samples.
* Change debian/watch to upstream's svn repo.
* Add patch to fix compile error caused by tomcat update.
  (Closes: #628303)
* Switch to source format 3.0.
* Switch to debhelper level 7.
* Remove useless Depends.
* Update Standards-Version: 3.9.2.
* Update README.source.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
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.
6
 
*
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.
11
 
*
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.
16
 
*
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.
21
 
*/
22
 
 
23
 
package org.jboss.remoting.transport.web;
24
 
 
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;
31
 
import java.util.Map;
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;
41
 
 
42
 
/**
43
 
 * @author <a href="mailto:tom.elrod@jboss.com">Tom Elrod</a>
44
 
 */
45
 
public abstract class WebServerInvoker extends ServerInvoker
46
 
{
47
 
   // header constants
48
 
   public static String HEADER_SESSION_ID = "sessionId";
49
 
   public static String HEADER_SUBSYSTEM = "subsystem";
50
 
 
51
 
 
52
 
   public WebServerInvoker(InvokerLocator locator)
53
 
   {
54
 
      super(locator);
55
 
   }
56
 
 
57
 
   public WebServerInvoker(InvokerLocator locator, Map configuration)
58
 
   {
59
 
      super(locator, configuration);
60
 
   }
61
 
 
62
 
   /**
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).
65
 
    *
66
 
    * @return false (HTTP is unidirrectional)
67
 
    */
68
 
   public boolean isTransportBiDirectional()
69
 
   {
70
 
      return false;
71
 
   }
72
 
 
73
 
   protected String getDefaultDataType()
74
 
   {
75
 
      return HTTPMarshaller.DATATYPE;
76
 
   }
77
 
 
78
 
   protected InvocationRequest getInvocationRequest(Map metadata, Object obj)
79
 
   {
80
 
      InvocationRequest request = null;
81
 
 
82
 
      if(obj instanceof InvocationRequest)
83
 
      {
84
 
         request = (InvocationRequest) obj;
85
 
         if(request.getRequestPayload() == null)
86
 
         {
87
 
            request.setRequestPayload(metadata);
88
 
         }
89
 
         else
90
 
         {
91
 
            request.getRequestPayload().putAll(metadata);
92
 
         }
93
 
      }
94
 
      else
95
 
      {
96
 
         request = createNewInvocationRequest(metadata, obj);
97
 
      }
98
 
      return request;
99
 
   }
100
 
 
101
 
   public UnMarshaller getUnMarshaller()
102
 
   {
103
 
      ClassLoader classLoader = getClassLoader(WebServerInvoker.class);
104
 
      Map map = passConfigMapToMarshalFactory ? configuration : null;
105
 
      UnMarshaller unmarshaller = MarshalFactory.getUnMarshaller(getLocator(), classLoader, map);
106
 
      if(unmarshaller == null)
107
 
      {
108
 
         unmarshaller = MarshalFactory.getUnMarshaller(getDataType(), getSerializationType());
109
 
      }
110
 
      return unmarshaller;
111
 
   }
112
 
 
113
 
   public Marshaller getMarshaller()
114
 
   {
115
 
      ClassLoader classLoader = getClassLoader(WebServerInvoker.class);
116
 
      Map map = passConfigMapToMarshalFactory ? configuration : null;
117
 
      Marshaller marshaller = MarshalFactory.getMarshaller(getLocator(), classLoader, map);
118
 
      if(marshaller == null)
119
 
      {
120
 
         marshaller = MarshalFactory.getMarshaller(getDataType(), getSerializationType());
121
 
      }
122
 
      return marshaller;
123
 
   }
124
 
 
125
 
 
126
 
   protected InvocationRequest createNewInvocationRequest(Map metadata, Object payload)
127
 
   {
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);
131
 
 
132
 
      InvocationRequest request = null;
133
 
      Map responseMap = new HashMap();
134
 
      boolean isLeasQuery = checkForLeaseQuery(metadata);
135
 
      if(isLeasQuery)
136
 
      {
137
 
         addLeaseInfo(responseMap);
138
 
         request = new CreatedInvocationRequest(sessionId, subSystem, "$PING$", null, responseMap, null);
139
 
      }
140
 
      else
141
 
      {
142
 
         request = new CreatedInvocationRequest(sessionId, subSystem, payload, metadata, null, null);
143
 
      }
144
 
      request.setReturnPayload(responseMap);
145
 
      return request;
146
 
   }
147
 
 
148
 
   private boolean checkForLeaseQuery(Map headers)
149
 
   {
150
 
      boolean isLeaseQuery = false;
151
 
 
152
 
         if(headers != null)
153
 
         {
154
 
            Object val = headers.get(HTTPMetadataConstants.REMOTING_LEASE_QUERY);
155
 
            if(val != null && val instanceof String)
156
 
            {
157
 
               isLeaseQuery = Boolean.valueOf((String)val).booleanValue();
158
 
            }
159
 
            else
160
 
            {
161
 
               val = headers.get(HTTPMetadataConstants.REMOTING_LEASE_QUERY_LOWER_CASE);
162
 
               if(val != null && val instanceof String)
163
 
               {
164
 
                  isLeaseQuery = Boolean.valueOf((String)val).booleanValue();
165
 
               }
166
 
            }
167
 
         }
168
 
      return isLeaseQuery;
169
 
   }
170
 
 
171
 
   private void addLeaseInfo(Map response)
172
 
   {
173
 
      boolean leaseManagement = isLeaseActivated();
174
 
      response.put("LEASING_ENABLED", new Boolean(leaseManagement));
175
 
 
176
 
      if(leaseManagement)
177
 
      {
178
 
         long leasePeriod = getLeasePeriod();
179
 
         response.put("LEASE_PERIOD", new Long(leasePeriod));
180
 
      }
181
 
   }
182
 
 
183
 
 
184
 
 
185
 
   protected String getSessionId(Map metadata)
186
 
   {
187
 
      String sessionId = (String) metadata.get(HEADER_SESSION_ID);
188
 
 
189
 
      if(sessionId == null || sessionId.length() == 0)
190
 
      {
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());
195
 
      }
196
 
 
197
 
      return sessionId;
198
 
   }
199
 
 
200
 
   /**
201
 
    * Will write out the object to byte array and check size of byte array. This is VERY expensive, but need for the
202
 
    * content length.
203
 
    *
204
 
    * @param response
205
 
    * @return
206
 
    */
207
 
   protected int getContentLength(Object response) throws IOException
208
 
 
209
 
   {
210
 
      if(response != null)
211
 
      {
212
 
         /**
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).
218
 
          */
219
 
         if(response instanceof String)
220
 
         {
221
 
            return ((String) response).length();
222
 
         }
223
 
         else
224
 
         {
225
 
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
226
 
            ObjectOutputStream oos = new ObjectOutputStream(bos);
227
 
            oos.writeObject(response);
228
 
            oos.flush();
229
 
            bos.flush();
230
 
            byte buffer[] = bos.toByteArray();
231
 
            return buffer.length;
232
 
         }
233
 
      }
234
 
      else
235
 
      {
236
 
         return 0;
237
 
      }
238
 
   }
239
 
 
240
 
   static protected class CreatedInvocationRequest extends InvocationRequest
241
 
   {
242
 
      public CreatedInvocationRequest(String sessionId, String subsystem, Object arg, Map requestPayload, Map returnPayload, InvokerLocator locator)
243
 
      {
244
 
         super(sessionId, subsystem, arg, requestPayload, returnPayload, locator);
245
 
      }
246
 
   }
247
 
   
248
 
   static private ClassLoader getClassLoader(final Class c)
249
 
   {
250
 
      if (SecurityUtility.skipAccessControl())
251
 
      {
252
 
         return c.getClassLoader();
253
 
      }
254
 
 
255
 
      return (ClassLoader)AccessController.doPrivileged( new PrivilegedAction()
256
 
      {
257
 
         public Object run()
258
 
         {
259
 
            return c.getClassLoader();
260
 
         }
261
 
      });
262
 
   }
263
 
}
 
 
b'\\ No newline at end of file'