Author: shane.bryzak(a)jboss.com
Date: 2009-12-06 15:35:02 -0500 (Sun, 06 Dec 2009)
New Revision: 11761
Added:
modules/remoting/trunk/src/main/java/org/jboss/seam/remoting/model/ModelHandler.java
Modified:
modules/remoting/trunk/src/main/java/org/jboss/seam/remoting/ExecutionHandler.java
modules/remoting/trunk/src/main/java/org/jboss/seam/remoting/Remoting.java
modules/remoting/trunk/src/main/java/org/jboss/seam/remoting/RequestContext.java
modules/remoting/trunk/src/main/java/org/jboss/seam/remoting/RequestHandler.java
Log:
added model handler
Modified:
modules/remoting/trunk/src/main/java/org/jboss/seam/remoting/ExecutionHandler.java
===================================================================
---
modules/remoting/trunk/src/main/java/org/jboss/seam/remoting/ExecutionHandler.java 2009-12-06
13:24:44 UTC (rev 11760)
+++
modules/remoting/trunk/src/main/java/org/jboss/seam/remoting/ExecutionHandler.java 2009-12-06
20:35:02 UTC (rev 11761)
@@ -18,6 +18,7 @@
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.jboss.seam.remoting.wrapper.Wrapper;
+import org.jboss.weld.conversation.ConversationManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -29,29 +30,17 @@
*/
public class ExecutionHandler implements RequestHandler
{
- private static final Logger log = LoggerFactory
- .getLogger(ExecutionHandler.class);
+ private static final Logger log = LoggerFactory.getLogger(ExecutionHandler.class);
- private static final byte[] HEADER_OPEN = "<header>".getBytes();
- private static final byte[] HEADER_CLOSE = "</header>".getBytes();
- private static final byte[] CONVERSATION_ID_TAG_OPEN =
"<conversationId>"
- .getBytes();
- private static final byte[] CONVERSATION_ID_TAG_CLOSE =
"</conversationId>"
- .getBytes();
-
- private static final byte[] CONTEXT_TAG_OPEN =
"<context>".getBytes();
- private static final byte[] CONTEXT_TAG_CLOSE =
"</context>".getBytes();
-
@Inject BeanManager beanManager;
+ @Inject ConversationManager conversationManager;
@Inject Conversation conversation;
/**
* The entry point for handling a request.
*
- * @param request
- * HttpServletRequest
- * @param response
- * HttpServletResponse
+ * @param request HttpServletRequest
+ * @param response HttpServletResponse
* @throws Exception
*/
public void handle(HttpServletRequest request,
@@ -78,12 +67,12 @@
SAXReader xmlReader = new SAXReader();
Document doc = xmlReader.read(new StringReader(requestData));
final Element env = doc.getRootElement();
- final RequestContext ctx = unmarshalContext(env);
+ final RequestContext ctx = new RequestContext(env.element("header"));
if (!ctx.getConversationId().isEmpty())
{
- // TODO restore the conversation if there is a conversation ID in the context
- //conversation.
+ // this is non portable ;/
+ conversationManager.beginOrRestoreConversation(ctx.getConversationId());
}
// Extract the calls from the request
@@ -103,35 +92,6 @@
}
/**
- * Unmarshals the context from the request envelope header.
- *
- * @param env
- * Element
- * @return RequestContext
- */
- private RequestContext unmarshalContext(Element env)
- {
- RequestContext ctx = new RequestContext();
-
- Element header = env.element("header");
- if (header != null)
- {
- Element context = header.element("context");
- if (context != null)
- {
-
- Element convId = context.element("conversationId");
- if (convId != null)
- {
- ctx.setConversationId(convId.getText());
- }
- }
- }
-
- return ctx;
- }
-
- /**
* Unmarshal the request into a list of Calls.
*
* @param env
Modified: modules/remoting/trunk/src/main/java/org/jboss/seam/remoting/Remoting.java
===================================================================
--- modules/remoting/trunk/src/main/java/org/jboss/seam/remoting/Remoting.java 2009-12-06
13:24:44 UTC (rev 11760)
+++ modules/remoting/trunk/src/main/java/org/jboss/seam/remoting/Remoting.java 2009-12-06
20:35:02 UTC (rev 11761)
@@ -33,9 +33,11 @@
//private static final String REQUEST_PATH_SUBSCRIPTION = "/subscription";
//private static final String REQUEST_PATH_POLL = "/poll";
private static final String REQUEST_PATH_INTERFACE = "/interface.js";
+ private static final String REQUEST_PATH_MODEL = "/model";
@Inject Instance<ExecutionHandler> executionHandlerInstance;
@Inject Instance<InterfaceGenerator> interfaceHandlerInstance;
+ @Inject Instance<ModelHandler> modelHandlerInstance;
public static final int DEFAULT_POLL_TIMEOUT = 10; // 10 seconds
public static final int DEFAULT_POLL_INTERVAL = 1; // 1 second
@@ -214,6 +216,10 @@
{
interfaceHandlerInstance.get().handle(request, response);
}
+ else if (REQUEST_PATH_MODEL.equals(pathInfo))
+ {
+ modelHandlerInstance.get().handle(request, response);
+ }
else
{
Matcher m = pathPattern.matcher(pathInfo);
Modified:
modules/remoting/trunk/src/main/java/org/jboss/seam/remoting/RequestContext.java
===================================================================
---
modules/remoting/trunk/src/main/java/org/jboss/seam/remoting/RequestContext.java 2009-12-06
13:24:44 UTC (rev 11760)
+++
modules/remoting/trunk/src/main/java/org/jboss/seam/remoting/RequestContext.java 2009-12-06
20:35:02 UTC (rev 11761)
@@ -1,11 +1,29 @@
package org.jboss.seam.remoting;
+import org.dom4j.Element;
+
/**
*
* @author Shane Bryzak
*/
public class RequestContext
{
+ public RequestContext(Element header)
+ {
+ if (header != null)
+ {
+ Element context = header.element("context");
+ if (context != null)
+ {
+ Element convId = context.element("conversationId");
+ if (convId != null)
+ {
+ setConversationId(convId.getText());
+ }
+ }
+ }
+ }
+
private String conversationId;
public String getConversationId()
Modified:
modules/remoting/trunk/src/main/java/org/jboss/seam/remoting/RequestHandler.java
===================================================================
---
modules/remoting/trunk/src/main/java/org/jboss/seam/remoting/RequestHandler.java 2009-12-06
13:24:44 UTC (rev 11760)
+++
modules/remoting/trunk/src/main/java/org/jboss/seam/remoting/RequestHandler.java 2009-12-06
20:35:02 UTC (rev 11761)
@@ -18,7 +18,18 @@
static final byte[] REF_TAG_OPEN_START = "<ref id=\"".getBytes();
static final byte[] REF_TAG_OPEN_END = "\">".getBytes();
static final byte[] REF_TAG_CLOSE = "</ref>".getBytes();
+
+
+ static final byte[] HEADER_OPEN = "<header>".getBytes();
+ static final byte[] HEADER_CLOSE = "</header>".getBytes();
+ static final byte[] CONVERSATION_ID_TAG_OPEN = "<conversationId>"
+ .getBytes();
+ static final byte[] CONVERSATION_ID_TAG_CLOSE = "</conversationId>"
+ .getBytes();
+ static final byte[] CONTEXT_TAG_OPEN = "<context>".getBytes();
+ static final byte[] CONTEXT_TAG_CLOSE = "</context>".getBytes();
+
void handle(HttpServletRequest request, HttpServletResponse response)
throws Exception;
}
Added:
modules/remoting/trunk/src/main/java/org/jboss/seam/remoting/model/ModelHandler.java
===================================================================
--- modules/remoting/trunk/src/main/java/org/jboss/seam/remoting/model/ModelHandler.java
(rev 0)
+++
modules/remoting/trunk/src/main/java/org/jboss/seam/remoting/model/ModelHandler.java 2009-12-06
20:35:02 UTC (rev 11761)
@@ -0,0 +1,116 @@
+package org.jboss.seam.remoting.model;
+
+import java.io.ByteArrayOutputStream;
+import java.io.StringReader;
+import java.util.Iterator;
+
+import javax.enterprise.context.Conversation;
+import javax.enterprise.inject.spi.BeanManager;
+import javax.inject.Inject;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.dom4j.Document;
+import org.dom4j.Element;
+import org.dom4j.io.SAXReader;
+import org.jboss.seam.remoting.Call;
+import org.jboss.seam.remoting.RequestContext;
+import org.jboss.seam.remoting.RequestHandler;
+import org.jboss.seam.remoting.wrapper.Wrapper;
+import org.jboss.weld.conversation.ConversationManager;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class ModelHandler implements RequestHandler
+{
+ private static final Logger log = LoggerFactory.getLogger(ModelHandler.class);
+
+ @Inject BeanManager beanManager;
+ @Inject ConversationManager conversationManager;
+ @Inject Conversation conversation;
+ @Inject ModelRegistry registry;
+
+ public void handle(HttpServletRequest request, HttpServletResponse response)
+ throws Exception
+ {
+ response.setContentType("text/xml");
+
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+
+ byte[] buffer = new byte[256];
+ int read = request.getInputStream().read(buffer);
+ while (read != -1)
+ {
+ out.write(buffer, 0, read);
+ read = request.getInputStream().read(buffer);
+ }
+
+ String requestData = new String(out.toByteArray());
+ log.debug("Processing model request: " + requestData);
+
+ SAXReader xmlReader = new SAXReader();
+ Document doc = xmlReader.read(new StringReader(requestData));
+ final Element env = doc.getRootElement();
+ final RequestContext ctx = new RequestContext(env.element("header"));
+
+ if (!ctx.getConversationId().isEmpty())
+ {
+ conversationManager.beginOrRestoreConversation(ctx.getConversationId());
+ }
+
+ Element modelElement = env.element("body").element("model");
+
+ String operation = modelElement.attributeValue("operation");
+ String id = modelElement.attributeValue("id");
+
+ if ("fetch".equals(operation))
+ {
+ Call action = null;
+ if (modelElement.elements("action").size() > 0)
+ {
+ Element actionElement = modelElement.element("action");
+ Element targetElement = actionElement.element("target");
+ Element qualifiersElement = actionElement.element("qualifiers");
+ Element methodElement = actionElement.element("method");
+ Element paramsElement = actionElement.element("params");
+ action = new Call(beanManager, id, targetElement.getTextTrim(),
+ qualifiersElement.getTextTrim(), methodElement.getTextTrim());
+
+ Element refsNode = modelElement.element("refs");
+
+ Iterator<?> iter = refsNode.elementIterator("ref");
+ while (iter.hasNext())
+ {
+ action.getContext().createWrapperFromElement((Element) iter.next());
+ }
+
+ for (Wrapper w : action.getContext().getInRefs().values())
+ {
+ w.unmarshal();
+ }
+
+ iter = paramsElement.elementIterator("param");
+ while (iter.hasNext())
+ {
+ Element param = (Element) iter.next();
+
+ action.addParameter(action.getContext().createWrapperFromElement(
+ (Element) param.elementIterator().next()));
+ }
+ }
+
+ // Unmarshal beans
+
+ // Unmarshal expressions
+
+ if (action != null)
+ {
+ action.execute();
+ }
+
+ }
+
+ // Store the conversation ID in the outgoing context
+ ctx.setConversationId(conversation.getId());
+ }
+}
Show replies by date