ExternalAttachmentDownloadLink.java

  1. /*
  2.  * Copyright 2010-2025 James Pether Sörling
  3.  *
  4.  * Licensed under the Apache License, Version 2.0 (the "License");
  5.  * you may not use this file except in compliance with the License.
  6.  * You may obtain a copy of the License at
  7.  *
  8.  *   http://www.apache.org/licenses/LICENSE-2.0
  9.  *
  10.  * Unless required by applicable law or agreed to in writing, software
  11.  * distributed under the License is distributed on an "AS IS" BASIS,
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13.  * See the License for the specific language governing permissions and
  14.  * limitations under the License.
  15.  *
  16.  *  $Id$
  17.  *  $HeadURL$
  18. */
  19. package com.hack23.cia.web.impl.ui.application.views.common.pagelinks.impl;

  20. import com.vaadin.server.StreamResource;
  21. import com.vaadin.ui.Link;

  22. /**
  23.  * The Class ExternalAttachmentDownloadLink.
  24.  */
  25. public final class ExternalAttachmentDownloadLink extends Link {

  26.     /** The Constant serialVersionUID. */
  27.     private static final long serialVersionUID = 1L;

  28.     /** The file name. */
  29.     private final String fileName;

  30.     /** The file type. */
  31.     private final String fileType;

  32.     /** The file url. */
  33.     private final String fileUrl;

  34.     /**
  35.      * Instantiates a new external attachment download link.
  36.      *
  37.      * @param fileName
  38.      *            the file name
  39.      * @param fileType
  40.      *            the file type
  41.      * @param fileUrl
  42.      *            the file url
  43.      */
  44.     public ExternalAttachmentDownloadLink(final String fileName, final String fileType, final String fileUrl) {
  45.         super();
  46.         this.fileName = fileName;
  47.         this.fileType = fileType;
  48.         this.fileUrl = fileUrl;
  49.         setCaption(fileName);
  50.         setDescription("Download " + fileName);
  51.         setTargetName("_blank");
  52.     }

  53.     @Override
  54.     public void attach() {
  55.         super.attach();

  56.         final StreamResource.StreamSource source = new StreamSourceImplementation(fileUrl);
  57.         final StreamResource resource = new StreamResource(source, fileName);

  58.         resource.getStream().setParameter("Content-Disposition", "attachment;filename=\"" + fileName + "\"");
  59.         resource.setMIMEType("application/" + fileType);
  60.         resource.setCacheTime(0);

  61.         setResource(resource);
  62.     }
  63. }