View Javadoc

1   /*
2    * Copyright 2012-2013 smartics, Kronseder & Reiner GmbH
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package de.smartics.maven.nexus;
17  
18  /**
19   * Utilities for the communication with the Nexus server.
20   */
21  public final class NexusUtils
22  {
23    // ********************************* Fields *********************************
24  
25    // --- constants ------------------------------------------------------------
26  
27    // --- members --------------------------------------------------------------
28  
29    // ****************************** Initializer *******************************
30  
31    // ****************************** Constructors ******************************
32  
33    /**
34     * Utility class.
35     */
36    private NexusUtils()
37    {
38    }
39  
40    // ****************************** Inner Classes *****************************
41  
42    // ********************************* Methods ********************************
43  
44    // --- init -----------------------------------------------------------------
45  
46    // --- get&set --------------------------------------------------------------
47  
48    // --- business -------------------------------------------------------------
49  
50    /**
51     * Figures out the base path to the Nexus server.
52     *
53     * @param url a path to a resource on the Nexus server.
54     * @return the base path to the Nexus server.
55     */
56    public static String calcNexusServerUrl(final String url)
57    {
58      final int startIndex = calcStartIndex(url);
59      if (startIndex != -1)
60      {
61        final int endIndex = calcEndIndex(url, startIndex);
62        if (endIndex != -1)
63        {
64          final String nexusBaseUrl =
65              url.substring(startIndex, endIndex);
66          return nexusBaseUrl;
67        }
68      }
69  
70      return null;
71    }
72  
73    private static int calcStartIndex(final String url)
74    {
75      int startIndex = url.indexOf("http://");
76      if (startIndex == -1)
77      {
78        startIndex = url.indexOf("https://");
79      }
80      return startIndex;
81    }
82  
83    private static int calcEndIndex(final String url, final int startIndex)
84    {
85      int endIndex = url.indexOf("/nexus", startIndex);
86      if (endIndex == -1)
87      {
88        endIndex = url.indexOf('/', startIndex + 8);
89      }
90      else
91      {
92        endIndex += 6;
93      }
94  
95      return endIndex;
96    }
97  
98    // --- object basics --------------------------------------------------------
99  
100 }