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