|
Lines 14-31
Link Here
|
| 14 |
import java.io.IOException; |
14 |
import java.io.IOException; |
| 15 |
import java.io.UnsupportedEncodingException; |
15 |
import java.io.UnsupportedEncodingException; |
| 16 |
import java.net.Proxy; |
16 |
import java.net.Proxy; |
|
|
17 |
import java.net.URL; |
| 17 |
import java.net.URLDecoder; |
18 |
import java.net.URLDecoder; |
| 18 |
import java.util.ArrayList; |
19 |
import java.util.ArrayList; |
| 19 |
import java.util.Collections; |
20 |
import java.util.Collections; |
|
|
21 |
import java.util.HashMap; |
| 20 |
import java.util.List; |
22 |
import java.util.List; |
| 21 |
import java.util.Map; |
23 |
import java.util.Map; |
| 22 |
import java.util.Set; |
24 |
import java.util.Set; |
|
|
25 |
import java.util.StringTokenizer; |
| 23 |
import java.util.regex.Matcher; |
26 |
import java.util.regex.Matcher; |
| 24 |
import java.util.regex.Pattern; |
27 |
import java.util.regex.Pattern; |
| 25 |
|
28 |
|
| 26 |
import org.apache.commons.httpclient.Header; |
29 |
import org.apache.commons.httpclient.Header; |
| 27 |
import org.apache.commons.httpclient.HttpClient; |
30 |
import org.apache.commons.httpclient.HttpClient; |
|
|
31 |
import org.apache.commons.httpclient.HttpException; |
| 32 |
import org.apache.commons.httpclient.HttpMethod; |
| 28 |
import org.apache.commons.httpclient.methods.GetMethod; |
33 |
import org.apache.commons.httpclient.methods.GetMethod; |
|
|
34 |
import org.apache.commons.httpclient.methods.PostMethod; |
| 29 |
import org.eclipse.core.runtime.CoreException; |
35 |
import org.eclipse.core.runtime.CoreException; |
| 30 |
import org.eclipse.core.runtime.IProgressMonitor; |
36 |
import org.eclipse.core.runtime.IProgressMonitor; |
| 31 |
import org.eclipse.core.runtime.IStatus; |
37 |
import org.eclipse.core.runtime.IStatus; |
|
Lines 34-39
Link Here
|
| 34 |
import org.eclipse.mylar.internal.tasks.core.WebQueryHit; |
40 |
import org.eclipse.mylar.internal.tasks.core.WebQueryHit; |
| 35 |
import org.eclipse.mylar.internal.tasks.core.WebTask; |
41 |
import org.eclipse.mylar.internal.tasks.core.WebTask; |
| 36 |
import org.eclipse.mylar.internal.tasks.ui.RetrieveTitleFromUrlJob; |
42 |
import org.eclipse.mylar.internal.tasks.ui.RetrieveTitleFromUrlJob; |
|
|
43 |
import org.eclipse.mylar.internal.tasks.web.restui.EHttpMethod; |
| 37 |
import org.eclipse.mylar.tasks.core.AbstractRepositoryConnector; |
44 |
import org.eclipse.mylar.tasks.core.AbstractRepositoryConnector; |
| 38 |
import org.eclipse.mylar.tasks.core.AbstractRepositoryQuery; |
45 |
import org.eclipse.mylar.tasks.core.AbstractRepositoryQuery; |
| 39 |
import org.eclipse.mylar.tasks.core.AbstractRepositoryTask; |
46 |
import org.eclipse.mylar.tasks.core.AbstractRepositoryTask; |
|
Lines 60-65
Link Here
|
| 60 |
|
67 |
|
| 61 |
public static final String PROPERTY_QUERY_REGEXP = "queryPattern"; |
68 |
public static final String PROPERTY_QUERY_REGEXP = "queryPattern"; |
| 62 |
|
69 |
|
|
|
70 |
public static final String PROPERTY_LOGIN_FORM_URL = "loginFormUrl"; |
| 71 |
|
| 72 |
public static final String PROPERTY_LOGIN_TOKEN_REGEXP = "loginTokenPattern"; |
| 73 |
|
| 74 |
public static final String PROPERTY_LOGIN_REQUEST_METHOD = "loginRequestMethod"; |
| 75 |
|
| 76 |
public static final String PROPERTY_LOGIN_REQUEST_URL = "loginRequestUrl"; |
| 77 |
|
| 63 |
public static final String PARAM_PREFIX = "param_"; |
78 |
public static final String PARAM_PREFIX = "param_"; |
| 64 |
|
79 |
|
| 65 |
public static final String PARAM_SERVER_URL = "serverUrl"; |
80 |
public static final String PARAM_SERVER_URL = "serverUrl"; |
|
Lines 68-73
Link Here
|
| 68 |
|
83 |
|
| 69 |
public static final String PARAM_PASSWORD = "password"; |
84 |
public static final String PARAM_PASSWORD = "password"; |
| 70 |
|
85 |
|
|
|
86 |
public static final String PARAM_LOGIN_TOKEN = "loginToken"; |
| 87 |
|
| 71 |
public String getRepositoryType() { |
88 |
public String getRepositoryType() { |
| 72 |
return WebTask.REPOSITORY_TYPE; |
89 |
return WebTask.REPOSITORY_TYPE; |
| 73 |
} |
90 |
} |
|
Lines 151-168
Link Here
|
| 151 |
public IStatus performQuery(AbstractRepositoryQuery query, TaskRepository repository, IProgressMonitor monitor, |
168 |
public IStatus performQuery(AbstractRepositoryQuery query, TaskRepository repository, IProgressMonitor monitor, |
| 152 |
QueryHitCollector resultCollector) { |
169 |
QueryHitCollector resultCollector) { |
| 153 |
if (query instanceof WebQuery) { |
170 |
if (query instanceof WebQuery) { |
| 154 |
String repositoryUser = repository.getUserName(); |
|
|
| 155 |
String repositoryPassword = repository.getPassword(); |
| 156 |
|
| 157 |
WebQuery webQuery = (WebQuery) query; |
171 |
WebQuery webQuery = (WebQuery) query; |
| 158 |
Map<String, String> queryParameters = webQuery.getQueryParameters(); |
172 |
Map<String, String> queryParameters = webQuery.getQueryParameters(); |
| 159 |
String queryUrl = evaluateParams(query.getUrl(), queryParameters, repository); |
173 |
String queryUrl = evaluateParams(query.getUrl(), queryParameters, repository); |
| 160 |
String queryPattern = evaluateParams(webQuery.getQueryPattern(), queryParameters, repository); |
174 |
String queryPattern = evaluateParams(webQuery.getQueryPattern(), queryParameters, repository); |
| 161 |
String taskPrefix = evaluateParams(webQuery.getTaskPrefix(), queryParameters, repository); |
175 |
String taskPrefix = evaluateParams(webQuery.getTaskPrefix(), queryParameters, repository); |
|
|
176 |
Map<String, String> loginParams = new HashMap<String, String>(); |
| 177 |
loginParams.put(WebRepositoryConnector.PROPERTY_LOGIN_FORM_URL, evaluateParams(repository |
| 178 |
.getProperty(WebRepositoryConnector.PROPERTY_LOGIN_FORM_URL), queryParameters, repository)); |
| 179 |
loginParams.put(WebRepositoryConnector.PROPERTY_LOGIN_TOKEN_REGEXP, evaluateParams(repository |
| 180 |
.getProperty(WebRepositoryConnector.PROPERTY_LOGIN_TOKEN_REGEXP), queryParameters, repository)); |
| 181 |
loginParams.put(WebRepositoryConnector.PROPERTY_LOGIN_REQUEST_METHOD, evaluateParams(repository |
| 182 |
.getProperty(WebRepositoryConnector.PROPERTY_LOGIN_REQUEST_METHOD), queryParameters, repository)); |
| 183 |
loginParams.put(WebRepositoryConnector.PROPERTY_LOGIN_REQUEST_URL, repository |
| 184 |
.getProperty(WebRepositoryConnector.PROPERTY_LOGIN_REQUEST_URL)); |
| 162 |
|
185 |
|
| 163 |
try { |
186 |
try { |
| 164 |
// if (regexp != null && regexp.trim().length() > 0) { |
187 |
// if (regexp != null && regexp.trim().length() > 0) { |
| 165 |
return performQuery(fetchResource(queryUrl, repositoryUser, repositoryPassword, repository.getProxy()), queryPattern, |
188 |
return performQuery(fetchResource(queryUrl, queryParameters, loginParams, repository), queryPattern, |
| 166 |
taskPrefix, monitor, resultCollector, repository); |
189 |
taskPrefix, monitor, resultCollector, repository); |
| 167 |
// } else { |
190 |
// } else { |
| 168 |
// return performRssQuery(queryUrl, taskPrefix, repositoryUrl, |
191 |
// return performRssQuery(queryUrl, taskPrefix, repositoryUrl, |
|
Lines 275-313
Link Here
|
| 275 |
* "Could not fetch resource: " + queryUrl, ex); } } |
298 |
* "Could not fetch resource: " + queryUrl, ex); } } |
| 276 |
*/ |
299 |
*/ |
| 277 |
|
300 |
|
| 278 |
public static String fetchResource(String url, String user, String password, Proxy proxySettings) throws IOException { |
301 |
public static String fetchResource(String url, Map<String, String> params, Map<String, String> loginParams, TaskRepository repository) throws IOException { |
| 279 |
HttpClient client = new HttpClient(); |
302 |
HttpClient client = new HttpClient(); |
| 280 |
WebClientUtil.setupHttpClient(client, proxySettings, url, user, password); |
303 |
WebClientUtil.setupHttpClient(client, repository.getProxy(), url, repository.getUserName(), repository.getPassword()); |
| 281 |
|
304 |
|
| 282 |
GetMethod get = new GetMethod(url); |
305 |
if (loginParams != null) { |
| 283 |
try { |
306 |
String loginFormUrl = loginParams.get(PROPERTY_LOGIN_FORM_URL); |
| 284 |
client.executeMethod(get); |
307 |
if (loginFormUrl != null && !"".equals(loginFormUrl)) { |
| 285 |
Header refreshHeader = get.getResponseHeader("Refresh"); |
308 |
GetMethod get = new GetMethod(loginFormUrl); |
| 286 |
|
309 |
String loginFormPage = null; |
| 287 |
if (refreshHeader != null) { |
310 |
try { |
| 288 |
String value = refreshHeader.getValue(); |
311 |
client.executeMethod(get); |
| 289 |
int n = value.indexOf(";url="); |
312 |
Header locationHeader = get.getResponseHeader("Location"); |
| 290 |
if (n != -1) { |
313 |
if (locationHeader != null) { |
| 291 |
value = value.substring(n + 5); |
314 |
get = new GetMethod(locationHeader.getValue()); |
| 292 |
int requestPath; |
315 |
client.executeMethod(get); |
| 293 |
if (value.charAt(0) == '/') { |
|
|
| 294 |
int colonSlashSlash = url.indexOf("://"); |
| 295 |
requestPath = url.indexOf('/', colonSlashSlash + 3); |
| 296 |
} else { |
| 297 |
requestPath = url.lastIndexOf('/'); |
| 298 |
} |
316 |
} |
| 299 |
|
317 |
refreshIfNeeded(loginFormUrl, client, get); |
| 300 |
String refreshUrl; |
318 |
loginFormPage = get.getResponseBodyAsString(); |
| 301 |
if (requestPath == -1) { |
319 |
} catch (Exception e) { |
| 302 |
refreshUrl = url + "/" + value; |
320 |
// ignore |
| 303 |
} else { |
321 |
e.printStackTrace(); |
| 304 |
refreshUrl = url.substring(0, requestPath + 1) + value; |
322 |
} finally { |
|
|
323 |
get.releaseConnection(); |
| 324 |
} |
| 325 |
if (loginFormPage != null) { |
| 326 |
String loginTokenPattern = loginParams.get(PROPERTY_LOGIN_TOKEN_REGEXP); |
| 327 |
if (loginTokenPattern != null && !"".equals(loginTokenPattern)) { |
| 328 |
Pattern p = Pattern.compile(loginTokenPattern); |
| 329 |
Matcher m = p.matcher(loginFormPage); |
| 330 |
if (m.find()) { |
| 331 |
// TODO is it ok to modify the params map? |
| 332 |
params.put(PARAM_PREFIX + PARAM_LOGIN_TOKEN, m.group(1)); |
| 333 |
} |
| 305 |
} |
334 |
} |
| 306 |
|
|
|
| 307 |
get = new GetMethod(refreshUrl); |
| 308 |
client.executeMethod(get); |
| 309 |
} |
335 |
} |
| 310 |
} |
336 |
} |
|
|
337 |
HttpMethod method = null; |
| 338 |
try { |
| 339 |
String loginUrlStr = loginParams.get(PROPERTY_LOGIN_REQUEST_URL); |
| 340 |
loginUrlStr = evaluateParams(loginUrlStr, params, repository); |
| 341 |
switch (EHttpMethod.valueOf(loginParams.get(PROPERTY_LOGIN_REQUEST_METHOD))) { |
| 342 |
case GET: |
| 343 |
method = new GetMethod(loginUrlStr); |
| 344 |
break; |
| 345 |
case POST: |
| 346 |
String query = new URL(loginUrlStr).getQuery(); |
| 347 |
if (query != null) { |
| 348 |
StringTokenizer tokenizer = new StringTokenizer(query, "&"); |
| 349 |
method = new PostMethod(loginUrlStr.substring(0, loginUrlStr.indexOf(query) - 1)); |
| 350 |
while (tokenizer.hasMoreTokens()) { |
| 351 |
String nameValuePair = tokenizer.nextToken(); |
| 352 |
String[] nv = nameValuePair.split("="); |
| 353 |
if (nv.length < 2) { |
| 354 |
((PostMethod) method).addParameter(nv[0], ""); |
| 355 |
} else { |
| 356 |
((PostMethod) method).addParameter(nv[0], nv[1]); |
| 357 |
} |
| 358 |
} |
| 359 |
} |
| 360 |
break; |
| 361 |
} |
| 362 |
client.executeMethod(method); |
| 363 |
} catch (Exception e) { |
| 364 |
// ignore |
| 365 |
} finally { |
| 366 |
if (method != null) { |
| 367 |
method.releaseConnection(); |
| 368 |
} |
| 369 |
} |
| 370 |
} |
| 371 |
|
| 372 |
GetMethod get = new GetMethod(url); |
| 373 |
try { |
| 374 |
client.executeMethod(get); |
| 375 |
get = refreshIfNeeded(url, client, get); |
| 311 |
return get.getResponseBodyAsString(); |
376 |
return get.getResponseBodyAsString(); |
| 312 |
|
377 |
|
| 313 |
} finally { |
378 |
} finally { |
|
Lines 315-320
Link Here
|
| 315 |
} |
380 |
} |
| 316 |
} |
381 |
} |
| 317 |
|
382 |
|
|
|
383 |
private static GetMethod refreshIfNeeded(String url, HttpClient client, GetMethod get) throws IOException, |
| 384 |
HttpException { |
| 385 |
Header refreshHeader = get.getResponseHeader("Refresh"); |
| 386 |
|
| 387 |
if (refreshHeader != null) { |
| 388 |
String value = refreshHeader.getValue(); |
| 389 |
int n = value.indexOf(";url="); |
| 390 |
if (n != -1) { |
| 391 |
value = value.substring(n + 5); |
| 392 |
int requestPath; |
| 393 |
if (value.charAt(0) == '/') { |
| 394 |
int colonSlashSlash = url.indexOf("://"); |
| 395 |
requestPath = url.indexOf('/', colonSlashSlash + 3); |
| 396 |
} else { |
| 397 |
requestPath = url.lastIndexOf('/'); |
| 398 |
} |
| 399 |
|
| 400 |
String refreshUrl; |
| 401 |
if (requestPath == -1) { |
| 402 |
refreshUrl = url + "/" + value; |
| 403 |
} else { |
| 404 |
refreshUrl = url.substring(0, requestPath + 1) + value; |
| 405 |
} |
| 406 |
|
| 407 |
get = new GetMethod(refreshUrl); |
| 408 |
client.executeMethod(get); |
| 409 |
} |
| 410 |
} |
| 411 |
return get; |
| 412 |
} |
| 413 |
|
| 318 |
public static String evaluateParams(String value, Map<String, String> params, TaskRepository repository) { |
414 |
public static String evaluateParams(String value, Map<String, String> params, TaskRepository repository) { |
| 319 |
return evaluateParams(evaluateParams(value, params), repository); |
415 |
return evaluateParams(evaluateParams(value, params), repository); |
| 320 |
} |
416 |
} |