Skip to content

Method: sessionInputBufferFrom(InputStream)

1: /*
2: * *********************************************************************************************************************
3: *
4: * blueMarine II: Semantic Media Centre
5: * http://tidalwave.it/projects/bluemarine2
6: *
7: * Copyright (C) 2015 - 2021 by Tidalwave s.a.s. (http://tidalwave.it)
8: *
9: * *********************************************************************************************************************
10: *
11: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
12: * the License. You may obtain a copy of the License at
13: *
14: * http://www.apache.org/licenses/LICENSE-2.0
15: *
16: * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
17: * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
18: * specific language governing permissions and limitations under the License.
19: *
20: * *********************************************************************************************************************
21: *
22: * git clone https://bitbucket.org/tidalwave/bluemarine2-src
23: * git clone https://github.com/tidalwave-it/bluemarine2-src
24: *
25: * *********************************************************************************************************************
26: */
27: package it.tidalwave.bluemarine2.downloader.impl;
28:
29: import javax.annotation.Nonnull;
30: import java.util.ArrayList;
31: import java.util.Collection;
32: import java.util.Date;
33: import java.util.List;
34: import java.util.stream.Collectors;
35: import java.io.IOException;
36: import java.io.InputStream;
37: import java.io.OutputStream;
38: import java.nio.file.Path;
39: import java.nio.file.Paths;
40: import java.net.MalformedURLException;
41: import java.net.URL;
42: import org.apache.http.Header;
43: import org.apache.http.HttpException;
44: import org.apache.http.HttpResponse;
45: import org.apache.http.client.cache.HttpCacheEntry;
46: import org.apache.http.client.cache.HttpCacheStorage;
47: import org.apache.http.client.cache.HttpCacheUpdateCallback;
48: import org.apache.http.client.cache.HttpCacheUpdateException;
49: import org.apache.http.client.cache.Resource;
50: import org.apache.http.impl.client.cache.FileResource;
51: import org.apache.http.impl.io.DefaultHttpResponseParser;
52: import org.apache.http.impl.io.DefaultHttpResponseWriter;
53: import org.apache.http.impl.io.HttpTransportMetricsImpl;
54: import org.apache.http.impl.io.SessionInputBufferImpl;
55: import org.apache.http.impl.io.SessionOutputBufferImpl;
56: import org.apache.http.message.BasicHeader;
57: import org.apache.http.message.BasicHttpResponse;
58: import it.tidalwave.util.annotation.VisibleForTesting;
59: import lombok.Cleanup;
60: import lombok.Getter;
61: import lombok.Setter;
62: import lombok.extern.slf4j.Slf4j;
63: import static java.nio.file.Files.*;
64: import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;
65: import static java.nio.file.StandardOpenOption.*;
66:
67: /***********************************************************************************************************************
68: *
69: * @author Fabrizio Giudici
70: *
71: **********************************************************************************************************************/
72: @Slf4j
73: public class SimpleHttpCacheStorage implements HttpCacheStorage
74: {
75: private static final String PATH_CONTENT = "content";
76: private static final String PATH_HEADERS = "headers";
77:
78: private static final Collection<String> NEVER_EXPIRING_HEADERS = List.of("Cache-Control", "Expires", "Pragma");
79:
80: @Getter @Setter
81: private Path folderPath = Paths.get(System.getProperty("java.io.tmpdir"));
82:
83: /** When this field is {@code true} the headers of items extracted from the cache are manipulated so it appears
84: * they never expire. */
85: @Getter @Setter
86: private boolean neverExpiring;
87:
88: /*******************************************************************************************************************
89: *
90: * {@inheritDoc}
91: *
92: ******************************************************************************************************************/
93: @Override
94: public void putEntry (@Nonnull final String key, @Nonnull final HttpCacheEntry entry)
95: throws IOException
96: {
97: try
98: {
99: log.debug("putEntry({}, {})", key, entry);
100: final Path cachePath = getCacheItemPath(new URL(key));
101: createDirectories(cachePath);
102: final Path cacheHeadersPath = cachePath.resolve(PATH_HEADERS);
103: final Path cacheContentPath = cachePath.resolve(PATH_CONTENT);
104:
105: @Cleanup final OutputStream os = newOutputStream(cacheHeadersPath, CREATE);
106: final SessionOutputBufferImpl sob = sessionOutputBufferFrom(os);
107: final DefaultHttpResponseWriter writer = new DefaultHttpResponseWriter(sob);
108: writer.write(responseFrom(entry));
109: sob.flush();
110:
111: if (entry.getResource().length() > 0)
112: {
113: copy(entry.getResource().getInputStream(), cacheContentPath, REPLACE_EXISTING);
114: }
115: }
116: catch (HttpException e)
117: {
118: throw new IOException(e);
119: }
120: }
121:
122: /*******************************************************************************************************************
123: *
124: * {@inheritDoc}
125: *
126: ******************************************************************************************************************/
127: @Override
128: public HttpCacheEntry getEntry (@Nonnull final String key)
129: throws IOException
130: {
131: log.debug("getEntry({})", key);
132: final Path cachePath = getCacheItemPath(new URL(key));
133: final Path cacheHeadersPath = cachePath.resolve(PATH_HEADERS);
134: final Path cacheContentPath = cachePath.resolve(PATH_CONTENT);
135:
136: if (!exists(cacheHeadersPath))
137: {
138: log.trace(">>>> cache miss: {}", cacheHeadersPath);
139: return null;
140: }
141:
142: try
143: {
144: @Cleanup final InputStream is = newInputStream(cacheHeadersPath);
145: final SessionInputBufferImpl sib = sessionInputBufferFrom(is);
146: final DefaultHttpResponseParser parser = new DefaultHttpResponseParser(sib);
147: return entryFrom(cacheContentPath, parser.parse());
148: }
149: catch (HttpException e)
150: {
151: throw new IOException(e);
152: }
153: }
154:
155: /*******************************************************************************************************************
156: *
157: * {@inheritDoc}
158: *
159: ******************************************************************************************************************/
160: @Override
161: public void removeEntry (@Nonnull final String key)
162: throws IOException
163: {
164: log.debug("removeEntry({})", key);
165: // FIXME
166: }
167:
168: /*******************************************************************************************************************
169: *
170: * {@inheritDoc}
171: *
172: ******************************************************************************************************************/
173: @Override
174: public void updateEntry (@Nonnull final String key, @Nonnull final HttpCacheUpdateCallback callback)
175: throws IOException, HttpCacheUpdateException
176: {
177: log.debug("updateEntry({}, {})", key, callback);
178: // FIXME
179: }
180:
181: /*******************************************************************************************************************
182: *
183: *
184: *
185: ******************************************************************************************************************/
186: @VisibleForTesting boolean isCachedResourcePresent (@Nonnull final String key)
187: throws MalformedURLException
188: {
189: final Path cachePath = getCacheItemPath(new URL(key));
190: final Path cacheHeadersPath = cachePath.resolve(PATH_HEADERS);
191: final Path cacheContentPath = cachePath.resolve(PATH_CONTENT);
192: log.trace(">>>> probing cached entry at {}", cachePath);
193:
194: return exists(cacheHeadersPath) && exists(cacheContentPath);
195: }
196:
197: /*******************************************************************************************************************
198: *
199: *
200: *
201: ******************************************************************************************************************/
202: @Nonnull
203: private Path getCacheItemPath (@Nonnull final URL url)
204: throws MalformedURLException
205: {
206: final int port = url.getPort();
207: final URL url2 = new URL(url.getProtocol(), url.getHost(), (port == 80) ? -1 : port, url.getFile());
208: final Path cachePath = Paths.get(url2.toString().replaceAll(":", ""));
209: return folderPath.resolve(cachePath);
210: }
211:
212: /*******************************************************************************************************************
213: *
214: *
215: *
216: ******************************************************************************************************************/
217: @Nonnull
218: private static SessionInputBufferImpl sessionInputBufferFrom (@Nonnull final InputStream is)
219: {
220: final HttpTransportMetricsImpl metrics = new HttpTransportMetricsImpl();
221: final SessionInputBufferImpl sib = new SessionInputBufferImpl(metrics, 100);
222: sib.bind(is);
223: return sib;
224: }
225:
226: /*******************************************************************************************************************
227: *
228: *
229: *
230: ******************************************************************************************************************/
231: @Nonnull
232: private static SessionOutputBufferImpl sessionOutputBufferFrom (@Nonnull final OutputStream os)
233: {
234: final HttpTransportMetricsImpl metrics = new HttpTransportMetricsImpl();
235: final SessionOutputBufferImpl sob = new SessionOutputBufferImpl(metrics, 100);
236: sob.bind(os);
237: return sob;
238: }
239:
240: /*******************************************************************************************************************
241: *
242: *
243: *
244: ******************************************************************************************************************/
245: @Nonnull
246: private static HttpResponse responseFrom (@Nonnull final HttpCacheEntry entry)
247: {
248: final BasicHttpResponse response = new BasicHttpResponse(entry.getStatusLine());
249: response.setHeaders(entry.getAllHeaders());
250: return response;
251: }
252:
253: /*******************************************************************************************************************
254: *
255: *
256: *
257: ******************************************************************************************************************/
258: @Nonnull
259: private HttpCacheEntry entryFrom (@Nonnull final Path cacheContentPath,
260: @Nonnull final HttpResponse response)
261: {
262: final Date date = new Date(); // FIXME: force hit?
263: // new Date(Files.getLastModifiedTime(cacheHeadersPath).toMillis());
264: final Resource resource = exists(cacheContentPath) ? new FileResource(cacheContentPath.toFile()) : null;
265:
266: List<Header> headers = new ArrayList<>(List.of(response.getAllHeaders()));
267:
268: if (neverExpiring)
269: {
270: headers = headers.stream().filter(header -> !NEVER_EXPIRING_HEADERS.contains(header.getName()))
271: .collect(Collectors.toList());
272: headers.add(new BasicHeader("Expires", "Mon, 31 Dec 2099 00:00:00 GMT"));
273: }
274:
275: return new HttpCacheEntry(date, date, response.getStatusLine(), headers.toArray(new Header[0]), resource);
276: }
277: }
278: