|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.elasticsearch.client; |
| 21 | + |
| 22 | +import org.apache.http.entity.ContentType; |
| 23 | +import org.apache.http.Header; |
| 24 | +import org.apache.http.HttpEntity; |
| 25 | +import org.apache.http.nio.entity.NStringEntity; |
| 26 | +import org.apache.http.nio.protocol.HttpAsyncResponseConsumer; |
| 27 | + |
| 28 | +import java.util.Arrays; |
| 29 | +import java.util.HashMap; |
| 30 | +import java.util.Map; |
| 31 | +import java.util.Objects; |
| 32 | + |
| 33 | +import static java.util.Collections.unmodifiableMap; |
| 34 | + |
| 35 | +/** |
| 36 | + * Portion the configuraiton of an HTTP request to Elasticsearch that |
| 37 | + * can be manipulated without changing Elasticsearch's behavior. |
| 38 | + */ |
| 39 | +public final class RequestOptions { |
| 40 | + |
| 41 | + public static Builder builder() { |
| 42 | + Builder builder = new Builder(); |
| 43 | + builder.setHeaders(NO_HEADERS); |
| 44 | + builder.setHttpAsyncResponseConsumerFactory(HttpAsyncResponseConsumerFactory.DEFAULT); |
| 45 | + return builder; |
| 46 | + } |
| 47 | + |
| 48 | + private static final Header[] NO_HEADERS = new Header[0]; |
| 49 | + |
| 50 | + private final Header[] headers; |
| 51 | + private final HttpAsyncResponseConsumerFactory httpAsyncResponseConsumerFactory; |
| 52 | + |
| 53 | + private RequestOptions(Builder builder) { |
| 54 | + this.headers = builder.headers; |
| 55 | + this.httpAsyncResponseConsumerFactory = builder.httpAsyncResponseConsumerFactory; |
| 56 | + } |
| 57 | + |
| 58 | + public Builder toBuilder() { |
| 59 | + Builder builder = new Builder(); |
| 60 | + builder.setHeaders(headers); |
| 61 | + builder.setHttpAsyncResponseConsumerFactory(httpAsyncResponseConsumerFactory); |
| 62 | + return builder; |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Headers to attach to the request. |
| 67 | + */ |
| 68 | + public Header[] getHeaders() { |
| 69 | + return headers; |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * The {@link HttpAsyncResponseConsumerFactory} used to create one |
| 74 | + * {@link HttpAsyncResponseConsumer} callback per retry. Controls how the |
| 75 | + * response body gets streamed from a non-blocking HTTP connection on the |
| 76 | + * client side. |
| 77 | + */ |
| 78 | + public HttpAsyncResponseConsumerFactory getHttpAsyncResponseConsumerFactory() { |
| 79 | + return httpAsyncResponseConsumerFactory; |
| 80 | + } |
| 81 | + |
| 82 | + @Override |
| 83 | + public String toString() { |
| 84 | + StringBuilder b = new StringBuilder(); |
| 85 | + b.append("RequestOptions{"); |
| 86 | + if (headers.length > 0) { |
| 87 | + b.append(", headers="); |
| 88 | + for (int h = 0; h < headers.length; h++) { |
| 89 | + if (h != 0) { |
| 90 | + b.append(','); |
| 91 | + } |
| 92 | + b.append(headers[h].toString()); |
| 93 | + } |
| 94 | + } |
| 95 | + if (httpAsyncResponseConsumerFactory != HttpAsyncResponseConsumerFactory.DEFAULT) { |
| 96 | + b.append(", consumerFactory=").append(httpAsyncResponseConsumerFactory); |
| 97 | + } |
| 98 | + return b.append('}').toString(); |
| 99 | + } |
| 100 | + |
| 101 | + @Override |
| 102 | + public boolean equals(Object obj) { |
| 103 | + if (obj == null || (obj.getClass() != getClass())) { |
| 104 | + return false; |
| 105 | + } |
| 106 | + if (obj == this) { |
| 107 | + return true; |
| 108 | + } |
| 109 | + |
| 110 | + RequestOptions other = (RequestOptions) obj; |
| 111 | + return Arrays.equals(headers, other.headers) |
| 112 | + && httpAsyncResponseConsumerFactory.equals(other.httpAsyncResponseConsumerFactory); |
| 113 | + } |
| 114 | + |
| 115 | + @Override |
| 116 | + public int hashCode() { |
| 117 | + return Objects.hash(Arrays.hashCode(headers), httpAsyncResponseConsumerFactory); |
| 118 | + } |
| 119 | + |
| 120 | + public static class Builder { |
| 121 | + private Header[] headers; |
| 122 | + private HttpAsyncResponseConsumerFactory httpAsyncResponseConsumerFactory; |
| 123 | + |
| 124 | + /** |
| 125 | + * Build the {@linkplain RequestOptions}. |
| 126 | + */ |
| 127 | + public RequestOptions builder() { |
| 128 | + return new RequestOptions(this); |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * Set the headers to attach to the request. |
| 133 | + */ |
| 134 | + public void setHeaders(Header... headers) { |
| 135 | + Objects.requireNonNull(headers, "headers cannot be null"); |
| 136 | + for (Header header : headers) { |
| 137 | + Objects.requireNonNull(header, "header cannot be null"); |
| 138 | + } |
| 139 | + this.headers = headers; |
| 140 | + } |
| 141 | + |
| 142 | + /** |
| 143 | + * set the {@link HttpAsyncResponseConsumerFactory} used to create one |
| 144 | + * {@link HttpAsyncResponseConsumer} callback per retry. Controls how the |
| 145 | + * response body gets streamed from a non-blocking HTTP connection on the |
| 146 | + * client side. |
| 147 | + */ |
| 148 | + public void setHttpAsyncResponseConsumerFactory(HttpAsyncResponseConsumerFactory httpAsyncResponseConsumerFactory) { |
| 149 | + this.httpAsyncResponseConsumerFactory = |
| 150 | + Objects.requireNonNull(httpAsyncResponseConsumerFactory, "httpAsyncResponseConsumerFactory cannot be null"); |
| 151 | + } |
| 152 | + } |
| 153 | +} |
0 commit comments