Skip to content

Commit a5acedf

Browse files
committed
make asset to implement autocloseable
- rename classpathassetsource
1 parent f643bb8 commit a5acedf

File tree

8 files changed

+209
-191
lines changed

8 files changed

+209
-191
lines changed

jooby/src/main/java/io/jooby/Asset.java

Lines changed: 4 additions & 176 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,17 @@
55
*/
66
package io.jooby;
77

8+
import io.jooby.internal.FileAsset;
89
import io.jooby.internal.JarAsset;
10+
import io.jooby.internal.URLAsset;
911
import sun.net.www.protocol.jar.JarURLConnection;
1012

1113
import javax.annotation.Nonnull;
12-
import java.io.FileInputStream;
1314
import java.io.IOException;
1415
import java.io.InputStream;
1516
import java.net.URISyntaxException;
1617
import java.net.URL;
17-
import java.net.URLConnection;
1818
import java.nio.ByteBuffer;
19-
import java.nio.file.Files;
2019
import java.nio.file.Path;
2120
import java.nio.file.Paths;
2221
import java.util.Base64;
@@ -27,173 +26,7 @@
2726
* @author edgar
2827
* @since 2.0.0
2928
*/
30-
public interface Asset {
31-
32-
/**
33-
* File system asset.
34-
*
35-
* @author edgar
36-
* @since 2.0.0.
37-
*/
38-
class FileAsset implements Asset {
39-
40-
/** File. */
41-
private Path file;
42-
43-
/**
44-
* Creates a new file asset.
45-
* @param file Asset file.
46-
*/
47-
public FileAsset(@Nonnull Path file) {
48-
this.file = file;
49-
}
50-
51-
@Override public long getSize() {
52-
try {
53-
return Files.size(file);
54-
} catch (IOException x) {
55-
throw SneakyThrows.propagate(x);
56-
}
57-
}
58-
59-
@Override public long getLastModified() {
60-
try {
61-
return Files.getLastModifiedTime(file).toMillis();
62-
} catch (IOException x) {
63-
throw SneakyThrows.propagate(x);
64-
}
65-
}
66-
67-
@Nonnull @Override public MediaType getContentType() {
68-
return MediaType.byFile(file);
69-
}
70-
71-
@Override public InputStream stream() {
72-
try {
73-
return new FileInputStream(file.toFile());
74-
} catch (IOException x) {
75-
throw SneakyThrows.propagate(x);
76-
}
77-
}
78-
79-
@Override public void release() {
80-
// NOOP
81-
}
82-
83-
@Override public boolean isDirectory() {
84-
return Files.isDirectory(file);
85-
}
86-
87-
@Override public boolean equals(Object obj) {
88-
if (obj instanceof FileAsset) {
89-
return file.equals(((FileAsset) obj).file);
90-
}
91-
return false;
92-
}
93-
94-
@Override public int hashCode() {
95-
return file.hashCode();
96-
}
97-
98-
@Override public String toString() {
99-
return file.toString();
100-
}
101-
}
102-
103-
/**
104-
* URL asset. Mostly represent a classpath file resource.
105-
*
106-
* @author edgar
107-
* @since 2.0.0
108-
*/
109-
class URLAsset implements Asset {
110-
111-
/** URL. */
112-
private final URL resource;
113-
114-
/** Path. */
115-
private final String path;
116-
117-
/** File size. */
118-
private long len;
119-
120-
/** Last modified since or <code>-1</code>. */
121-
private long lastModified;
122-
123-
/** Asset content. */
124-
private InputStream content;
125-
126-
/**
127-
* Creates a new URL asset.
128-
*
129-
* @param resource Asset resource url.
130-
* @param path Asset path.
131-
*/
132-
private URLAsset(@Nonnull URL resource, @Nonnull String path) {
133-
this.resource = resource;
134-
this.path = path;
135-
}
136-
137-
@Override public long getSize() {
138-
checkOpen();
139-
return len;
140-
}
141-
142-
@Override public long getLastModified() {
143-
checkOpen();
144-
return lastModified;
145-
}
146-
147-
@Nonnull @Override public MediaType getContentType() {
148-
return MediaType.byFile(path);
149-
}
150-
151-
@Override public InputStream stream() {
152-
checkOpen();
153-
return content;
154-
}
155-
156-
@Override public void release() {
157-
try {
158-
content.close();
159-
} catch (IOException | NullPointerException x) {
160-
// NPE when content is a directory
161-
}
162-
}
163-
164-
@Override public boolean equals(Object obj) {
165-
if (obj instanceof URLAsset) {
166-
return path.equals(((URLAsset) obj).path);
167-
}
168-
return false;
169-
}
170-
171-
@Override public int hashCode() {
172-
return path.hashCode();
173-
}
174-
175-
@Override public String toString() {
176-
return path;
177-
}
178-
179-
@Override public boolean isDirectory() {
180-
return false;
181-
}
182-
183-
private void checkOpen() {
184-
try {
185-
if (content == null) {
186-
URLConnection connection = resource.openConnection();
187-
connection.setUseCaches(false);
188-
len = connection.getContentLengthLong();
189-
lastModified = connection.getLastModified();
190-
content = connection.getInputStream();
191-
}
192-
} catch (IOException x) {
193-
throw SneakyThrows.propagate(x);
194-
}
195-
}
196-
}
29+
public interface Asset extends AutoCloseable {
19730

19831
/**
19932
* Creates a file system asset.
@@ -217,7 +50,7 @@ static Asset create(@Nonnull String path, @Nonnull URL resource) {
21750
return new JarAsset((JarURLConnection) resource.openConnection());
21851
}
21952
if ("file".equals(resource.getProtocol())) {
220-
return new FileAsset(Paths.get(resource.toURI()));
53+
return create(Paths.get(resource.toURI()));
22154
}
22255
return new URLAsset(resource, path);
22356
} catch (IOException | URISyntaxException x) {
@@ -273,9 +106,4 @@ static Asset create(@Nonnull String path, @Nonnull URL resource) {
273106
* @return Asset content.
274107
*/
275108
InputStream stream();
276-
277-
/**
278-
* Release this asset.
279-
*/
280-
void release();
281109
}

jooby/src/main/java/io/jooby/AssetHandler.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public AssetHandler(AssetSource... sources) {
3838
this.sources = sources;
3939
}
4040

41-
@Nonnull @Override public Object apply(@Nonnull Context ctx) {
41+
@Nonnull @Override public Object apply(@Nonnull Context ctx) throws Exception {
4242
String filepath = ctx.pathMap().getOrDefault(filekey, "index.html");
4343
Asset asset = resolve(filepath);
4444
if (asset == null) {
@@ -51,7 +51,7 @@ public AssetHandler(AssetSource... sources) {
5151
String ifnm = ctx.header("If-None-Match").value((String) null);
5252
if (ifnm != null && ifnm.equals(asset.getEtag())) {
5353
ctx.send(StatusCode.NOT_MODIFIED);
54-
asset.release();
54+
asset.close();
5555
return ctx;
5656
} else {
5757
ctx.setResponseHeader("ETag", asset.getEtag());
@@ -65,7 +65,7 @@ public AssetHandler(AssetSource... sources) {
6565
long ifms = ctx.header("If-Modified-Since").longValue(-1);
6666
if (lastModified / ONE_SEC <= ifms / ONE_SEC) {
6767
ctx.send(StatusCode.NOT_MODIFIED);
68-
asset.release();
68+
asset.close();
6969
return ctx;
7070
}
7171
ctx.setResponseHeader("Last-Modified", Instant.ofEpochMilli(lastModified));

jooby/src/main/java/io/jooby/AssetSource.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*/
66
package io.jooby;
77

8-
import io.jooby.internal.ClasspathAssetSource;
8+
import io.jooby.internal.ClassPathAssetSource;
99
import io.jooby.internal.FileDiskAssetSource;
1010
import io.jooby.internal.FolderDiskAssetSource;
1111

@@ -42,7 +42,7 @@ public interface AssetSource {
4242
* @return An asset source.
4343
*/
4444
static @Nonnull AssetSource create(@Nonnull ClassLoader loader, @Nonnull String location) {
45-
return new ClasspathAssetSource(loader, location);
45+
return new ClassPathAssetSource(loader, location);
4646
}
4747

4848
/**

jooby/src/main/java/io/jooby/internal/ClasspathAssetSource.java renamed to jooby/src/main/java/io/jooby/internal/ClassPathAssetSource.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,9 @@
22

33
import io.jooby.Asset;
44
import io.jooby.AssetSource;
5-
import io.jooby.SneakyThrows;
65

76
import javax.annotation.Nonnull;
87
import javax.annotation.Nullable;
9-
import java.io.IOException;
108
import java.net.JarURLConnection;
119
import java.net.URL;
1210
import java.net.URLConnection;
@@ -15,7 +13,7 @@
1513
import java.util.jar.JarFile;
1614
import java.util.zip.ZipEntry;
1715

18-
public class ClasspathAssetSource implements AssetSource {
16+
public class ClassPathAssetSource implements AssetSource {
1917

2018
private final ClassLoader loader;
2119

@@ -25,7 +23,7 @@ public class ClasspathAssetSource implements AssetSource {
2523

2624
private final String prefix;
2725

28-
public ClasspathAssetSource(ClassLoader loader, String source) {
26+
public ClassPathAssetSource(ClassLoader loader, String source) {
2927
this.loader = loader;
3028
this.source = source.startsWith("/") ? source.substring(1) : source;
3129
this.prefix = sourcePrefix(this.source);
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package io.jooby.internal;
2+
3+
import io.jooby.Asset;
4+
import io.jooby.MediaType;
5+
import io.jooby.SneakyThrows;
6+
7+
import javax.annotation.Nonnull;
8+
import java.io.FileInputStream;
9+
import java.io.IOException;
10+
import java.io.InputStream;
11+
import java.nio.file.Files;
12+
import java.nio.file.Path;
13+
14+
/**
15+
* File system asset.
16+
*
17+
* @author edgar
18+
* @since 2.0.0.
19+
*/
20+
public class FileAsset implements Asset {
21+
22+
/** File. */
23+
private Path file;
24+
25+
/**
26+
* Creates a new file asset.
27+
* @param file Asset file.
28+
*/
29+
public FileAsset(@Nonnull Path file) {
30+
this.file = file;
31+
}
32+
33+
@Override public long getSize() {
34+
try {
35+
return Files.size(file);
36+
} catch (IOException x) {
37+
throw SneakyThrows.propagate(x);
38+
}
39+
}
40+
41+
@Override public long getLastModified() {
42+
try {
43+
return Files.getLastModifiedTime(file).toMillis();
44+
} catch (IOException x) {
45+
throw SneakyThrows.propagate(x);
46+
}
47+
}
48+
49+
@Nonnull @Override public MediaType getContentType() {
50+
return MediaType.byFile(file);
51+
}
52+
53+
@Override public InputStream stream() {
54+
try {
55+
return new FileInputStream(file.toFile());
56+
} catch (IOException x) {
57+
throw SneakyThrows.propagate(x);
58+
}
59+
}
60+
61+
@Override public void close() {
62+
// NOOP
63+
}
64+
65+
@Override public boolean isDirectory() {
66+
return Files.isDirectory(file);
67+
}
68+
69+
@Override public boolean equals(Object obj) {
70+
if (obj instanceof io.jooby.internal.FileAsset) {
71+
return file.equals(((io.jooby.internal.FileAsset) obj).file);
72+
}
73+
return false;
74+
}
75+
76+
@Override public int hashCode() {
77+
return file.hashCode();
78+
}
79+
80+
@Override public String toString() {
81+
return file.toString();
82+
}
83+
}

jooby/src/main/java/io/jooby/internal/JarAsset.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public class JarAsset implements Asset {
2020
public JarAsset(JarURLConnection connection) throws IOException {
2121
connection.setUseCaches(false);
2222
jar = connection.getJarFile();
23-
this.entry = jar.getEntry(connection.getEntryName());
23+
entry = jar.getEntry(connection.getEntryName());
2424
}
2525

2626
@Override public boolean isDirectory() {
@@ -47,11 +47,10 @@ public JarAsset(JarURLConnection connection) throws IOException {
4747
}
4848
}
4949

50-
@Override public void release() {
50+
@Override public void close() {
5151
try {
52-
IOUtils.close();
5352
jar.close();
54-
} catch (IOException x) {
53+
} catch (Exception x) {
5554
// silence
5655
}
5756
}

0 commit comments

Comments
 (0)