1 package org.apache.archiva.repository.storage;
2
3 /*
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
20 */
21
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.io.OutputStream;
25 import java.nio.channels.ReadableByteChannel;
26 import java.nio.channels.WritableByteChannel;
27 import java.nio.file.Path;
28 import java.time.Instant;
29 import java.util.List;
30
31 /**
32 * A instance of this interface represents information about a specific asset in a repository.
33 * The asset may be an real artifact, a directory, or a virtual asset.
34 *
35 * Each asset has a unique path relative to the repository.
36 *
37 * The implementation may read the data directly from the filesystem or underlying storage implementation.
38 *
39 * @author Martin Stockhammer <martin_s@apache.org>
40 */
41 public interface StorageAsset
42 {
43
44 /**
45 * Returns the storage this asset belongs to.
46 * @return
47 */
48 RepositoryStorage getStorage();
49
50 /**
51 * Returns the complete path relative to the repository to the given asset.
52 *
53 * @return A path starting with '/' that uniquely identifies the asset in the repository.
54 */
55 String getPath();
56
57 /**
58 * Returns the name of the asset. It may be just the filename.
59 * @return
60 */
61 String getName();
62
63 /**
64 * Returns the time of the last modification.
65 *
66 * @return
67 */
68 Instant getModificationTime();
69
70 /**
71 * Returns true, if this asset is a container type and contains further child assets.
72 * @return
73 */
74 boolean isContainer();
75
76 /**
77 * List the child assets.
78 *
79 * @return The list of children. If there are no children and if the asset is not a container, a empty list will be returned.
80 */
81 List<StorageAsset> list();
82
83 /**
84 * The size in bytes of the asset. If the asset does not have a size, -1 should be returned.
85 *
86 * @return The size if the asset has a size, otherwise -1
87 */
88 long getSize();
89
90 /**
91 * Returns the input stream of the artifact content.
92 * It will throw a IOException, if the stream could not be created.
93 * Implementations should create a new stream instance for each invocation and make sure that the
94 * stream is proper closed after usage.
95 *
96 * @return The InputStream representing the content of the artifact.
97 * @throws IOException
98 */
99 InputStream getReadStream() throws IOException;
100
101 /**
102 * Returns a NIO representation of the data.
103 *
104 * @return A channel to the asset data.
105 * @throws IOException
106 */
107 ReadableByteChannel getReadChannel() throws IOException;
108
109 /**
110 *
111 * Returns an output stream where you can write data to the asset. The operation is not locked or synchronized.
112 * User of this method have to make sure, that the stream is proper closed after usage.
113 *
114 * @param replace If true, the original data will be replaced, otherwise the data will be appended.
115 * @return The OutputStream where the data can be written.
116 * @throws IOException
117 */
118 OutputStream getWriteStream( boolean replace) throws IOException;
119
120 /**
121 * Returns a NIO representation of the asset where you can write the data.
122 *
123 * @param replace True, if the content should be replaced by the data written to the stream.
124 * @return The Channel for writing the data.
125 * @throws IOException
126 */
127 WritableByteChannel getWriteChannel( boolean replace) throws IOException;
128
129 /**
130 * Replaces the content. The implementation may do an atomic move operation, or keep a backup. If
131 * the operation fails, the implementation should try to restore the old data, if possible.
132 *
133 * The original file may be deleted, if the storage was successful.
134 *
135 * @param newData Replaces the data by the content of the given file.
136 */
137 boolean replaceDataFromFile( Path newData) throws IOException;
138
139 /**
140 * Returns true, if the asset exists.
141 *
142 * @return True, if the asset exists, otherwise false.
143 */
144 boolean exists();
145
146 /**
147 * Creates the asset in the underlying storage, if it does not exist.
148 */
149 void create() throws IOException;
150
151 /**
152 * Returns the real path to the asset, if it exist. Not all implementations may implement this method.
153 * The method throws {@link UnsupportedOperationException}, if and only if {@link #isFileBased()} returns false.
154 *
155 * @return The filesystem path to the asset.
156 * @throws UnsupportedOperationException If the underlying storage is not file based.
157 */
158 Path getFilePath() throws UnsupportedOperationException;
159
160 /**
161 * Returns true, if the asset can return a file path for the given asset. If this is true, the {@link #getFilePath()}
162 * will not throw a {@link UnsupportedOperationException}
163 *
164 * @return
165 */
166 boolean isFileBased();
167
168 /**
169 * Returns true, if there is a parent to this asset.
170 * @return
171 */
172 boolean hasParent();
173
174 /**
175 * Returns the parent of this asset.
176 * @return The asset, or <code>null</code>, if it does not exist.
177 */
178 StorageAsset getParent();
179
180 /**
181 * Returns the asset relative to the given path
182 * @param toPath
183 * @return
184 */
185 StorageAsset resolve(String toPath);
186 }