| 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 | |
package com.jcabi.http.request; |
| 31 | |
|
| 32 | |
import com.jcabi.aspects.Immutable; |
| 33 | |
import com.jcabi.aspects.Loggable; |
| 34 | |
import com.jcabi.http.ImmutableHeader; |
| 35 | |
import com.jcabi.http.Request; |
| 36 | |
import com.jcabi.http.RequestBody; |
| 37 | |
import com.jcabi.http.RequestURI; |
| 38 | |
import com.jcabi.http.Response; |
| 39 | |
import com.jcabi.http.Wire; |
| 40 | |
import com.jcabi.immutable.Array; |
| 41 | |
import java.io.IOException; |
| 42 | |
import java.io.InputStream; |
| 43 | |
import java.net.URI; |
| 44 | |
import java.net.URL; |
| 45 | |
import java.util.Collection; |
| 46 | |
import java.util.LinkedList; |
| 47 | |
import java.util.Map; |
| 48 | |
import lombok.EqualsAndHashCode; |
| 49 | |
import lombok.ToString; |
| 50 | |
import org.apache.http.Header; |
| 51 | |
import org.apache.http.HttpEntity; |
| 52 | |
import org.apache.http.client.config.RequestConfig; |
| 53 | |
import org.apache.http.client.methods.CloseableHttpResponse; |
| 54 | |
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase; |
| 55 | |
import org.apache.http.entity.BufferedHttpEntity; |
| 56 | |
import org.apache.http.entity.InputStreamEntity; |
| 57 | |
import org.apache.http.impl.client.HttpClients; |
| 58 | |
import org.apache.http.util.EntityUtils; |
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | |
|
| 70 | |
|
| 71 | |
@Immutable |
| 72 | 0 | @EqualsAndHashCode(of = "base") |
| 73 | 0 | @ToString(of = "base") |
| 74 | |
@Loggable(Loggable.DEBUG) |
| 75 | |
@SuppressWarnings("PMD.TooManyMethods") |
| 76 | |
public final class ApacheRequest implements Request { |
| 77 | |
|
| 78 | |
|
| 79 | |
|
| 80 | |
|
| 81 | |
|
| 82 | 1 | private static final Wire WIRE = new Wire() { |
| 83 | |
|
| 84 | |
@Override |
| 85 | |
public Response send(final Request req, final String home, |
| 86 | |
final String method, |
| 87 | |
final Collection<Map.Entry<String, String>> headers, |
| 88 | |
final InputStream content, |
| 89 | |
final int connect, |
| 90 | |
final int read) throws IOException { |
| 91 | 18 | final CloseableHttpResponse response = |
| 92 | |
HttpClients.createSystem().execute( |
| 93 | |
this.httpRequest( |
| 94 | |
home, method, headers, content, |
| 95 | |
connect, read |
| 96 | |
) |
| 97 | |
); |
| 98 | |
try { |
| 99 | 16 | return new DefaultResponse( |
| 100 | |
req, |
| 101 | |
response.getStatusLine().getStatusCode(), |
| 102 | |
response.getStatusLine().getReasonPhrase(), |
| 103 | |
this.headers(response.getAllHeaders()), |
| 104 | |
this.consume(response.getEntity()) |
| 105 | |
); |
| 106 | |
} finally { |
| 107 | 16 | response.close(); |
| 108 | |
} |
| 109 | |
} |
| 110 | |
|
| 111 | |
|
| 112 | |
|
| 113 | |
|
| 114 | |
|
| 115 | |
|
| 116 | |
|
| 117 | |
|
| 118 | |
|
| 119 | |
|
| 120 | |
|
| 121 | |
|
| 122 | |
public HttpEntityEnclosingRequestBase httpRequest(final String home, |
| 123 | |
final String method, |
| 124 | |
final Collection<Map.Entry<String, String>> headers, |
| 125 | |
final InputStream content, |
| 126 | |
final int connect, |
| 127 | |
final int read) throws IOException { |
| 128 | 18 | final HttpEntityEnclosingRequestBase req = |
| 129 | 18 | new HttpEntityEnclosingRequestBase() { |
| 130 | |
@Override |
| 131 | |
public String getMethod() { |
| 132 | 36 | return method; |
| 133 | |
} |
| 134 | |
}; |
| 135 | 17 | final URI uri = URI.create(home); |
| 136 | 18 | req.setConfig( |
| 137 | |
RequestConfig.custom() |
| 138 | |
.setCircularRedirectsAllowed(false) |
| 139 | |
.setRedirectsEnabled(false) |
| 140 | |
.setConnectTimeout(connect) |
| 141 | |
.setSocketTimeout(read) |
| 142 | |
.build() |
| 143 | |
); |
| 144 | 18 | req.setURI(uri); |
| 145 | 18 | req.setEntity( |
| 146 | |
new BufferedHttpEntity(new InputStreamEntity(content)) |
| 147 | |
); |
| 148 | 18 | for (final Map.Entry<String, String> header : headers) { |
| 149 | 11 | req.addHeader(header.getKey(), header.getValue()); |
| 150 | 11 | } |
| 151 | 18 | return req; |
| 152 | |
} |
| 153 | |
|
| 154 | |
|
| 155 | |
|
| 156 | |
|
| 157 | |
|
| 158 | |
|
| 159 | |
private byte[] consume(final HttpEntity entity) throws IOException { |
| 160 | |
final byte[] body; |
| 161 | 16 | if (entity == null) { |
| 162 | 0 | body = new byte[0]; |
| 163 | |
} else { |
| 164 | 16 | body = EntityUtils.toByteArray(entity); |
| 165 | |
} |
| 166 | 16 | return body; |
| 167 | |
} |
| 168 | |
|
| 169 | |
|
| 170 | |
|
| 171 | |
|
| 172 | |
|
| 173 | |
@SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops") |
| 174 | |
private Array<Map.Entry<String, String>> headers(final Header... list) { |
| 175 | 17 | final Collection<Map.Entry<String, String>> headers = |
| 176 | |
new LinkedList<>(); |
| 177 | 85 | for (final Header header : list) { |
| 178 | 70 | headers.add( |
| 179 | |
new ImmutableHeader( |
| 180 | |
header.getName(), |
| 181 | |
header.getValue() |
| 182 | |
) |
| 183 | |
); |
| 184 | |
} |
| 185 | 17 | return new Array<Map.Entry<String, String>>(headers); |
| 186 | |
} |
| 187 | |
}; |
| 188 | |
|
| 189 | |
|
| 190 | |
|
| 191 | |
|
| 192 | |
private final transient Request base; |
| 193 | |
|
| 194 | |
|
| 195 | |
|
| 196 | |
|
| 197 | |
|
| 198 | |
public ApacheRequest(final URL url) { |
| 199 | 0 | this(url.toString()); |
| 200 | 0 | } |
| 201 | |
|
| 202 | |
|
| 203 | |
|
| 204 | |
|
| 205 | |
|
| 206 | |
public ApacheRequest(final URI uri) { |
| 207 | 23 | this(uri.toString()); |
| 208 | 23 | } |
| 209 | |
|
| 210 | |
|
| 211 | |
|
| 212 | |
|
| 213 | |
|
| 214 | 23 | public ApacheRequest(final String uri) { |
| 215 | 23 | this.base = new BaseRequest(ApacheRequest.WIRE, uri); |
| 216 | 23 | } |
| 217 | |
|
| 218 | |
@Override |
| 219 | |
public RequestURI uri() { |
| 220 | 3 | return this.base.uri(); |
| 221 | |
} |
| 222 | |
|
| 223 | |
@Override |
| 224 | |
public Request header(final String name, final Object value) { |
| 225 | 0 | return this.base.header(name, value); |
| 226 | |
} |
| 227 | |
|
| 228 | |
@Override |
| 229 | |
public Request reset(final String name) { |
| 230 | 0 | return this.base.reset(name); |
| 231 | |
} |
| 232 | |
|
| 233 | |
@Override |
| 234 | |
public RequestBody body() { |
| 235 | 0 | return this.base.body(); |
| 236 | |
} |
| 237 | |
|
| 238 | |
@Override |
| 239 | |
public RequestBody multipartBody() { |
| 240 | 0 | return this.base.multipartBody(); |
| 241 | |
} |
| 242 | |
|
| 243 | |
@Override |
| 244 | |
public Request method(final String method) { |
| 245 | 11 | return this.base.method(method); |
| 246 | |
} |
| 247 | |
|
| 248 | |
@Override |
| 249 | |
public Request timeout(final int connect, final int read) { |
| 250 | 0 | return this.base.timeout(connect, read); |
| 251 | |
} |
| 252 | |
|
| 253 | |
@Override |
| 254 | |
public Response fetch() throws IOException { |
| 255 | 1 | return this.base.fetch(); |
| 256 | |
} |
| 257 | |
|
| 258 | |
@Override |
| 259 | |
public Response fetch(final InputStream stream) throws IOException { |
| 260 | 0 | return this.base.fetch(stream); |
| 261 | |
} |
| 262 | |
|
| 263 | |
@Override |
| 264 | |
public <T extends Wire> Request through(final Class<T> type, |
| 265 | |
final Object... args) { |
| 266 | 8 | return this.base.through(type, args); |
| 267 | |
} |
| 268 | |
|
| 269 | |
} |