org.takes.facets.forward.RsForward Java Examples

The following examples show how to use org.takes.facets.forward.RsForward. 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: TkInvalidate.java    From jare with MIT License 5 votes vote down vote up
@Override
public Response act(final Request req) throws IOException {
    final String url = new RqHref.Base(req).href()
        .param("url").iterator().next();
    final String path = String.format(
        "/?u=%s",
        URLEncoder.encode(
            url,
            "UTF-8"
        )
    );
    final AmazonCloudFront aws = AmazonCloudFrontClientBuilder.standard()
        .withCredentials(
            new AWSStaticCredentialsProvider(
                new BasicAWSCredentials(this.key, this.secret)
            )
        )
        .build();
    final CreateInvalidationResult result = aws.createInvalidation(
        new CreateInvalidationRequest(
            "E2QC66VZY6F0QA",
            new InvalidationBatch(
                new Paths().withItems(path).withQuantity(1),
                UUID.randomUUID().toString()
            )
        )
    );
    return new RsForward(
        new RsFlash(
            String.format(
                "URL \"%s\" was invalidated (ID=\"%s\", Status=\"%s\")",
                url,
                result.getInvalidation().getId(),
                result.getInvalidation().getStatus()
            )
        ),
        "/domains"
    );
}
 
Example #2
Source File: TkSecure.java    From takes with MIT License 5 votes vote down vote up
@Override
public Response act(final Request request) throws Exception {
    if (new RqAuth(request).identity().equals(Identity.ANONYMOUS)) {
        throw new RsForward(
            new RsFlash("access denied", Level.WARNING),
            this.loc
        );
    }
    return this.origin.act(request);
}
 
Example #3
Source File: PsBasicTest.java    From takes with MIT License 5 votes vote down vote up
/**
 * PsBasic can handle connection with invalid credential.
 * @throws Exception If some problem inside
 */
@Test
public void handleConnectionWithInvalidCredential() throws Exception {
    RsForward forward = new RsForward();
    try {
        new PsBasic(
            "RealmB",
            new PsBasic.Empty()
        ).enter(
            new RqWithHeaders(
                new RqFake(
                    RqMethod.GET,
                    String.format(
                        "?invalid_code=%s",
                        RandomStringUtils.randomAlphanumeric(Tv.TEN)
                    )
                ),
                PsBasicTest.header("username", "wrong")
            )
        );
    } catch (final RsForward ex) {
        forward = ex;
    }
    MatcherAssert.assertThat(
        new RsPrint(forward).printHead(),
        Matchers.allOf(
            Matchers.containsString("HTTP/1.1 401 Unauthorized"),
            Matchers.containsString(
                "WWW-Authenticate: Basic ream=\"RealmB\""
            )
        )
    );
}
 
Example #4
Source File: TkSecureTest.java    From takes with MIT License 5 votes vote down vote up
/**
 * TkSecure can fail on anonymous access.
 * @throws Exception If some problem inside
 */
@Test(expected = RsForward.class)
public void failsOnAnonymous() throws Exception {
    new TkSecure(
        new Take() {
            @Override
            public Response act(final Request request) {
                return new RsEmpty();
            }
        }
    ).act(new RqFake());
}
 
Example #5
Source File: PsBasic.java    From takes with MIT License 4 votes vote down vote up
@Override
public Opt<Identity> enter(final Request request) throws IOException {
    final Iterator<String> headers = new RqHeaders.Smart(
        new RqHeaders.Base(request)
    ).header("authorization").iterator();
    if (!headers.hasNext()) {
        throw new RsForward(
            new RsWithHeader(
                String.format(
                    "WWW-Authenticate: Basic ream=\"%s\" ",
                    this.realm
                )
            ),
            HttpURLConnection.HTTP_UNAUTHORIZED,
            new RqHref.Base(request).href()
        );
    }
    final String decoded = new UncheckedText(
        new Trimmed(
            new TextOf(
                DatatypeConverter.parseBase64Binary(
                    PsBasic.AUTH.split(headers.next())[1]
                )
            )
        )
    ).asString();
    final String user = decoded.split(":")[0];
    final Opt<Identity> identity = this.entry.enter(
        user,
        decoded.substring(user.length() + 1)
    );
    if (!identity.has()) {
        throw new RsForward(
            new RsWithHeader(
                new RsFlash("access denied", Level.WARNING),
                String.format(
                    "WWW-Authenticate: Basic ream=\"%s\"",
                    this.realm
                )
            ),
            HttpURLConnection.HTTP_UNAUTHORIZED,
            new RqHref.Base(request).href()
        );
    }
    return identity;
}
 
Example #6
Source File: TkAdd.java    From jare with MIT License 2 votes vote down vote up
/**
 * Make forward.
 * @param rsp Response
 * @return Forward
 */
private static RsForward forward(final Response rsp) {
    return new RsForward(rsp, "/domains");
}