Line data Source code
1 : // Copyright (C) 2014 The Android Open Source Project 2 : // 3 : // Licensed under the Apache License, Version 2.0 (the "License"); 4 : // you may not use this file except in compliance with the License. 5 : // You may obtain a copy of the License at 6 : // 7 : // http://www.apache.org/licenses/LICENSE-2.0 8 : // 9 : // Unless required by applicable law or agreed to in writing, software 10 : // distributed under the License is distributed on an "AS IS" BASIS, 11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 : // See the License for the specific language governing permissions and 13 : // limitations under the License. 14 : 15 : package com.google.gerrit.acceptance; 16 : 17 : import com.google.common.base.CharMatcher; 18 : import com.google.gerrit.common.Nullable; 19 : import java.io.IOException; 20 : import java.net.URI; 21 : import org.apache.http.HttpHost; 22 : import org.apache.http.client.HttpClient; 23 : import org.apache.http.client.fluent.Executor; 24 : import org.apache.http.client.fluent.Request; 25 : import org.apache.http.impl.client.HttpClientBuilder; 26 : 27 : public class HttpSession { 28 : protected TestAccount account; 29 : protected final String url; 30 : private final Executor executor; 31 : 32 132 : public HttpSession(GerritServer server, @Nullable TestAccount account) { 33 132 : this.url = CharMatcher.is('/').trimTrailingFrom(server.getUrl()); 34 132 : URI uri = URI.create(url); 35 132 : HttpClient noRedirectClient = HttpClientBuilder.create().disableRedirectHandling().build(); 36 132 : this.executor = Executor.newInstance(noRedirectClient); 37 132 : this.account = account; 38 132 : if (account != null) { 39 132 : executor.auth( 40 132 : new HttpHost(uri.getHost(), uri.getPort()), account.username(), account.httpPassword()); 41 : } 42 132 : } 43 : 44 : public String url() { 45 2 : return url; 46 : } 47 : 48 : public RestResponse execute(Request request) throws IOException { 49 29 : return new RestResponse(executor.execute(request).returnResponse()); 50 : } 51 : }