Download Service in Gatein

If you work with Gatein and you need to download a resource received by an external service, you must use the Download Service. It is declared in the exo.portal.component.web.server library. In your portlet action you can use the following code:

import org.exoplatform.container.ExoContainer;
import org.exoplatform.container.ExoContainerContext;
import org.exoplatform.download.DownloadResource;
import org.exoplatform.download.DownloadService;
import org.exoplatform.download.InputStreamDownloadResource;
...
public String download(MyBean bean) {
...

  ExoContainer container = ExoContainerContext.getCurrentContainer();

  DownloadService dservice = (DownloadService)   container.getComponentInstanceOfType(DownloadService.class);

  InputStream inpuStream = new ByteArrayInputStream(bytes);

  DownloadResource downloadResource = new InputStreamDownloadResource(nomeFile, inpuStream, getContentType(nomeFile));

  downloadResource.setDownloadName(nomeFile);

  downloadId = dservice.addDownloadResource(downloadResource);

  return downloadId;

                }

where bytes is the bytes array received by an external service and nomeFile is the name of the file that you would download. This code insert the bytes array in an internal buffer of the portal. The variable downloadId is the generated downloadId to call the resource from your page.

Below an example of a Richfaces command link in a page that allows to download the resource through the generated downloadId:

<a4j:commandLink id="idDownload"

action="#{elencoRichieste.download(myBean)}"

oncomplete="window.location.href='/portal/download?resourceId=#{elencoRichieste.downloadId}'"

ajaxSingle="true" reRender="idDownload"/>

The button calls the download action from the JSF component and then find the generated downloadId after the action.  The JSF component need also the following code:

                            private String downloadId;

                            ...

                            public String getDownloadId() {

  return downloadId;

  }

  public void setDownloadId(String downloadId) {

  this.downloadId = downloadId;

  }

After the call to the action, the resource is download through the oncomplete property connecting to the URL /portal/download of Gatein and passing the generated resourceId