View Javadoc
1   package org.apache.maven.plugin.jira;
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.text.NumberFormat;
23  import java.text.ParsePosition;
24  import java.util.HashMap;
25  import java.util.Map;
26  import java.util.StringTokenizer;
27  
28  import org.apache.commons.httpclient.HttpClient;
29  import org.apache.commons.httpclient.methods.GetMethod;
30  import org.apache.maven.plugin.logging.Log;
31  import org.apache.maven.wagon.proxy.ProxyInfo;
32  
33  /**
34   * A helper class with common JIRA related functionality.
35   *
36   * @author Dennis Lundberg
37   * @version $Id: JiraHelper.java 1685894 2015-06-16 19:29:09Z khmarbaise $
38   */
39  public class JiraHelper
40  {
41      private static final String PID = "?pid="; // MCHANGES-281 addd ?
42  
43      /**
44       * Parse out the base URL for JIRA and the JIRA project id from the issue management URL.
45       *
46       * @param issueManagementUrl The URL to the issue management system
47       * @return A <code>Map</code> containing the URL and project id
48       */
49      static Map<String, String> getJiraUrlAndProjectId( String issueManagementUrl )
50      {
51          String url = issueManagementUrl;
52  
53          if ( url.endsWith( "/" ) )
54          {
55              // MCHANGES-218
56              url = url.substring( 0, url.lastIndexOf( '/' ) );
57          }
58  
59          // chop off the parameter part
60          int pos = url.indexOf( '?' );
61  
62          // and get the id while we're at it
63          String id = "";
64  
65          if ( pos >= 0 )
66          {
67              // project id
68              id = url.substring( url.lastIndexOf( '=' ) + 1 );
69          }
70  
71          String jiraUrl = url.substring( 0, url.lastIndexOf( '/' ) );
72  
73          if ( jiraUrl.endsWith( "secure" ) )
74          {
75              jiraUrl = jiraUrl.substring( 0, jiraUrl.lastIndexOf( '/' ) );
76          }
77          else
78          {
79              // If the issueManagement.url points to a component, then "browse"
80              // will not be at the end - it might be in the middle somewhere.
81              // Try to find it.
82              final int index = jiraUrl.indexOf( "/browse" );
83              if ( index != -1 )
84              {
85                  jiraUrl = jiraUrl.substring( 0, index );
86              }
87          }
88  
89          HashMap<String, String> urlMap = new HashMap<String, String>( 4 );
90  
91          urlMap.put( "url", jiraUrl );
92  
93          urlMap.put( "id", id );
94  
95          return urlMap;
96      }
97  
98      /**
99       * Try to get a JIRA pid from the issue management URL.
100      *
101      * @param log Used to tell the user what happened
102      * @param issueManagementUrl The URL to the issue management system
103      * @param client The client used to connect to JIRA
104      * @return The JIRA id for the project, or null if it can't be found
105      */
106     public static String getPidFromJira( Log log, String issueManagementUrl, HttpClient client )
107     {
108         String jiraId = null;
109         GetMethod gm = new GetMethod( issueManagementUrl );
110 
111         String projectPage;
112         try
113         {
114             client.executeMethod( gm );
115             log.debug( "Successfully reached JIRA." );
116             projectPage = gm.getResponseBodyAsString();
117         }
118         catch ( Exception e )
119         {
120             if ( log.isDebugEnabled() )
121             {
122                 log.error( "Unable to reach the JIRA project page:", e );
123             }
124             else
125             {
126                 log.error( "Unable to reach the JIRA project page. Cause is: " + e.getLocalizedMessage() );
127             }
128             return null;
129         }
130 
131         int pidIndex = projectPage.indexOf( PID );
132 
133         if ( pidIndex == -1 )
134         {
135             log.error( "Unable to extract a JIRA pid from the page at the url " + issueManagementUrl );
136         }
137         else
138         {
139             NumberFormat nf = NumberFormat.getInstance();
140             Number pidNumber = nf.parse( projectPage, new ParsePosition( pidIndex + PID.length() ) );
141             jiraId = Integer.toString( pidNumber.intValue() );
142             log.debug( "Found the pid " + jiraId + " at " + issueManagementUrl );
143         }
144         return jiraId;
145     }
146 
147     /**
148      * Check if the specified host is in the list of non proxy hosts.
149      * <p/>
150      * Method copied from org.apache.maven.wagon.proxy.ProxyUtils. Can be deleted when maven-changes-plugin references a
151      * more recent version of maven-project
152      *
153      * @param proxy the proxy info object contains set of properties.
154      * @param targetHost the target hostname
155      * @return true if the hostname is in the list of non proxy hosts, false otherwise.
156      */
157     public static boolean validateNonProxyHosts( ProxyInfo proxy, String targetHost )
158     {
159         String tHost = targetHost;
160         if ( tHost == null )
161         {
162             tHost = "";
163         }
164         if ( proxy == null )
165         {
166             return false;
167         }
168         String nonProxyHosts = proxy.getNonProxyHosts();
169         if ( nonProxyHosts == null )
170         {
171             return false;
172         }
173 
174         StringTokenizer tokenizer = new StringTokenizer( nonProxyHosts, "|" );
175 
176         while ( tokenizer.hasMoreTokens() )
177         {
178             String pattern = tokenizer.nextToken();
179             pattern = pattern.replaceAll( "\\.", "\\\\." ).replaceAll( "\\*", ".*" );
180             if ( tHost.matches( pattern ) )
181             {
182                 return true;
183             }
184         }
185         return false;
186     }
187 
188     private JiraHelper()
189     {
190         // utility class
191     }
192 
193     /**
194      * Parse out the base URL for JIRA and the JIRA project name from the issue management URL. The issue management URL
195      * is assumed to be of the format http(s)://host:port/browse/{projectname}
196      *
197      * @param issueManagementUrl The URL to the issue management system
198      * @return A <code>Map</code> containing the URL and project name
199      * @since 2.8
200      */
201     public static Map<String, String> getJiraUrlAndProjectName( String issueManagementUrl )
202     {
203         final int indexBrowse = issueManagementUrl.indexOf( "/browse/" );
204 
205         String jiraUrl;
206         String project;
207 
208         if ( indexBrowse != -1 )
209         {
210             jiraUrl = issueManagementUrl.substring( 0, indexBrowse );
211 
212             final int indexBrowseEnd = indexBrowse + "/browse/".length();
213 
214             final int indexProject = issueManagementUrl.indexOf( "/", indexBrowseEnd );
215 
216             if ( indexProject != -1 )
217             {
218                 // Project name has trailing '/'
219                 project = issueManagementUrl.substring( indexBrowseEnd, indexProject );
220             }
221             else
222             {
223                 // Project name without trailing '/'
224                 project = issueManagementUrl.substring( indexBrowseEnd );
225             }
226         }
227         else
228         {
229             throw new IllegalArgumentException( "Invalid browse URL" );
230         }
231 
232         HashMap<String, String> urlMap = new HashMap<String, String>( 4 );
233         urlMap.put( "url", jiraUrl );
234         urlMap.put( "project", project );
235 
236         return urlMap;
237     }
238 
239     /**
240      * @since 2.8
241      */
242     public static String getBaseUrl( String url )
243     {
244         int index = url.indexOf( "/", 8 ); // Ignore http:// or https://
245         return url.substring( 0, index );
246     }
247 }