Author: ron.sigal(a)jboss.com
Date: 2009-01-17 23:38:58 -0500 (Sat, 17 Jan 2009)
New Revision: 4832
Modified:
remoting2/branches/2.2/src/main/org/jboss/remoting/transport/servlet/ServletServerInvoker.java
Log:
JBREM-1079: (1) If "createUniqueObjectName" is "true", uses
super.getMBeanObjectName(); (2) if invocation comes from Remoting client and wasn't a
lease query, wraps response in an InvocationResponse; (3) doesn't send 204 to Remoting
client unless method was HEAD
Modified:
remoting2/branches/2.2/src/main/org/jboss/remoting/transport/servlet/ServletServerInvoker.java
===================================================================
---
remoting2/branches/2.2/src/main/org/jboss/remoting/transport/servlet/ServletServerInvoker.java 2009-01-18
04:22:42 UTC (rev 4831)
+++
remoting2/branches/2.2/src/main/org/jboss/remoting/transport/servlet/ServletServerInvoker.java 2009-01-18
04:38:58 UTC (rev 4832)
@@ -60,25 +60,74 @@
*/
public class ServletServerInvoker extends WebServerInvoker implements
ServletServerInvokerMBean
{
+ public static final String UNWRAP_SINGLETON_ARRAYS =
"unwrapSingletonArrays";
+ public static final String CREATE_UNIQUE_OBJECT_NAME =
"createUniqueObjectName";
+
private static final Logger log = Logger.getLogger(ServletServerInvoker.class);
+
+ private boolean unwrapSingletonArrays;
+ private boolean createUniqueObjectName;
public ServletServerInvoker(InvokerLocator locator)
{
super(locator);
+ init();
}
public ServletServerInvoker(InvokerLocator locator, Map configuration)
{
super(locator, configuration);
+ init();
}
protected String getDefaultDataType()
{
return HTTPMarshaller.DATATYPE;
}
-
+
+ protected void init()
+ {
+ Object val = configuration.get(UNWRAP_SINGLETON_ARRAYS);
+ if (val != null)
+ {
+ try
+ {
+ unwrapSingletonArrays = Boolean.valueOf((String)val).booleanValue();
+ log.debug(this + " setting unwrapSingletonArrays to " +
unwrapSingletonArrays);
+ }
+ catch (Exception e)
+ {
+ log.warn(this + " could not convert " +
+ UNWRAP_SINGLETON_ARRAYS + " value of " +
+ val + " to a boolean value.");
+ }
+ }
+
+ val = configuration.get(CREATE_UNIQUE_OBJECT_NAME);
+ if (val != null)
+ {
+ try
+ {
+ createUniqueObjectName = Boolean.valueOf((String)val).booleanValue();
+ log.debug(this + " setting createUniqueObjectName to " +
createUniqueObjectName);
+ }
+ catch (Exception e)
+ {
+ log.warn(this + " could not convert " +
+ CREATE_UNIQUE_OBJECT_NAME + " value of " +
+ val + " to a boolean value.");
+ }
+ }
+ }
+
public String getMBeanObjectName()
{
+ if (createUniqueObjectName)
+ {
+ log.trace("returning (from super) " + super.getMBeanObjectName());
+ return super.getMBeanObjectName();
+ }
+ log.trace("returning (from here)
jboss.remoting:service=invoker,transport=servlet");
return "jboss.remoting:service=invoker,transport=servlet";
}
@@ -96,7 +145,36 @@
}
Map urlParams = request.getParameterMap();
- metadata.putAll(urlParams);
+ if (unwrapSingletonArrays)
+ {
+ Iterator it = urlParams.keySet().iterator();
+ while (it.hasNext())
+ {
+ Object key = it.next();
+ Object value = urlParams.get(key);
+ String[] valueArray = (String[]) value;
+ if (valueArray.length == 1)
+ {
+ value = valueArray[0];
+ }
+ metadata.put(key, value);
+ }
+ }
+ else
+ {
+ metadata.putAll(urlParams);
+ }
+
+ if(log.isTraceEnabled())
+ {
+ log.trace("metadata:");
+ Iterator it = metadata.keySet().iterator();
+ while (it.hasNext())
+ {
+ Object key = it.next();
+ log.trace(" " + key + ": " + metadata.get(key));
+ }
+ }
String requestContentType = request.getContentType();
@@ -173,6 +251,14 @@
throws ServletException, IOException
{
byte[] retval = new byte[0];
+
+ // Check if client is HTTPClientInvoker
+ boolean isRemotingUserAgent = false;
+ String userAgent = request.getHeader(HTTPMetadataConstants.REMOTING_USER_AGENT);
+ if (userAgent != null)
+ {
+ isRemotingUserAgent = userAgent.startsWith("JBossRemoting");
+ }
Map metadata = new HashMap();
@@ -186,61 +272,88 @@
}
Map urlParams = request.getParameterMap();
- metadata.putAll(urlParams);
+ if (unwrapSingletonArrays)
+ {
+ Iterator it = urlParams.keySet().iterator();
+ while (it.hasNext())
+ {
+ Object key = it.next();
+ Object value = urlParams.get(key);
+ String[] valueArray = (String[]) value;
+ if (valueArray.length == 1)
+ {
+ value = valueArray[0];
+ }
+ metadata.put(key, value);
+ }
+ }
+ else
+ {
+ metadata.putAll(urlParams);
+ }
metadata.put(HTTPMetadataConstants.METHODTYPE, request.getMethod());
- metadata.put(HTTPMetadataConstants.PATH, request.getPathTranslated());
+ String path = request.getPathTranslated();
+ if (path != null)
+ metadata.put(HTTPMetadataConstants.PATH, path);
String requestContentType = request.getContentType();
try
{
- Object invocationResponse = null;
-
- ServletInputStream inputStream = request.getInputStream();
- UnMarshaller unmarshaller = getUnMarshaller();
- Object obj = null;
- if (unmarshaller instanceof VersionedUnMarshaller)
- obj = ((VersionedUnMarshaller)unmarshaller).read(new
ByteArrayInputStream(requestByte), metadata, Version.getDefaultVersion());
- else
- obj = unmarshaller.read(new ByteArrayInputStream(requestByte), metadata);
- inputStream.close();
-
+ InvocationRequest invocationRequest = null;
+ Object responseObject = null;
boolean isError = false;
- InvocationRequest invocationRequest = null;
- if(obj instanceof InvocationRequest)
+ String method = request.getMethod();
+ if (method.equals("GET") || method.equals("HEAD") ||
(method.equals("OPTIONS") && request.getContentLength() <= 0))
{
- invocationRequest = (InvocationRequest) obj;
+ invocationRequest = createNewInvocationRequest(metadata, null);
}
else
{
- if(WebUtil.isBinary(requestContentType))
+ ServletInputStream inputStream = request.getInputStream();
+ UnMarshaller unmarshaller = getUnMarshaller();
+ Object obj = null;
+ if (unmarshaller instanceof VersionedUnMarshaller)
+ obj = ((VersionedUnMarshaller)unmarshaller).read(new
ByteArrayInputStream(requestByte), metadata, Version.getDefaultVersion());
+ else
+ obj = unmarshaller.read(new ByteArrayInputStream(requestByte), metadata);
+ inputStream.close();
+
+ if(obj instanceof InvocationRequest)
{
- invocationRequest = getInvocationRequest(metadata, obj);
+ invocationRequest = (InvocationRequest) obj;
}
else
{
- invocationRequest = createNewInvocationRequest(metadata, obj);
+ if(WebUtil.isBinary(requestContentType))
+ {
+ invocationRequest = getInvocationRequest(metadata, obj);
+ }
+ else
+ {
+ invocationRequest = createNewInvocationRequest(metadata, obj);
+ }
}
}
try
{
// call transport on the subclass, get the result to handback
- invocationResponse = invoke(invocationRequest);
+ responseObject = invoke(invocationRequest);
}
catch(Throwable ex)
{
log.debug("Error thrown calling invoke on server invoker.", ex);
- invocationResponse = ex;
+ responseObject = ex;
if (checkForExceptionReturn(metadata))
{
String sessionId = invocationRequest.getSessionId();
ServletThrowable st = new ServletThrowable(ex);
- invocationResponse = new InvocationResponse(sessionId, st, true, null);
+ responseObject = new InvocationResponse(sessionId, st, true, null);
}
else
{
@@ -249,16 +362,19 @@
}
//Start with response code of 204 (no content), then if is a return from
handler, change to 200 (ok)
- int status = 204;
- if(invocationResponse != null)
+ int status = 200;
+ if(responseObject != null)
{
if(isError)
{
- response.sendError(500, "Error occurred processing invocation
request. ");
+ status = 500;
}
- else
+ }
+ else
+ {
+ if (!isRemotingUserAgent || "HEAD".equals(request.getMethod()))
{
- status = 200;
+ status = 204;
}
}
@@ -284,21 +400,25 @@
- // can't set message anymore as is depricated
+ // can't set message anymore as is deprecated
response.setStatus(status);
+
+ if (isRemotingUserAgent && !(invocationRequest instanceof
CreatedInvocationRequest))
+ {
+ responseObject = new InvocationResponse(invocationRequest.getSessionId(),
+ responseObject, isError,
responseMap);
+ }
- if(invocationResponse != null)
+ if(responseObject != null)
{
- String responseContentType = invocationResponse == null ? requestContentType
: WebUtil.getContentType(invocationResponse);
+ String responseContentType = responseObject == null ? requestContentType :
WebUtil.getContentType(responseObject);
response.setContentType(responseContentType);
- //int iContentLength = getContentLength(invocationResponse);
- //response.setContentLength(iContentLength);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
Marshaller marshaller = getMarshaller();
if (marshaller instanceof VersionedMarshaller)
- ((VersionedMarshaller) marshaller).write(invocationResponse, outputStream,
Version.getDefaultVersion());
+ ((VersionedMarshaller) marshaller).write(responseObject, outputStream,
Version.getDefaultVersion());
else
- marshaller.write(invocationResponse, outputStream);
+ marshaller.write(responseObject, outputStream);
retval = outputStream.toByteArray();
response.setContentLength(retval.length);
}