View Javadoc
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  
21  import java.io.ByteArrayInputStream;
22  import java.io.IOException;
23  import java.io.InputStream;
24  import java.net.URL;
25  
26  import org.slf4j.Logger;
27  import org.slf4j.LoggerFactory;
28  
29  import com.vaadin.server.StreamResource;
30  import com.vaadin.ui.Link;
31  
32  /**
33   * The Class ExternalAttachmentDownloadLink.
34   */
35  public final class ExternalAttachmentDownloadLink extends Link {
36  
37  	/** The Constant serialVersionUID. */
38  	private static final long serialVersionUID = 1L;
39  
40  	/** The Constant LOGGER. */
41  	private static final Logger LOGGER = LoggerFactory.getLogger(ExternalAttachmentDownloadLink.class);
42  
43  	/** The file name. */
44  	private final String fileName;
45  
46  	/** The file type. */
47  	private final String fileType;
48  
49  	/** The file url. */
50  	private final String fileUrl;
51  
52  	/**
53  	 * Instantiates a new external attachment download link.
54  	 *
55  	 * @param fileName
56  	 *            the file name
57  	 * @param fileType
58  	 *            the file type
59  	 * @param fileUrl
60  	 *            the file url
61  	 */
62  	public ExternalAttachmentDownloadLink(final String fileName, final String fileType, final String fileUrl) {
63  		super();
64  		this.fileName = fileName;
65  		this.fileType = fileType;
66  		this.fileUrl = fileUrl;
67  		setCaption(fileName);
68  		setDescription("Download " + fileName);
69  		setTargetName("_blank");
70  	}
71  
72  	@Override
73  	public void attach() {
74  		super.attach();
75  
76  		final StreamResource.StreamSource source = new StreamResource.StreamSource() {
77  
78  			/** The Constant serialVersionUID. */
79  			private static final long serialVersionUID = 1L;
80  
81  			@Override
82  			public InputStream getStream() {
83  
84  				try {
85  					return new URL(fileUrl).openStream();
86  				} catch (final IOException e) {
87  					LOGGER.warn("Problem opening url:"+ fileUrl,e);
88  					return new ByteArrayInputStream(new byte[0]);
89  				}
90  			}
91  		};
92  
93  		final StreamResource resource = new StreamResource(source, fileName);
94  
95  		resource.getStream().setParameter("Content-Disposition", "attachment;filename=\"" + fileName + "\"");
96  		resource.setMIMEType("application/" + fileType);
97  		resource.setCacheTime(0);
98  
99  		setResource(resource);
100 	}
101 }