| 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.ByteArrayOutputStream; |
| 42 | |
import java.io.IOException; |
| 43 | |
import java.io.InputStream; |
| 44 | |
import java.io.OutputStream; |
| 45 | |
import java.net.HttpURLConnection; |
| 46 | |
import java.net.URI; |
| 47 | |
import java.net.URL; |
| 48 | |
import java.net.URLConnection; |
| 49 | |
import java.util.Collection; |
| 50 | |
import java.util.LinkedList; |
| 51 | |
import java.util.List; |
| 52 | |
import java.util.Map; |
| 53 | |
import lombok.EqualsAndHashCode; |
| 54 | |
import lombok.ToString; |
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 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 JdkRequest 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 | 92 | final URLConnection raw = new URL(home).openConnection(); |
| 92 | 93 | if (!(raw instanceof HttpURLConnection)) { |
| 93 | 0 | throw new IOException( |
| 94 | |
String.format( |
| 95 | |
"'%s' opens %s instead of expected HttpURLConnection", |
| 96 | |
home, raw.getClass().getName() |
| 97 | |
) |
| 98 | |
); |
| 99 | |
} |
| 100 | 93 | final HttpURLConnection conn = HttpURLConnection.class.cast(raw); |
| 101 | |
try { |
| 102 | 93 | conn.setConnectTimeout(connect); |
| 103 | 93 | conn.setReadTimeout(read); |
| 104 | 93 | conn.setRequestMethod(method); |
| 105 | 93 | conn.setUseCaches(false); |
| 106 | 93 | conn.setInstanceFollowRedirects(false); |
| 107 | 93 | for (final Map.Entry<String, String> header : headers) { |
| 108 | 41 | conn.addRequestProperty(header.getKey(), header.getValue()); |
| 109 | 41 | } |
| 110 | 93 | if (method.equals(Request.POST) || method.equals(Request.PUT) |
| 111 | |
|| method.equals(Request.PATCH)) { |
| 112 | 17 | conn.setDoOutput(true); |
| 113 | 17 | final OutputStream output = conn.getOutputStream(); |
| 114 | |
try { |
| 115 | 17 | this.writeFully(content, output); |
| 116 | |
} finally { |
| 117 | 17 | output.close(); |
| 118 | 17 | content.close(); |
| 119 | 17 | } |
| 120 | |
} |
| 121 | 93 | return new DefaultResponse( |
| 122 | |
req, |
| 123 | |
conn.getResponseCode(), |
| 124 | |
conn.getResponseMessage(), |
| 125 | |
this.headers(conn.getHeaderFields()), |
| 126 | |
this.body(conn) |
| 127 | |
); |
| 128 | 0 | } catch (final IOException exp) { |
| 129 | 0 | throw new IOException( |
| 130 | |
String.format("Failed %s request to %s", method, home), |
| 131 | |
exp |
| 132 | |
); |
| 133 | |
} finally { |
| 134 | 93 | conn.disconnect(); |
| 135 | |
} |
| 136 | |
} |
| 137 | |
|
| 138 | |
|
| 139 | |
|
| 140 | |
|
| 141 | |
|
| 142 | |
|
| 143 | |
private void writeFully(final InputStream content, |
| 144 | |
final OutputStream output) throws IOException { |
| 145 | |
|
| 146 | 17 | final byte[] buffer = new byte[8192]; |
| 147 | 17 | for (int bytes = content.read(buffer); bytes != -1; |
| 148 | 10 | bytes = content.read(buffer)) { |
| 149 | 10 | output.write(buffer, 0, bytes); |
| 150 | |
} |
| 151 | 17 | } |
| 152 | |
|
| 153 | |
|
| 154 | |
|
| 155 | |
|
| 156 | |
|
| 157 | |
private Array<Map.Entry<String, String>> headers( |
| 158 | |
final Map<String, List<String>> fields) { |
| 159 | 91 | final Collection<Map.Entry<String, String>> headers = |
| 160 | |
new LinkedList<Map.Entry<String, String>>(); |
| 161 | |
for (final Map.Entry<String, List<String>> field |
| 162 | 91 | : fields.entrySet()) { |
| 163 | 452 | if (field.getKey() == null) { |
| 164 | 92 | continue; |
| 165 | |
} |
| 166 | 359 | for (final String value : field.getValue()) { |
| 167 | 362 | headers.add(new ImmutableHeader(field.getKey(), value)); |
| 168 | 362 | } |
| 169 | 358 | } |
| 170 | 91 | return new Array<>(headers); |
| 171 | |
} |
| 172 | |
|
| 173 | |
|
| 174 | |
|
| 175 | |
|
| 176 | |
|
| 177 | |
|
| 178 | |
private byte[] body(final HttpURLConnection conn) throws IOException { |
| 179 | |
final InputStream input; |
| 180 | 91 | if (conn.getResponseCode() >= HttpURLConnection.HTTP_BAD_REQUEST) { |
| 181 | 5 | input = conn.getErrorStream(); |
| 182 | |
} else { |
| 183 | 86 | input = conn.getInputStream(); |
| 184 | |
} |
| 185 | |
final byte[] body; |
| 186 | 92 | if (input == null) { |
| 187 | 4 | body = new byte[0]; |
| 188 | |
} else { |
| 189 | |
try { |
| 190 | |
|
| 191 | 87 | final byte[] buffer = new byte[8192]; |
| 192 | 89 | final ByteArrayOutputStream output = |
| 193 | |
new ByteArrayOutputStream(); |
| 194 | 89 | for (int bytes = input.read(buffer); bytes != -1; |
| 195 | 40 | bytes = input.read(buffer)) { |
| 196 | 40 | output.write(buffer, 0, bytes); |
| 197 | |
} |
| 198 | 89 | body = output.toByteArray(); |
| 199 | |
} finally { |
| 200 | 89 | input.close(); |
| 201 | 89 | } |
| 202 | |
} |
| 203 | 93 | return body; |
| 204 | |
} |
| 205 | |
}; |
| 206 | |
|
| 207 | |
|
| 208 | |
|
| 209 | |
|
| 210 | |
private final transient Request base; |
| 211 | |
|
| 212 | |
|
| 213 | |
|
| 214 | |
|
| 215 | |
|
| 216 | |
public JdkRequest(final URL url) { |
| 217 | 0 | this(url.toString()); |
| 218 | 0 | } |
| 219 | |
|
| 220 | |
|
| 221 | |
|
| 222 | |
|
| 223 | |
|
| 224 | |
public JdkRequest(final URI uri) { |
| 225 | 60 | this(uri.toString()); |
| 226 | 60 | } |
| 227 | |
|
| 228 | |
|
| 229 | |
|
| 230 | |
|
| 231 | |
|
| 232 | 60 | public JdkRequest(final String uri) { |
| 233 | 60 | this.base = new BaseRequest(JdkRequest.WIRE, uri); |
| 234 | 60 | } |
| 235 | |
|
| 236 | |
@Override |
| 237 | |
public RequestURI uri() { |
| 238 | 3 | return this.base.uri(); |
| 239 | |
} |
| 240 | |
|
| 241 | |
@Override |
| 242 | |
public Request header(final String name, final Object value) { |
| 243 | 0 | return this.base.header(name, value); |
| 244 | |
} |
| 245 | |
|
| 246 | |
@Override |
| 247 | |
public Request reset(final String name) { |
| 248 | 0 | return this.base.reset(name); |
| 249 | |
} |
| 250 | |
|
| 251 | |
@Override |
| 252 | |
public RequestBody body() { |
| 253 | 0 | return this.base.body(); |
| 254 | |
} |
| 255 | |
|
| 256 | |
@Override |
| 257 | |
public RequestBody multipartBody() { |
| 258 | 0 | return this.base.multipartBody(); |
| 259 | |
} |
| 260 | |
|
| 261 | |
@Override |
| 262 | |
public Request method(final String method) { |
| 263 | 11 | return this.base.method(method); |
| 264 | |
} |
| 265 | |
|
| 266 | |
@Override |
| 267 | |
public Request timeout(final int connect, final int read) { |
| 268 | 0 | return this.base.timeout(connect, read); |
| 269 | |
} |
| 270 | |
|
| 271 | |
@Override |
| 272 | |
public Response fetch() throws IOException { |
| 273 | 2 | return this.base.fetch(); |
| 274 | |
} |
| 275 | |
|
| 276 | |
@Override |
| 277 | |
public Response fetch(final InputStream stream) throws IOException { |
| 278 | 0 | return this.base.fetch(stream); |
| 279 | |
} |
| 280 | |
|
| 281 | |
@Override |
| 282 | |
public <T extends Wire> Request through(final Class<T> type, |
| 283 | |
final Object... args) { |
| 284 | 44 | return this.base.through(type, args); |
| 285 | |
} |
| 286 | |
|
| 287 | |
} |