org.cactoos.scalar.Constant Java Examples

The following examples show how to use org.cactoos.scalar.Constant. 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: SlicedTest.java    From cactoos with MIT License 6 votes vote down vote up
@Test
public void failSlicing() {
    new Assertion<>(
        "Must fail on slicing",
        () -> new Constant<>(
            new Sliced<>(
                0, 0,
                new IteratorOf<>(
                    1
                )
            ).next()
        ).value(),
        new Throws<>(
            "The iterator doesn't have items any more",
            NoSuchElementException.class
        )
    ).affirm();
}
 
Example #2
Source File: FuncOfTest.java    From cactoos with MIT License 5 votes vote down vote up
@Test
public void convertsScalarIntoFunc() throws Exception {
    final Constant<Integer> scalar = new Constant<>(1);
    new Assertion<>(
        "Result of func must be equal to the original value",
        new FuncOf<>(scalar).apply(new Object()),
        new IsEqual<>(scalar.value())
    ).affirm();
}
 
Example #3
Source File: HtSecureWire.java    From cactoos-http with MIT License 4 votes vote down vote up
@Override
public Input send(final Input input) throws Exception {
    return new HtWire(this.address, new Constant<>(this.port), this.socket)
        .send(input);
}
 
Example #4
Source File: HtWire.java    From cactoos-http with MIT License 4 votes vote down vote up
/**
 * Ctor.
 * @param addr The address of the server
 */
public HtWire(final String addr) {
    // @checkstyle MagicNumber (1 line)
    this(addr, new Constant<>(80), Socket::new);
}
 
Example #5
Source File: PsToken.java    From takes with MIT License 4 votes vote down vote up
@Override
public Opt<Identity> enter(final Request req) throws IOException {
    // @checkstyle ExecutableStatementCount (100 lines)
    Opt<Identity> user = new Opt.Empty<>();
    final UncheckedText head = new Unchecked<>(
        new FirstOf<>(
            text -> new StartsWith(
                new Trimmed(text),
                new TextOf("Bearer")
            ).value(),
            new Mapped<>(
                UncheckedText::new,
                new RqHeaders.Base(req).header(this.header)
            ),
            new Constant<>(new UncheckedText(""))
        )
    ).value();
    if (new Unchecked<>(new Not(new IsBlank(head))).value()) {
        final String jwt = new UncheckedText(
            new Trimmed(new TextOf(head.asString().split(" ", 2)[1]))
        ).asString();
        final String[] parts = jwt.split("\\.");
        final byte[] jwtheader = parts[0].getBytes(
            Charset.defaultCharset()
        );
        final byte[] jwtpayload = parts[1].getBytes(
            Charset.defaultCharset()
        );
        final byte[] jwtsign = parts[2].getBytes(Charset.defaultCharset());
        final ByteBuffer tocheck = ByteBuffer.allocate(
            jwtheader.length + jwtpayload.length + 1
        );
        tocheck.put(jwtheader).put(".".getBytes(Charset.defaultCharset()))
            .put(jwtpayload);
        final byte[] checked = this.signature.sign(tocheck.array());
        if (Arrays.equals(jwtsign, checked)) {
            try (JsonReader rdr = Json.createReader(
                new StringReader(
                    new String(
                        Base64.getDecoder().decode(jwtpayload),
                        Charset.defaultCharset()
                    )
                )
            )) {
                user = new Opt.Single<>(
                    new Identity.Simple(
                        rdr.readObject().getString(Token.Jwt.SUBJECT)
                    )
                );
            }
        }
    }
    return user;
}
 
Example #6
Source File: HtWire.java    From cactoos-http with MIT License 2 votes vote down vote up
/**
 * Ctor.
 * @param addr The address of the server
 * @param tcp The TCP port
 */
public HtWire(final String addr, final int tcp) {
    this(addr, new Constant<>(tcp), Socket::new);
}