Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Timeouts #99

Merged
merged 13 commits into from
Jun 29, 2015
10 changes: 10 additions & 0 deletions src/main/java/com/jcabi/http/Request.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
* @see com.jcabi.http.request.ApacheRequest
*/
@Immutable
@SuppressWarnings("PMD.AvoidDuplicateLiterals")
public interface Request {

/**
Expand Down Expand Up @@ -152,6 +153,15 @@ Request header(
@NotNull(message = "request is never NULL")
Request method(@NotNull(message = "method can't be NULL") String method);

/**
* Use this timeout values.
* @param connect The connect timeout to use in ms
* @param read The read timeout to use in ms
* @return New alternated request
*/
@NotNull(message = "request is never NULL")
Request timeout(int connect, int read);

/**
* Execute it with a specified HTTP method.
* @return Response
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/com/jcabi/http/Wire.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,15 @@ public interface Wire {
* @param method HTTP method
* @param headers Headers
* @param content HTTP body
* @param connect The connect timeout
* @param read The read timeout
* @return Response obtained
* @throws IOException if fails
* @checkstyle ParameterNumber (6 lines)
*/
Response send(Request req, String home, String method,
Collection<Map.Entry<String, String>> headers, InputStream content)
Collection<Map.Entry<String, String>> headers, InputStream content,
int connect, int read)
throws IOException;

}
26 changes: 23 additions & 3 deletions src/main/java/com/jcabi/http/request/ApacheRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,15 @@ public final class ApacheRequest implements Request {
public Response send(final Request req, final String home,
final String method,
final Collection<Map.Entry<String, String>> headers,
final InputStream content) throws IOException {
final InputStream content,
final int connect,
final int read) throws IOException {
final CloseableHttpResponse response =
HttpClients.createSystem().execute(
this.httpRequest(home, method, headers, content)
this.httpRequest(
home, method, headers, content,
connect, read
)
);
try {
return new DefaultResponse(
Expand All @@ -106,14 +111,22 @@ public Response send(final Request req, final String home,
}
/**
* Create request.
* @param home Home URI
* @param method Method to use
* @param headers HTTP Headers to use
* @param content Content to send
* @param connect Connect timeout
* @param read Read timeout
* @return Request
* @throws IOException If an IO Exception occurs
* @checkstyle ParameterNumber (6 lines)
*/
public HttpEntityEnclosingRequestBase httpRequest(final String home,
final String method,
final Collection<Map.Entry<String, String>> headers,
final InputStream content) throws IOException {
final InputStream content,
final int connect,
final int read) throws IOException {
final HttpEntityEnclosingRequestBase req =
new HttpEntityEnclosingRequestBase() {
@Override
Expand All @@ -126,6 +139,8 @@ public String getMethod() {
RequestConfig.custom()
.setCircularRedirectsAllowed(false)
.setRedirectsEnabled(false)
.setConnectTimeout(connect)
.setSocketTimeout(read)
.build()
);
req.setURI(uri);
Expand Down Expand Up @@ -235,6 +250,11 @@ public Request method(
return this.base.method(method);
}

@Override
public Request timeout(final int connect, final int read) {
return this.base.timeout(connect, read);
}

@Override
public Response fetch() throws IOException {
return this.base.fetch();
Expand Down
50 changes: 48 additions & 2 deletions src/main/java/com/jcabi/http/request/BaseRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,16 @@ final class BaseRequest implements Request {
*/
private final transient String mtd;

/**
* Socket timeout to use.
*/
private final transient int connect;

/**
* Read timeout to use.
*/
private final transient int read;

/**
* Headers.
*/
Expand Down Expand Up @@ -142,6 +152,24 @@ final class BaseRequest implements Request {
BaseRequest(final Wire wre, final String uri,
final Iterable<Map.Entry<String, String>> headers,
final String method, final byte[] body) {
this(wre, uri, headers, method, body, 0, 0);
}

/**
* Public ctor.
* @param wre Wire
* @param uri The resource to work with
* @param headers Headers
* @param method HTTP method
* @param body HTTP request body
* @param cnct Connect timeout for http connection
* @param rdd Read timeout for http connection
* @checkstyle ParameterNumber (5 lines)
*/
BaseRequest(final Wire wre, final String uri,
final Iterable<Map.Entry<String, String>> headers,
final String method, final byte[] body,
final int cnct, final int rdd) {
this.wire = wre;
URI addr = URI.create(uri);
if (addr.getPath().isEmpty()) {
Expand All @@ -151,6 +179,8 @@ final class BaseRequest implements Request {
this.hdrs = new Array<Map.Entry<String, String>>(headers);
this.mtd = method;
this.content = body.clone();
this.connect = cnct;
this.read = rdd;
}

@Override
Expand Down Expand Up @@ -209,6 +239,19 @@ public Request method(
);
}

@Override
public Request timeout(final int cnct, final int rdd) {
return new BaseRequest(
this.wire,
this.home,
this.hdrs,
this.mtd,
this.content,
cnct,
rdd
);
}

@Override
public Response fetch() throws IOException {
return this.fetchResponse(new ByteArrayInputStream(this.content));
Expand Down Expand Up @@ -260,7 +303,9 @@ public <T extends Wire> Request through(final Class<T> type,
this.home,
this.hdrs,
this.mtd,
this.content
this.content,
this.connect,
this.read
);
}

Expand Down Expand Up @@ -298,7 +343,8 @@ private Response fetchResponse(final InputStream stream)
final long start = System.currentTimeMillis();
final Response response = this.wire.send(
this, this.home, this.mtd,
this.hdrs, stream
this.hdrs, stream, this.connect,
this.read
);
final URI uri = URI.create(this.home);
Logger.info(
Expand Down
9 changes: 8 additions & 1 deletion src/main/java/com/jcabi/http/request/FakeRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,9 @@ public final class FakeRequest implements Request {
public Response send(final Request req, final String home,
final String method,
final Collection<Map.Entry<String, String>> headers,
final InputStream text) throws IOException {
final InputStream text,
final int connect,
final int read) throws IOException {
return new DefaultResponse(
req,
FakeRequest.this.code,
Expand Down Expand Up @@ -185,6 +187,11 @@ public Request method(
return this.base.method(method);
}

@Override
public Request timeout(final int connect, final int read) {
return this.base.timeout(connect, read);
}

@Override
public Response fetch() throws IOException {
return this.base.fetch();
Expand Down
11 changes: 10 additions & 1 deletion src/main/java/com/jcabi/http/request/JdkRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,9 @@ public final class JdkRequest implements Request {
public Response send(final Request req, final String home,
final String method,
final Collection<Map.Entry<String, String>> headers,
final InputStream content) throws IOException {
final InputStream content,
final int connect,
final int read) throws IOException {
final URLConnection raw = new URL(home).openConnection();
if (!(raw instanceof HttpURLConnection)) {
throw new IOException(
Expand All @@ -96,6 +98,8 @@ public Response send(final Request req, final String home,
}
final HttpURLConnection conn = HttpURLConnection.class.cast(raw);
try {
conn.setConnectTimeout(connect);
conn.setReadTimeout(read);
conn.setRequestMethod(method);
conn.setUseCaches(false);
conn.setInstanceFollowRedirects(false);
Expand Down Expand Up @@ -261,6 +265,11 @@ public Request method(
return this.base.method(method);
}

@Override
public Request timeout(final int connect, final int read) {
return this.base.timeout(connect, read);
}

@Override
public Response fetch() throws IOException {
return this.base.fetch();
Expand Down
8 changes: 5 additions & 3 deletions src/main/java/com/jcabi/http/wire/AutoRedirectingWire.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,11 @@ public AutoRedirectingWire(final Wire wire, final int retries) {
public Response send(final Request req, final String home,
final String method,
final Collection<Map.Entry<String, String>> headers,
final InputStream content) throws IOException {
final InputStream content,
final int connect,
final int read) throws IOException {
Response response = this.origin.send(
req, home, method, headers, content
req, home, method, headers, content, connect, read
);
int attempt = 1;
final URI uri = URI.create(home);
Expand All @@ -130,7 +132,7 @@ public Response send(final Request req, final String home,
}
response = this.origin.send(
req, location.toString(),
method, headers, content
method, headers, content, connect, read
);
try {
TimeUnit.SECONDS.sleep((long) attempt);
Expand Down
8 changes: 6 additions & 2 deletions src/main/java/com/jcabi/http/wire/BasicAuthWire.java
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,9 @@ public BasicAuthWire(@NotNull(message = "wire can't be NULL")
public Response send(final Request req, final String home,
final String method,
final Collection<Map.Entry<String, String>> headers,
final InputStream content) throws IOException {
final InputStream content,
final int connect,
final int read) throws IOException {
final Collection<Map.Entry<String, String>> hdrs =
new LinkedList<Map.Entry<String, String>>();
boolean absent = true;
Expand Down Expand Up @@ -143,6 +145,8 @@ public Response send(final Request req, final String home,
throw new IllegalStateException(ex);
}
}
return this.origin.send(req, home, method, hdrs, content);
return this.origin.send(
req, home, method, hdrs, content, connect, read
);
}
}
31 changes: 26 additions & 5 deletions src/main/java/com/jcabi/http/wire/CachingWire.java
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,9 @@ public CachingWire(
public Response send(final Request req, final String home,
final String method,
final Collection<Map.Entry<String, String>> headers,
final InputStream content) throws IOException {
final InputStream content,
final int connect,
final int read) throws IOException {
final URI uri = req.uri().get();
final StringBuilder label = new StringBuilder(Tv.HUNDRED)
.append(method).append(' ').append(uri.getPath());
Expand All @@ -169,14 +171,18 @@ public Response send(final Request req, final String home,
try {
rsp = CachingWire.CACHE.get(this).get(
new CachingWire.Query(
this.origin, req, home, headers, content
this.origin, req, home, headers, content,
connect, read
)
);
} catch (final ExecutionException ex) {
throw new IOException(ex);
}
} else {
rsp = this.origin.send(req, home, method, headers, content);
rsp = this.origin.send(
req, home, method, headers, content,
connect, read
);
}
return rsp;
}
Expand Down Expand Up @@ -207,23 +213,37 @@ private static final class Query {
* Body.
*/
private final transient InputStream body;
/**
* Connect timeout.
*/
private final transient int connect;
/**
* Read timeout.
*/
private final transient int read;

/**
* Ctor.
* @param wire Original wire
* @param req Request
* @param home URI to fetch
* @param hdrs Headers
* @param input Input body
* @param cnct Connect timeout
* @param rdd Read timeout
* @checkstyle ParameterNumberCheck (5 lines)
*/
Query(final Wire wire, final Request req, final String home,
final Collection<Map.Entry<String, String>> hdrs,
final InputStream input) {
final InputStream input, final int cnct,
final int rdd) {
this.origin = wire;
this.request = req;
this.uri = home;
this.headers = hdrs;
this.body = input;
this.connect = cnct;
this.read = rdd;
}
/**
* Fetch.
Expand All @@ -232,7 +252,8 @@ private static final class Query {
*/
public Response fetch() throws IOException {
return this.origin.send(
this.request, this.uri, Request.GET, this.headers, this.body
this.request, this.uri, Request.GET, this.headers, this.body,
this.connect, this.read
);
}
}
Expand Down
8 changes: 6 additions & 2 deletions src/main/java/com/jcabi/http/wire/CookieOptimizingWire.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,9 @@ public CookieOptimizingWire(@NotNull(message = "wire can't be NULL")
public Response send(final Request req, final String home,
final String method,
final Collection<Map.Entry<String, String>> headers,
final InputStream content) throws IOException {
final InputStream content,
final int connect,
final int read) throws IOException {
final Collection<Map.Entry<String, String>> hdrs =
new LinkedList<Map.Entry<String, String>>();
final ConcurrentMap<String, String> cookies =
Expand Down Expand Up @@ -137,6 +139,8 @@ public Response send(final Request req, final String home,
)
);
}
return this.origin.send(req, home, method, hdrs, content);
return this.origin.send(
req, home, method, hdrs, content, connect, read
);
}
}
Loading