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 =
40          new ConcurrentHashMap<String, Map<String, String>>();
41  
42      public static Map<String, String> getFilesystem( String id )
43      {
44          Map<String, String> fs = mounts.get( id );
45          if ( fs == null )
46          {
47              fs = new ConcurrentHashMap<String, String>();
48              Map<String, String> prev = mounts.putIfAbsent( id, fs );
49              if ( prev != null )
50              {
51                  fs = prev;
52              }
53          }
54          return fs;
55      }
56  
57      public static Map<String, String> openConnection( Wagon wagon, AuthenticationInfo auth, ProxyInfo proxy,
58                                                        Properties headers )
59          throws ConnectionException, AuthenticationException
60      {
61          URI uri = URI.create( wagon.getRepository().getUrl() );
62  
63          String query = uri.getQuery();
64          if ( query != null )
65          {
66              verify( query, "config", String.valueOf( ( (Configurable) wagon ).getConfiguration() ) );
67  
68              verify( query, "userAgent", ( headers != null ) ? headers.getProperty( "User-Agent" ) : null );
69              verify( query, "requestTimeout", Integer.toString( wagon.getTimeout() ) );
70  
71              verify( query, "serverUsername", ( auth != null ) ? auth.getUserName() : null );
72              verify( query, "serverPassword", ( auth != null ) ? auth.getPassword() : null );
73              verify( query, "serverPrivateKey", ( auth != null ) ? auth.getPrivateKey() : null );
74              verify( query, "serverPassphrase", ( auth != null ) ? auth.getPassphrase() : null );
75  
76              verify( query, "proxyHost", ( proxy != null ) ? proxy.getHost() : null );
77              verify( query, "proxyPort", ( proxy != null ) ? Integer.toString( proxy.getPort() ) : null );
78              verify( query, "proxyUsername", ( proxy != null ) ? proxy.getUserName() : null );
79              verify( query, "proxyPassword", ( proxy != null ) ? proxy.getPassword() : null );
80          }
81  
82          return getFilesystem( uri.getHost() );
83      }
84  
85      private static void verify( String query, String key, String value )
86          throws ConnectionException
87      {
88          int index = query.indexOf( key + "=" );
89          if ( index < 0 )
90          {
91              return;
92          }
93          String expected = query.substring( index + key.length() + 1 );
94          index = expected.indexOf( "&" );
95          if ( index >= 0 )
96          {
97              expected = expected.substring( 0, index );
98          }
99  
100         if ( expected.length() == 0 )
101         {
102             if ( value != null )
103             {
104                 throw new ConnectionException( "Bad " + key + ": " + value );
105             }
106         }
107         else if ( !expected.equals( value ) )
108         {
109             throw new ConnectionException( "Bad " + key + ": " + value );
110         }
111     }
112 
113 }