Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
   /*
    * JBoss, a division of Red Hat
    * Copyright 2011, 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.producer.handlers;
  
  import  com.google.common.base.Function;
  import  com.google.common.collect.HashMultimap;
  import  com.google.common.collect.Multimap;
 
 import java.util.Map;
 import java.util.Set;

Handles PortletManagement calls on behalf of the producer.

Author(s):
Chris Laprun
Version:
$Revision: 11147 $
Since:
2.4
 
 {
    private static final String GET_PORTLET_PROPERTY_DESCRIPTION = "GetPortletPropertyDescription";
    private static final String GET_PORTLET_PROPERTIES = "GetPortletProperties";
    private static final String PORTLET_CONTEXT = "PortletContext";
    private static final String GET_PORTLET_DESCRIPTION = "GetPortletDescription";
 
    private static final Logger log = LoggerFactory.getLogger(PortletManagementHandler.class);
 
    {
       super(producer);
    }
 
    {
       WSRP2ExceptionFactory.throwOperationFailedIfValueIsMissing(getPortletDescription);
       Registration registration = .getRegistrationOrFailIfInvalid(getPortletDescription.getRegistrationContext());
 
       PortletContext portletContext = getPortletDescription.getPortletContext();
       WSRP2ExceptionFactory.throwMissingParametersIfValueIsMissing(portletContext);
 
       UserContext userContext = getPortletDescription.getUserContext();
       checkUserAuthorization(userContext);
 
       // RegistrationLocal.setRegistration is called further down the invocation in ServiceDescriptionHandler.getPortletDescription
       final List<StringdesiredLocales = WSRPUtils.replaceByEmptyListIfNeeded(getPortletDescription.getDesiredLocales());
       PortletDescription description = .getPortletDescription(portletContextdesiredLocalesregistration);
       return WSRPTypeFactory.createPortletDescriptionResponse(description);
    }
 
    {
       WSRP2ExceptionFactory.throwOperationFailedIfValueIsMissing(getPortletPropertyDescription);
 
       PortletContext portletContext = getPortletPropertyDescription.getPortletContext();
       WSRP2ExceptionFactory.throwMissingParametersIfValueIsMissing(portletContext);
 
       try
       {
          Registration registration = .getRegistrationOrFailIfInvalid(getPortletPropertyDescription.getRegistrationContext());
          RegistrationLocal.setRegistration(registration);
 
          UserContext userContext = getPortletPropertyDescription.getUserContext();
          checkUserAuthorization(userContext);
 
          Portlet portlet = getPortletFrom(portletContextregistration);
          PortletInfo info = portlet.getInfo();
          PreferencesInfo prefsInfo = info.getPreferences();
 
          List<PropertyDescriptiondescs = Collections.emptyList();
          if (prefsInfo != null)
          {
             Set keySet = prefsInfo.getKeys();
             descs = new ArrayList<PropertyDescription>(keySet.size());
             for (Object key : keySet)
             {
                PreferenceInfo prefInfo = prefsInfo.getPreference((String)key);
 
                // WSRP Spec 8.7: return only the portion of the Portlet's persistent state the user is allowed to modify
                // if read only status is not determined, we consider it as being read-only to be safe
                Boolean readOnly = prefInfo.isReadOnly();
                if (readOnly != null && !readOnly)
                {
                   //todo: check what we should use key
                   //todo: right now we only support String properties
                   List<StringdesiredLocales = getPortletPropertyDescription.getDesiredLocales();
                   desiredLocales = WSRPUtils.replaceByEmptyListIfNeeded(desiredLocales);
                   PropertyDescription desc = WSRPTypeFactory.createPropertyDescription(prefInfo.getKey(), .);
                   desc.setLabel(Utils.convertToWSRPLocalizedString(prefInfo.getDisplayName(), desiredLocales));
                   desc.setHint(Utils.convertToWSRPLocalizedString(prefInfo.getDescription(), desiredLocales));
                   descs.add(desc);
                }
             }
          }
 
          return WSRPTypeFactory.createPortletPropertyDescriptionResponse(descs);
       }
       finally
       {
          RegistrationLocal.setRegistration(null);
       }
    }
 
    public PortletContext clonePortlet(ClonePortlet clonePortlet)
    {
       WSRP2ExceptionFactory.throwOperationFailedIfValueIsMissing(clonePortlet"ClonePortlet");
 
       PortletContext portletContext = clonePortlet.getPortletContext();
       WSRP2ExceptionFactory.throwMissingParametersIfValueIsMissing(portletContext"PortletContext""ClonePortlet");
 
       Registration registration = .getRegistrationOrFailIfInvalid(clonePortlet.getRegistrationContext());
 
       UserContext userContext = clonePortlet.getUserContext();
       checkUserAuthorization(userContext);
 
       org.gatein.pc.api.PortletContext portalPC = WSRPUtils.convertToPortalPortletContext(portletContext);
       try
       {
          RegistrationLocal.setRegistration(registration);
          return WSRPUtils.convertToWSRPPortletContext(response);
       }
       catch (NoSuchPortletException e)
       {
          throw WSRP2ExceptionFactory.throwWSException(InvalidHandle.class"Failed to create clone for portlet '" + portletContext.getPortletHandle(), e);
       }
       catch (InvalidPortletIdException e)
       {
          throw WSRP2ExceptionFactory.throwWSException(InconsistentParameters.class"Failed to create clone for portlet '" + portletContext.getPortletHandle(), e);
       }
       catch (PortletInvokerException e)
       {
          throw WSRP2ExceptionFactory.throwWSException(OperationFailed.class"Failed to create clone for portlet '" + portletContext.getPortletHandle(), e);
       }
       finally
       {
          RegistrationLocal.setRegistration(null);
       }
    }
 
    {
       WSRP2ExceptionFactory.throwOperationFailedIfValueIsMissing(destroyPortlets"DestroyPortlets");
 
       List<Stringhandles = destroyPortlets.getPortletHandles();
       WSRP2ExceptionFactory.throwMissingParametersIfValueIsMissing(handles"portlet handles to be destroyed""DestroyPortlets");
       handles = WSRPUtils.replaceByEmptyListIfNeeded(handles);
 
       Registration registration = .getRegistrationOrFailIfInvalid(destroyPortlets.getRegistrationContext());
 
       List<org.gatein.pc.api.PortletContextportletContexts = new ArrayList<org.gatein.pc.api.PortletContext>(handles.size());
       for (String handle : handles)
       {
          portletContexts.add(org.gatein.pc.api.PortletContext.createPortletContext(handle));
       }
 
       try
       {
          RegistrationLocal.setRegistration(registration);
          List<DestroyCloneFailurefailuresList = .getPortletInvoker().destroyClones(portletContexts);
          int failuresNumber = failuresList.size();
          List<FailedPortletsfailedPortlets;
          if (failuresNumber > 0)
          {
             // for each reason of failure, record the associated portlet handles, expecting one portlet handle per message
             Multimap<StringStringreasonToHandles = HashMultimap.create(failuresNumber, 1);
             for (DestroyCloneFailure failure : failuresList)
             {
                reasonToHandles.put(failure.getMessage(), failure.getPortletId());
             }
 
             // create a FailedPortlets object for each reason
             failedPortlets = new ArrayList<FailedPortlets>(reasonToHandles.size());
             for (String reason : reasonToHandles.keys())
             {
                failedPortlets.add(WSRPTypeFactory.createFailedPortlets(reasonToHandles.get(reason), ..reason));
             }
          }
          else
          {
             failedPortlets = null;
          }
 
          return WSRPTypeFactory.createDestroyPortletsResponse(failedPortlets);
       }
       catch (PortletInvokerException e)
       {
          throw WSRP2ExceptionFactory.throwWSException(OperationFailed.class"Failed to destroy clones"e);
       }
       finally
       {
          RegistrationLocal.setRegistration(null);
       }
    }
 
    {
       throw WSRP2ExceptionFactory.throwWSException(OperationNotSupported.class"Lifetime operations are not currently supported."null);
    }
 
    {
       throw WSRP2ExceptionFactory.throwWSException(OperationNotSupported.class"Lifetime operations are not currently supported."null);
    }
 
    public CopyPortletsResponse copyPortlets(CopyPortlets copyPortlets)
    {
       WSRP2ExceptionFactory.throwOperationFailedIfValueIsMissing(copyPortlets"copyPortlets");
 
       List<PortletContextportletContexts = copyPortlets.getFromPortletContexts();
       if (!ParameterValidation.existsAndIsNotEmpty(portletContexts) || WSRPUtils.isSingletonListWithNullOrEmptyElement(portletContexts))
       {
          throw WSRP2ExceptionFactory.createWSException(MissingParameters.class"Missing required portletContext in CopyPortlets."null);
       }
 
       Registration fromRegistration = .getRegistrationOrFailIfInvalid(copyPortlets.getFromRegistrationContext());
 
       RegistrationContext toRegistationContext = copyPortlets.getToRegistrationContext();
 
       //if toRegistrationContext 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 = .getRegistrationOrFailIfInvalid(toRegistationContext);
 
       UserContext fromUserContext = copyPortlets.getFromUserContext();
       checkUserAuthorization(fromUserContext);
       UserContext toUserContext = copyPortlets.getToUserContext();
       checkUserAuthorization(toUserContext);
 
       try
       {
          RegistrationLocal.setRegistration(fromRegistration);
 
          Map<StringFailedPortletsfailedPortletsMap = new HashMap<StringFailedPortlets>(portletContexts.size());
 
          List<CopiedPortletcopiedPortlets = 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 straightforward 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.
                // Not 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 = .getPortletInvoker().exportPortlet(.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 = .getPortletInvoker().importPortlet(.exportedPortletContext);
 
                PortletContext wsrpClonedPC = WSRPUtils.convertToWSRPPortletContext(copiedPortletContext);
 
                CopiedPortlet copiedPortlet = WSRPTypeFactory.createCopiedPortlet(wsrpClonedPCportletContext.getPortletHandle());
                copiedPortlets.add(copiedPortlet);
             }
             catch (Exception e)
             {
                if (.isWarnEnabled())
                {
                   .warn("Error occured while trying to export a portlet."e);
                }
 
                ErrorCodes.Codes errorCode;
                String reason;
                final String message = e.getLocalizedMessage();
                if (e instanceof NoSuchPortletException || e instanceof InvalidHandle
                   || (e instanceof IllegalArgumentException && message != null && message.contains(.....)))
                {
                   errorCode = ..;
                   reason = "The specified portlet handle is invalid";
                }
                else // default error message.
                {
                   errorCode = ..;
                   reason = "Error preparing portlet for export";
                }
 
                if (!failedPortletsMap.containsKey(errorCode.name()))
                {
                   List<StringportletHandles = new ArrayList<String>();
                   portletHandles.add(portletContext.getPortletHandle());
 
                   FailedPortlets failedPortlets = WSRPTypeFactory.createFailedPortlets(portletHandleserrorCodereason);
                   failedPortletsMap.put(errorCode.name(), failedPortlets);
                }
                else
                {
                   FailedPortlets failedPortlets = failedPortletsMap.get(errorCode.name());
                   failedPortlets.getPortletHandles().add(portletContext.getPortletHandle());
                }
             }
          }
 
          List<FailedPortletsfailedPortlets = new ArrayList<FailedPortlets>(failedPortletsMap.values());
          //TODO: handle resources properly
          ResourceList resourceList = null;
          return WSRPTypeFactory.createCopyPortletsResponse(copiedPortletsfailedPortletsresourceList);
       }
       catch (Exception e)
       {
          throw WSRP2ExceptionFactory.throwWSException(OperationFailed.class"Operation Failed while trying to CopyPortlets."e);
       }
       finally
       {
          RegistrationLocal.setRegistration(null);
       }
    }
 
    public PortletContext setPortletProperties(SetPortletProperties setPortletProperties)
    {
       WSRP2ExceptionFactory.throwOperationFailedIfValueIsMissing(setPortletProperties"SetPortletProperties");
 
       PortletContext portletContext = setPortletProperties.getPortletContext();
       WSRP2ExceptionFactory.throwMissingParametersIfValueIsMissing(portletContext"PortletContext""SetPortletProperties");
 
       PropertyList propertyList = setPortletProperties.getPropertyList();
       WSRP2ExceptionFactory.throwMissingParametersIfValueIsMissing(propertyList"PropertyList""SetPortletProperties");
 
       Registration registration = .getRegistrationOrFailIfInvalid(setPortletProperties.getRegistrationContext());
 
       checkUserAuthorization(setPortletProperties.getUserContext());
 
       List<Propertyproperties = propertyList.getProperties();
       properties = WSRPUtils.replaceByEmptyListIfNeeded(properties);
       List<ResetPropertyresetProperties = propertyList.getResetProperties();
       resetProperties = WSRPUtils.replaceByEmptyListIfNeeded(resetProperties);
       int changesCount = 0;
       if (ParameterValidation.existsAndIsNotEmpty(properties))
       {
          changesCount += properties.size();
 
          // check that we don't set and reset the same property
          if (ParameterValidation.existsAndIsNotEmpty(resetProperties))
          {
             List<QNamenames = new ArrayList<QName>(WSRPUtils.transform(propertiesnew Function<PropertyQName>()
             {
                public QName apply(Property from)
                {
                   return from.getName();
                }
             }));
             names.retainAll(WSRPUtils.transform(resetPropertiesnew Function<ResetPropertyQName>()
             {
                public QName apply(ResetProperty from)
                {
                   return from.getName();
                }
             }));
 
             if (!names.isEmpty())
             {
                WSRP2ExceptionFactory.throwWSException(InconsistentParameters.class,
                   "Attempted to set and reset at the same time the following properties: " + namesnull);
             }
          }
       }
       if (ParameterValidation.existsAndIsNotEmpty(resetProperties))
       {
          changesCount += resetProperties.size();
       }
 
       if (changesCount > 0)
       {
          List<PropertyChangechanges = new ArrayList<PropertyChange>(changesCount);
 
          if (properties != null)
          {
             for (Property property : properties)
             {
                String value = property.getStringValue();
 
                // todo: deal with XML values...
                // List<Object> values = property.getAny();
                // todo: deal with language?
                // String lang = property.getLang(); 
 
                changes.add(PropertyChange.newUpdate(property.getName().toString(), value));
             }
          }
 
          if (resetProperties != null)
          {
             for (ResetProperty resetProperty : resetProperties)
             {
                changes.add(PropertyChange.newReset(resetProperty.getName().toString()));
             }
          }
 
          try
          {
             RegistrationLocal.setRegistration(registration);
             org.gatein.pc.api.PortletContext resultContext =
                .getPortletInvoker().setProperties(WSRPUtils.convertToPortalPortletContext(portletContext),
                   changes.toArray(new PropertyChange[changes.size()]));
             return WSRPUtils.convertToWSRPPortletContext(resultContext);
          }
          catch (NoSuchPortletException e)
          {
             throw WSRP2ExceptionFactory.throwWSException(InvalidHandle.class"Failed to set properties for portlet '" + portletContext.getPortletHandle() + "'"e);
          }
          catch (InvalidPortletIdException e)
          {
             throw WSRP2ExceptionFactory.throwWSException(InconsistentParameters.class"Failed to set properties for portlet '" + portletContext.getPortletHandle() + "'"e);
          }
          catch (PortletInvokerException e)
          {
             throw WSRP2ExceptionFactory.throwWSException(OperationFailed.class"Failed to set properties for portlet '" + portletContext.getPortletHandle() + "'"e);
          }
          finally
          {
             RegistrationLocal.setRegistration(null);
          }
       }
 
       return portletContext;
    }
 
    public PropertyList getPortletProperties(GetPortletProperties getPortletProperties)
    {
       WSRP2ExceptionFactory.throwOperationFailedIfValueIsMissing(getPortletProperties);
 
       PortletContext portletContext = getPortletProperties.getPortletContext();
       WSRP2ExceptionFactory.throwMissingParametersIfValueIsMissing(portletContext);
 
       Registration registration = .getRegistrationOrFailIfInvalid(getPortletProperties.getRegistrationContext());
 
       UserContext userContext = getPortletProperties.getUserContext();
       checkUserAuthorization(userContext);
 
       List<Stringnames = getPortletProperties.getNames();
       names = WSRPUtils.replaceByEmptyListIfNeeded(names);
 
       Set<Stringkeys = new HashSet<String>(names);
 
       try
       {
          PropertyMap properties;
          org.gatein.pc.api.PortletContext jbpContext = WSRPUtils.convertToPortalPortletContext(portletContext);
 
          RegistrationLocal.setRegistration(registration);
          if (!keys.isEmpty())
          {
             properties = .getPortletInvoker().getProperties(jbpContextkeys);
          }
          else
          {
             properties = .getPortletInvoker().getProperties(jbpContext);
          }
 
          //todo: we need to check that the user can actually modify the properties
          Portlet portlet = getPortletFrom(portletContextregistration);
          PortletInfo info = portlet.getInfo();
 
          PropertyList result = WSRPTypeFactory.createPropertyList();
          int propertyNb = properties.size();
 
          if (propertyNb > 0)
          {
             PreferenceInfo prefInfo;
             String key;
             List<Stringvalues;
             LocalizedString displayName;
 
             for (Map.Entry<StringList<String>> entry : properties.entrySet())
             {
                key = entry.getKey();
                values = entry.getValue();
                prefInfo = info.getPreferences().getPreference(key);
                String lang = .;
                if (prefInfo != null)
                {
                   displayName = prefInfo.getDisplayName();
                   if (displayName != null)
                   {
                      lang = WSRPUtils.toString(displayName.getDefaultLocale());
                   }
                }
 
                // todo: support multi-valued properties
                if (values.size() != 1)
                {
                   throw new UnsupportedOperationException("Currently doesn't support multi-valued properties!");
                }
                result.getProperties().add(WSRPTypeFactory.createProperty(keylangvalues.get(0))); //todo: check what we should use key
             }
          }
 
          return result;
       }
       catch (PortletInvokerException e)
       {
          throw WSRP2ExceptionFactory.throwWSException(InvalidHandle.class"Could not retrieve properties for portlet '" + portletContext + "'"e);
       }
       finally
       {
          RegistrationLocal.setRegistration(null);
       }
    }
 
    public ExportPortletsResponse exportPortlets(ExportPortlets exportPortletsthrows AccessDenied,
    {
       WSRP2ExceptionFactory.throwMissingParametersIfValueIsMissing(exportPortlets"ExportPortlets""ExportPortlets");
 
       List<PortletContextportletContexts = exportPortlets.getPortletContext();
       if (!ParameterValidation.existsAndIsNotEmpty(portletContexts) || WSRPUtils.isSingletonListWithNullOrEmptyElement(portletContexts))
       {
          throw WSRP2ExceptionFactory.createWSException(MissingParameters.class"Missing required list of portlets to export in ExportPortlets."null);
       }
 
       Registration registration = .getRegistrationOrFailIfInvalid(exportPortlets.getRegistrationContext());
 
       UserContext userContext = exportPortlets.getUserContext();
       checkUserAuthorization(userContext);
 
       boolean exportByValueRequired;
       if (exportPortlets.isExportByValueRequired() != null)
       {
          exportByValueRequired = exportPortlets.isExportByValueRequired();
       }
       else
       {
          exportByValueRequired = false;
       }
 
 
       //check that the export manager can handle export by value
       final ExportManager exportManager = .getExportManager();
       if (exportByValueRequired && !exportManager.supportsExportByValue())
       {
          //TODO: instead of passing a string here, we should pass a resource so that its localized
          WSRP2ExceptionFactory.throwWSException(ExportByValueNotSupported.class"The consumer is requesting portlets to be exported by value, but this consumer only supports export by reference."null);
       }
 
 
       List<ExportedPortletexportedPortlets = new ArrayList<ExportedPortlet>(portletContexts.size());
       Map<StringFailedPortletsfailedPortletsMap = new HashMap<StringFailedPortlets>(portletContexts.size());
 
       try
       {
          RegistrationLocal.setRegistration(registration);
 
          ExportContext exportContext;
          final Lifetime askedLifetime = exportPortlets.getLifetime();
          if (askedLifetime != null)
          {
             final XMLGregorianCalendar askedLifetimeCurrentTime = askedLifetime.getCurrentTime();
             long currentTime = toLongDate(askedLifetimeCurrentTime);
             long terminationTime = toLongDate(askedLifetime.getTerminationTime());
             long refreshDuration = askedLifetime.getRefreshDuration().getTimeInMillis(askedLifetimeCurrentTime.toGregorianCalendar());
             exportContext = exportManager.createExportContext(exportByValueRequiredcurrentTimeterminationTimerefreshDuration);
          }
          else
          {
             exportContext = exportManager.createExportContext(exportByValueRequired, -1, -1, -1);
          }
 
          for (PortletContext portletContext : portletContexts)
          {
             try
             {
                WSRP2ExceptionFactory.throwOperationFailedIfValueIsMissing(portletContext"Portlet context");
                String portletHandle = portletContext.getPortletHandle();
                WSRP2ExceptionFactory.throwMissingParametersIfValueIsMissing(portletHandle"Portlet handle""PortletContext");
 
                org.gatein.pc.api.PortletContext portalPC = WSRPUtils.convertToPortalPortletContext(portletContext);
 
                org.gatein.pc.api.PortletContext exportedPortalPC = .getPortletInvoker().exportPortlet(.portalPC);
                if (exportedPortalPC == null)
                {
                   WSRP2ExceptionFactory.throwWSException(InvalidHandle.class"Could not find a portlet with handle " + portletHandle + " in the producer"null);
                }
 
                byte[] portletState = WSRPUtils.getStateOrNullFor(exportedPortalPC);
 
                //get the exportPortletData
                ExportPortletData exportPortletData = exportManager.createExportPortletData(exportContextportletHandleportletState);
 
                //Create the exportedPortlet
                byte[] exportPortletBytes = exportManager.encodeExportPortletData(exportContextexportPortletData);
                ExportedPortlet exportedPortlet = WSRPTypeFactory.createExportedPortlet(portletHandleexportPortletBytes);
                exportedPortlets.add(exportedPortlet);
             }
 
             catch (Exception e)
             {
                if (.isWarnEnabled())
                {
                   .warn("Error occured while trying to export a portlet."e);
                }
 
                ErrorCodes.Codes errorCode;
                String reason;
                if (e instanceof NoSuchPortletException || e instanceof InvalidHandle)
                {
                   errorCode = ..;
                   reason = "The specified portlet handle is invalid";
                }
                else // default error message.
                {
                   errorCode = ..;
                   reason = "Error preparing portlet for export";
                }
 
                final String errorCodeName = errorCode.name();
                if (!failedPortletsMap.containsKey(errorCodeName))
                {
                   List<StringportletHandles = new ArrayList<String>();
                   portletHandles.add(portletContext.getPortletHandle());
 
                   FailedPortlets failedPortlets = WSRPTypeFactory.createFailedPortlets(portletHandleserrorCodereason);
                   failedPortletsMap.put(errorCodeNamefailedPortlets);
                }
                else
                {
                   FailedPortlets failedPortlets = failedPortletsMap.get(errorCodeName);
                   failedPortlets.getPortletHandles().add(portletContext.getPortletHandle());
                }
             }
          }
 
          //TODO: handle resourceLists better (should be using for things like errors)
          ResourceList resourceList = null;
 
          byte[] exportContextBytes = exportManager.encodeExportContextData(exportContext);
 
          Lifetime lifetime = null;
 
          if (exportContext.getCurrentTime() > 0)
          {
             lifetime = new Lifetime();
             lifetime.setCurrentTime(toXMLGregorianCalendar(exportContext.getCurrentTime()));
             lifetime.setTerminationTime(toXMLGregorianCalendar(exportContext.getTermintationTime()));
             lifetime.setRefreshDuration(toDuration(exportContext.getRefreshDuration()));
          }
 
          return WSRPTypeFactory.createExportPortletsResponse(exportContextBytesexportedPortletsnew ArrayList<FailedPortlets>(failedPortletsMap.values()), lifetimeresourceList);
       }
       catch (Exception e)
       {
          throw WSRP2ExceptionFactory.throwWSException(OperationFailed.class"Operation Failed while trying to ExportPortlets."e);
       }
       finally
       {
          RegistrationLocal.setRegistration(null);
       }
    }
 
    {
       WSRP2ExceptionFactory.throwOperationFailedIfValueIsMissing(importPortlets"ImportPortlets");
 
       List<ImportPortletimportPortletList = importPortlets.getImportPortlet();
       if (!ParameterValidation.existsAndIsNotEmpty(importPortletList) || WSRPUtils.isSingletonListWithNullOrEmptyElement(importPortletList))
       {
          throw WSRP2ExceptionFactory.createWSException(MissingParameters.class"Missing required list of portlets to import in ImportPortlets."null);
       }
 
       Registration registration = .getRegistrationOrFailIfInvalid(importPortlets.getRegistrationContext());
 
       // check if we have a valid userContext or not
       UserContext userContext = importPortlets.getUserContext();
       checkUserAuthorization(userContext);
 
       try
       {
          RegistrationLocal.setRegistration(registration);
 
          byte[] importContext = importPortlets.getImportContext();
 
          Lifetime lifeTime = importPortlets.getLifetime();
 
          List<ImportedPortletimportedPortlets = new ArrayList<ImportedPortlet>();
          Map<StringImportPortletsFailedfailedPortletsMap = new HashMap<StringImportPortletsFailed>();
 
          ExportContext exportContext;
          final ExportManager exportManager = .getExportManager();
          try
          {
             exportContext = exportManager.createExportContext(importContext);
          }
          catch (Exception e)
          {
             throw WSRPExceptionFactory.createWSException(OperationFailed.class"Invalid ImportContext."e);
          }
 
          for (ImportPortlet importPortlet : importPortletList)
          {
             try
             {
                byte[] portletData = importPortlet.getExportData();
 
                ExportPortletData exportPortletData;
                if (lifeTime != null)
                {
                   long currentTime = toLongDate(lifeTime.getCurrentTime());
                   long terminationTime = toLongDate(lifeTime.getTerminationTime());
                   long refreshDuration = lifeTime.getRefreshDuration().getTimeInMillis(lifeTime.getCurrentTime().toGregorianCalendar());
                   exportPortletData = exportManager.createExportPortletData(exportContextcurrentTimeterminationTimerefreshDurationportletData);
                }
                else
                {
                   exportPortletData = exportManager.createExportPortletData(exportContext, -1, -1, -1, portletData);
                }
 
                String portletHandle = exportPortletData.getPortletHandle();
                byte[] portletState = exportPortletData.getPortletState();
 
                PortletContext pc = WSRPTypeFactory.createPortletContext(portletHandleportletState);
                org.gatein.pc.api.PortletContext pcPortletContext = WSRPUtils.convertToPortalPortletContext(pc);
 
                org.gatein.pc.api.PortletContext cpc = .getPortletInvoker().importPortlet(.pcPortletContext);
                PortletContext wpc = WSRPUtils.convertToWSRPPortletContext(cpc);
 
                ImportedPortlet importedPortlet = WSRPTypeFactory.createImportedPortlet(importPortlet.getImportID(), wpc);
 
                importedPortlets.add(importedPortlet);
             }
             catch (Exception e)
             {
                if (.isWarnEnabled())
                {
                   .warn("Error occured while trying to import a portlet."e);
                }
 
                ErrorCodes.Codes errorCode;
                String reason;
                if (e instanceof NoSuchPortletException || e instanceof InvalidHandle)
                {
                   errorCode = ..;
                   reason = "The specified portlet handle is invalid";
                }
                else if (e instanceof OperationFailed)
                {
                   errorCode = ..;
                   reason = e.getMessage();
                }
                else if (e instanceof PortletInvokerException || e instanceof UnsupportedOperationException || e instanceof IllegalArgumentException)
                {
                   errorCode = ..;
                   reason = "Error trying to create imported portlet.";
                }
                else // default error message.
                {
                   errorCode = ..;
                   reason = "Error importing portlet.";
                }
 
                if (!failedPortletsMap.containsKey(errorCode.name()))
                {
                   List<StringportleIDs = new ArrayList<String>();
                   portleIDs.add(importPortlet.getImportID());
 
                   ImportPortletsFailed failedPortlets = WSRPTypeFactory.createImportPortletsFailed(portleIDserrorCodereason);
                   failedPortletsMap.put(errorCode.name(), failedPortlets);
                }
                else
                {
                   ImportPortletsFailed failedPortlets = failedPortletsMap.get(errorCode.name());
                   failedPortlets.getImportID().add(importPortlet.getImportID());
                }
             }
          }
 
          ResourceList resourceList = null//TODO: figure out what exactly should be stored in the resource list here
 
          return WSRPTypeFactory.createImportPortletsResponse(importedPortletsnew ArrayList<ImportPortletsFailed>(failedPortletsMap.values()), resourceList);
       }
       finally
       {
          RegistrationLocal.setRegistration(null);
       }
    }
 
    public List<ExtensionreleaseExport(ReleaseExport releaseExport)
    {
       try
       {
          if (releaseExport != null && releaseExport.getExportContext() != null)
          {
             .getExportManager().releaseExport(releaseExport.getExportContext());
          }
       }
       catch (Exception e)
       {
          if (.isWarnEnabled())
          {
             .warn("Error occured while trying to perform a ReleaseExport"e);
          }
       }
 
       //this method shouldn't return anything
       return WSRPTypeFactory.createReturnAny().getExtensions();
    }
 
    {
       //this method is only valid if the producer can handle exporting by reference.
       if (.getExportManager().getPersistenceManager() == null)
       {
          WSRP2ExceptionFactory.throwWSException(OperationNotSupported.class"This producer does not support export by reference."null);
       }
 
       WSRP2ExceptionFactory.throwOperationFailedIfValueIsMissing(setExportLifetime"setExportLifetimePortlets");
 
       byte[] exportContextBytes = setExportLifetime.getExportContext();
       //NOTE: we can't throw a MissingParameterException since its not allowed as part of the spec
       if (exportContextBytes == null)
       {
          WSRPExceptionFactory.throwWSException(OperationFailed.class"Cannot call setExportLifetime with an empty ExportContext."null);
       }
 
       Registration registration = .getRegistrationOrFailIfInvalid(setExportLifetime.getRegistrationContext());
 
       // check if we have a valid userContext or not
       UserContext userContext = setExportLifetime.getUserContext();
       checkUserAuthorization(userContext);
 
       try
       {
          RegistrationLocal.setRegistration(registration);
 
          ExportContext exportContext;
          long currentTime = toLongDate(setExportLifetime.getLifetime().getCurrentTime());
          long terminationTime = toLongDate(setExportLifetime.getLifetime().getTerminationTime());
          long refreshDuration = setExportLifetime.getLifetime().getRefreshDuration().getTimeInMillis(setExportLifetime.getLifetime().getCurrentTime().toGregorianCalendar());
          exportContext = .getExportManager().setExportLifetime(exportContextBytescurrentTimeterminationTimerefreshDuration);
 
          return getLifetime(exportContext);
       }
       catch (Exception e)
       {
          throw WSRPExceptionFactory.createWSException(OperationFailed.class"Operation Failed while trying to setExportLifetime."e);
       }
       finally
       {