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 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
35
36
37
38
39 public class JiraHelper
40 {
41 private static final String PID = "?pid=";
42
43
44
45
46
47
48
49 static Map<String, String> getJiraUrlAndProjectId( String issueManagementUrl )
50 {
51 String url = issueManagementUrl;
52
53 if ( url.endsWith( "/" ) )
54 {
55
56 url = url.substring( 0, url.lastIndexOf( '/' ) );
57 }
58
59
60 int pos = url.indexOf( '?' );
61
62
63 String id = "";
64
65 if ( pos >= 0 )
66 {
67
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
80
81
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
100
101
102
103
104
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
149
150
151
152
153
154
155
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
191 }
192
193
194
195
196
197
198
199
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
219 project = issueManagementUrl.substring( indexBrowseEnd, indexProject );
220 }
221 else
222 {
223
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
241
242 public static String getBaseUrl( String url )
243 {
244 int index = url.indexOf( "/", 8 );
245 return url.substring( 0, index );
246 }
247 }