ExternalAttachmentDownloadLink.java

  1. /*
  2.  * Copyright 2014 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 java.io.ByteArrayInputStream;
  21. import java.io.IOException;
  22. import java.io.InputStream;
  23. import java.net.URL;

  24. import org.slf4j.Logger;
  25. import org.slf4j.LoggerFactory;

  26. import com.vaadin.server.StreamResource;
  27. import com.vaadin.ui.Link;

  28. /**
  29.  * The Class ExternalAttachmentDownloadLink.
  30.  */
  31. public final class ExternalAttachmentDownloadLink extends Link {

  32.     /** The Constant serialVersionUID. */
  33.     private static final long serialVersionUID = 1L;

  34.     /** The Constant LOGGER. */
  35.     private static final Logger LOGGER = LoggerFactory.getLogger(ExternalAttachmentDownloadLink.class);

  36.     /** The file name. */
  37.     private final String fileName;

  38.     /** The file type. */
  39.     private final String fileType;

  40.     /** The file url. */
  41.     private final String fileUrl;

  42.     /**
  43.      * Instantiates a new external attachment download link.
  44.      *
  45.      * @param fileName
  46.      *            the file name
  47.      * @param fileType
  48.      *            the file type
  49.      * @param fileUrl
  50.      *            the file url
  51.      */
  52.     public ExternalAttachmentDownloadLink(final String fileName, final String fileType, final String fileUrl) {
  53.         super();
  54.         this.fileName = fileName;
  55.         this.fileType = fileType;
  56.         this.fileUrl = fileUrl;
  57.         setCaption(fileName);
  58.         setDescription("Download " + fileName);
  59.         setTargetName("_blank");
  60.     }

  61.     @Override
  62.     public void attach() {
  63.         super.attach();

  64.         final StreamResource.StreamSource source = new StreamResource.StreamSource() {

  65.             /** The Constant serialVersionUID. */
  66.             private static final long serialVersionUID = 1L;

  67.             @Override
  68.             public InputStream getStream() {

  69.                 try {
  70.                     return new URL(fileUrl).openStream();
  71.                 } catch (final IOException e) {
  72.                     LOGGER.warn("Problem opening url:"+ fileUrl,e);
  73.                     return new ByteArrayInputStream(new byte[0]);
  74.                 }
  75.             }
  76.         };

  77.         final StreamResource resource = new StreamResource(source, fileName);

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

  81.         setResource(resource);
  82.     }
  83. }