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.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   * Generates {@link Resource} instances whose body is stored in a temporary file.
46   *
47   * @since 4.1
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       * @since 5.4
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         } else {
105             return generate(requestId, null, null, 0, 0);
106         }
107     }
108 
109     @Override
110     public Resource generate(final String requestId, final byte[] content) throws ResourceIOException {
111         if (content != null) {
112             return generate(requestId, null, content, 0, content.length);
113         } else {
114             return generate(requestId, null, null, 0, 0);
115         }
116     }
117 
118     /**
119      * @deprecated Do not use.
120      */
121     @Deprecated
122     @Override
123     public Resource copy(
124             final String requestId,
125             final Resource resource) throws ResourceIOException {
126         return resource;
127     }
128 
129 }