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  
25  import org.apache.commons.httpclient.HttpClient;
26  import org.apache.commons.httpclient.methods.GetMethod;
27  import org.apache.maven.plugin.logging.Log;
28  
29  /**
30   * A helper class with common JIRA related functionality.
31   *
32   * @author Dennis Lundberg
33   * @version $Id: JiraHelper.html 816584 2012-05-08 12:33:35Z hboutemy $
34   */
35  public class JiraHelper
36  {
37      private static final String PID = "pid=";
38  
39      /**
40       * Try to get a JIRA pid from the issue management URL.
41       *
42       * @param log     Used to tell the user what happened
43       * @param issueManagementUrl The URL to the issue management system
44       * @param client  The client used to connect to JIRA
45       * @return The JIRA id for the project, or null if it can't be found
46       */
47      public static String getPidFromJira( Log log, String issueManagementUrl, HttpClient client )
48      {
49          String jiraId = null;
50          GetMethod gm = new GetMethod( issueManagementUrl );
51      
52          String projectPage;
53          try
54          {
55              client.executeMethod( gm );
56              log.debug( "Successfully reached JIRA." );
57              projectPage = gm.getResponseBodyAsString();
58          }
59          catch ( Exception e )
60          {
61              if ( log.isDebugEnabled() )
62              {
63                  log.error( "Unable to reach the JIRA project page:", e );
64              }
65              else
66              {
67                  log.error( "Unable to reach the JIRA project page. Cause is: " + e.getLocalizedMessage() );
68              }
69              return null;
70          }
71  
72          int pidIndex = projectPage.indexOf( PID );
73  
74          if ( pidIndex == -1 )
75          {
76              log.error( "Unable to extract a JIRA pid from the page at the url " + issueManagementUrl );
77          }
78          else
79          {
80              NumberFormat nf = NumberFormat.getInstance();
81              Number pidNumber = nf.parse( projectPage, new ParsePosition( pidIndex + PID.length() ) );
82              jiraId = Integer.toString( pidNumber.intValue() );
83              log.debug( "Found the pid " + jiraId + " at " + issueManagementUrl );
84          }
85          return jiraId;
86      }
87  }