Line data Source code
1 : // Copyright (C) 2013 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 static com.google.common.net.HttpHeaders.ACCEPT; 18 : import static com.google.common.net.HttpHeaders.CONTENT_TYPE; 19 : import static com.google.gerrit.json.OutputFormat.JSON_COMPACT; 20 : import static java.nio.charset.StandardCharsets.UTF_8; 21 : import static java.util.Objects.requireNonNull; 22 : 23 : import com.google.gerrit.common.Nullable; 24 : import com.google.gerrit.extensions.restapi.RawInput; 25 : import java.io.IOException; 26 : import org.apache.http.Header; 27 : import org.apache.http.client.fluent.Request; 28 : import org.apache.http.entity.BufferedHttpEntity; 29 : import org.apache.http.entity.InputStreamEntity; 30 : import org.apache.http.entity.StringEntity; 31 : import org.apache.http.message.BasicHeader; 32 : 33 : public class RestSession extends HttpSession { 34 : 35 : public RestSession(GerritServer server, @Nullable TestAccount account) { 36 132 : super(server, account); 37 132 : } 38 : 39 : public RestResponse get(String endPoint) throws IOException { 40 18 : return getWithHeaders(endPoint); 41 : } 42 : 43 : public RestResponse getJsonAccept(String endPoint) throws IOException { 44 1 : return getWithHeaders(endPoint, new BasicHeader(ACCEPT, "application/json")); 45 : } 46 : 47 : public RestResponse getWithHeaders(String endPoint, Header... headers) throws IOException { 48 18 : Request get = Request.Get(getUrl(endPoint)); 49 18 : if (headers != null) { 50 18 : get.setHeaders(headers); 51 : } 52 18 : return execute(get); 53 : } 54 : 55 : public RestResponse head(String endPoint) throws IOException { 56 1 : return execute(Request.Head(getUrl(endPoint))); 57 : } 58 : 59 : public RestResponse put(String endPoint) throws IOException { 60 7 : return put(endPoint, /* content = */ null); 61 : } 62 : 63 : public RestResponse put(String endPoint, Object content) throws IOException { 64 12 : return putWithHeaders(endPoint, content); 65 : } 66 : 67 : public RestResponse putWithHeaders(String endPoint, Header... headers) throws IOException { 68 2 : return putWithHeaders(endPoint, /* content= */ null, headers); 69 : } 70 : 71 : public RestResponse putWithHeaders(String endPoint, Object content, Header... headers) 72 : throws IOException { 73 12 : Request put = Request.Put(getUrl(endPoint)); 74 12 : if (headers != null) { 75 12 : put.setHeaders(headers); 76 : } 77 12 : if (content != null) { 78 9 : addContentToRequest(put, content); 79 : } 80 12 : return execute(put); 81 : } 82 : 83 : public RestResponse putRaw(String endPoint, RawInput stream) throws IOException { 84 1 : requireNonNull(stream); 85 1 : Request put = Request.Put(getUrl(endPoint)); 86 1 : put.addHeader(new BasicHeader(CONTENT_TYPE, stream.getContentType())); 87 1 : put.body( 88 : new BufferedHttpEntity( 89 1 : new InputStreamEntity(stream.getInputStream(), stream.getContentLength()))); 90 1 : return execute(put); 91 : } 92 : 93 : public RestResponse post(String endPoint) throws IOException { 94 8 : return post(endPoint, /* content = */ null); 95 : } 96 : 97 : public RestResponse post(String endPoint, Object content) throws IOException { 98 12 : return postWithHeaders(endPoint, content); 99 : } 100 : 101 : public RestResponse postWithHeaders(String endPoint, Object content, Header... headers) 102 : throws IOException { 103 12 : Request post = Request.Post(getUrl(endPoint)); 104 12 : if (headers != null) { 105 12 : post.setHeaders(headers); 106 : } 107 12 : if (content != null) { 108 9 : addContentToRequest(post, content); 109 : } 110 12 : return execute(post); 111 : } 112 : 113 : private static void addContentToRequest(Request request, Object content) { 114 12 : request.addHeader(new BasicHeader(CONTENT_TYPE, "application/json")); 115 12 : request.body(new StringEntity(JSON_COMPACT.newGson().toJson(content), UTF_8)); 116 12 : } 117 : 118 : public RestResponse delete(String endPoint) throws IOException { 119 8 : return execute(Request.Delete(getUrl(endPoint))); 120 : } 121 : 122 : public RestResponse deleteWithHeaders(String endPoint, Header... headers) throws IOException { 123 1 : Request delete = Request.Delete(getUrl(endPoint)); 124 1 : if (headers != null) { 125 1 : delete.setHeaders(headers); 126 : } 127 1 : return execute(delete); 128 : } 129 : 130 : private String getUrl(String endPoint) { 131 29 : return url + (account != null ? "/a" : "") + endPoint; 132 : } 133 : }