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(exportByValueRequired, currentTime, terminationTime, refreshDuration);
}
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 = producer.getPortletInvoker().exportPortlet(PortletStateType.OPAQUE, 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(exportContext, portletHandle, portletState);
//Create the exportedPortlet
byte[] exportPortletBytes = exportManager.encodeExportPortletData(exportContext, exportPortletData);
ExportedPortlet exportedPortlet = WSRPTypeFactory.createExportedPortlet(portletHandle, exportPortletBytes);
exportedPortlets.add(exportedPortlet);
}
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";
}
final String errorCodeName = errorCode.name();
if (!failedPortletsMap.containsKey(errorCodeName))
{
List<String> portletHandles = new ArrayList<String>();
portletHandles.add(portletContext.getPortletHandle());
FailedPortlets failedPortlets = WSRPTypeFactory.createFailedPortlets(portletHandles, errorCode, reason);
failedPortletsMap.put(errorCodeName, failedPortlets);
}
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(exportContextBytes, exportedPortlets, new ArrayList<FailedPortlets>(failedPortletsMap.values()), lifetime, resourceList);
}
catch (Exception e)