View Javadoc
1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  package org.apache.hc.client5.http.cache;
28  
29  import java.util.Collection;
30  import java.util.Map;
31  
32  import org.apache.hc.client5.http.impl.Operations;
33  import org.apache.hc.core5.annotation.Contract;
34  import org.apache.hc.core5.annotation.ThreadingBehavior;
35  import org.apache.hc.core5.concurrent.Cancellable;
36  import org.apache.hc.core5.concurrent.FutureCallback;
37  import org.apache.hc.core5.util.Args;
38  
39  /**
40   * {@link HttpAsyncCacheStorage} implementation that emulates asynchronous
41   * behavior using an instance of classic {@link HttpCacheStorage}.
42   *
43   * @since 5.0
44   */
45  @Contract(threading = ThreadingBehavior.SAFE_CONDITIONAL)
46  public final class HttpAsyncCacheStorageAdaptor implements HttpAsyncCacheStorage {
47  
48      private final HttpCacheStorage cacheStorage;
49  
50      public HttpAsyncCacheStorageAdaptor(final HttpCacheStorage cacheStorage) {
51          this.cacheStorage = Args.notNull(cacheStorage, "Cache storage");
52      }
53  
54      @Override
55      public Cancellable putEntry(final String key, final HttpCacheEntry entry, final FutureCallback<Boolean> callback) {
56          Args.notEmpty(key, "Key");
57          Args.notNull(entry, "Cache entry");
58          Args.notNull(callback, "Callback");
59          try {
60              cacheStorage.putEntry(key, entry);
61              callback.completed(Boolean.TRUE);
62          } catch (final Exception ex) {
63              callback.failed(ex);
64          }
65          return Operations.nonCancellable();
66      }
67  
68      @Override
69      public Cancellable getEntry(final String key, final FutureCallback<HttpCacheEntry> callback) {
70          Args.notEmpty(key, "Key");
71          Args.notNull(callback, "Callback");
72          try {
73              final HttpCacheEntry entry = cacheStorage.getEntry(key);
74              callback.completed(entry);
75          } catch (final Exception ex) {
76              callback.failed(ex);
77          }
78          return Operations.nonCancellable();
79      }
80  
81      @Override
82      public Cancellable removeEntry(final String key, final FutureCallback<Boolean> callback) {
83          Args.notEmpty(key, "Key");
84          Args.notNull(callback, "Callback");
85          try {
86              cacheStorage.removeEntry(key);
87              callback.completed(Boolean.TRUE);
88          } catch (final Exception ex) {
89              callback.failed(ex);
90          }
91          return Operations.nonCancellable();
92      }
93  
94      @Override
95      public Cancellable updateEntry(
96              final String key, final HttpCacheCASOperation casOperation, final FutureCallback<Boolean> callback) {
97          Args.notEmpty(key, "Key");
98          Args.notNull(casOperation, "CAS operation");
99          Args.notNull(callback, "Callback");
100         try {
101             cacheStorage.updateEntry(key, casOperation);
102             callback.completed(Boolean.TRUE);
103         } catch (final Exception ex) {
104             callback.failed(ex);
105         }
106         return Operations.nonCancellable();
107     }
108 
109     @Override
110     public Cancellable getEntries(final Collection<String> keys, final FutureCallback<Map<String, HttpCacheEntry>> callback) {
111         Args.notNull(keys, "Key");
112         Args.notNull(callback, "Callback");
113         try {
114             callback.completed(cacheStorage.getEntries(keys));
115         } catch (final Exception ex) {
116             callback.failed(ex);
117         }
118         return Operations.nonCancellable();
119     }
120 
121 }