View Javadoc
1   package org.apache.maven.wagon.shared.http;
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 org.apache.http.HttpHost;
23  import org.apache.http.auth.AuthScope;
24  
25  /**
26   * @since 2.8
27   */
28  public class BasicAuthScope
29  {
30      private String host;
31  
32      private String port;
33  
34      private String realm;
35  
36      /**
37       * @return the host
38       */
39      public String getHost()
40      {
41          return host;
42      }
43  
44      /**
45       * @param host the host to set
46       */
47      public void setHost( String host )
48      {
49          this.host = host;
50      }
51  
52      /**
53       * @return the realm
54       */
55      public String getRealm()
56      {
57          return realm;
58      }
59  
60      /**
61       * @param realm the realm to set
62       */
63      public void setRealm( String realm )
64      {
65          this.realm = realm;
66      }
67  
68      /**
69       * Create an authScope given the /repository/host and /repository/password
70       * and the /server/basicAuth or /server/proxyBasicAuth host, port and realm
71       * settings. The basicAuth setting should override the repository settings
72       * host and/or port if host, port or realm is set to "ANY".
73       * <p/>
74       * Realm can also be set to a specific string and will be set if
75       * /server/basicAuthentication/realm is non-null
76       *
77       * @param host The server setting's /server/host value
78       * @param port The server setting's /server/port value
79       * @return
80       */
81      public AuthScope getScope( String host, int port )
82      {
83          if ( getHost() != null //
84              && "ANY".compareTo( getHost() ) == 0 //
85              && getPort() != null //
86              && "ANY".compareTo( getPort() ) == 0 //
87              && getRealm() != null //
88              && "ANY".compareTo( getRealm() ) == 0 )
89          {
90              return AuthScope.ANY;
91          }
92          String scopeHost = host;
93          if ( getHost() != null )
94          {
95              if ( "ANY".compareTo( getHost() ) == 0 )
96              {
97                  scopeHost = AuthScope.ANY_HOST;
98              }
99              else
100             {
101                 scopeHost = getHost();
102             }
103         }
104 
105         int scopePort = port > -1 ? port : AuthScope.ANY_PORT;
106         // -1 for server/port settings does this, but providing an override here
107         // in
108         // the BasicAuthScope config
109         if ( getPort() != null )
110         {
111             if ( "ANY".compareTo( getPort() ) == 0 )
112             {
113                 scopePort = AuthScope.ANY_PORT;
114             }
115             else
116             {
117                 scopePort = Integer.parseInt( getPort() );
118             }
119         }
120 
121         String scopeRealm = AuthScope.ANY_REALM;
122         if ( getRealm() != null )
123         {
124             if ( "ANY".compareTo( getRealm() ) != 0 )
125             {
126                 scopeRealm = getRealm();
127             }
128             else
129             {
130                 scopeRealm = getRealm();
131             }
132         }
133 
134         return new AuthScope( scopeHost, scopePort, scopeRealm );
135     }
136 
137     /**
138      * @return the port
139      */
140     public String getPort()
141     {
142         return port;
143     }
144 
145     /**
146      * @param port the port to set
147      */
148     public void setPort( String port )
149     {
150         this.port = port;
151     }
152 
153     /**
154      * Given a HttpHost, return scope with overrides from appropriate basicAuth
155      * configuration.
156      * <p>
157      * Note: Protocol is ignored. AuthScope impl ignores it as well, but if that
158      * changed, there could be a problem.
159      * </p>
160      *
161      * @param targetHost
162      * @return
163      */
164     public AuthScope getScope( HttpHost targetHost )
165     {
166         return getScope( targetHost.getHostName(), targetHost.getPort() );
167     }
168 }