org.takes.rs.RsWithBody Java Examples

The following examples show how to use org.takes.rs.RsWithBody. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: FbStatus.java    From takes with MIT License 6 votes vote down vote up
/**
 * Ctor.
 * @param check HTTP status code predicate
 * @since 0.16.10
 */
public FbStatus(final Iterable<Integer> check) {
    this(check, new Fallback() {
        @Override
        public Opt<Response> route(final RqFallback req)
            throws Exception {
            final Response res = new RsWithStatus(req.code());
            return new Opt.Single<>(
                new RsWithType(
                    new RsWithBody(
                        res,
                        String.format(
                            "%s: %s", FbStatus.WHITESPACE.split(
                                res.head().iterator().next(),
                                2
                            )[1], req.throwable().getLocalizedMessage()
                        )
                    ), "text/plain"
                )
            );
        }
    });
}
 
Example #2
Source File: TkClasspath.java    From takes with MIT License 6 votes vote down vote up
/**
 * Ctor.
 * @param prefix Prefix
 */
public TkClasspath(final String prefix) {
    super(
        new Take() {
            @Override
            public Response act(final Request request) throws IOException {
                final String name = String.format(
                    "%s%s", prefix, new RqHref.Base(request).href().path()
                );
                final InputStream input = this.getClass()
                    .getResourceAsStream(name);
                if (input == null) {
                    throw new HttpException(
                        HttpURLConnection.HTTP_NOT_FOUND,
                        String.format("%s not found in classpath", name)
                    );
                }
                return new RsWithBody(input);
            }
        }
    );
}
 
Example #3
Source File: TkFiles.java    From takes with MIT License 6 votes vote down vote up
/**
 * Ctor.
 * @param base Base directory
 */
public TkFiles(final File base) {
    super(
        new Take() {
            @Override
            public Response act(final Request request) throws IOException {
                final File file = new File(
                    base, new RqHref.Base(request).href().path()
                );
                if (!file.exists()) {
                    throw new HttpException(
                        HttpURLConnection.HTTP_NOT_FOUND,
                        String.format(
                            "%s not found", file.getAbsolutePath()
                        )
                    );
                }
                return new RsWithBody(Files.newInputStream(file.toPath()));
            }
        }
    );
}
 
Example #4
Source File: PsByFlagTest.java    From takes with MIT License 6 votes vote down vote up
/**
 * PsByFlag wraps response with authenticated user.
 * @throws IOException If some problem inside
 */
@Test
public void exitTest() throws IOException {
    final Response response = new RsWithStatus(
        new RsWithType(
            new RsWithBody("<html>This is test response</html>"),
            "text/html"
        ),
        HttpURLConnection.HTTP_OK
    );
    MatcherAssert.assertThat(
        new PsByFlag(
            ImmutableMap.<Pattern, Pass>of(
                Pattern.compile("key"), new PsFake(true)
            )
        ).exit(response, Mockito.mock(Identity.class)),
        Matchers.is(response)
    );
}
 
Example #5
Source File: Pages.java    From jpeek with MIT License 5 votes vote down vote up
@Override
public Response apply(final String path) throws IOException {
    return new RsWithBody(
        new TextOf(
            this.home.resolve(path)
        ).asString()
    );
}
 
Example #6
Source File: TkProxy.java    From takes with MIT License 5 votes vote down vote up
/**
 * Creates the response received from the target host.
 *
 * @param home Home host
 * @param dest Destination URL
 * @param rsp Response received from the target host
 * @return Response
 */
private Response response(final String home, final URI dest,
    final com.jcabi.http.Response rsp) {
    final Collection<String> hdrs = new LinkedList<>();
    hdrs.add(
        String.format(
            "X-Takes-TkProxy: from %s to %s by %s",
            home, dest, this.label
        )
    );
    for (final Map.Entry<String, List<String>> entry
        : rsp.headers().entrySet()) {
        for (final String value : entry.getValue()) {
            final String val;
            if (TkProxy.isHost(entry.getKey())) {
                val = this.target.toString();
            } else {
                val = value;
            }
            hdrs.add(String.format("%s: %s", entry.getKey(), val));
        }
    }
    return new RsWithStatus(
        new RsWithBody(
            new RsWithHeaders(hdrs),
            rsp.binary()
        ),
        rsp.status(),
        rsp.reason()
    );
}
 
Example #7
Source File: HmRsTextBodyTest.java    From takes with MIT License 5 votes vote down vote up
/**
 * HmRsTextBody can test if body equals text.
 */
@Test
public void testsBodyValueContainsText() {
    final String same = "<h1>Hello</h1>";
    MatcherAssert.assertThat(
        new RsWithBody(same),
        new HmRsTextBody(same)
    );
}
 
Example #8
Source File: HmRsTextBodyTest.java    From takes with MIT License 5 votes vote down vote up
/**
 * HmRsTextBody can test if body doesn't equal to text.
 */
@Test
public void testsBodyValueDoesNotContainsText() {
    MatcherAssert.assertThat(
        new RsWithBody("Some response"),
        new IsNot<>(new HmRsTextBody("expected something else"))
    );
}
 
Example #9
Source File: TakesContact.java    From tutorials with MIT License 5 votes vote down vote up
@Override
public Response act(Request req) throws IOException {
    return new RsWithStatus(
        new RsWithType(
            new RsWithBody("Contact us at https://www.baeldung.com"), 
            "text/html"), 200);
}