Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Mylyn/Architecture/Web

Cancellation

The org.eclipse.mylyn.web.core</core> plug-in helper classes for sending HTTP requests that can be cancelled by a user through the <code>IProgressMonitor interface. The basic principle is that all network i/o, i.e. opening of sockets, reading and writing of streams, is done on separate threads while the IProgressMonitor is polled for its cancelled state. If a cancellation is detected the network operation is aborted by closing the socket or stream.

The web core plug-in maintains a thread pool that is shared by all plug-ins using it's API.

Apache HttpClient

Below is an source of the WebUtil.getTitleFromUrl() method that demonstrates how to send a GET request and parse the response while providing cancellation support.

In order to provide cancellation while a connection is established WebUtil.createHostConfiguration(HttpClient, AbstractWebLocation, IProgressMonitor) should be used to create a host configuration. The returned host configuration uses a custom socket factory that provides cancellation support while a socket is being connected.

Requests should be executed through WebUtil.execute(HttpClient, HostConfiguration, HttpMethod, IProgressMonitor). This method will invoke HttpClient.executeMethod() and provide cancellation support while the HTTP request is sent and the response is parsed.

For reading the response WebUtil.getResponseBodyAsStream(HttpMethodBase, IProgressMonitor) should be used which returns an PollingInputStream that wraps the input stream that is provided by HttpClient and supports cancellation. It is very important to always close the returned input stream in a finally block. If the stream is not closed it will leave a zombie thread which will eventually exhaust the thread pool.

public static String getTitleFromUrl(AbstractWebLocation location, IProgressMonitor monitor) throws IOException, ParseException {
	monitor = Policy.monitorFor(monitor);
	try {
		monitor.beginTask("Retrieving " + location.getUrl(), IProgressMonitor.UNKNOWN);
 
		HttpClient client = new HttpClient();
		WebUtil.configureHttpClient(client, "");
 
		GetMethod method = new GetMethod(location.getUrl());
		try {
			HostConfiguration hostConfiguration = WebUtil.createHostConfiguration(client, location, monitor);
			int result = WebUtil.execute(client, hostConfiguration, method, monitor);
			if (result == HttpStatus.SC_OK) {
				InputStream in = WebUtil.getResponseBodyAsStream(method, monitor);
				try {
					BufferedReader reader = new BufferedReader(new InputStreamReader(in));
					HtmlStreamTokenizer tokenizer = new HtmlStreamTokenizer(reader, null);
					for (Token token = tokenizer.nextToken(); token.getType() != Token.EOF; token = okenizer.nextToken()) {
						if (token.getType() == Token.TAG) {
							HtmlTag tag = (HtmlTag) token.getValue();
							if (tag.getTagType() == Tag.TITLE) {
								return getText(tokenizer);
							}
						}
					}
				} finally {
					in.close();
				}
			}
		} finally {
			method.releaseConnection();
		}
	} finally {
		monitor.done();
	}
	return null;
}

Authentication

Supported Methods:

  • HTML Form: Username, Password
  • HTTP Basic: Username, Password
  • HTTP Digest: Username, Password
  • HTTP NTLM: Realm, Username, Password
  • SSL Certificate: Certificate

Proxy Support

  • HTTP/HTTPS Proxy
  • Socks Proxy

Back to the top