001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.wicket.markup.html.link;
018:
019: import java.io.File;
020:
021: import org.apache.wicket.request.target.resource.ResourceStreamRequestTarget;
022: import org.apache.wicket.util.resource.FileResourceStream;
023: import org.apache.wicket.util.resource.IResourceStream;
024: import org.apache.wicket.util.string.Strings;
025:
026: /**
027: * A link that streams a file to the client. When clicked this link will prompt
028: * the save as dialog in the browser.
029: *
030: * @author Igor Vaynberg (ivaynberg)
031: */
032: public class DownloadLink extends Link {
033: /**
034: *
035: */
036: private static final long serialVersionUID = 1L;
037:
038: /**
039: * File to stream
040: */
041: private final File file;
042:
043: /**
044: * File name to stream
045: */
046: private final String fileName;
047:
048: /**
049: * Constructor. File name used will be the result of
050: * <code>file.getName()</code>
051: *
052: * @param id
053: * component id
054: * @param file
055: * file to stream to client
056: */
057: public DownloadLink(String id, File file) {
058: super (id);
059: if (file == null) {
060: throw new IllegalArgumentException("file cannot be null");
061: }
062: this .file = file;
063: this .fileName = file.getName();
064: }
065:
066: /**
067: * Constructor
068: *
069: * @param id
070: * component id
071: * @param file
072: * file to stream to client
073: * @param fileName
074: * name of the file
075: */
076: public DownloadLink(String id, File file, String fileName) {
077: super (id);
078: if (file == null) {
079: throw new IllegalArgumentException("file cannot be null");
080: }
081: if (Strings.isEmpty(fileName)) {
082: throw new IllegalArgumentException(
083: "fileName cannot be an empty string");
084: }
085: this .file = file;
086: this .fileName = fileName;
087: }
088:
089: /**
090: *
091: * @see org.apache.wicket.markup.html.link.Link#onClick()
092: */
093: public void onClick() {
094: IResourceStream resourceStream = new FileResourceStream(
095: new org.apache.wicket.util.file.File(file));
096: getRequestCycle().setRequestTarget(
097: new ResourceStreamRequestTarget(resourceStream) {
098: public String getFileName() {
099: return fileName;
100: }
101: });
102: }
103: }
|