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 | |
|
50 | |
static Map<String,String> getJiraUrlAndProjectId( String issueManagementUrl ) |
51 | |
{ |
52 | 8 | String url = issueManagementUrl; |
53 | |
|
54 | 8 | if ( url.endsWith( "/" ) ) |
55 | |
{ |
56 | |
|
57 | 2 | url = url.substring( 0, url.lastIndexOf( '/' ) ); |
58 | |
} |
59 | |
|
60 | |
|
61 | 8 | int pos = url.indexOf( '?' ); |
62 | |
|
63 | |
|
64 | 8 | String id = ""; |
65 | |
|
66 | 8 | if ( pos >= 0 ) |
67 | |
{ |
68 | |
|
69 | 2 | id = url.substring( url.lastIndexOf( '=' ) + 1 ); |
70 | |
} |
71 | |
|
72 | 8 | String jiraUrl = url.substring( 0, url.lastIndexOf( '/' ) ); |
73 | |
|
74 | 8 | if ( jiraUrl.endsWith( "secure" ) ) |
75 | |
{ |
76 | 2 | jiraUrl = jiraUrl.substring( 0, jiraUrl.lastIndexOf( '/' ) ); |
77 | |
} |
78 | |
else |
79 | |
{ |
80 | |
|
81 | |
|
82 | |
|
83 | 6 | final int index = jiraUrl.indexOf( "/browse" ); |
84 | 6 | if ( index != -1 ) |
85 | |
{ |
86 | 6 | jiraUrl = jiraUrl.substring( 0, index ); |
87 | |
} |
88 | |
} |
89 | |
|
90 | 8 | HashMap<String,String> urlMap = new HashMap<String,String>( 4 ); |
91 | |
|
92 | 8 | urlMap.put( "url", jiraUrl ); |
93 | |
|
94 | 8 | urlMap.put( "id", id ); |
95 | |
|
96 | 8 | return urlMap; |
97 | |
} |
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
|
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 | |
|
150 | |
|
151 | |
|
152 | |
|
153 | |
|
154 | |
|
155 | |
|
156 | |
|
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 | |
|
192 | 0 | } |
193 | |
|
194 | |
|
195 | |
|
196 | |
|
197 | |
|
198 | |
|
199 | |
|
200 | |
|
201 | |
|
202 | |
|
203 | |
public static Map<String, String> getJiraUrlAndProjectName( String issueManagementUrl ) |
204 | |
{ |
205 | 4 | final int indexBrowse = issueManagementUrl.indexOf( "/browse/" ); |
206 | |
|
207 | 4 | String jiraUrl = ""; |
208 | 4 | String project = ""; |
209 | |
|
210 | 4 | if ( indexBrowse != -1 ) |
211 | |
{ |
212 | 4 | jiraUrl = issueManagementUrl.substring( 0, indexBrowse ); |
213 | |
|
214 | 4 | final int indexBrowseEnd = indexBrowse + "/browse/".length(); |
215 | |
|
216 | 4 | final int indexProject = issueManagementUrl.indexOf( "/", indexBrowseEnd ); |
217 | |
|
218 | 4 | if ( indexProject != -1 ) |
219 | |
{ |
220 | |
|
221 | 2 | project = issueManagementUrl.substring( indexBrowseEnd, indexProject ); |
222 | |
} |
223 | |
else |
224 | |
{ |
225 | |
|
226 | 2 | project = issueManagementUrl.substring( indexBrowseEnd ); |
227 | |
} |
228 | 4 | } |
229 | |
else |
230 | |
{ |
231 | 0 | throw new IllegalArgumentException( "Invalid browse URL" ); |
232 | |
} |
233 | |
|
234 | 4 | HashMap<String, String> urlMap = new HashMap<String, String>( 4 ); |
235 | 4 | urlMap.put( "url", jiraUrl ); |
236 | 4 | urlMap.put( "project", project ); |
237 | |
|
238 | 4 | return urlMap; |
239 | |
} |
240 | |
|
241 | |
|
242 | |
|
243 | |
|
244 | |
public static String getBaseUrl( String url ) |
245 | |
{ |
246 | 4 | int index = url.indexOf( "/", 8 ); |
247 | 4 | return url.substring( 0, index ); |
248 | |
} |
249 | |
} |