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.util.concurrent.ConcurrentHashMap;
30 import java.util.concurrent.ConcurrentMap;
31
32 import org.apache.hc.core5.http.ProtocolVersion;
33 import org.apache.hc.core5.http.URIScheme;
34 import org.apache.hc.core5.util.VersionInfo;
35
36 final class ViaCacheGenerator {
37
38 final static ViaCacheGenerator INSTANCE = new ViaCacheGenerator();
39
40 final ConcurrentMap<ProtocolVersion, String> internalCache = new ConcurrentHashMap<>(4);
41
42 String generateViaHeader(final VersionInfo vi, final ProtocolVersion pv) {
43 final StringBuilder buf = new StringBuilder();
44 if (!URIScheme.HTTP.same(pv.getProtocol())) {
45 buf.append(pv.getProtocol()).append('/');
46 }
47 buf.append(pv.getMajor()).append('.').append(pv.getMinor());
48 buf.append(' ').append("localhost").append(' ');
49 buf.append("(Apache-HttpClient/");
50 final String release = (vi != null) ? vi.getRelease() : VersionInfo.UNAVAILABLE;
51 buf.append(release).append(" (cache))");
52 return buf.toString();
53 }
54
55 String lookup(final ProtocolVersion pv) {
56 return internalCache.computeIfAbsent(pv, v -> {
57 final VersionInfo vi = VersionInfo.loadVersionInfo("org.apache.hc.client5", getClass().getClassLoader());
58 return generateViaHeader(vi, v);
59 });
60 }
61
62 }