-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathHttpResponseHandler.java
More file actions
122 lines (102 loc) · 4.72 KB
/
Copy pathHttpResponseHandler.java
File metadata and controls
122 lines (102 loc) · 4.72 KB
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package com.github.dockerjava.netty.handler;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.codec.http.HttpContent;
import io.netty.handler.codec.http.HttpHeaderNames;
import io.netty.handler.codec.http.HttpObject;
import io.netty.handler.codec.http.HttpRequest;
import io.netty.handler.codec.http.HttpResponse;
import io.netty.handler.codec.http.LastHttpContent;
import java.nio.charset.Charset;
import com.github.dockerjava.api.async.ResultCallback;
import com.github.dockerjava.api.exception.BadRequestException;
import com.github.dockerjava.api.exception.ConflictException;
import com.github.dockerjava.api.exception.DockerException;
import com.github.dockerjava.api.exception.InternalServerErrorException;
import com.github.dockerjava.api.exception.NotAcceptableException;
import com.github.dockerjava.api.exception.NotFoundException;
import com.github.dockerjava.api.exception.NotModifiedException;
import com.github.dockerjava.api.exception.UnauthorizedException;
/**
* Handler that is responsible to handle an incoming {@link HttpResponse}. It evaluates the status code and triggers the appropriate
* lifecycle methods at the passed {@link ResultCallback}.
*
* @author Marcus Linke
*/
public class HttpResponseHandler extends SimpleChannelInboundHandler<HttpObject> {
private HttpResponse response;
private ByteBuf errorBody = Unpooled.buffer();
private HttpRequestProvider requestProvider;
private ResultCallback<?> resultCallback;
public HttpResponseHandler(HttpRequestProvider requestProvider, ResultCallback<?> resultCallback) {
super(false);
this.requestProvider = requestProvider;
this.resultCallback = resultCallback;
}
@Override
protected void channelRead0(final ChannelHandlerContext ctx, HttpObject msg) throws Exception {
if (msg instanceof HttpResponse) {
response = (HttpResponse) msg;
resultCallback.onStart(() -> ctx.channel().close());
} else if (msg instanceof HttpContent) {
HttpContent content = (HttpContent) msg;
ByteBuf byteBuf = content.content();
switch (response.status().code()) {
case 200:
case 201:
case 204:
ctx.fireChannelRead(byteBuf);
break;
default:
errorBody.writeBytes(byteBuf);
}
if (content instanceof LastHttpContent) {
try {
switch (response.status().code()) {
case 101:
case 200:
case 201:
case 204:
break;
case 301:
case 302:
if (response.headers().contains(HttpHeaderNames.LOCATION)) {
String location = response.headers().get(HttpHeaderNames.LOCATION);
HttpRequest redirected = requestProvider.getHttpRequest(location);
ctx.channel().writeAndFlush(redirected);
}
break;
case 304:
throw new NotModifiedException(getBodyAsMessage(errorBody));
case 400:
throw new BadRequestException(getBodyAsMessage(errorBody));
case 401:
throw new UnauthorizedException(getBodyAsMessage(errorBody));
case 404:
throw new NotFoundException(getBodyAsMessage(errorBody));
case 406:
throw new NotAcceptableException(getBodyAsMessage(errorBody));
case 409:
throw new ConflictException(getBodyAsMessage(errorBody));
case 500:
throw new InternalServerErrorException(getBodyAsMessage(errorBody));
default:
throw new DockerException(getBodyAsMessage(errorBody), response.status().code());
}
} catch (Throwable e) {
resultCallback.onError(e);
} finally {
resultCallback.onComplete();
}
}
}
}
private String getBodyAsMessage(ByteBuf body) {
String result = body.readBytes(body.readableBytes()).toString(Charset.forName("UTF-8"));
body.discardReadBytes();
body.release();
return result;
}
}