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 1334151 2012-05-04 20:08:34Z olamy $
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
45       * management URL.
46       *
47       * @param issueManagementUrl The URL to the issue management system
48       * @return A <code>Map</code> containing the URL and project id
49       */
50      static Map<String,String> getJiraUrlAndProjectId( String issueManagementUrl )
51      {
52          String url = issueManagementUrl;
53  
54          if ( url.endsWith( "/" ) )
55          {
56              // MCHANGES-218
57              url = url.substring( 0, url.lastIndexOf( '/' ) );
58          }
59  
60          // chop off the parameter part
61          int pos = url.indexOf( '?' );
62  
63          // and get the id while we're at it
64          String id = "";
65  
66          if ( pos >= 0 )
67          {
68              // project id
69              id = url.substring( url.lastIndexOf( '=' ) + 1 );
70          }
71  
72          String jiraUrl = url.substring( 0, url.lastIndexOf( '/' ) );
73  
74          if ( jiraUrl.endsWith( "secure" ) )
75          {
76              jiraUrl = jiraUrl.substring( 0, jiraUrl.lastIndexOf( '/' ) );
77          }
78          else
79          {
80              // If the issueManagement.url points to a component, then "browse"
81              // will not be at the end - it might be in the middle somewhere.
82              // Try to find it.
83              final int index = jiraUrl.indexOf( "/browse" );
84              if ( index != -1 )
85              {
86                  jiraUrl = jiraUrl.substring( 0, index );
87              }
88          }
89  
90          HashMap<String,String> urlMap = new HashMap<String,String>( 4 );
91  
92          urlMap.put( "url", jiraUrl );
93  
94          urlMap.put( "id", id );
95  
96          return urlMap;
97      }
98  
99      /**
100      * Try to get a JIRA pid from the issue management URL.
101      *
102      * @param log     Used to tell the user what happened
103      * @param issueManagementUrl The URL to the issue management system
104      * @param client  The client used to connect to JIRA
105      * @return The JIRA id for the project, or null if it can't be found
106      */
107     public static String getPidFromJira( Log log, String issueManagementUrl, HttpClient client )
108     {
109         String jiraId = null;
110         GetMethod gm = new GetMethod( issueManagementUrl );
111 
112         String projectPage;
113         try
114         {
115             client.executeMethod( gm );
116             log.debug( "Successfully reached JIRA." );
117             projectPage = gm.getResponseBodyAsString();
118         }
119         catch ( Exception e )
120         {
121             if ( log.isDebugEnabled() )
122             {
123                 log.error( "Unable to reach the JIRA project page:", e );
124             }
125             else
126             {
127                 log.error( "Unable to reach the JIRA project page. Cause is: " + e.getLocalizedMessage() );
128             }
129             return null;
130         }
131 
132         int pidIndex = projectPage.indexOf( PID );
133 
134         if ( pidIndex == -1 )
135         {
136             log.error( "Unable to extract a JIRA pid from the page at the url " + issueManagementUrl );
137         }
138         else
139         {
140             NumberFormat nf = NumberFormat.getInstance();
141             Number pidNumber = nf.parse( projectPage, new ParsePosition( pidIndex + PID.length() ) );
142             jiraId = Integer.toString( pidNumber.intValue() );
143             log.debug( "Found the pid " + jiraId + " at " + issueManagementUrl );
144         }
145         return jiraId;
146     }
147 
148     /**
149      * Check if the specified host is in the list of non proxy hosts.
150      * <p/>
151      * Method copied from org.apache.maven.wagon.proxy.ProxyUtils. Can be deleted when maven-changes-plugin
152      * references a more recent version of maven-project
153      *
154      * @param proxy      the proxy info object contains set of properties.
155      * @param targetHost the target hostname
156      * @return true if the hostname is in the list of non proxy hosts, false otherwise.
157      */
158     public static boolean validateNonProxyHosts( ProxyInfo proxy, String targetHost )
159     {
160         String tHost = targetHost;
161         if ( tHost == null )
162         {
163             tHost = new String();
164         }
165         if ( proxy == null )
166         {
167             return false;
168         }
169         String nonProxyHosts = proxy.getNonProxyHosts();
170         if ( nonProxyHosts == null )
171         {
172             return false;
173         }
174 
175         StringTokenizer tokenizer = new StringTokenizer( nonProxyHosts, "|" );
176 
177         while ( tokenizer.hasMoreTokens() )
178         {
179             String pattern = tokenizer.nextToken();
180             pattern = pattern.replaceAll( "\\.", "\\\\." ).replaceAll( "\\*", ".*" );
181             if ( tHost.matches( pattern ) )
182             {
183                 return true;
184             }
185         }
186         return false;
187     }
188 
189     private JiraHelper()
190     {
191         // utility class
192     }
193 }