Author: chris.laprun(a)jboss.com
Date: 2010-06-28 04:16:53 -0400 (Mon, 28 Jun 2010)
New Revision: 3498
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/common/src/main/java/org/gatein/wsrp/spec/v1/V1ToV2Converter.java
Log:
- Added methods to deal with events and PRPs.
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-28
08:10:07 UTC (rev 3497)
+++
components/wsrp/trunk/common/src/main/java/org/gatein/wsrp/WSRPTypeFactory.java 2010-06-28
08:16:53 UTC (rev 3498)
@@ -45,13 +45,18 @@
import org.oasis.wsrp.v2.ClonePortlet;
import org.oasis.wsrp.v2.DestroyPortlets;
import org.oasis.wsrp.v2.DestroyPortletsResponse;
+import org.oasis.wsrp.v2.Event;
import org.oasis.wsrp.v2.EventDescription;
+import org.oasis.wsrp.v2.EventParams;
+import org.oasis.wsrp.v2.EventPayload;
import org.oasis.wsrp.v2.FailedPortlets;
import org.oasis.wsrp.v2.GetMarkup;
import org.oasis.wsrp.v2.GetPortletDescription;
import org.oasis.wsrp.v2.GetPortletProperties;
import org.oasis.wsrp.v2.GetPortletPropertyDescription;
import org.oasis.wsrp.v2.GetServiceDescription;
+import org.oasis.wsrp.v2.HandleEvents;
+import org.oasis.wsrp.v2.HandleEventsResponse;
import org.oasis.wsrp.v2.InitCookie;
import org.oasis.wsrp.v2.InteractionParams;
import org.oasis.wsrp.v2.LocalizedString;
@@ -62,6 +67,7 @@
import org.oasis.wsrp.v2.ModelDescription;
import org.oasis.wsrp.v2.ModifyRegistration;
import org.oasis.wsrp.v2.NamedString;
+import org.oasis.wsrp.v2.NamedStringArray;
import org.oasis.wsrp.v2.NavigationalContext;
import org.oasis.wsrp.v2.ParameterDescription;
import org.oasis.wsrp.v2.PerformBlockingInteraction;
@@ -86,7 +92,9 @@
import org.oasis.wsrp.v2.UploadContext;
import org.oasis.wsrp.v2.UserContext;
+import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.namespace.QName;
+import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.Map;
@@ -1124,13 +1132,9 @@
for (Map.Entry<String, String[]> entry :
publicNavigationalState.entrySet())
{
String name = entry.getKey();
- NamedString namedString;
for (String value : entry.getValue())
{
- namedString = new NamedString();
- namedString.setName(name);
- namedString.setValue(value);
- context.getPublicValues().add(namedString);
+ context.getPublicValues().add(WSRPTypeFactory.createNamedString(name,
value));
}
}
}
@@ -1156,4 +1160,87 @@
desc.setName(name);
return desc;
}
+
+ public static HandleEventsResponse createHandleEventsReponse()
+ {
+ return new HandleEventsResponse();
+ }
+
+ public static HandleEvents createHandleEvents(PortletContext portletContext,
RuntimeContext runtimeContext,
+ MarkupParams markupParams, EventParams
eventParams)
+ {
+ ParameterValidation.throwIllegalArgExceptionIfNull(portletContext,
"PortletContext");
+
ParameterValidation.throwIllegalArgExceptionIfNullOrEmpty(portletContext.getPortletHandle(),
"portlet handle", "PortletContext");
+ ParameterValidation.throwIllegalArgExceptionIfNull(runtimeContext,
"RuntimeContext");
+ ParameterValidation.throwIllegalArgExceptionIfNull(markupParams,
"MarkupParams");
+ ParameterValidation.throwIllegalArgExceptionIfNull(eventParams,
"EventParams");
+
+ HandleEvents handleEvents = new HandleEvents();
+ handleEvents.setPortletContext(portletContext);
+ handleEvents.setEventParams(eventParams);
+ handleEvents.setMarkupParams(markupParams);
+ handleEvents.setRuntimeContext(runtimeContext);
+ return handleEvents;
+ }
+
+ public static EventParams createEventParams(List<Event> events, StateChange
portletStateChange)
+ {
+ if (!ParameterValidation.existsAndIsNotEmpty(events))
+ {
+ throw new IllegalArgumentException("Must provide at least one Event to
EventParams.");
+ }
+ ParameterValidation.throwIllegalArgExceptionIfNull(portletStateChange,
"StateChange");
+
+ EventParams eventParams = new EventParams();
+ eventParams.setPortletStateChange(portletStateChange);
+ eventParams.getEvents().addAll(events);
+ return eventParams;
+ }
+
+ public static Event createEvent(QName name, Serializable payload)
+ {
+ ParameterValidation.throwIllegalArgExceptionIfNull(name, "Event name");
+ Event event = new Event();
+ event.setName(name);
+ if (payload != null)
+ {
+ Class<? extends Object> type = payload.getClass();
+ XmlRootElement annotation = type.getAnnotation(XmlRootElement.class);
+ if (annotation != null)
+ {
+ event.setType(new QName(annotation.namespace(), annotation.name()));
+ event.setPayload(WSRPTypeFactory.createEventPayloadAsAny(payload));
+ }
+ else
+ {
+ event.setPayload(WSRPTypeFactory.createEventPayloadAsNamedString(payload));
+ }
+ }
+ return event;
+ }
+
+ public static EventPayload createEventPayloadAsAny(Object value)
+ {
+ EventPayload payload = new EventPayload();
+ payload.setAny(value);
+ return payload;
+ }
+
+ public static NamedString createNamedString(String name, String value)
+ {
+ NamedString namedString = new NamedString();
+ namedString.setName(name);
+ namedString.setValue(value);
+ return namedString;
+ }
+
+ public static EventPayload createEventPayloadAsNamedString(Object payload)
+ {
+ // todo: fix me GTNWSRP-49
+ EventPayload result = new EventPayload();
+ NamedStringArray value = new NamedStringArray();
+ value.getNamedString().add(createNamedString("event",
payload.toString()));
+ result.setNamedStringArray(value);
+ return result;
+ }
}
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-28
08:10:07 UTC (rev 3497)
+++ components/wsrp/trunk/common/src/main/java/org/gatein/wsrp/WSRPUtils.java 2010-06-28
08:16:53 UTC (rev 3498)
@@ -25,6 +25,7 @@
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;
@@ -40,9 +41,11 @@
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;
@@ -50,6 +53,7 @@
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;
@@ -539,6 +543,98 @@
return null;
}
+ public static Map<String, String[]> createPublicNSFrom(List<NamedString>
publicParams)
+ {
+ // GTNWSRP-38: public NS
+ Map<String, String[]> publicNS = new HashMap<String,
String[]>(publicParams.size());
+ for (NamedString publicParam : publicParams)
+ {
+ String paramName = publicParam.getName();
+ String[] values = publicNS.get(paramName);
+ if (ParameterValidation.existsAndIsNotEmpty(values))
+ {
+ int valuesNb = values.length;
+ String[] newValues = new String[valuesNb + 1];
+ System.arraycopy(values, 0, newValues, 0, valuesNb);
+ newValues[valuesNb] = publicParam.getValue();
+ publicNS.put(paramName, newValues);
+ }
+ else
+ {
+ values = new String[]{publicParam.getValue()};
+ publicNS.put(paramName, values);
+ }
+ }
+ 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$
Modified:
components/wsrp/trunk/common/src/main/java/org/gatein/wsrp/spec/v1/V1ToV2Converter.java
===================================================================
---
components/wsrp/trunk/common/src/main/java/org/gatein/wsrp/spec/v1/V1ToV2Converter.java 2010-06-28
08:10:07 UTC (rev 3497)
+++
components/wsrp/trunk/common/src/main/java/org/gatein/wsrp/spec/v1/V1ToV2Converter.java 2010-06-28
08:16:53 UTC (rev 3498)
@@ -1050,8 +1050,8 @@
return null;
}
}
-
- public static GetPortletDescription toV2GetPortletDescription (V1GetPortletDescription
getPortletDescription)
+
+ public static GetPortletDescription toV2GetPortletDescription(V1GetPortletDescription
getPortletDescription)
{
if (getPortletDescription != null)
{
@@ -1059,9 +1059,9 @@
PortletContext portletContext =
toV2PortletContext(getPortletDescription.getPortletContext());
UserContext userContext =
toV2UserContext(getPortletDescription.getUserContext());
GetPortletDescription result =
WSRPTypeFactory.createGetPortletDescription(registrationContext, portletContext,
userContext);
-
+
result.getDesiredLocales().addAll(getPortletDescription.getDesiredLocales());
-
+
return result;
}
else
@@ -1168,10 +1168,7 @@
{
if (v1NamedString != null)
{
- NamedString result = new NamedString();
- result.setName(v1NamedString.getName());
- result.setValue(v1NamedString.getValue());
- return result;
+ return WSRPTypeFactory.createNamedString(v1NamedString.getName(),
v1NamedString.getValue());
}
else
{