Author: mwringe
Date: 2010-09-22 11:04:28 -0400 (Wed, 22 Sep 2010)
New Revision: 4309
Added:
components/wsrp/trunk/wsrp-producer-war/src/main/webapp/WEB-INF/lib/
Modified:
components/wsrp/trunk/common/src/main/java/org/gatein/wsrp/WSRPTypeFactory.java
components/wsrp/trunk/producer/src/main/java/org/gatein/wsrp/producer/handlers/PortletManagementHandler.java
components/wsrp/trunk/wsrp-producer-war/src/test/java/org/gatein/wsrp/protocol/v2/PortletManagementTestCase.java
Log:
GTNWSRP-71: Add copy portlet support.
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-09-22
15:01:36 UTC (rev 4308)
+++
components/wsrp/trunk/common/src/main/java/org/gatein/wsrp/WSRPTypeFactory.java 2010-09-22
15:04:28 UTC (rev 4309)
@@ -45,6 +45,9 @@
import org.oasis.wsrp.v2.ClientData;
import org.oasis.wsrp.v2.ClonePortlet;
import org.oasis.wsrp.v2.Contact;
+import org.oasis.wsrp.v2.CopiedPortlet;
+import org.oasis.wsrp.v2.CopyPortlets;
+import org.oasis.wsrp.v2.CopyPortletsResponse;
import org.oasis.wsrp.v2.DestroyPortlets;
import org.oasis.wsrp.v2.DestroyPortletsResponse;
import org.oasis.wsrp.v2.EmployerInfo;
@@ -1754,4 +1757,44 @@
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;
+ }
}
Modified:
components/wsrp/trunk/producer/src/main/java/org/gatein/wsrp/producer/handlers/PortletManagementHandler.java
===================================================================
---
components/wsrp/trunk/producer/src/main/java/org/gatein/wsrp/producer/handlers/PortletManagementHandler.java 2010-09-22
15:01:36 UTC (rev 4308)
+++
components/wsrp/trunk/producer/src/main/java/org/gatein/wsrp/producer/handlers/PortletManagementHandler.java 2010-09-22
15:04:28 UTC (rev 4309)
@@ -26,7 +26,6 @@
import com.google.common.base.Function;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.Multimap;
-import org.gatein.common.NotYetImplemented;
import org.gatein.common.i18n.LocalizedString;
import org.gatein.common.logging.Logger;
import org.gatein.common.logging.LoggerFactory;
@@ -57,6 +56,7 @@
import org.gatein.wsrp.spec.v2.WSRP2ExceptionFactory;
import org.oasis.wsrp.v2.AccessDenied;
import org.oasis.wsrp.v2.ClonePortlet;
+import org.oasis.wsrp.v2.CopiedPortlet;
import org.oasis.wsrp.v2.CopyPortlets;
import org.oasis.wsrp.v2.CopyPortletsResponse;
import org.oasis.wsrp.v2.DestroyPortlets;
@@ -93,6 +93,7 @@
import org.oasis.wsrp.v2.Property;
import org.oasis.wsrp.v2.PropertyDescription;
import org.oasis.wsrp.v2.PropertyList;
+import org.oasis.wsrp.v2.RegistrationContext;
import org.oasis.wsrp.v2.ReleaseExport;
import org.oasis.wsrp.v2.ResetProperty;
import org.oasis.wsrp.v2.ResourceList;
@@ -314,7 +315,113 @@
throws AccessDenied, InconsistentParameters, InvalidHandle, InvalidRegistration,
InvalidUserCategory,
MissingParameters, ModifyRegistrationRequired, OperationFailed,
OperationNotSupported, ResourceSuspended
{
- throw new NotYetImplemented();
+ WSRP2ExceptionFactory.throwOperationFailedIfValueIsMissing(copyPortlets,
"copyPortlets");
+
+ List<PortletContext> portletContexts =
copyPortlets.getFromPortletContexts();
+
+ if (!ParameterValidation.existsAndIsNotEmpty(portletContexts))
+ {
+ throw WSRP2ExceptionFactory.createWSException(MissingParameters.class,
"Missing required portletContext in CopyPortlets.", null);
+ }
+
+ Registration fromRegistration =
producer.getRegistrationOrFailIfInvalid(copyPortlets.getFromRegistrationContext());
+
+ RegistrationContext toRegistationContext =
copyPortlets.getToRegistrationContext();
+
+ //if toRegistrationCotnext is null, then we use the fromRegistrationContext (from
spec).
+ //NOTE: this means we can't move between a PortletContext on a registered
consumer to a non-registered consumer
+ // between two non-registered consumers will still be ok.
+ if (toRegistationContext == null)
+ {
+ toRegistationContext = copyPortlets.getFromRegistrationContext();
+ }
+
+ Registration toRegistration =
producer.getRegistrationOrFailIfInvalid(toRegistationContext);
+
+ UserContext fromUserContext = copyPortlets.getFromUserContext();
+ checkUserAuthorization(fromUserContext);
+ UserContext toUserContext = copyPortlets.getToUserContext();
+ checkUserAuthorization(toUserContext);
+
+ try
+ {
+ RegistrationLocal.setRegistration(fromRegistration);
+
+ Map<String, FailedPortlets> failedPortletsMap = new HashMap<String,
FailedPortlets>(portletContexts.size());
+
+ List<CopiedPortlet> copiedPortlets = new
ArrayList<CopiedPortlet>(portletContexts.size());
+
+ for (PortletContext portletContext : portletContexts)
+ {
+ try
+ {
+ org.gatein.pc.api.PortletContext portalPC =
WSRPUtils.convertToPortalPortletContext(portletContext);
+
+ //NOTE: There are two ways we can do a copy. We can export using one
registration and import using another. This seems the most straight forward way to do
this, just seems a little overkill.
+ // OR we can copy the portlet, then use the RegistrationManager and
RegistrationPolicy to delete the PC from one registration and add it to another. But we
don't actually
+ // create the copy under the toRegistration and we would need to add extra
checks here to make sure the toRegistration has the proper permissions.
+ // Note sure why there is even a copy portlet operation since it can be
replicated by an export and then an import operation.
+
+ org.gatein.pc.api.PortletContext exportedPortletContext =
producer.getPortletInvoker().exportPortlet(PortletStateType.OPAQUE, portalPC);
+ //Change the registration to the new registration and try and do an
import. This should force the new import to be under the new registration context
+ RegistrationLocal.setRegistration(toRegistration);
+ org.gatein.pc.api.PortletContext copiedPortletContext =
producer.getPortletInvoker().importPortlet(PortletStateType.OPAQUE,
exportedPortletContext);
+
+ PortletContext wsrpClonedPC =
WSRPUtils.convertToWSRPPortletContext(copiedPortletContext);
+
+ CopiedPortlet copiedPortlet =
WSRPTypeFactory.createCopiedPortlet(wsrpClonedPC, portletContext.getPortletHandle());
+ copiedPortlets.add(copiedPortlet);
+ }
+ catch (Exception e)
+ {
+ if (log.isWarnEnabled())
+ {
+ log.warn("Error occured while trying to export a portlet.",
e);
+ }
+
+ ErrorCodes.Codes errorCode;
+ String reason;
+ if (e instanceof NoSuchPortletException || e instanceof InvalidHandle)
+ {
+ errorCode = ErrorCodes.Codes.INVALIDHANDLE;
+ reason = "The specified portlet handle is invalid";
+ }
+ else // default error message.
+ {
+ errorCode = ErrorCodes.Codes.OPERATIONFAILED;
+ reason = "Error preparing portlet for export";
+ }
+
+ if (!failedPortletsMap.containsKey(errorCode.name()))
+ {
+ List<String> portletHandles = new ArrayList<String>();
+ portletHandles.add(portletContext.getPortletHandle());
+
+ FailedPortlets failedPortlets =
WSRPTypeFactory.createFailedPortlets(portletHandles, errorCode, reason);
+ failedPortletsMap.put(errorCode.name(), failedPortlets);
+ }
+ else
+ {
+ FailedPortlets failedPortlets =
failedPortletsMap.get(errorCode.name());
+
failedPortlets.getPortletHandles().add(portletContext.getPortletHandle());
+ }
+ }
+ }
+
+ List<FailedPortlets> failedPortlets = new
ArrayList<FailedPortlets>(failedPortletsMap.values());
+ //TODO: handle resources properly
+ ResourceList resourceList = null;
+ CopyPortletsResponse copyPortletsResponse =
WSRPTypeFactory.createCopyPortletsResponse(copiedPortlets, failedPortlets, resourceList);
+ return copyPortletsResponse;
+ }
+ catch (Exception e)
+ {
+ throw WSRP2ExceptionFactory.throwWSException(OperationFailed.class,
"Operation Failed while trying to CopyPortlets.", e);
+ }
+ finally
+ {
+ RegistrationLocal.setRegistration(null);
+ }
}
public PortletContext setPortletProperties(SetPortletProperties setPortletProperties)
Modified:
components/wsrp/trunk/wsrp-producer-war/src/test/java/org/gatein/wsrp/protocol/v2/PortletManagementTestCase.java
===================================================================
---
components/wsrp/trunk/wsrp-producer-war/src/test/java/org/gatein/wsrp/protocol/v2/PortletManagementTestCase.java 2010-09-22
15:01:36 UTC (rev 4308)
+++
components/wsrp/trunk/wsrp-producer-war/src/test/java/org/gatein/wsrp/protocol/v2/PortletManagementTestCase.java 2010-09-22
15:04:28 UTC (rev 4309)
@@ -29,7 +29,9 @@
import org.gatein.exports.impl.ExportManagerImpl;
import org.gatein.pc.api.PortletStateType;
import org.gatein.pc.api.state.PropertyMap;
+import org.gatein.pc.portlet.impl.state.StateConverterV0;
import org.gatein.pc.portlet.state.SimplePropertyMap;
+import org.gatein.pc.portlet.state.StateConverter;
import org.gatein.pc.portlet.state.producer.PortletState;
import org.gatein.pc.portlet.state.producer.ProducerPortletInvoker;
import org.gatein.wsrp.WSRPTypeFactory;
@@ -47,6 +49,8 @@
import org.junit.Test;
import org.junit.runner.RunWith;
import org.oasis.wsrp.v2.BlockingInteractionResponse;
+import org.oasis.wsrp.v2.CopyPortlets;
+import org.oasis.wsrp.v2.CopyPortletsResponse;
import org.oasis.wsrp.v2.ExportPortlets;
import org.oasis.wsrp.v2.ExportPortletsResponse;
import org.oasis.wsrp.v2.ExportedPortlet;
@@ -658,6 +662,8 @@
@Test
public void testExportWithState() throws Exception
{
+ try
+ {
undeploy(TEST_BASIC_PORTLET_WAR);
String sessionPortletArchive = "test-portletstate-portlet.war";
deploy(sessionPortletArchive);
@@ -694,13 +700,20 @@
//quick check that the imported portlet has the right state
ImportPortletsResponse importResponse =
createImportPortletsResponse("foo", portletContextFromExport);
+ assertEquals(1, importResponse.getImportedPortlets().size());
checkStatePortlet(importResponse.getImportedPortlets().get(0).getNewPortletContext().getPortletHandle(),
"new value");
-
}
finally
{
undeploy(sessionPortletArchive);
}
+ }
+ catch (Exception e)
+ {
+ System.out.println("ERROR: an error occured " + this.getClass() +
" testExportWithState");
+ e.printStackTrace();
+ throw e;
+ }
}
//Tests the situation in which we have a stateful export from one server and importing
into another
@@ -760,7 +773,6 @@
protected byte[] createSessionByteValue(String portletHandle, String value) throws
Exception
{
- ProducerPortletInvoker ppinvoker =
(ProducerPortletInvoker)producer.getPortletInvoker();
Map<String, List<String>> properties = new HashMap<String,
List<String>>();
List<String> values = new ArrayList<String>();
values.add(value);
@@ -768,7 +780,9 @@
PropertyMap property = new SimplePropertyMap(properties);
PortletState sstate = new PortletState(portletHandle, property);
- return ppinvoker.getStateConverter().marshall(PortletStateType.OPAQUE, sstate);
+
+ StateConverter stateConverter = new StateConverterV0();
+ return stateConverter.marshall(PortletStateType.OPAQUE, sstate);
}
@Test
@@ -1040,6 +1054,102 @@
return WSRPTypeFactory.createImportPortlets(registrationContext, importContext,
importPortletsList, userContext, lifetime);
}
+
+ @Test
+ public void testSimpleCopyPortletNullRegistrations() throws Exception
+ {
+ String handle = getDefaultHandle();
+ List<PortletContext> portletContexts = createPortletContextList(handle);
+
+ CopyPortlets copyPortlets = createSimpleCopyPortlets(portletContexts);
+ CopyPortletsResponse response = producer.copyPortlets(copyPortlets);
+
+ checkSimpleCopyPortlets(response, handle);
+ }
+
+// Enable when we can have registered and non-registered consumer active at the same
time.
+ @Test
+ public void testSimpleCopyPortletFromRegistrationNull() throws Exception
+ {
+ try
+ {
+ RegistrationData toRegistrationData =
WSRPTypeFactory.createRegistrationData("CONSUMERB",
"CONSUMERAGENTB.0.0", true);
+ RegistrationContext toregistrationContext =
producer.register(toRegistrationData);
+
+ String handle = getDefaultHandle();
+ List<PortletContext> portletContexts = createPortletContextList(handle);
+
+ CopyPortlets copyPortlets = createSimpleCopyPortlets(portletContexts);
+ copyPortlets.setToRegistrationContext(toregistrationContext);
+
+ CopyPortletsResponse response = producer.copyPortlets(copyPortlets);
+
+ checkSimpleCopyPortlets(response, handle);
+ }
+ catch (Exception e)
+ {
+ System.out.println("ERROR: An exception occurred when running
testSimpleCopyPortletFromRegistrationNull");
+ e.printStackTrace();
+ throw new Exception(e);
+ }
+ }
+
+ @Test
+ public void testSimpleCopyPortletToRegistrationNull() throws Exception
+ {
+
producer.getConfigurationService().getConfiguration().getRegistrationRequirements().setRegistrationRequired(true);
+
+ RegistrationData fromRegistrationData =
WSRPTypeFactory.createRegistrationData("CONSUMERA",
"CONSUMERAGENAT.0.0", true);
+ RegistrationContext fromRegistrationContext =
producer.register(fromRegistrationData);
+
+ String handle = getDefaultHandle();
+ List<PortletContext> portletContexts = createPortletContextList(handle);
+
+ CopyPortlets copyPortlets = createSimpleCopyPortlets(portletContexts);
+ copyPortlets.setFromRegistrationContext(fromRegistrationContext);
+ CopyPortletsResponse response = producer.copyPortlets(copyPortlets);
+
+ checkSimpleCopyPortlets(response, handle);
+ }
+
+ @Test
+ public void testSimpleCopyPortletWithRegistrations() throws Exception
+ {
+
producer.getConfigurationService().getConfiguration().getRegistrationRequirements().setRegistrationRequired(true);
+
+ RegistrationData fromRegistrationData =
WSRPTypeFactory.createRegistrationData("CONSUMERA",
"CONSUMERAGENTA.0.0", true);
+ RegistrationContext fromRegistrationContext =
producer.register(fromRegistrationData);
+ RegistrationData toRegistrationData =
WSRPTypeFactory.createRegistrationData("CONSUMERB",
"CONSUMERAGENTB.0.0", true);
+ RegistrationContext toregistrationContext = producer.register(toRegistrationData);
+
+ String handle = getDefaultHandle();
+ List<PortletContext> portletContexts = createPortletContextList(handle);
+
+ CopyPortlets copyPortlets = createSimpleCopyPortlets(portletContexts);
+ copyPortlets.setFromRegistrationContext(fromRegistrationContext);
+ copyPortlets.setToRegistrationContext(toregistrationContext);
+
+ CopyPortletsResponse response = producer.copyPortlets(copyPortlets);
+
+ checkSimpleCopyPortlets(response, handle);
+ }
+
+ protected CopyPortlets createSimpleCopyPortlets(List<PortletContext>
portletContexts)
+ {
+ RegistrationContext toRegistrationContext = null;
+ UserContext toUserContext = null;
+ RegistrationContext fromRegistrationContext = null;
+ UserContext fromUserContext = null;
+ return WSRPTypeFactory.createCopyPortlets(toRegistrationContext, toUserContext,
fromRegistrationContext, fromUserContext, portletContexts);
+ }
+
+ protected void checkSimpleCopyPortlets(CopyPortletsResponse response, String handle)
+ {
+ assertEquals(0, response.getFailedPortlets().size());
+ assertEquals(1, response.getCopiedPortlets().size());
+ assertEquals(handle, response.getCopiedPortlets().get(0).getFromPortletHandle());
+ }
+
protected String getMostUsedPortletWARFileName()
{
return TEST_BASIC_PORTLET_WAR;