Skip to main content
summaryrefslogtreecommitdiffstats
blob: 20d475e57957663591d984d7be45a27ba688b0a9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/*******************************************************************************
 * Copyright (c) 2010 Tasktop Technologies and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Tasktop Technologies - initial API and implementation
 *     Eike Stepper - fixes for bug 323568
 *******************************************************************************/

package org.eclipse.mylyn.commons.net.http;

import java.io.IOException;

import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.HttpMethodBase;
import org.eclipse.mylyn.commons.core.operations.IOperationMonitor;
import org.eclipse.mylyn.commons.core.operations.OperationUtil;
import org.eclipse.mylyn.commons.net.AuthenticationCredentials;
import org.eclipse.mylyn.commons.net.WebUtil;
import org.eclipse.mylyn.internal.commons.net.http.CommonGetMethod3;
import org.eclipse.mylyn.internal.commons.net.http.CommonHeadMethod3;
import org.eclipse.mylyn.internal.commons.net.http.CommonPostMethod3;

/**
 * @author Steffen Pingel
 * @since 3.7
 */
public abstract class HttpOperation3<T> {

	private final CommonHttpClient3 client;

	public HttpOperation3(CommonHttpClient3 client) {
		this.client = client;
	}

	protected CommonHttpMethod3 createGetMethod(String requestPath) {
		return new CommonGetMethod3(requestPath);
	}

	protected CommonHttpMethod3 createPostMethod(String requestPath) {
		return new CommonPostMethod3(requestPath);
	}

	protected CommonHttpMethod3 createHeadMethod(String requestPath) {
		return new CommonHeadMethod3(requestPath);
	}

	protected int execute(HttpMethod method, IOperationMonitor monitor) throws IOException {
		monitor = OperationUtil.convert(monitor);

		// force authentication
		if (needsAuthentication()) {
			client.authenticate(monitor);
		}

		// first attempt
		int code = executeInternal(method, monitor);
		if (needsReauthentication(code, monitor)) {
			WebUtil.releaseConnection((HttpMethodBase) method, monitor);
			client.authenticate(monitor);

			// second attempt
			return executeInternal(method, monitor);
		} else {
			return code;
		}
	}

	private int executeInternal(HttpMethod method, IOperationMonitor monitor) throws IOException {
		int code;
		try {
			code = WebUtil.execute(client.getHttpClient(), client.getHostConfiguration(monitor), method, monitor);
		} catch (IOException e) {
			WebUtil.releaseConnection((HttpMethodBase) method, monitor);
			throw e;
		} catch (RuntimeException e) {
			WebUtil.releaseConnection((HttpMethodBase) method, monitor);
			throw e;
		}
		return code;
	}

	protected final CommonHttpClient3 getClient() {
		return client;
	}

	protected boolean hasCredentials(AuthenticationCredentials credentials) {
		return credentials != null;
	}

	protected boolean needsAuthentication() {
		return false;
	}

	private boolean needsReauthentication(int code, IOperationMonitor monitor) throws IOException {
		return client.needsReauthentication(code, monitor);
	}

}

Back to the top