Author: chris.laprun(a)jboss.com
Date: 2010-06-30 11:22:03 -0400 (Wed, 30 Jun 2010)
New Revision: 3527
Added:
components/wsrp/trunk/common/src/main/java/org/gatein/wsrp/payload/
components/wsrp/trunk/common/src/main/java/org/gatein/wsrp/payload/PayloadUtils.java
components/wsrp/trunk/common/src/main/java/org/gatein/wsrp/payload/XSDTypeConverter.java
Modified:
components/wsrp/trunk/common/src/main/java/org/gatein/wsrp/WSRPTypeFactory.java
components/wsrp/trunk/common/src/main/java/org/gatein/wsrp/WSRPUtils.java
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/NavigationalStateUpdatingHandler.java
components/wsrp/trunk/producer/src/main/java/org/gatein/wsrp/producer/EventRequestProcessor.java
Log:
- GTNWSRP-39, GTNWSRP-49:
+ Extracted payload handling to PayloadUtils.
+ Removed use of NamedStringArray as it is currently not obvious how to make them fit
with JSR-286 (might have to revisit later).
+ Now should "properly" marshall Serializable to XML (though it needs to be
tested and interoperability is a question).
Modified: components/wsrp/trunk/common/src/main/java/org/gatein/wsrp/WSRPTypeFactory.java
===================================================================
---
components/wsrp/trunk/common/src/main/java/org/gatein/wsrp/WSRPTypeFactory.java 2010-06-30
12:39:15 UTC (rev 3526)
+++
components/wsrp/trunk/common/src/main/java/org/gatein/wsrp/WSRPTypeFactory.java 2010-06-30
15:22:03 UTC (rev 3527)
@@ -38,6 +38,7 @@
import org.gatein.pc.api.WindowState;
import org.gatein.pc.api.cache.CacheLevel;
import org.gatein.pc.api.spi.PortletInvocationContext;
+import org.gatein.wsrp.payload.PayloadUtils;
import org.gatein.wsrp.spec.v2.ErrorCodes;
import org.oasis.wsrp.v2.BlockingInteractionResponse;
import org.oasis.wsrp.v2.CacheControl;
@@ -1206,15 +1207,18 @@
{
Class<? extends Object> type = payload.getClass();
XmlRootElement annotation = type.getAnnotation(XmlRootElement.class);
+ QName typeName;
if (annotation != null)
{
- event.setType(new QName(annotation.namespace(), annotation.name()));
- event.setPayload(WSRPTypeFactory.createEventPayloadAsAny(payload));
+ typeName = new QName(annotation.namespace(), annotation.name());
}
else
{
- event.setPayload(WSRPTypeFactory.createEventPayloadAsNamedString(payload));
+ // use the java type
+ typeName = new QName(type.getName());
}
+ event.setType(typeName);
+ event.setPayload(PayloadUtils.getPayloadAsEventPayload(typeName, payload));
}
return event;
}
Modified: components/wsrp/trunk/common/src/main/java/org/gatein/wsrp/WSRPUtils.java
===================================================================
--- components/wsrp/trunk/common/src/main/java/org/gatein/wsrp/WSRPUtils.java 2010-06-30
12:39:15 UTC (rev 3526)
+++ components/wsrp/trunk/common/src/main/java/org/gatein/wsrp/WSRPUtils.java 2010-06-30
15:22:03 UTC (rev 3527)
@@ -25,7 +25,6 @@
import com.google.common.base.Function;
import com.google.common.collect.Lists;
-import org.gatein.common.NotYetImplemented;
import org.gatein.common.i18n.LocaleFormat;
import org.gatein.common.net.URLTools;
import org.gatein.common.util.ConversionException;
@@ -41,11 +40,9 @@
import org.gatein.pc.api.state.AccessMode;
import org.gatein.wsrp.registration.LocalizedString;
import org.gatein.wsrp.registration.RegistrationPropertyDescription;
-import org.oasis.wsrp.v2.EventPayload;
import org.oasis.wsrp.v2.InteractionParams;
import org.oasis.wsrp.v2.MarkupParams;
import org.oasis.wsrp.v2.NamedString;
-import org.oasis.wsrp.v2.NamedStringArray;
import org.oasis.wsrp.v2.NavigationalContext;
import org.oasis.wsrp.v2.PropertyDescription;
import org.oasis.wsrp.v2.StateChange;
@@ -53,7 +50,6 @@
import org.slf4j.LoggerFactory;
import javax.servlet.http.HttpServletRequest;
-import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
@@ -568,73 +564,6 @@
return publicNS;
}
- public static Serializable getPayloadAsSerializable(EventPayload payload)
- {
- // GTNWSRP-49
- if (payload == null)
- {
- return null;
- }
-
- // for now only deal with plain Strings...
- Object any = payload.getAny();
- if (any == null)
- {
- NamedStringArray namedStringArray = payload.getNamedStringArray();
- if (namedStringArray != null)
- {
- List<NamedString> namedStrings = namedStringArray.getNamedString();
- if (ParameterValidation.existsAndIsNotEmpty(namedStrings))
- {
- int size = namedStrings.size();
- switch (size)
- {
- case 1:
- return namedStrings.get(0).getValue();
-
- default:
- String processedName = null;
- String[] value = new String[size];
- int i = 0;
- // check that all NamedString have all the same name (multi-valued
object) or fail
- for (NamedString namedString : namedStrings)
- {
- String name = namedString.getName();
- if (processedName != null &&
!name.equals(processedName))
- {
- throw new UnsupportedOperationException("Cannot currently
deal with NamedStringArray whose elements don't all have the same name!");
- }
- processedName = name;
- value[i++] = namedString.getValue();
- }
- return value;
- }
- }
-
- return null;
- }
- else
- {
- return null;
- }
- }
- else
- {
- throw new UnsupportedOperationException("TODO: Cannot deal with non
NamedStringArray payloads at the moment! Got: " + any);
- }
- }
-
- public EventPayload getPayloadAsEventPayload(Serializable payload)
- {
- // todo: complete GTNWSRP-49
- if (payload instanceof String)
- {
- String payloadAsString = (String)payload;
- return WSRPTypeFactory.createEventPayloadAsNamedString(payloadAsString);
- }
- throw new NotYetImplemented();
- }
-
/**
* @author <a href="mailto:chris.laprun@jboss.com">Chris
Laprun</a>
* @version $Revision$
Added:
components/wsrp/trunk/common/src/main/java/org/gatein/wsrp/payload/PayloadUtils.java
===================================================================
--- components/wsrp/trunk/common/src/main/java/org/gatein/wsrp/payload/PayloadUtils.java
(rev 0)
+++
components/wsrp/trunk/common/src/main/java/org/gatein/wsrp/payload/PayloadUtils.java 2010-06-30
15:22:03 UTC (rev 3527)
@@ -0,0 +1,140 @@
+/*
+ * JBoss, a division of Red Hat
+ * Copyright 2010, Red Hat Middleware, LLC, and individual
+ * contributors as indicated by the @authors tag. See the
+ * copyright.txt in the distribution for a full listing of
+ * individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+
+package org.gatein.wsrp.payload;
+
+import org.gatein.common.util.ParameterValidation;
+import org.gatein.wsrp.WSRPTypeFactory;
+import org.oasis.wsrp.v2.EventPayload;
+import org.oasis.wsrp.v2.NamedStringArray;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+
+import javax.xml.XMLConstants;
+import javax.xml.bind.JAXBContext;
+import javax.xml.bind.JAXBElement;
+import javax.xml.bind.Marshaller;
+import javax.xml.bind.Unmarshaller;
+import javax.xml.namespace.QName;
+import javax.xml.parsers.DocumentBuilderFactory;
+import java.io.Serializable;
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * @author <a href="mailto:chris.laprun@jboss.com">Chris
Laprun</a>
+ * @version $Revision$
+ */
+public class PayloadUtils
+{
+ private final static Map<String, XSDTypeConverter> converters = new
HashMap<String, XSDTypeConverter>(19);
+
+ static
+ {
+ XSDTypeConverter[] converterArray = XSDTypeConverter.values();
+ for (XSDTypeConverter converter : converterArray)
+ {
+ converters.put(converter.typeName(), converter);
+ }
+ }
+
+ public static Serializable getPayloadAsSerializable(QName type, EventPayload payload)
+ {
+ // GTNWSRP-49
+ if (payload == null)
+ {
+ return null;
+ }
+
+ ParameterValidation.throwIllegalArgExceptionIfNull(type, "Payload expected
type");
+
+ Object any = payload.getAny();
+ if (any == null)
+ {
+ NamedStringArray namedStringArray = payload.getNamedStringArray();
+ if (namedStringArray != null)
+ {
+ throw new UnsupportedOperationException("Don't know how to convert
NamedStringArray to a Serializable in an interroperable way. :(");
+ }
+ else
+ {
+ return null;
+ }
+ }
+ else
+ {
+ Element element = (Element)any;
+ String typeName = type.getLocalPart();
+
+ if (XMLConstants.W3C_XML_SCHEMA_NS_URI.equals(type.getNamespaceURI()))
+ {
+ // if we want a default simple datatype, convert it directly
+ XSDTypeConverter converter = converters.get(typeName);
+ if (converter == null)
+ {
+ throw new IllegalArgumentException("Don't know how to deal with
standard type: " + type);
+ }
+
+ return converter.convert(element.getTextContent());
+ }
+ else
+ {
+ // attempt to load the payload as a java class whose name is the type local
part
+ try
+ {
+ ClassLoader loader = Thread.currentThread().getContextClassLoader();
+ Class<? extends Serializable> clazz =
loader.loadClass(typeName).asSubclass(Serializable.class);
+ JAXBContext jaxbContext = JAXBContext.newInstance(clazz);
+ Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
+ JAXBElement result = unmarshaller.unmarshal(element, clazz);
+ return (Serializable)result.getValue();
+ }
+ catch (Exception e)
+ {
+ throw new IllegalArgumentException("Couldn't unmarshall element
" + element + " with expected type " + type, e);
+ }
+ }
+ }
+ }
+
+ public static EventPayload getPayloadAsEventPayload(QName type, Serializable payload)
+ {
+ // todo: complete GTNWSRP-49
+ try
+ {
+ Class payloadClass = payload.getClass();
+ JAXBContext context = JAXBContext.newInstance(payloadClass);
+ Marshaller marshaller = context.createMarshaller();
+ JAXBElement<Serializable> element = new
JAXBElement<Serializable>(type, payloadClass, payload);
+ DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
+ builderFactory.setNamespaceAware(true);
+ Document document = builderFactory.newDocumentBuilder().newDocument();
+ marshaller.marshal(element, document);
+ return WSRPTypeFactory.createEventPayloadAsAny(document.getDocumentElement());
+ }
+ catch (Exception e)
+ {
+ throw new IllegalArgumentException("Couldn't marshall payload " +
payload + " with expected type " + type, e);
+ }
+ }
+}
Added:
components/wsrp/trunk/common/src/main/java/org/gatein/wsrp/payload/XSDTypeConverter.java
===================================================================
---
components/wsrp/trunk/common/src/main/java/org/gatein/wsrp/payload/XSDTypeConverter.java
(rev 0)
+++
components/wsrp/trunk/common/src/main/java/org/gatein/wsrp/payload/XSDTypeConverter.java 2010-06-30
15:22:03 UTC (rev 3527)
@@ -0,0 +1,196 @@
+/*
+ * JBoss, a division of Red Hat
+ * Copyright 2010, Red Hat Middleware, LLC, and individual
+ * contributors as indicated by the @authors tag. See the
+ * copyright.txt in the distribution for a full listing of
+ * individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+
+package org.gatein.wsrp.payload;
+
+import java.io.Serializable;
+
+import static javax.xml.bind.DatatypeConverter.*;
+
+/**
+ * @author <a href="mailto:chris.laprun@jboss.com">Chris
Laprun</a>
+ * @version $Revision$
+ */
+public enum XSDTypeConverter
+{
+ ANY_SIMPLE_TYPE("anySimpleType")
+ {
+ @Override
+ public Serializable convert(String value)
+ {
+ return parseAnySimpleType(value);
+ }
+ },
+ BASE64_BINARY("base64Binary")
+ {
+ @Override
+ public Serializable convert(String value)
+ {
+ return parseBase64Binary(value);
+ }
+ },
+ BOOLEAN("boolean")
+ {
+ @Override
+ public Serializable convert(String value)
+ {
+ return parseBoolean(value);
+ }
+ },
+ BYTE("byte")
+ {
+ @Override
+ public Serializable convert(String value)
+ {
+ return parseByte(value);
+ }
+ },
+ DATE("date")
+ {
+ @Override
+ public Serializable convert(String value)
+ {
+ return parseDate(value);
+ }
+ },
+ DATE_TIME("dateTime")
+ {
+ @Override
+ public Serializable convert(String value)
+ {
+ return parseDateTime(value);
+ }
+ },
+ DECIMAL("decimal")
+ {
+ @Override
+ public Serializable convert
+ (String
+ value)
+ {
+ return parseDecimal(value);
+ }
+ },
+ DOUBLE("double")
+ {
+ @Override
+ public Serializable convert(String value)
+ {
+ return parseDouble(value);
+ }
+ },
+ FLOAT("float")
+ {
+ @Override
+ public Serializable convert(String value)
+ {
+ return parseFloat(value);
+ }
+ },
+ HEX_BINARY("hexBinary")
+ {
+ @Override
+ public Serializable convert(String value)
+ {
+ return parseHexBinary(value);
+ }
+ },
+ INT("int")
+ {
+ @Override
+ public Serializable convert(String value)
+ {
+ return parseInt(value);
+ }
+ },
+ INTEGER("integer")
+ {
+ @Override
+ public Serializable convert(String value)
+ {
+ return parseInteger(value);
+ }
+ },
+ LONG("long")
+ {
+ @Override
+ public Serializable convert(String value)
+ {
+ return parseLong(value);
+ }
+ },
+ SHORT("short")
+ {
+ @Override
+ public Serializable convert(String value)
+ {
+ return parseShort(value);
+ }
+ },
+ STRING("string")
+ {
+ @Override
+ public Serializable convert(String value)
+ {
+ return parseString(value);
+ }
+ },
+ TIME("time")
+ {
+ @Override
+ public Serializable convert(String value)
+ {
+ return parseTime(value);
+ }
+ },
+ UNSIGNED_INT("unsignedInt")
+ {
+ @Override
+ public Serializable convert(String value)
+ {
+ return parseUnsignedInt(value);
+ }
+ },
+ UNSIGNED_SHORT("unsignedShort")
+ {
+ @Override
+ public Serializable convert(String value)
+ {
+ return parseUnsignedShort(value);
+ }
+ };
+
+ private XSDTypeConverter(String typeName)
+ {
+ this.typeName = typeName;
+ }
+
+ private String typeName;
+
+ public String typeName()
+ {
+ return typeName;
+ }
+
+ public abstract Serializable convert(String value);
+}
Modified:
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/NavigationalStateUpdatingHandler.java
===================================================================
---
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/NavigationalStateUpdatingHandler.java 2010-06-30
12:39:15 UTC (rev 3526)
+++
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/NavigationalStateUpdatingHandler.java 2010-06-30
15:22:03 UTC (rev 3527)
@@ -30,6 +30,7 @@
import org.gatein.pc.api.invocation.response.UpdateNavigationalStateResponse;
import org.gatein.pc.api.spi.InstanceContext;
import org.gatein.wsrp.WSRPUtils;
+import org.gatein.wsrp.payload.PayloadUtils;
import org.oasis.wsrp.v2.Event;
import org.oasis.wsrp.v2.EventPayload;
import org.oasis.wsrp.v2.NamedString;
@@ -95,7 +96,7 @@
for (Event event : events)
{
EventPayload payload = event.getPayload();
- result.queueEvent(new UpdateNavigationalStateResponse.Event(event.getName(),
WSRPUtils.getPayloadAsSerializable(payload)));
+ result.queueEvent(new UpdateNavigationalStateResponse.Event(event.getName(),
PayloadUtils.getPayloadAsSerializable(event.getType(), payload)));
}
}
Modified:
components/wsrp/trunk/producer/src/main/java/org/gatein/wsrp/producer/EventRequestProcessor.java
===================================================================
---
components/wsrp/trunk/producer/src/main/java/org/gatein/wsrp/producer/EventRequestProcessor.java 2010-06-30
12:39:15 UTC (rev 3526)
+++
components/wsrp/trunk/producer/src/main/java/org/gatein/wsrp/producer/EventRequestProcessor.java 2010-06-30
15:22:03 UTC (rev 3527)
@@ -23,6 +23,7 @@
package org.gatein.wsrp.producer;
+import org.gatein.common.NotYetImplemented;
import org.gatein.common.util.ParameterValidation;
import org.gatein.pc.api.invocation.EventInvocation;
import org.gatein.pc.api.invocation.PortletInvocation;
@@ -31,6 +32,7 @@
import org.gatein.pc.api.state.AccessMode;
import org.gatein.wsrp.WSRPTypeFactory;
import org.gatein.wsrp.WSRPUtils;
+import org.gatein.wsrp.payload.PayloadUtils;
import org.gatein.wsrp.spec.v2.WSRP2ExceptionFactory;
import org.oasis.wsrp.v2.Event;
import org.oasis.wsrp.v2.EventParams;
@@ -138,13 +140,18 @@
List<Event> events = handleEvents.getEventParams().getEvents();
+ if (events.size() > 1)
+ {
+ throw new NotYetImplemented("Need to support multiple events at
once...");
+ }
+
// since we currently don't support sending multiple events to process at once,
assume there's only one
Event event = events.get(0);
eventInvocation.setName(event.getName());
EventPayload payload = event.getPayload();
- eventInvocation.setPayload(WSRPUtils.getPayloadAsSerializable(payload));
+ eventInvocation.setPayload(PayloadUtils.getPayloadAsSerializable(event.getType(),
payload));
return eventInvocation;
}