Skip to content

Commit b6c88d2

Browse files
committed
#1136 RsStatus
1 parent b53b64d commit b6c88d2

28 files changed

+146
-29
lines changed

src/main/java/org/takes/http/BkBasic.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,6 @@ private static Response failure(final Throwable err, final int code) {
157157
* @param socket Socket
158158
* @return Request with custom headers
159159
*/
160-
@SuppressWarnings("PMD.AvoidDuplicateLiterals")
161160
private static Request addSocketHeaders(final Request req,
162161
final Socket socket) {
163162
return new RqWithHeaders(
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) 2014-2022 Yegor Bugayenko
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included
14+
* in all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
package org.takes.rs;
25+
26+
import java.io.IOException;
27+
import java.util.regex.Matcher;
28+
import java.util.regex.Pattern;
29+
import lombok.EqualsAndHashCode;
30+
import org.takes.Response;
31+
32+
/**
33+
* HTTP status of the Response.
34+
*
35+
* <p>All implementations of this interface must be immutable and thread-safe.
36+
*
37+
* @since 1.22
38+
*/
39+
public interface RsStatus extends Response {
40+
41+
/**
42+
* Get status.
43+
* @return Status
44+
* @throws IOException If fails
45+
*/
46+
int status() throws IOException;
47+
48+
/**
49+
* Request decorator, for HTTP URI query parsing.
50+
*
51+
* <p>The class is immutable and thread-safe.
52+
*
53+
* @since 1.22
54+
*/
55+
@EqualsAndHashCode(callSuper = true)
56+
final class Base extends RsWrap implements RsStatus {
57+
58+
/**
59+
* HTTP status line pattern.
60+
*/
61+
private static final Pattern LINE = Pattern.compile(
62+
"HTTP/[0-9.]+\\s+([0-9]+)\\s+.*"
63+
);
64+
65+
/**
66+
* Ctor.
67+
* @param res Original response
68+
*/
69+
public Base(final Response res) {
70+
super(res);
71+
}
72+
73+
@Override
74+
public int status() throws IOException {
75+
final String line = this.head().iterator().next();
76+
final Matcher matcher = RsStatus.Base.LINE.matcher(line);
77+
if (!matcher.matches()) {
78+
throw new IllegalArgumentException(
79+
String.format(
80+
"Invalid status line: '%s'", line
81+
)
82+
);
83+
}
84+
return Integer.parseInt(matcher.group(1));
85+
}
86+
}
87+
}
88+

src/main/java/org/takes/servlet/HttpServletRequestFake.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,7 @@
6161
@SuppressWarnings({
6262
"PMD.TooManyMethods",
6363
"PMD.ExcessiveImports",
64-
"PMD.ExcessivePublicCount",
65-
"PMD.AvoidDuplicateLiterals"
64+
"PMD.ExcessivePublicCount"
6665
})
6766
public final class HttpServletRequestFake implements HttpServletRequest {
6867
/**

src/main/java/org/takes/tk/TkSslOnly.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ public TkSslOnly(final Take take) {
5757
}
5858

5959
@Override
60-
@SuppressWarnings("PMD.AvoidDuplicateLiterals")
6160
public Response act(final Request req) throws Exception {
6261
final String href = new RqHref.Base(req).href().toString();
6362
final String proto = new RqHeaders.Smart(

src/test/java/org/takes/facets/auth/PsByFlagTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
* Test case for {@link PsByFlag}.
4646
* @since 0.10
4747
*/
48-
@SuppressWarnings("PMD.AvoidDuplicateLiterals") final class PsByFlagTest {
48+
final class PsByFlagTest {
4949
/**
5050
* PsByFlag can skip if nothing found.
5151
* @throws Exception If some problem inside

src/test/java/org/takes/facets/auth/codecs/CcAesTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
* Test case for {@link CcAes}.
3939
* @since 0.13.8
4040
*/
41-
@SuppressWarnings("PMD.AvoidDuplicateLiterals") final class CcAesTest {
41+
final class CcAesTest {
4242
/**
4343
* CcAes can encrypt identity.
4444
* @throws Exception If fails

src/test/java/org/takes/facets/auth/signatures/SiHmacTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
* Test case for {@link SiHmac}.
3333
* @since 1.3
3434
*/
35-
@SuppressWarnings("PMD.AvoidDuplicateLiterals") final class SiHmacTest {
35+
final class SiHmacTest {
3636
/**
3737
* SiHmac corrects wrong bit length.
3838
*/

src/test/java/org/takes/facets/fork/FkContentTypeTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
* Test case for {@link FkContentType}.
3838
* @since 1.0
3939
*/
40-
@SuppressWarnings("PMD.AvoidDuplicateLiterals") final class FkContentTypeTest {
40+
final class FkContentTypeTest {
4141

4242
/**
4343
* Content-Type header.

src/test/java/org/takes/facets/fork/FkRegexTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
* Test case for {@link FkRegex}.
3636
* @since 0.4
3737
*/
38-
@SuppressWarnings("PMD.AvoidDuplicateLiterals")
3938
final class FkRegexTest {
4039

4140
/**

src/test/java/org/takes/facets/fork/TkConsumesTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,6 @@ void failsOnUnsupportedAcceptHeader() {
113113
* Checks TkConsumes equals method.
114114
*/
115115
@Test
116-
@SuppressWarnings("PMD.AvoidDuplicateLiterals")
117116
void equalsAndHashCodeEqualTest() {
118117
final Take take = new TkText("text");
119118
final String type = "Content-Type: text/plain";

src/test/java/org/takes/misc/HrefTest.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
* Test case for {@link Href}.
3535
* @since 0.7
3636
*/
37-
@SuppressWarnings("PMD.AvoidDuplicateLiterals")
3837
final class HrefTest {
3938

4039
/**
@@ -147,7 +146,6 @@ void acceptsNonProperlyEncodedUrl() {
147146
/**
148147
* Href can build an URI with fragment.
149148
*/
150-
@SuppressWarnings("PMD.AvoidDuplicateLiterals")
151149
@Test
152150
void buildsUriWithFragmentAndParams() {
153151
MatcherAssert.assertThat(
@@ -159,7 +157,6 @@ void buildsUriWithFragmentAndParams() {
159157
/**
160158
* Href can build an URI with fragment and no params.
161159
*/
162-
@SuppressWarnings("PMD.AvoidDuplicateLiterals")
163160
@Test
164161
void buildsUriWithFragmentAndNoParams() {
165162
MatcherAssert.assertThat(

src/test/java/org/takes/rq/RqFakeTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
* Test case for {@link RqFake}.
3838
* @since 0.24
3939
*/
40-
@SuppressWarnings({"PMD.AvoidUsingHardCodedIP", "PMD.AvoidDuplicateLiterals"})
40+
@SuppressWarnings("PMD.AvoidUsingHardCodedIP")
4141
public final class RqFakeTest {
4242

4343
/**

src/test/java/org/takes/rq/RqHrefTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
* Test case for {@link RqHref.Base}.
3737
* @since 0.1
3838
*/
39-
@SuppressWarnings("PMD.AvoidDuplicateLiterals") final class RqHrefTest {
39+
final class RqHrefTest {
4040

4141
/**
4242
* RqHref.Base can parse a query.

src/test/java/org/takes/rq/RqSocketTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
* Test case for {@link RqSocket}.
3939
* @since 1.0
4040
*/
41-
@SuppressWarnings({"PMD.AvoidUsingHardCodedIP", "PMD.AvoidDuplicateLiterals"})
41+
@SuppressWarnings("PMD.AvoidUsingHardCodedIP")
4242
final class RqSocketTest {
4343

4444
/**

src/test/java/org/takes/rq/RqWithHeaderTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
* Test case for {@link RqWithHeader}.
3434
* @since 0.9
3535
*/
36-
@SuppressWarnings("PMD.AvoidDuplicateLiterals") final class RqWithHeaderTest {
36+
final class RqWithHeaderTest {
3737

3838
/**
3939
* RqWithHeader can add a header.

src/test/java/org/takes/rq/RqWithHeadersTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
* Test case for {@link RqWithHeaders}.
3838
* @since 1.0
3939
*/
40-
@SuppressWarnings("PMD.AvoidDuplicateLiterals") final class RqWithHeadersTest {
40+
final class RqWithHeadersTest {
4141

4242
/**
4343
* RqWithHeaders can add headers.

src/test/java/org/takes/rq/multipart/RqMtFakeTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
* Test case for {@link RqMtFake}.
4646
* @since 0.33
4747
*/
48-
@SuppressWarnings({"PMD.AvoidDuplicateLiterals", "PMD.TooManyMethods"})
48+
@SuppressWarnings("PMD.TooManyMethods")
4949
final class RqMtFakeTest {
5050
/**
5151
* Format string for {@code Content-Length} header.

src/test/java/org/takes/rs/RsBodyPrintTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
* Test case for {@link RsBodyPrint}.
3434
* @since 1.19
3535
*/
36-
@SuppressWarnings("PMD.AvoidDuplicateLiterals") final class RsBodyPrintTest {
36+
final class RsBodyPrintTest {
3737

3838
@Test
3939
void simple() throws IOException {

src/test/java/org/takes/rs/RsPrettyJsonTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838
* Test case for {@link RsPrettyJson}.
3939
* @since 1.0
4040
*/
41-
@SuppressWarnings("PMD.AvoidDuplicateLiterals")
4241
final class RsPrettyJsonTest {
4342

4443
/**

src/test/java/org/takes/rs/RsPrintTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
* Test case for {@link RsPrint}.
3737
* @since 1.19
3838
*/
39-
@SuppressWarnings("PMD.AvoidDuplicateLiterals")
4039
final class RsPrintTest {
4140

4241
/**
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) 2014-2022 Yegor Bugayenko
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included
14+
* in all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
package org.takes.rs;
25+
26+
import java.io.IOException;
27+
import org.hamcrest.MatcherAssert;
28+
import org.hamcrest.Matchers;
29+
import org.junit.jupiter.api.Test;
30+
31+
/**
32+
* Test case for {@link RsStatus}.
33+
* @since 1.22
34+
*/
35+
final class RsStatusTest {
36+
37+
@Test
38+
void readsStatusCode() throws IOException {
39+
final int status = 200;
40+
MatcherAssert.assertThat(
41+
new RsStatus.Base(new RsWithStatus(new RsEmpty(), status)).status(),
42+
Matchers.equalTo(status)
43+
);
44+
}
45+
}

src/test/java/org/takes/rs/xe/XeWhenTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
* Test case for {@link XeWhen}.
3737
* @since 0.13
3838
*/
39-
@SuppressWarnings("PMD.AvoidDuplicateLiterals")
4039
final class XeWhenTest {
4140

4241
/**

src/test/java/org/takes/servlet/HttpServletResponseFakeTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141
*
4242
* @since 1.14
4343
*/
44-
@SuppressWarnings("PMD.AvoidDuplicateLiterals")
4544
final class HttpServletResponseFakeTest {
4645
/**
4746
* Set-Cookie header name.

src/test/java/org/takes/tk/TkGzipTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@
4646
* Test case for {@link TkGzip}.
4747
* @since 0.17
4848
*/
49-
@SuppressWarnings("PMD.AvoidDuplicateLiterals")
5049
final class TkGzipTest {
5150

5251
/**

src/test/java/org/takes/tk/TkHtmlTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
* Test case for {@link TkHtml}.
4040
* @since 0.10
4141
*/
42-
@SuppressWarnings("PMD.AvoidDuplicateLiterals")
4342
final class TkHtmlTest {
4443

4544
/**

src/test/java/org/takes/tk/TkRedirectTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
* Test case for {@link TkRedirect}.
3737
* @since 0.10
3838
*/
39-
@SuppressWarnings("PMD.AvoidDuplicateLiterals") final class TkRedirectTest {
39+
final class TkRedirectTest {
4040
/**
4141
* Constant variable for HTTP header testing.
4242
*/

src/test/java/org/takes/tk/TkSmartRedirectTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
* Test case for {@link TkSmartRedirect}.
3333
* @since 0.10
3434
*/
35-
@SuppressWarnings("PMD.AvoidDuplicateLiterals")
3635
final class TkSmartRedirectTest {
3736
/**
3837
* TkRedirect should carry on the query and the fragment.

src/test/java/org/takes/tk/TkTextTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
* Test case for {@link TkText}.
4040
* @since 0.4
4141
*/
42-
@SuppressWarnings("PMD.AvoidDuplicateLiterals")
4342
final class TkTextTest {
4443

4544
/**

0 commit comments

Comments
 (0)