1 package org.apache.maven.plugin.jira;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
31
32
33
34
35 public class JiraHelper
36 {
37 private static final String PID = "pid=";
38
39
40
41
42
43
44
45
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 }