Skip to main content
summaryrefslogtreecommitdiffstats
blob: e13a19efb1e8db7c3b0837797e7a327414f758f3 (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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*******************************************************************************
 * Copyright (c) 2011 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
 *******************************************************************************/

package org.eclipse.mylyn.commons.repositories.http.core;

import java.io.IOException;
import java.io.InputStream;

import org.apache.http.HttpEntity;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.util.EntityUtils;
import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.mylyn.commons.core.operations.CancellableOperationMonitorThread;
import org.eclipse.mylyn.commons.core.operations.ICancellableOperation;
import org.eclipse.mylyn.commons.core.operations.IOperationMonitor;
import org.eclipse.mylyn.commons.core.operations.OperationUtil;

/**
 * @author Steffen Pingel
 */
public class CommonHttpResponse implements ICancellableOperation {

	private CancellableInputStream entityStream;

	private final IOperationMonitor monitor;

	private final HttpRequest request;

	private final HttpResponse response;

	private final CancellableOperationMonitorThread monitorThread;

	public CommonHttpResponse(HttpRequest request, HttpResponse response,
			CancellableOperationMonitorThread monitorThread, IOperationMonitor monitor) {
		Assert.isNotNull(request);
		Assert.isNotNull(response);
		Assert.isNotNull(monitorThread);
		Assert.isNotNull(monitor);
		this.request = request;
		this.response = response;
		this.monitorThread = monitorThread;
		this.monitor = monitor;
	}

	public CommonHttpResponse(HttpRequest request, HttpResponse response, IOperationMonitor monitor) {
		this(request, response, CancellableOperationMonitorThread.getInstance(), monitor);
	}

	/**
	 * @deprecated use
	 *             {@link CommonHttpResponse#CommonHttpResponse(HttpRequest, HttpResponse, CancellableOperationMonitorThread, IOperationMonitor)	
	 */
	@Deprecated
	public CommonHttpResponse(HttpRequest request, HttpResponse response) {
		this(request, response, CancellableOperationMonitorThread.getInstance(), OperationUtil.convert(null));
	}

	@Override
	public void abort() {
		abortStream();
		if (request instanceof HttpUriRequest) {
			try {
				((HttpUriRequest) request).abort();
			} catch (UnsupportedOperationException e) {
			}
		}
	}

	public HttpRequest getRequest() {
		return request;
	}

	public String getRequestPath() {
		if (request instanceof HttpUriRequest) {
			return ((HttpUriRequest) request).getURI().getPath();
		} else {
			return null;
		}
	}

	public HttpResponse getResponse() {
		return response;
	}

	public String getResponseCharSet() {
		return EntityUtils.getContentCharSet(response.getEntity());
	}

	public synchronized InputStream getResponseEntityAsStream() throws IOException {
		if (entityStream != null) {
			throw new IllegalStateException();
		}
		HttpEntity entity = response.getEntity();
		if (entity == null) {
			throw new IOException("Expected entity"); //$NON-NLS-1$
		}
		entityStream = new CancellableInputStream(this, entity.getContent());
		monitorThread.addOperation(this);
		return entityStream;
	}

	/**
	 * @deprecated use {@link #getResponseEntityAsStream()} instead
	 */
	@Deprecated
	public InputStream getResponseEntityAsStream(IProgressMonitor monitor) throws IOException {
		return getResponseEntityAsStream();
	}

	public int getStatusCode() {
		return response.getStatusLine().getStatusCode();
	}

	@Override
	public boolean isCanceled() {
		return monitor.isCanceled();
	}

	public void release() {
		releaseStream();
		HttpUtil.release(request, response, monitor);
	}

	/**
	 * @deprecated use {@link #release()} instead
	 */
	@Deprecated
	public void release(IProgressMonitor monitor) {
		release();
	}

	private synchronized void abortStream() {
		if (entityStream != null) {
			CancellableOperationMonitorThread.getInstance().removeOperation(this);
			entityStream.cancel();
		}
	}

	private synchronized void releaseStream() {
		if (entityStream != null) {
			CancellableOperationMonitorThread.getInstance().removeOperation(this);
			entityStream = null;
		}
	}

	void notifyStreamClosed() {
		release();
	}

}

Back to the top