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 package org.apache.hc.client5.http.impl.cache;
28
29 import java.io.File;
30 import java.io.FileOutputStream;
31 import java.io.IOException;
32 import java.security.MessageDigest;
33 import java.security.NoSuchAlgorithmException;
34
35 import org.apache.hc.client5.http.cache.Resource;
36 import org.apache.hc.client5.http.cache.ResourceFactory;
37 import org.apache.hc.client5.http.cache.ResourceIOException;
38 import org.apache.hc.core5.annotation.Contract;
39 import org.apache.hc.core5.annotation.ThreadingBehavior;
40 import org.apache.hc.core5.net.PercentCodec;
41 import org.apache.hc.core5.util.Args;
42 import org.apache.hc.core5.util.TextUtils;
43
44
45
46
47
48
49 @Contract(threading = ThreadingBehavior.STATELESS)
50 public class FileResourceFactory implements ResourceFactory {
51
52 private final File cacheDir;
53
54 public FileResourceFactory(final File cacheDir) {
55 super();
56 this.cacheDir = cacheDir;
57 }
58
59 static String generateUniqueCacheFileName(final String requestId, final String eTag, final byte[] content, final int off, final int len) {
60 final StringBuilder buf = new StringBuilder();
61 if (eTag != null) {
62 PercentCodec.RFC3986.encode(buf, eTag);
63 buf.append('@');
64 } else if (content != null) {
65 final MessageDigest sha256;
66 try {
67 sha256 = MessageDigest.getInstance("SHA-256");
68 } catch (final NoSuchAlgorithmException ex) {
69 throw new IllegalStateException(ex);
70 }
71 sha256.update(content, off, len);
72 buf.append(TextUtils.toHexString(sha256.digest()));
73 buf.append('@');
74 }
75 PercentCodec.RFC3986.encode(buf, requestId);
76 return buf.toString();
77 }
78
79
80
81
82 @Override
83 public Resource generate(
84 final String requestId,
85 final String eTag,
86 final byte[] content, final int off, final int len) throws ResourceIOException {
87 Args.notNull(requestId, "Request id");
88 final String filename = generateUniqueCacheFileName(requestId, eTag, content, off, len);
89 final File file = new File(cacheDir, filename);
90 try (FileOutputStream outStream = new FileOutputStream(file, false)) {
91 if (content != null) {
92 outStream.write(content, off, len);
93 }
94 } catch (final IOException ex) {
95 throw new ResourceIOException(ex.getMessage(), ex);
96 }
97 return new FileResource(file);
98 }
99
100 @Override
101 public Resource generate(final String requestId, final byte[] content, final int off, final int len) throws ResourceIOException {
102 if (content != null) {
103 return generate(requestId, null, content, off, len);
104 }
105 return generate(requestId, null, null, 0, 0);
106 }
107
108 @Override
109 public Resource generate(final String requestId, final byte[] content) throws ResourceIOException {
110 if (content != null) {
111 return generate(requestId, null, content, 0, content.length);
112 }
113 return generate(requestId, null, null, 0, 0);
114 }
115
116
117
118
119 @Deprecated
120 @Override
121 public Resource copy(
122 final String requestId,
123 final Resource resource) throws ResourceIOException {
124 return resource;
125 }
126
127 }