Coverage Report - org.apache.maven.plugin.jira.JiraHelper
 
Classes in this File Line Coverage Branch Coverage Complexity
JiraHelper
47%
34/72
38%
10/26
4,167
 
 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 1379884 2012-09-01 22:32:02Z dennisl $
 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  4
         String url = issueManagementUrl;
 53  
 
 54  4
         if ( url.endsWith( "/" ) )
 55  
         {
 56  
             // MCHANGES-218
 57  1
             url = url.substring( 0, url.lastIndexOf( '/' ) );
 58  
         }
 59  
 
 60  
         // chop off the parameter part
 61  4
         int pos = url.indexOf( '?' );
 62  
 
 63  
         // and get the id while we're at it
 64  4
         String id = "";
 65  
 
 66  4
         if ( pos >= 0 )
 67  
         {
 68  
             // project id
 69  1
             id = url.substring( url.lastIndexOf( '=' ) + 1 );
 70  
         }
 71  
 
 72  4
         String jiraUrl = url.substring( 0, url.lastIndexOf( '/' ) );
 73  
 
 74  4
         if ( jiraUrl.endsWith( "secure" ) )
 75  
         {
 76  1
             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  3
             final int index = jiraUrl.indexOf( "/browse" );
 84  3
             if ( index != -1 )
 85  
             {
 86  3
                 jiraUrl = jiraUrl.substring( 0, index );
 87  
             }
 88  
         }
 89  
 
 90  4
         HashMap<String,String> urlMap = new HashMap<String,String>( 4 );
 91  
 
 92  4
         urlMap.put( "url", jiraUrl );
 93  
 
 94  4
         urlMap.put( "id", id );
 95  
 
 96  4
         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  0
         String jiraId = null;
 110  0
         GetMethod gm = new GetMethod( issueManagementUrl );
 111  
 
 112  
         String projectPage;
 113  
         try
 114  
         {
 115  0
             client.executeMethod( gm );
 116  0
             log.debug( "Successfully reached JIRA." );
 117  0
             projectPage = gm.getResponseBodyAsString();
 118  
         }
 119  0
         catch ( Exception e )
 120  
         {
 121  0
             if ( log.isDebugEnabled() )
 122  
             {
 123  0
                 log.error( "Unable to reach the JIRA project page:", e );
 124  
             }
 125  
             else
 126  
             {
 127  0
                 log.error( "Unable to reach the JIRA project page. Cause is: " + e.getLocalizedMessage() );
 128  
             }
 129  0
             return null;
 130  0
         }
 131  
 
 132  0
         int pidIndex = projectPage.indexOf( PID );
 133  
 
 134  0
         if ( pidIndex == -1 )
 135  
         {
 136  0
             log.error( "Unable to extract a JIRA pid from the page at the url " + issueManagementUrl );
 137  
         }
 138  
         else
 139  
         {
 140  0
             NumberFormat nf = NumberFormat.getInstance();
 141  0
             Number pidNumber = nf.parse( projectPage, new ParsePosition( pidIndex + PID.length() ) );
 142  0
             jiraId = Integer.toString( pidNumber.intValue() );
 143  0
             log.debug( "Found the pid " + jiraId + " at " + issueManagementUrl );
 144  
         }
 145  0
         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  0
         String tHost = targetHost;
 161  0
         if ( tHost == null )
 162  
         {
 163  0
             tHost = new String();
 164  
         }
 165  0
         if ( proxy == null )
 166  
         {
 167  0
             return false;
 168  
         }
 169  0
         String nonProxyHosts = proxy.getNonProxyHosts();
 170  0
         if ( nonProxyHosts == null )
 171  
         {
 172  0
             return false;
 173  
         }
 174  
 
 175  0
         StringTokenizer tokenizer = new StringTokenizer( nonProxyHosts, "|" );
 176  
 
 177  0
         while ( tokenizer.hasMoreTokens() )
 178  
         {
 179  0
             String pattern = tokenizer.nextToken();
 180  0
             pattern = pattern.replaceAll( "\\.", "\\\\." ).replaceAll( "\\*", ".*" );
 181  0
             if ( tHost.matches( pattern ) )
 182  
             {
 183  0
                 return true;
 184  
             }
 185  0
         }
 186  0
         return false;
 187  
     }
 188  
 
 189  
     private JiraHelper()
 190  0
     {
 191  
         // utility class
 192  0
     }
 193  
 
 194  
     /**
 195  
      * Parse out the base URL for JIRA and the JIRA project name from the issue
 196  
      * management URL.
 197  
      * The issue management URL is assumed to be of the format http(s)://host:port/browse/{projectname}
 198  
      *
 199  
      * @param issueManagementUrl The URL to the issue management system
 200  
      * @return A <code>Map</code> containing the URL and project name
 201  
      * @since 2.8
 202  
      */
 203  
     public static Map<String, String> getJiraUrlAndProjectName( String issueManagementUrl )
 204  
     {
 205  2
         final int indexBrowse = issueManagementUrl.indexOf( "/browse/" );
 206  
 
 207  2
         String jiraUrl = "";
 208  2
         String project = "";
 209  
 
 210  2
         if ( indexBrowse != -1 )
 211  
         {
 212  2
             jiraUrl = issueManagementUrl.substring( 0, indexBrowse );
 213  
 
 214  2
             final int indexBrowseEnd = indexBrowse + "/browse/".length();
 215  
 
 216  2
             final int indexProject = issueManagementUrl.indexOf( "/", indexBrowseEnd );
 217  
 
 218  2
             if ( indexProject != -1 )
 219  
             {
 220  
                 //Project name has trailing '/'
 221  1
                 project = issueManagementUrl.substring( indexBrowseEnd, indexProject );
 222  
             }
 223  
             else
 224  
             {
 225  
                 //Project name without trailing '/'
 226  1
                 project = issueManagementUrl.substring( indexBrowseEnd );
 227  
             }
 228  2
         }
 229  
         else
 230  
         {
 231  0
             throw new IllegalArgumentException( "Invalid browse URL" );
 232  
         }
 233  
 
 234  2
         HashMap<String, String> urlMap = new HashMap<String, String>( 4 );
 235  2
         urlMap.put( "url", jiraUrl );
 236  2
         urlMap.put( "project", project );
 237  
 
 238  2
         return urlMap;
 239  
     }
 240  
 
 241  
     /**
 242  
      * @since 2.8
 243  
      */
 244  
     public static String getBaseUrl( String url )
 245  
     {
 246  2
         int index = url.indexOf( "/", 8 ); //Ignore http:// or https://
 247  2
         return url.substring( 0, index );
 248  
     }
 249  
 }