Skip to main content
summaryrefslogtreecommitdiffstats
blob: a822dc4d9c7196e11dcb83bf5665c68f71a11f57 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/********************************************************************************
 * Copyright (c) 2015-2018 Contributors to the Eclipse Foundation
 *
 * See the NOTICE file(s) distributed with this work for additional
 * information regarding copyright ownership.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v. 2.0 which is available at
 * http://www.eclipse.org/legal/epl-2.0.
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 ********************************************************************************/


package org.eclipse.mdm.api.base.file;

import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Path;
import java.util.Collection;

import org.eclipse.mdm.api.base.model.Entity;
import org.eclipse.mdm.api.base.model.FileLink;

/**
 * Provides stream and download operations to access externally linked files.
 *
 * @since 1.0.0
 * @author Viktor Stoehr, Gigatronik Ingolstadt GmbH
 */
public interface FileService {

	// ======================================================================
	// Public methods
	// ======================================================================

	/**
	 * Sequential download of given {@link FileLink}s into given target {@code
	 * Path}. Remote paths linked multiple times are downloaded only once.
	 *
	 * @param entity
	 *            Used for security checks.
	 * @param target
	 *            Must be a directory.
	 * @param fileLinks
	 *            Collection of {@code FileLink}s to download.
	 * @throws IOException
	 *             Thrown if unable to download files.
	 */
	default void downloadSequential(Entity entity, Path target, Collection<FileLink> fileLinks) throws IOException {
		downloadSequential(entity, target, fileLinks, null);
	}

	/**
	 * Sequential download of given {@link FileLink}s into given target {@code
	 * Path}. Remote paths linked multiple times are downloaded only once. The
	 * download progress may be traced with a progress listener.
	 *
	 * @param entity
	 *            Used for security checks.
	 * @param target
	 *            Must be a directory.
	 * @param fileLinks
	 *            Collection of {@code FileLink}s to download.
	 * @param progressListener
	 *            The progress listener.
	 * @throws IOException
	 *             Thrown if unable to download files.
	 */
	void downloadSequential(Entity entity, Path target, Collection<FileLink> fileLinks,
			ProgressListener progressListener) throws IOException;

	/**
	 * Parallel download of given {@link FileLink}s into given target {@code
	 * Path}. Remote paths linked multiple times are downloaded only once.
	 *
	 * @param entity
	 *            Used for security checks.
	 * @param target
	 *            Must be a directory.
	 * @param fileLinks
	 *            Collection of {@code FileLink}s to download.
	 * @throws IOException
	 *             Thrown if unable to download files.
	 */
	default void downloadParallel(Entity entity, Path target, Collection<FileLink> fileLinks) throws IOException {
		downloadParallel(entity, target, fileLinks, null);
	}

	/**
	 * Parallel download of given {@link FileLink}s into given target {@code
	 * Path}. Remote paths linked multiple times are downloaded only once. The
	 * download progress may be traced with a progress listener.
	 *
	 * @param entity
	 *            Used for security checks.
	 * @param target
	 *            Must be a directory.
	 * @param fileLinks
	 *            Collection of {@code FileLink}s to download.
	 * @param progressListener
	 *            The progress listener.
	 * @throws IOException
	 *             Thrown if unable to download files.
	 */
	void downloadParallel(Entity entity, Path target, Collection<FileLink> fileLinks, ProgressListener progressListener)
			throws IOException;

	/**
	 * Downloads given {@link FileLink} into given target {@code Path}.
	 *
	 * @param entity
	 *            Used for security checks.
	 * @param target
	 *            Must be a directory.
	 * @param fileLink
	 *            The {@code FileLink} to download.
	 * @throws IOException
	 *             Thrown if unable to download file.
	 */
	default void download(Entity entity, Path target, FileLink fileLink) throws IOException {
		download(entity, target, fileLink, null);
	}

	/**
	 * Downloads given {@link FileLink} into given target {@code Path}. The
	 * download progress may be traced with a progress listener.
	 *
	 * @param entity
	 *            Used for security checks.
	 * @param target
	 *            Must be a directory.
	 * @param fileLink
	 *            The {@code FileLink} to download.
	 * @param progressListener
	 *            The progress listener.
	 * @throws IOException
	 *             Thrown if unable to download file.
	 */
	void download(Entity entity, Path target, FileLink fileLink, ProgressListener progressListener) throws IOException;

	/**
	 * Opens an {@code InputStream} for given {@link FileLink}. The returned
	 * stream should be consumed with a try-with-resources statement to ensure
	 * the stream is closed properly, e.g:
	 *
	 * <pre>
	 * try (InputStream is = openStream(entity, fileLink)) {
	 * 	// do something useful
	 * }
	 * </pre>
	 *
	 * @param entity
	 *            Used for security checks.
	 * @param fileLink
	 *            The {@code FileLink} to be provided as a stream.
	 * @return A consumable {@code InputStream} is returned.
	 * @throws IOException
	 *             Thrown if unable to provide as stream.
	 */
	default InputStream openStream(Entity entity, FileLink fileLink) throws IOException {
		return openStream(entity, fileLink, null);
	}

	/**
	 * Opens an {@code InputStream} for given {@link FileLink}. The progress of
	 * the stream consumption may be traced with a progress listener. The
	 * returned stream should be consumed with a try-with-resources statement to
	 * ensure the stream is closed properly, e.g:
	 *
	 * <pre>
	 * try (InputStream is = openStream(entity, fileLink)) {
	 * 	// do something useful
	 * }
	 * </pre>
	 *
	 * @param entity
	 *            Used for security checks.
	 * @param fileLink
	 *            The {@code FileLink} to be provided as a stream.
	 * @param progressListener
	 *            The progress listener.
	 * @return A consumable {@code InputStream} is returned.
	 * @throws IOException
	 *             Thrown if unable to provide as stream.
	 */
	InputStream openStream(Entity entity, FileLink fileLink, ProgressListener progressListener) throws IOException;

	/**
	 * Loads the file size for given {@link FileLink}. The file size is stored
	 * in the given {@code FileLink}.
	 *
	 * @param entity
	 *            Used for security checks.
	 * @param fileLink
	 *            The {@code FileLink} whose file size is requested.
	 * @throws IOException
	 *             Thrown if unable to load file size.
	 */
	void loadSize(Entity entity, FileLink fileLink) throws IOException;

	// ======================================================================
	// Inner classes
	// ======================================================================

	/**
	 * A {@link FileService} consumer may implement this interface to get
	 * notified about stream or download progress.
	 */
	@FunctionalInterface
	interface ProgressListener {

		/**
		 * Progress notification.
		 *
		 * @param bytes
		 *            Number of transferred bytes since last notification.
		 * @param percent
		 *            The overall percentage, where percent p &isin; [0.0, 1.0].
		 */
		void progress(int bytes, float percent);

	}

}

Back to the top