forked from jooby-project/jooby
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAsset.java
More file actions
112 lines (97 loc) · 2.61 KB
/
Asset.java
File metadata and controls
112 lines (97 loc) · 2.61 KB
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
/**
* Jooby https://jooby.io
* Apache License Version 2.0 https://jooby.io/LICENSE.txt
* Copyright 2014 Edgar Espina
*/
package io.jooby;
import io.jooby.internal.FileAsset;
import io.jooby.internal.JarAsset;
import io.jooby.internal.URLAsset;
import javax.annotation.Nonnull;
import java.io.IOException;
import java.io.InputStream;
import java.net.JarURLConnection;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.ByteBuffer;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Base64;
/**
* Represent an static resource file. Asset from file system and classpath are supported.
*
* @author edgar
* @since 2.0.0
*/
public interface Asset extends AutoCloseable {
/**
* Creates a file system asset.
*
* @param resource File resource.
* @return File resource asset.
*/
static Asset create(@Nonnull Path resource) {
return new FileAsset(resource);
}
/**
* Creates a URL asset with the given path.
* @param path Asset path.
* @param resource Asset URL.
* @return URL asset.
*/
static Asset create(@Nonnull String path, @Nonnull URL resource) {
try {
if ("jar".equals(resource.getProtocol())) {
return new JarAsset((JarURLConnection) resource.openConnection());
}
if ("file".equals(resource.getProtocol())) {
return create(Paths.get(resource.toURI()));
}
return new URLAsset(resource, path);
} catch (IOException | URISyntaxException x) {
throw SneakyThrows.propagate(x);
}
}
/**
* @return Asset size (in bytes) or <code>-1</code> if undefined.
*/
long getSize();
/**
* @return The last modified date if possible or -1 when isn't.
*/
long getLastModified();
/**
* @return True if the asset is a directory (when possible).
*/
boolean isDirectory();
/**
* Computes a weak e-tag value from asset.
*
* @return A weak e-tag.
*/
default @Nonnull String getEtag() {
StringBuilder b = new StringBuilder(32);
b.append("W/\"");
Base64.Encoder encoder = Base64.getEncoder();
int hashCode = hashCode();
long lastModified = getLastModified();
long length = getSize();
ByteBuffer buffer = ByteBuffer.allocate(Long.BYTES);
buffer.putLong(lastModified ^ hashCode);
b.append(Long.toHexString(lastModified ^ hashCode));
buffer.clear();
buffer.putLong(length ^ hashCode);
b.append(encoder.encodeToString(buffer.array()));
b.append('"');
return b.toString();
}
/**
* @return Asset media type.
*/
@Nonnull
MediaType getContentType();
/**
* @return Asset content.
*/
InputStream stream();
}