Skip to main content
summaryrefslogtreecommitdiffstats
blob: ef42c16665d566397689b6e042a6137bf1e53975 (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
/*******************************************************************************
 * Copyright (c) 2013 Frank Becker 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:
 *     Frank Becker - initial API and implementation
 *******************************************************************************/

package org.eclipse.mylyn.internal.bugzilla.rest.core;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.text.MessageFormat;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpRequestBase;
import org.eclipse.mylyn.commons.core.operations.IOperationMonitor;
import org.eclipse.mylyn.commons.repositories.core.auth.AuthenticationException;
import org.eclipse.mylyn.commons.repositories.core.auth.AuthenticationRequest;
import org.eclipse.mylyn.commons.repositories.core.auth.AuthenticationType;
import org.eclipse.mylyn.commons.repositories.core.auth.UserCredentials;
import org.eclipse.mylyn.commons.repositories.http.core.CommonHttpResponse;
import org.eclipse.mylyn.commons.repositories.http.core.HttpUtil;
import org.eclipse.mylyn.internal.bugzilla.rest.core.response.data.LoginToken;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

public class BugzillaRestAuthenticatedGetRequest<T> extends BugzillaRestRequest<T> {

	private final String urlSuffix;

	private final TypeToken<?> responseType;

	public BugzillaRestAuthenticatedGetRequest(BugzillaRestHttpClient client, String urlSuffix,
			TypeToken<?> responseType) {
		super(client);
		this.urlSuffix = urlSuffix;
		this.responseType = responseType;
	}

	@Override
	protected void authenticate(IOperationMonitor monitor) throws IOException {
		UserCredentials credentials = getClient().getLocation().getCredentials(AuthenticationType.REPOSITORY);
		if (credentials == null) {
			throw new IllegalStateException("Authentication requested without valid credentials"); //$NON-NLS-1$
		}
		HttpRequestBase request = new HttpGet(baseUrl() + MessageFormat.format("/login?login={0}&password={1}", //$NON-NLS-1$
				new Object[] { credentials.getUserName(), credentials.getPassword() }));
		request.setHeader(CONTENT_TYPE, TEXT_XML_CHARSET_UTF_8);
		request.setHeader(ACCEPT, APPLICATION_JSON);
		HttpResponse response = getClient().execute(request, monitor);
		try {
			if (response.getStatusLine().getStatusCode() == HttpStatus.SC_UNAUTHORIZED) {
				getClient().setAuthenticated(false);
				throw new AuthenticationException("Authentication failed", //$NON-NLS-1$
						new AuthenticationRequest<AuthenticationType<UserCredentials>>(getClient().getLocation(),
								AuthenticationType.REPOSITORY));
			} else {
				TypeToken<LoginToken> type = new TypeToken<LoginToken>() {
				};
				InputStream is = response.getEntity().getContent();
				InputStreamReader in = new InputStreamReader(is);
				LoginToken loginToken = new Gson().fromJson(in, type.getType());
				((BugzillaRestHttpClient) getClient()).setLoginToken(loginToken);
				getClient().setAuthenticated(true);
			}
		} finally {
			HttpUtil.release(request, response, monitor);
		}
	}

	@Override
	protected HttpRequestBase createHttpRequestBase() {
		String bugUrl = getUrlSuffix();
		LoginToken token = ((BugzillaRestHttpClient) getClient()).getLoginToken();
		if ((!(this instanceof BugzillaRestValidateRequest) && !(this instanceof BugzillaRestUnauthenticatedGetRequest))
				&& token != null && bugUrl.length() > 0) {
			if (!bugUrl.endsWith("?")) { //$NON-NLS-1$
				bugUrl += "&"; //$NON-NLS-1$
			}
			bugUrl += "token=" + token.getToken(); //$NON-NLS-1$
		}
		HttpRequestBase request = new HttpGet(baseUrl() + bugUrl);
		request.setHeader(CONTENT_TYPE, TEXT_XML_CHARSET_UTF_8);
		request.setHeader(ACCEPT, APPLICATION_JSON);
		return request;
	}

	@Override
	protected T execute(IOperationMonitor monitor) throws IOException, BugzillaRestException {
		if (needsAuthentication()) {
			authenticate(monitor);
		}
		HttpRequestBase request = createHttpRequestBase();
		CommonHttpResponse response = execute(request, monitor);
		return processAndRelease(response, monitor);
	}

	@Override
	protected T parseFromJson(InputStreamReader in) throws BugzillaRestException {
		return new Gson().fromJson(in, responseType.getType());
	}

	@Override
	protected String getUrlSuffix() {
		return urlSuffix;
	}

}

Back to the top