Author: chris.laprun(a)jboss.com
Date: 2010-10-07 11:38:39 -0400 (Thu, 07 Oct 2010)
New Revision: 4591
Added:
components/wsrp/trunk/common/src/main/java/org/gatein/wsrp/payload/SerializableNamedStringArray.java
Modified:
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
Log:
- GTNWSRP-49: properly support events using NamedStringArray...
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-07
14:49:15 UTC (rev 4590)
+++
components/wsrp/trunk/common/src/main/java/org/gatein/wsrp/WSRPTypeFactory.java 2010-10-07
15:38:39 UTC (rev 4591)
@@ -1425,13 +1425,10 @@
return namedString;
}
- public static EventPayload createEventPayloadAsNamedString(Object payload)
+ public static EventPayload createEventPayloadAsNamedString(NamedStringArray payload)
{
- // todo: fix me GTNWSRP-49
EventPayload result = new EventPayload();
- NamedStringArray value = new NamedStringArray();
- value.getNamedString().add(createNamedString("event",
payload.toString()));
- result.setNamedStringArray(value);
+ result.setNamedStringArray(payload);
return result;
}
@@ -1755,44 +1752,49 @@
return userProfile;
}
-
+
public static CopyPortlets createCopyPortlets(RegistrationContext
toRegistrationContext, UserContext toUserContext, RegistrationContext
fromRegistrationContext, UserContext fromUserContext, List<PortletContext>
fromPortletContexts)
{
if (!ParameterValidation.existsAndIsNotEmpty(fromPortletContexts))
{
throw new IllegalArgumentException("Must provide at least one
PortletContext to CopyPortlets.");
}
-
+
CopyPortlets copyPortlets = new CopyPortlets();
copyPortlets.setToRegistrationContext(toRegistrationContext);
copyPortlets.setToUserContext(toUserContext);
copyPortlets.setFromRegistrationContext(fromRegistrationContext);
copyPortlets.setFromUserContext(fromUserContext);
copyPortlets.getFromPortletContexts().addAll(fromPortletContexts);
-
+
return copyPortlets;
}
-
+
public static CopyPortletsResponse
createCopyPortletsResponse(List<CopiedPortlet> copiedPortlets,
List<FailedPortlets> failedPortlets, ResourceList resourceList)
{
CopyPortletsResponse response = new CopyPortletsResponse();
response.getCopiedPortlets().addAll(copiedPortlets);
response.getFailedPortlets().addAll(failedPortlets);
response.setResourceList(resourceList);
-
+
return response;
}
-
+
public static CopiedPortlet createCopiedPortlet(PortletContext newPortletContext,
String fromPortletHandle)
{
ParameterValidation.throwIllegalArgExceptionIfNull(newPortletContext,
"newPortletContext");
ParameterValidation.throwIllegalArgExceptionIfNullOrEmpty(fromPortletHandle,
"fromPortletHandle", "createCopiedPortlet");
-
+
CopiedPortlet copiedPortlet = new CopiedPortlet();
-
+
copiedPortlet.setNewPortletContext(newPortletContext);
copiedPortlet.setFromPortletHandle(fromPortletHandle);
-
+
return copiedPortlet;
}
+
+ public static NamedStringArray createNamedStringArray()
+ {
+ return new NamedStringArray();
+ }
}
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-07
14:49:15 UTC (rev 4590)
+++
components/wsrp/trunk/common/src/main/java/org/gatein/wsrp/payload/PayloadUtils.java 2010-10-07
15:38:39 UTC (rev 4591)
@@ -74,7 +74,7 @@
NamedStringArray namedStringArray = payload.getNamedStringArray();
if (namedStringArray != null)
{
- throw new UnsupportedOperationException("Don't know how to convert
NamedStringArray to a Serializable in an interroperable way. :(");
+ return new SerializableNamedStringArray(namedStringArray);
}
else
{
@@ -119,22 +119,30 @@
public static EventPayload getPayloadAsEventPayload(QName type, Serializable payload)
{
- // todo: complete GTNWSRP-49
- try
+ if (payload instanceof SerializableNamedStringArray)
{
- 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());
+ SerializableNamedStringArray stringArray =
(SerializableNamedStringArray)payload;
+ return
WSRPTypeFactory.createEventPayloadAsNamedString(stringArray.toNamedStringArray());
}
- catch (Exception e)
+ else
{
- throw new IllegalArgumentException("Couldn't marshall payload " +
payload + " with expected type " + type, e);
+ // 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/SerializableNamedStringArray.java
===================================================================
---
components/wsrp/trunk/common/src/main/java/org/gatein/wsrp/payload/SerializableNamedStringArray.java
(rev 0)
+++
components/wsrp/trunk/common/src/main/java/org/gatein/wsrp/payload/SerializableNamedStringArray.java 2010-10-07
15:38:39 UTC (rev 4591)
@@ -0,0 +1,84 @@
+/*
+ * 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.wsrp.WSRPTypeFactory;
+import org.oasis.wsrp.v2.NamedString;
+import org.oasis.wsrp.v2.NamedStringArray;
+
+import java.io.Serializable;
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * @author <a href="mailto:chris.laprun@jboss.com">Chris
Laprun</a>
+ * @version $Revision$
+ */
+public class SerializableNamedStringArray implements Serializable
+{
+ private SerializableNamedString[] strings;
+
+ SerializableNamedStringArray(NamedStringArray array)
+ {
+ initFrom(array);
+ }
+
+ void initFrom(NamedStringArray array)
+ {
+ List<NamedString> namedString = array.getNamedString();
+ strings = new SerializableNamedString[namedString.size()];
+ int i = 0;
+ for (NamedString string : namedString)
+ {
+ strings[i++] = new SerializableNamedString(string.getName(),
string.getValue());
+ }
+ }
+
+ NamedStringArray toNamedStringArray()
+ {
+ NamedStringArray array = WSRPTypeFactory.createNamedStringArray();
+
+ List<NamedString> namedString = new
ArrayList<NamedString>(strings.length);
+ for (SerializableNamedString string : strings)
+ {
+ namedString.add(WSRPTypeFactory.createNamedString(string.name, string.value));
+ }
+
+ array.getNamedString().addAll(namedString);
+ return array;
+ }
+
+ private static class SerializableNamedString implements Serializable
+ {
+ String name;
+ String value;
+
+ private SerializableNamedString(String name, String value)
+ {
+ this.name = name;
+ this.value = value;
+ }
+ }
+}