|
| 1 | +/* |
| 2 | + * Copyright 2000-2025 Vaadin Ltd. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 5 | + * use this file except in compliance with the License. You may obtain a copy of |
| 6 | + * 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, WITHOUT |
| 12 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | + * License for the specific language governing permissions and limitations under |
| 14 | + * the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.vaadin.flow.server; |
| 18 | + |
| 19 | +import java.io.File; |
| 20 | +import java.util.Optional; |
| 21 | + |
| 22 | +import com.vaadin.flow.dom.Element; |
| 23 | +import com.vaadin.flow.function.SerializableFunction; |
| 24 | +import com.vaadin.flow.server.streams.ClassDownloadHandler; |
| 25 | +import com.vaadin.flow.server.streams.DownloadResponse; |
| 26 | +import com.vaadin.flow.server.streams.FileDownloadHandler; |
| 27 | +import com.vaadin.flow.server.streams.InputStreamDownloadHandler; |
| 28 | +import com.vaadin.flow.server.streams.ServletResourceDownloadHandler; |
| 29 | + |
| 30 | +/** |
| 31 | + * Interface for handling download of data from the server to the client. |
| 32 | + * |
| 33 | + * @since 24.8 |
| 34 | + */ |
| 35 | +@FunctionalInterface |
| 36 | +public interface DownloadHandler extends ElementRequestHandler { |
| 37 | + |
| 38 | + /** |
| 39 | + * Method that is called when the client wants to download from the url |
| 40 | + * stored for this specific handler registration. |
| 41 | + * |
| 42 | + * @param event |
| 43 | + * download event containing the necessary data for writing the |
| 44 | + * response |
| 45 | + */ |
| 46 | + void handleDownloadRequest(DownloadRequest event); |
| 47 | + |
| 48 | + default void handleRequest(VaadinRequest request, VaadinResponse response, |
| 49 | + VaadinSession session, Element owner) { |
| 50 | + String fileName = getUrlPostfix() == null ? "" : getUrlPostfix(); |
| 51 | + |
| 52 | + DownloadRequest event = new DownloadRequest(request, response, session, |
| 53 | + fileName); |
| 54 | + event.withOwningComponent(owner) |
| 55 | + .withContentType(Optional |
| 56 | + .ofNullable(response.getService().getMimeType(fileName)) |
| 57 | + .orElse("application/octet-stream")); |
| 58 | + |
| 59 | + handleDownloadRequest(event); |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Get a download handler for serving given {@link File}. |
| 64 | + * |
| 65 | + * @param file |
| 66 | + * file to server for download |
| 67 | + * @return DownloadHandler instance for file |
| 68 | + */ |
| 69 | + static DownloadHandler forFile(File file) { |
| 70 | + return new FileDownloadHandler(file); |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Get a download handler for serving given {@link File}. |
| 75 | + * |
| 76 | + * @param file |
| 77 | + * file to server for download |
| 78 | + * @param name |
| 79 | + * download name to use |
| 80 | + * @return DownloadHandler instance for file |
| 81 | + */ |
| 82 | + static DownloadHandler forFile(File file, String name) { |
| 83 | + return new FileDownloadHandler(file, name); |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Generate a download handler for class resource. |
| 88 | + * <p> |
| 89 | + * For instance for the file {@code resources/com/example/ui/MyData.json} |
| 90 | + * and class {@code com.example.ui.MyData} the definition would be |
| 91 | + * {@code forClassResource(MyData.class, "MyData.json")} |
| 92 | + * |
| 93 | + * @param clazz |
| 94 | + * class for resource module |
| 95 | + * @param resourceName |
| 96 | + * name of class resource |
| 97 | + * @return DownloadHandler instance for class resource |
| 98 | + */ |
| 99 | + static DownloadHandler forClassResource(Class<?> clazz, |
| 100 | + String resourceName) { |
| 101 | + return new ClassDownloadHandler(clazz, resourceName); |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * Generate a download handler for class resource. |
| 106 | + * <p> |
| 107 | + * For instance for the file {@code resources/com/example/ui/MyData.json} |
| 108 | + * and class {@code com.example.ui.MyData} the definition would be |
| 109 | + * {@code forClassResource(MyData.class, "MyData.json", "Data.json")} |
| 110 | + * |
| 111 | + * @param clazz |
| 112 | + * class for resource module |
| 113 | + * @param resourceName |
| 114 | + * name of class resource |
| 115 | + * @param fileName |
| 116 | + * download resourceName to use |
| 117 | + * @return DownloadHandler instance for class resource |
| 118 | + */ |
| 119 | + static DownloadHandler forClassResource(Class<?> clazz, String resourceName, |
| 120 | + String fileName) { |
| 121 | + return new ClassDownloadHandler(clazz, resourceName, fileName); |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * Generate a download handler for a servlet resource. |
| 126 | + * <p> |
| 127 | + * For instance for the file {@code webapp/WEB-INF/servlet.json} the path |
| 128 | + * would be {@code /WEB-INF/servlet.json} |
| 129 | + * |
| 130 | + * @param path |
| 131 | + * the servlet path to the file |
| 132 | + * @return DownloadHandler instance for servlet resource |
| 133 | + */ |
| 134 | + static DownloadHandler forServletResource(String path) { |
| 135 | + return new ServletResourceDownloadHandler(path); |
| 136 | + } |
| 137 | + |
| 138 | + /** |
| 139 | + * Generate a download handler for a servlet resource. |
| 140 | + * <p> |
| 141 | + * For instance for the file {@code webapp/WEB-INF/servlet.json} the path |
| 142 | + * would be {@code /WEB-INF/servlet.json} |
| 143 | + * <p> |
| 144 | + * Name is appended to the download url as the logical name of the target |
| 145 | + * file. |
| 146 | + * |
| 147 | + * @param path |
| 148 | + * the servlet path to the file |
| 149 | + * @param name |
| 150 | + * resource name |
| 151 | + * @return DownloadHandler instance for servlet resource |
| 152 | + */ |
| 153 | + static DownloadHandler forServletResource(String path, String name) { |
| 154 | + return new ServletResourceDownloadHandler(path, name); |
| 155 | + } |
| 156 | + |
| 157 | + /** |
| 158 | + * Generate a function for downloading from a generated inputStream. |
| 159 | + * |
| 160 | + * @param handler |
| 161 | + * handler function that will be called on download |
| 162 | + * @return DownloadHandler instance for inputStream |
| 163 | + */ |
| 164 | + static DownloadHandler fromInputStream( |
| 165 | + SerializableFunction<DownloadRequest, DownloadResponse> handler) { |
| 166 | + return new InputStreamDownloadHandler(handler); |
| 167 | + } |
| 168 | + |
| 169 | + /** |
| 170 | + * Generate a function for downloading from a generated inputStream. |
| 171 | + * |
| 172 | + * @param handler |
| 173 | + * handler function that will be called on download |
| 174 | + * @param name |
| 175 | + * resource name |
| 176 | + * @return DownloadHandler instance for inputStream |
| 177 | + */ |
| 178 | + static DownloadHandler fromInputStream( |
| 179 | + SerializableFunction<DownloadRequest, DownloadResponse> handler, |
| 180 | + String name) { |
| 181 | + return new InputStreamDownloadHandler(handler, name); |
| 182 | + } |
| 183 | +} |
0 commit comments