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