View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.eclipse.aether.transport.wagon;
20  
21  import java.net.URI;
22  import java.util.Map;
23  import java.util.Properties;
24  import java.util.concurrent.ConcurrentHashMap;
25  import java.util.concurrent.ConcurrentMap;
26  
27  import org.apache.maven.wagon.ConnectionException;
28  import org.apache.maven.wagon.Wagon;
29  import org.apache.maven.wagon.authentication.AuthenticationException;
30  import org.apache.maven.wagon.authentication.AuthenticationInfo;
31  import org.apache.maven.wagon.proxy.ProxyInfo;
32  
33  /**
34   */
35  class MemWagonUtils {
36  
37      private static final ConcurrentMap<String, Map<String, String>> mounts = new ConcurrentHashMap<>();
38  
39      public static Map<String, String> getFilesystem(String id) {
40          Map<String, String> fs = mounts.get(id);
41          if (fs == null) {
42              fs = new ConcurrentHashMap<>();
43              Map<String, String> prev = mounts.putIfAbsent(id, fs);
44              if (prev != null) {
45                  fs = prev;
46              }
47          }
48          return fs;
49      }
50  
51      public static Map<String, String> openConnection(
52              Wagon wagon, AuthenticationInfo auth, ProxyInfo proxy, Properties headers)
53              throws ConnectionException, AuthenticationException {
54          URI uri = URI.create(wagon.getRepository().getUrl());
55  
56          String query = uri.getQuery();
57          if (query != null) {
58              verify(query, "config", String.valueOf(((Configurable) wagon).getConfiguration()));
59  
60              verify(query, "userAgent", (headers != null) ? headers.getProperty("User-Agent") : null);
61              verify(query, "requestTimeout", Integer.toString(wagon.getTimeout()));
62  
63              verify(query, "serverUsername", (auth != null) ? auth.getUserName() : null);
64              verify(query, "serverPassword", (auth != null) ? auth.getPassword() : null);
65              verify(query, "serverPrivateKey", (auth != null) ? auth.getPrivateKey() : null);
66              verify(query, "serverPassphrase", (auth != null) ? auth.getPassphrase() : null);
67  
68              verify(query, "proxyHost", (proxy != null) ? proxy.getHost() : null);
69              verify(query, "proxyPort", (proxy != null) ? Integer.toString(proxy.getPort()) : null);
70              verify(query, "proxyUsername", (proxy != null) ? proxy.getUserName() : null);
71              verify(query, "proxyPassword", (proxy != null) ? proxy.getPassword() : null);
72          }
73  
74          return getFilesystem(uri.getHost());
75      }
76  
77      private static void verify(String query, String key, String value) throws ConnectionException {
78          int index = query.indexOf(key + "=");
79          if (index < 0) {
80              return;
81          }
82          String expected = query.substring(index + key.length() + 1);
83          index = expected.indexOf("&");
84          if (index >= 0) {
85              expected = expected.substring(0, index);
86          }
87  
88          if (expected.length() == 0) {
89              if (value != null) {
90                  throw new ConnectionException("Bad " + key + ": " + value);
91              }
92          } else if (!expected.equals(value)) {
93              throw new ConnectionException("Bad " + key + ": " + value);
94          }
95      }
96  }