/**
   * Transforms the report into a StreamResource for downloading
   *
   * @param format
   * @return
   */
  protected StreamResource createResourceForExport(final Format format) {
    return new StreamResource(
        new StreamSource() {
          private static final long serialVersionUID = -5207351556320212325L;

          @Override
          public InputStream getStream() {
            if (jasperReport != null) {
              ByteArrayOutputStream os = new ByteArrayOutputStream();
              reportGenerator.setShowMargins(showMargins.getValue());
              reportGenerator.executeReport(
                  jasperReport,
                  currentParameters,
                  jrDataSource,
                  format,
                  ((WrappedHttpSession) VaadinSession.getCurrent().getSession()).getHttpSession(),
                  VaadinSession.getCurrent().getLocale(),
                  os);
              return new ByteArrayInputStream(os.toByteArray());
            } else {
              return null;
            }
          }
        },
        "report." + format.name());
  }