Author: chris.laprun(a)jboss.com
Date: 2010-10-08 10:40:14 -0400 (Fri, 08 Oct 2010)
New Revision: 4610
Modified:
components/wsrp/trunk/common/src/main/java/org/gatein/wsrp/WSRPConstants.java
components/wsrp/trunk/common/src/main/java/org/gatein/wsrp/WSRPTypeFactory.java
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
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/handlers/NavigationalStateUpdatingHandler.java
components/wsrp/trunk/producer/src/main/java/org/gatein/wsrp/producer/handlers/processors/EventRequestProcessor.java
components/wsrp/trunk/wsrp-producer-war/src/test/java/org/gatein/wsrp/protocol/v2/MarkupTestCase.java
Log:
- GTNWSRP-49: Improvements to marshalling / unmarshalling, which should now be more
accurate. Still not working with NetUnity's producer though... and somehow feeling
that this should not be that complex. :(
Modified: components/wsrp/trunk/common/src/main/java/org/gatein/wsrp/WSRPConstants.java
===================================================================
---
components/wsrp/trunk/common/src/main/java/org/gatein/wsrp/WSRPConstants.java 2010-10-08
14:09:25 UTC (rev 4609)
+++
components/wsrp/trunk/common/src/main/java/org/gatein/wsrp/WSRPConstants.java 2010-10-08
14:40:14 UTC (rev 4610)
@@ -193,6 +193,23 @@
/** A QName representing the string type defined as part of XML Schemas Part 2 */
public static final QName XSD_STRING = new
QName("http://www.w3.org/2001/XMLSchema", "string");
+ public static final QName XSD_INTEGER = new
QName("http://www.w3.org/2001/XMLSchema", "integer");
+ public static final QName XSD_INT = new
QName("http://www.w3.org/2001/XMLSchema", "int");
+ public static final QName XSD_LONG = new
QName("http://www.w3.org/2001/XMLSchema", "long");
+ public static final QName XSD_SHORT = new
QName("http://www.w3.org/2001/XMLSchema", "short");
+ public static final QName XSD_DECIMAL = new
QName("http://www.w3.org/2001/XMLSchema", "decimal");
+ public static final QName XSD_FLOAT = new
QName("http://www.w3.org/2001/XMLSchema", "float");
+ public static final QName XSD_DOUBLE = new
QName("http://www.w3.org/2001/XMLSchema", "double");
+ public static final QName XSD_BOOLEAN = new
QName("http://www.w3.org/2001/XMLSchema", "boolean");
+ public static final QName XSD_BYTE = new
QName("http://www.w3.org/2001/XMLSchema", "byte");
+ public static final QName XSD_DATE_TIME = new
QName("http://www.w3.org/2001/XMLSchema", "dateTime");
+ public static final QName XSD_BASE_64_BINARY = new
QName("http://www.w3.org/2001/XMLSchema", "base64Binary");
+ public static final QName XSD_HEX_BINARY = new
QName("http://www.w3.org/2001/XMLSchema", "hexBinary");
+ public static final QName XSD_UNSIGNED_INT = new
QName("http://www.w3.org/2001/XMLSchema", "unsignedInt");
+ public static final QName XSD_UNSIGNED_SHORT = new
QName("http://www.w3.org/2001/XMLSchema", "unsignedShort");
+ public static final QName XSD_TIME = new
QName("http://www.w3.org/2001/XMLSchema", "time");
+ public static final QName XSD_DATE = new
QName("http://www.w3.org/2001/XMLSchema", "date");
+ public static final QName XSD_ANY_SIMPLE_TYPE = new
QName("http://www.w3.org/2001/XMLSchema", "anySimpleType");
private WSRPConstants()
{
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-10-08
14:09:25 UTC (rev 4609)
+++
components/wsrp/trunk/common/src/main/java/org/gatein/wsrp/WSRPTypeFactory.java 2010-10-08
14:40:14 UTC (rev 4610)
@@ -1401,10 +1401,7 @@
event.setName(name);
if (payload != null)
{
- Class<? extends Object> type = payload.getClass();
- QName typeName = new QName(type.getName());
- event.setType(typeName);
- event.setPayload(PayloadUtils.getPayloadAsEventPayload(typeName, payload));
+ event.setPayload(PayloadUtils.getPayloadAsEventPayload(event, payload));
}
return event;
}
Modified:
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 2010-10-08
14:09:25 UTC (rev 4609)
+++
components/wsrp/trunk/common/src/main/java/org/gatein/wsrp/payload/PayloadUtils.java 2010-10-08
14:40:14 UTC (rev 4610)
@@ -24,7 +24,9 @@
package org.gatein.wsrp.payload;
import org.gatein.common.util.ParameterValidation;
+import org.gatein.pc.api.info.EventInfo;
import org.gatein.wsrp.WSRPTypeFactory;
+import org.oasis.wsrp.v2.Event;
import org.oasis.wsrp.v2.EventPayload;
import org.oasis.wsrp.v2.NamedStringArray;
import org.w3c.dom.Document;
@@ -47,26 +49,37 @@
*/
public class PayloadUtils
{
- private final static Map<String, XSDTypeConverter> converters = new
HashMap<String, XSDTypeConverter>(19);
+ private final static Map<String, XSDTypeConverter> typeToConverters = new
HashMap<String, XSDTypeConverter>(19);
+ private final static Map<QName, XSDTypeConverter> nameToConverters = new
HashMap<QName, XSDTypeConverter>(5);
+ private final static Map<Class, XSDTypeConverter> classToConverters = new
HashMap<Class, XSDTypeConverter>(19);
static
{
XSDTypeConverter[] converterArray = XSDTypeConverter.values();
for (XSDTypeConverter converter : converterArray)
{
- converters.put(converter.typeName(), converter);
+ typeToConverters.put(converter.typeName(), converter);
+
+ // reverse mapping, unfortunately, there's not a one to one mapping from
Java to XSD so converters with
+ // null Java types are considered secondary... Semantic analysis could possibly
be used...
+ Class javaType = converter.getJavaType();
+ if (javaType != null)
+ {
+ classToConverters.put(javaType, converter);
+ }
}
}
- public static Serializable getPayloadAsSerializable(QName type, EventPayload payload)
+ public static Serializable getPayloadAsSerializable(Event event, EventInfo eventInfo)
{
// GTNWSRP-49
+ EventPayload payload = event.getPayload();
if (payload == null)
{
return null;
}
- ParameterValidation.throwIllegalArgExceptionIfNull(type, "Payload expected
type");
+ ParameterValidation.throwIllegalArgExceptionIfNull(event, "Payload expected
type");
Object any = payload.getAny();
if (any == null)
@@ -84,18 +97,22 @@
else
{
Element element = (Element)any;
+ QName type = event.getType();
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);
+ XSDTypeConverter converter = typeToConverters.get(typeName);
if (converter == null)
{
throw new IllegalArgumentException("Don't know how to deal with
standard type: " + type);
}
- return converter.convert(element.getTextContent());
+ // record which converter was used so that we can use it when it's time
to marshall it back to XML
+ nameToConverters.put(event.getName(), converter);
+
+ return converter.parseFromXML(element.getTextContent());
}
else
{
@@ -111,13 +128,13 @@
}
catch (Exception e)
{
- throw new IllegalArgumentException("Couldn't unmarshall element
" + element + " with expected type " + type, e);
+ throw new IllegalArgumentException("Couldn't unmarshall element
" + element + " with expected type " + event, e);
}
}
}
}
- public static EventPayload getPayloadAsEventPayload(QName type, Serializable payload)
+ public static EventPayload getPayloadAsEventPayload(Event eventNeedingType,
Serializable payload)
{
if (payload instanceof SerializableNamedStringArray)
{
@@ -127,12 +144,40 @@
else
{
// todo: complete GTNWSRP-49
+ QName name = eventNeedingType.getName();
+
+ // we will use the payload class name as type for serialiation if we can't
find something better...
+ Class payloadClass = payload.getClass();
+ QName type = new QName(payloadClass.getName());
+
+ // first, try to get a converter from the event name and use the converter type
+ XSDTypeConverter converter = nameToConverters.get(name);
+ if (converter != null)
+ {
+ // remove from map to avoid memory leak
+ nameToConverters.remove(name);
+
+ type = converter.getXSDType();
+ }
+ else
+ {
+ // otherwise, try to get a converter from the payload class
+ converter = classToConverters.get(payloadClass);
+
+ if (converter != null)
+ {
+ type = converter.getXSDType();
+ }
+ }
+
+ // else, use the class name as type for the serialization
+ eventNeedingType.setType(type);
try
{
- Class payloadClass = payload.getClass();
JAXBContext context = JAXBContext.newInstance(payloadClass);
Marshaller marshaller = context.createMarshaller();
- JAXBElement<Serializable> element = new
JAXBElement<Serializable>(type, payloadClass, payload);
+
+ JAXBElement<Serializable> element = new
JAXBElement<Serializable>(name, payloadClass, payload);
DocumentBuilderFactory builderFactory =
DocumentBuilderFactory.newInstance();
builderFactory.setNamespaceAware(true);
Document document = builderFactory.newDocumentBuilder().newDocument();
Modified:
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 2010-10-08
14:09:25 UTC (rev 4609)
+++
components/wsrp/trunk/common/src/main/java/org/gatein/wsrp/payload/XSDTypeConverter.java 2010-10-08
14:40:14 UTC (rev 4610)
@@ -23,7 +23,13 @@
package org.gatein.wsrp.payload;
+import org.gatein.wsrp.WSRPConstants;
+
+import javax.xml.namespace.QName;
import java.io.Serializable;
+import java.math.BigDecimal;
+import java.math.BigInteger;
+import java.util.Calendar;
import static javax.xml.bind.DatatypeConverter.*;
@@ -36,146 +42,475 @@
ANY_SIMPLE_TYPE("anySimpleType")
{
@Override
- public Serializable convert(String value)
+ public Serializable parseFromXML(String value)
{
return parseAnySimpleType(value);
}
+
+ @Override
+ public String printToXML(Serializable value)
+ {
+ return printAnySimpleType((String)value);
+ }
+
+ @Override
+ public Class getJavaType()
+ {
+ return Object.class;
+ }
+
+ @Override
+ public QName getXSDType()
+ {
+ return WSRPConstants.XSD_ANY_SIMPLE_TYPE;
+ }
},
BASE64_BINARY("base64Binary")
{
@Override
- public Serializable convert(String value)
+ public Serializable parseFromXML(String value)
{
return parseBase64Binary(value);
}
+
+ @Override
+ public String printToXML(Serializable value)
+ {
+ return printBase64Binary((byte[])value);
+ }
+
+ @Override
+ public Class getJavaType()
+ {
+ return byte[].class;
+ }
+
+ @Override
+ public QName getXSDType()
+ {
+ return WSRPConstants.XSD_BASE_64_BINARY;
+ }
},
BOOLEAN("boolean")
{
@Override
- public Serializable convert(String value)
+ public Serializable parseFromXML(String value)
{
return parseBoolean(value);
}
+
+ @Override
+ public String printToXML(Serializable value)
+ {
+ return printBoolean((Boolean)value);
+ }
+
+ @Override
+ public Class getJavaType()
+ {
+ return boolean.class;
+ }
+
+ @Override
+ public QName getXSDType()
+ {
+ return WSRPConstants.XSD_BOOLEAN;
+ }
},
BYTE("byte")
{
@Override
- public Serializable convert(String value)
+ public Serializable parseFromXML(String value)
{
return parseByte(value);
}
+
+ @Override
+ public String printToXML(Serializable value)
+ {
+ return printByte((Byte)value);
+ }
+
+ @Override
+ public Class getJavaType()
+ {
+ return byte.class;
+ }
+
+ @Override
+ public QName getXSDType()
+ {
+ return WSRPConstants.XSD_BYTE;
+ }
},
DATE("date")
{
@Override
- public Serializable convert(String value)
+ public Serializable parseFromXML(String value)
{
return parseDate(value);
}
+
+ @Override
+ public String printToXML(Serializable value)
+ {
+ return printDate((Calendar)value);
+ }
+
+ @Override
+ public Class getJavaType()
+ {
+// return Calendar.class;
+ return null;
+ }
+
+ @Override
+ public QName getXSDType()
+ {
+ return WSRPConstants.XSD_DATE;
+ }
},
DATE_TIME("dateTime")
{
@Override
- public Serializable convert(String value)
+ public Serializable parseFromXML(String value)
{
return parseDateTime(value);
}
+
+ @Override
+ public String printToXML(Serializable value)
+ {
+ return printDateTime((Calendar)value);
+ }
+
+ @Override
+ public Class getJavaType()
+ {
+ return Calendar.class;
+ }
+
+ @Override
+ public QName getXSDType()
+ {
+ return WSRPConstants.XSD_DATE_TIME;
+ }
},
DECIMAL("decimal")
{
@Override
- public Serializable convert(String value)
+ public Serializable parseFromXML(String value)
{
return parseDecimal(value);
}
+
+ @Override
+ public String printToXML(Serializable value)
+ {
+ return printDecimal((BigDecimal)value);
+ }
+
+ @Override
+ public Class getJavaType()
+ {
+ return BigDecimal.class;
+ }
+
+ @Override
+ public QName getXSDType()
+ {
+ return WSRPConstants.XSD_DECIMAL;
+ }
},
DOUBLE("double")
{
@Override
- public Serializable convert(String value)
+ public Serializable parseFromXML(String value)
{
return parseDouble(value);
}
+
+ @Override
+ public String printToXML(Serializable value)
+ {
+ return printDouble((Double)value);
+ }
+
+ @Override
+ public Class getJavaType()
+ {
+ return double.class;
+ }
+
+ @Override
+ public QName getXSDType()
+ {
+ return WSRPConstants.XSD_DOUBLE;
+ }
},
FLOAT("float")
{
@Override
- public Serializable convert(String value)
+ public Serializable parseFromXML(String value)
{
return parseFloat(value);
}
+
+ @Override
+ public String printToXML(Serializable value)
+ {
+ return printFloat((Float)value);
+ }
+
+ @Override
+ public Class getJavaType()
+ {
+ return float.class;
+ }
+
+ @Override
+ public QName getXSDType()
+ {
+ return WSRPConstants.XSD_FLOAT;
+ }
},
HEX_BINARY("hexBinary")
{
@Override
- public Serializable convert(String value)
+ public Serializable parseFromXML(String value)
{
return parseHexBinary(value);
}
+
+ @Override
+ public String printToXML(Serializable value)
+ {
+ return printHexBinary((byte[])value);
+ }
+
+ @Override
+ public Class getJavaType()
+ {
+// return byte[].class;
+ return null;
+ }
+
+ @Override
+ public QName getXSDType()
+ {
+ return WSRPConstants.XSD_HEX_BINARY;
+ }
},
INT("int")
{
@Override
- public Serializable convert(String value)
+ public Serializable parseFromXML(String value)
{
return parseInt(value);
}
+
+ @Override
+ public String printToXML(Serializable value)
+ {
+ return printInt((Integer)value);
+ }
+
+ @Override
+ public Class getJavaType()
+ {
+ return int.class;
+ }
+
+ @Override
+ public QName getXSDType()
+ {
+ return WSRPConstants.XSD_INT;
+ }
},
INTEGER("integer")
{
@Override
- public Serializable convert(String value)
+ public Serializable parseFromXML(String value)
{
return parseInteger(value);
}
+
+ @Override
+ public String printToXML(Serializable value)
+ {
+ return printInteger((BigInteger)value);
+ }
+
+ @Override
+ public Class getJavaType()
+ {
+ return BigInteger.class;
+ }
+
+ @Override
+ public QName getXSDType()
+ {
+ return WSRPConstants.XSD_INTEGER;
+ }
},
LONG("long")
{
@Override
- public Serializable convert(String value)
+ public Serializable parseFromXML(String value)
{
return parseLong(value);
}
+
+ @Override
+ public String printToXML(Serializable value)
+ {
+ return printLong((Long)value);
+ }
+
+ @Override
+ public Class getJavaType()
+ {
+ return long.class;
+ }
+
+ @Override
+ public QName getXSDType()
+ {
+ return WSRPConstants.XSD_LONG;
+ }
},
SHORT("short")
{
@Override
- public Serializable convert(String value)
+ public Serializable parseFromXML(String value)
{
return parseShort(value);
}
+
+ @Override
+ public String printToXML(Serializable value)
+ {
+ return printShort((Short)value);
+ }
+
+ @Override
+ public Class getJavaType()
+ {
+ return short.class;
+ }
+
+ @Override
+ public QName getXSDType()
+ {
+ return WSRPConstants.XSD_SHORT;
+ }
},
STRING("string")
{
@Override
- public Serializable convert(String value)
+ public Serializable parseFromXML(String value)
{
return parseString(value);
}
+
+ @Override
+ public String printToXML(Serializable value)
+ {
+ return printString((String)value);
+ }
+
+ @Override
+ public Class getJavaType()
+ {
+ return String.class;
+ }
+
+ @Override
+ public QName getXSDType()
+ {
+ return WSRPConstants.XSD_STRING;
+ }
},
TIME("time")
{
@Override
- public Serializable convert(String value)
+ public Serializable parseFromXML(String value)
{
return parseTime(value);
}
+
+ @Override
+ public String printToXML(Serializable value)
+ {
+ return printTime((Calendar)value);
+ }
+
+ @Override
+ public Class getJavaType()
+ {
+// return Calendar.class;
+ return null;
+ }
+
+ @Override
+ public QName getXSDType()
+ {
+ return WSRPConstants.XSD_TIME;
+ }
},
UNSIGNED_INT("unsignedInt")
{
@Override
- public Serializable convert(String value)
+ public Serializable parseFromXML(String value)
{
return parseUnsignedInt(value);
}
+
+ @Override
+ public String printToXML(Serializable value)
+ {
+ return printUnsignedInt((Long)value);
+ }
+
+ @Override
+ public Class getJavaType()
+ {
+// return long.class;
+ return null;
+ }
+
+ @Override
+ public QName getXSDType()
+ {
+ return WSRPConstants.XSD_UNSIGNED_INT;
+ }
},
UNSIGNED_SHORT("unsignedShort")
{
@Override
- public Serializable convert(String value)
+ public Serializable parseFromXML(String value)
{
return parseUnsignedShort(value);
}
+
+ @Override
+ public String printToXML(Serializable value)
+ {
+ return printUnsignedShort((Integer)value);
+ }
+
+ @Override
+ public Class getJavaType()
+ {
+// return int.class;
+ return null;
+ }
+
+ @Override
+ public QName getXSDType()
+ {
+ return WSRPConstants.XSD_UNSIGNED_SHORT;
+ }
};
private XSDTypeConverter(String typeName)
@@ -190,5 +525,11 @@
return typeName;
}
- public abstract Serializable convert(String value);
+ public abstract Serializable parseFromXML(String value);
+
+ public abstract String printToXML(Serializable value);
+
+ public abstract Class getJavaType();
+
+ public abstract QName getXSDType();
}
Modified:
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/handlers/NavigationalStateUpdatingHandler.java
===================================================================
---
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/handlers/NavigationalStateUpdatingHandler.java 2010-10-08
14:09:25 UTC (rev 4609)
+++
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/handlers/NavigationalStateUpdatingHandler.java 2010-10-08
14:40:14 UTC (rev 4610)
@@ -26,6 +26,7 @@
import org.gatein.common.util.ParameterValidation;
import org.gatein.pc.api.OpaqueStateString;
import org.gatein.pc.api.StateEvent;
+import org.gatein.pc.api.info.EventInfo;
import org.gatein.pc.api.invocation.PortletInvocation;
import org.gatein.pc.api.invocation.response.UpdateNavigationalStateResponse;
import org.gatein.pc.api.spi.InstanceContext;
@@ -33,7 +34,6 @@
import org.gatein.wsrp.consumer.WSRPConsumerImpl;
import org.gatein.wsrp.payload.PayloadUtils;
import org.oasis.wsrp.v2.Event;
-import org.oasis.wsrp.v2.EventPayload;
import org.oasis.wsrp.v2.NamedString;
import org.oasis.wsrp.v2.NavigationalContext;
import org.oasis.wsrp.v2.PortletContext;
@@ -96,8 +96,8 @@
{
for (Event event : events)
{
- EventPayload payload = event.getPayload();
- result.queueEvent(new UpdateNavigationalStateResponse.Event(event.getName(),
PayloadUtils.getPayloadAsSerializable(event.getType(), payload)));
+ EventInfo eventInfo =
consumer.getProducerInfo().getInfoForEvent(event.getName());
+ result.queueEvent(new UpdateNavigationalStateResponse.Event(event.getName(),
PayloadUtils.getPayloadAsSerializable(event, eventInfo)));
}
}
Modified:
components/wsrp/trunk/producer/src/main/java/org/gatein/wsrp/producer/handlers/processors/EventRequestProcessor.java
===================================================================
---
components/wsrp/trunk/producer/src/main/java/org/gatein/wsrp/producer/handlers/processors/EventRequestProcessor.java 2010-10-08
14:09:25 UTC (rev 4609)
+++
components/wsrp/trunk/producer/src/main/java/org/gatein/wsrp/producer/handlers/processors/EventRequestProcessor.java 2010-10-08
14:40:14 UTC (rev 4610)
@@ -37,7 +37,6 @@
import org.gatein.wsrp.spec.v2.WSRP2ExceptionFactory;
import org.oasis.wsrp.v2.Event;
import org.oasis.wsrp.v2.EventParams;
-import org.oasis.wsrp.v2.EventPayload;
import org.oasis.wsrp.v2.HandleEvents;
import org.oasis.wsrp.v2.HandleEventsResponse;
import org.oasis.wsrp.v2.InvalidHandle;
@@ -150,10 +149,8 @@
Event event = events.get(0);
eventInvocation.setName(event.getName());
- EventPayload payload = event.getPayload();
+ eventInvocation.setPayload(PayloadUtils.getPayloadAsSerializable(event, null));
- eventInvocation.setPayload(PayloadUtils.getPayloadAsSerializable(event.getType(),
payload));
-
return eventInvocation;
}
Modified:
components/wsrp/trunk/wsrp-producer-war/src/test/java/org/gatein/wsrp/protocol/v2/MarkupTestCase.java
===================================================================
---
components/wsrp/trunk/wsrp-producer-war/src/test/java/org/gatein/wsrp/protocol/v2/MarkupTestCase.java 2010-10-08
14:09:25 UTC (rev 4609)
+++
components/wsrp/trunk/wsrp-producer-war/src/test/java/org/gatein/wsrp/protocol/v2/MarkupTestCase.java 2010-10-08
14:40:14 UTC (rev 4610)
@@ -842,8 +842,8 @@
assertEquals(1, events.size());
Event event = events.get(0);
assertEquals(new QName("urn:jboss:gatein:samples:event",
"eventsample"), event.getName());
- assertEquals(new QName("java.lang.String"), event.getType());
- assertEquals("param-value",
PayloadUtils.getPayloadAsSerializable(event.getType(), event.getPayload()));
+ assertEquals(WSRPConstants.XSD_STRING, event.getType());
+ assertEquals("param-value",
PayloadUtils.getPayloadAsSerializable(event, null));
// send event
HandleEvents handleEvents = WSRPTypeFactory.createHandleEvents(null,