forked from yegor256/takes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBkBasic.java
179 lines (166 loc) · 5.16 KB
/
BkBasic.java
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*
* The MIT License (MIT)
*
* Copyright (c) 2014-2022 Yegor Bugayenko
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.takes.http;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.Socket;
import lombok.EqualsAndHashCode;
import org.cactoos.bytes.BytesOf;
import org.cactoos.io.InputStreamOf;
import org.takes.HttpException;
import org.takes.Request;
import org.takes.Response;
import org.takes.Take;
import org.takes.rq.RqLive;
import org.takes.rq.RqWithHeaders;
import org.takes.rs.RsPrint;
import org.takes.rs.RsText;
import org.takes.rs.RsWithStatus;
/**
* Basic back-end.
*
* <p>The class is immutable and thread-safe.
*
* @since 0.1
*/
@EqualsAndHashCode
@SuppressWarnings("PMD.DataClass")
public final class BkBasic implements Back {
/**
* Local address header name.
*/
public static final String LOCALADDR = "X-Takes-LocalAddress";
/**
* Local port header name.
*/
public static final String LOCALPORT = "X-Takes-LocalPort";
/**
* Remote address header name.
*/
public static final String REMOTEADDR = "X-Takes-RemoteAddress";
/**
* Remote port header name.
*/
public static final String REMOTEPORT = "X-Takes-RemotePort";
/**
* Take.
*/
private final Take take;
/**
* Ctor.
* @param tks Take
*/
public BkBasic(final Take tks) {
this.take = tks;
}
@SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops")
@Override
public void accept(final Socket socket) throws IOException {
try (
InputStream input = socket.getInputStream();
BufferedOutputStream output = new BufferedOutputStream(
socket.getOutputStream()
)
) {
while (true) {
this.print(
BkBasic.addSocketHeaders(
new RqLive(input),
socket
),
output
);
if (input.available() <= 0) {
break;
}
}
}
}
/**
* Print response to output stream, safely.
* @param req Request
* @param output Output
* @throws IOException If fails
*/
@SuppressWarnings("PMD.AvoidCatchingThrowable")
private void print(final Request req, final OutputStream output)
throws IOException {
try {
new RsPrint(this.take.act(req)).print(output);
} catch (final HttpException ex) {
new RsPrint(BkBasic.failure(ex, ex.code())).print(output);
// @checkstyle IllegalCatchCheck (10 lines)
} catch (final Throwable ex) {
new RsPrint(
BkBasic.failure(
ex,
HttpURLConnection.HTTP_INTERNAL_ERROR
)
).print(output);
}
}
/**
* Make a failure response.
* @param err Error
* @param code HTTP error code
* @return Response
*/
private static Response failure(final Throwable err, final int code) {
return new RsWithStatus(
new RsText(
new InputStreamOf(
new BytesOf(err)
)
),
code
);
}
/**
* Adds custom headers with information about socket.
* @param req Request
* @param socket Socket
* @return Request with custom headers
*/
private static Request addSocketHeaders(final Request req,
final Socket socket) {
return new RqWithHeaders(
req,
String.format(
"%s: %s",
BkBasic.LOCALADDR,
socket.getLocalAddress().getHostAddress()
),
String.format("%s: %d", BkBasic.LOCALPORT, socket.getLocalPort()),
String.format(
"%s: %s",
BkBasic.REMOTEADDR,
socket.getInetAddress().getHostAddress()
),
String.format("%s: %d", BkBasic.REMOTEPORT, socket.getPort())
);
}
}