View Javadoc
1   package org.eclipse.aether.internal.impl;
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.util.regex.Pattern;
23  
24  import javax.inject.Named;
25  import javax.inject.Singleton;
26  
27  import org.eclipse.aether.RepositorySystemSession;
28  import org.eclipse.aether.impl.OfflineController;
29  import org.eclipse.aether.repository.RemoteRepository;
30  import org.eclipse.aether.transfer.RepositoryOfflineException;
31  import org.eclipse.aether.util.ConfigUtils;
32  
33  import static java.util.Objects.requireNonNull;
34  
35  /**
36   * 
37   */
38  @Singleton
39  @Named
40  public class DefaultOfflineController
41      implements OfflineController
42  {
43  
44      static final String CONFIG_PROP_OFFLINE_PROTOCOLS = "aether.offline.protocols";
45  
46      static final String CONFIG_PROP_OFFLINE_HOSTS = "aether.offline.hosts";
47  
48      private static final Pattern SEP = Pattern.compile( "\\s*,\\s*" );
49  
50      public DefaultOfflineController()
51      {
52          // enables default constructor
53      }
54  
55      public void checkOffline( RepositorySystemSession session, RemoteRepository repository )
56          throws RepositoryOfflineException
57      {
58          requireNonNull( session, "session cannot be null" );
59          requireNonNull( repository, "repository cannot be null" );
60          if ( isOfflineProtocol( session, repository ) || isOfflineHost( session, repository ) )
61          {
62              return;
63          }
64  
65          throw new RepositoryOfflineException( repository );
66      }
67  
68      private boolean isOfflineProtocol( RepositorySystemSession session, RemoteRepository repository )
69      {
70          String[] protocols = getConfig( session, CONFIG_PROP_OFFLINE_PROTOCOLS );
71          if ( protocols != null )
72          {
73              String protocol = repository.getProtocol();
74              if ( protocol.length() > 0 )
75              {
76                  for ( String p : protocols )
77                  {
78                      if ( p.equalsIgnoreCase( protocol ) )
79                      {
80                          return true;
81                      }
82                  }
83              }
84          }
85          return false;
86      }
87  
88      private boolean isOfflineHost( RepositorySystemSession session, RemoteRepository repository )
89      {
90          String[] hosts = getConfig( session, CONFIG_PROP_OFFLINE_HOSTS );
91          if ( hosts != null )
92          {
93              String host = repository.getHost();
94              if ( host.length() > 0 )
95              {
96                  for ( String h : hosts )
97                  {
98                      if ( h.equalsIgnoreCase( host ) )
99                      {
100                         return true;
101                     }
102                 }
103             }
104         }
105         return false;
106     }
107 
108     private String[] getConfig( RepositorySystemSession session, String key )
109     {
110         String value = ConfigUtils.getString( session, "", key ).trim();
111         if ( value.isEmpty() )
112         {
113             return null;
114         }
115         return SEP.split( value );
116     }
117 
118 }