Java Code Examples for org.takes.misc.Opt#Empty

The following examples show how to use org.takes.misc.Opt#Empty . 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: RqLive.java    From takes with MIT License 6 votes vote down vote up
/**
 * Builds current read header.
 * @param data Current read character
 * @param baos Current read header
 * @return Read header
 */
private static Opt<String> newHeader(final Opt<Integer> data,
    final ByteArrayOutputStream baos) {
    Opt<String> header = new Opt.Empty<>();
    if (data.get() != ' ' && data.get() != '\t') {
        header = new Opt.Single<>(
            new UncheckedText(
                new TextOf(
                    baos.toByteArray()
                )
            ).asString()
        );
        baos.reset();
    }
    return header;
}
 
Example 2
Source File: PsCookie.java    From takes with MIT License 6 votes vote down vote up
@Override
public Opt<Identity> enter(final Request req) throws IOException {
    final Iterator<String> cookies = new RqCookies.Base(req)
        .cookie(this.cookie).iterator();
    Opt<Identity> user = new Opt.Empty<>();
    if (cookies.hasNext()) {
        user = new Opt.Single<>(
            this.codec.decode(
                new UncheckedBytes(
                    new BytesOf(cookies.next())
                ).asBytes()
            )
        );
    }
    return user;
}
 
Example 3
Source File: PsBasic.java    From takes with MIT License 6 votes vote down vote up
@Override
public Opt<Identity> enter(final String user, final String pwd) {
    final Opt<String> urn = this.urn(user, pwd);
    final Opt<Identity> identity;
    if (urn.has()) {
        try {
            identity = new Opt.Single<>(
                new Identity.Simple(
                    URLDecoder.decode(
                        urn.get(), PsBasic.Default.ENCODING
                    )
                )
            );
        } catch (final UnsupportedEncodingException ex) {
            throw new IllegalStateException(ex);
        }
    } else {
        identity = new Opt.Empty<>();
    }
    return identity;
}
 
Example 4
Source File: FkHitRefresh.java    From takes with MIT License 6 votes vote down vote up
@Override
public Opt<Response> route(final Request req) throws Exception {
    final Iterator<String> header =
        new RqHeaders.Base(req).header("X-Takes-HitRefresh").iterator();
    final Opt<Response> resp;
    if (header.hasNext()) {
        if (this.handle.expired()) {
            this.exec.run();
            this.handle.touch();
        }
        resp = new Opt.Single<>(this.take.act(req));
    } else {
        resp = new Opt.Empty<>();
    }
    return resp;
}
 
Example 5
Source File: FbStatus.java    From takes with MIT License 6 votes vote down vote up
/**
 * Ctor.
 * @param check Check
 * @param fallback Fallback
 */
@SuppressWarnings
    (
        {
            "PMD.CallSuperInConstructor",
            "PMD.ConstructorOnlyInitializesOrCallOtherConstructors"
        }
    )
public FbStatus(final Iterable<Integer> check,
    final Scalar<Fallback> fallback) {
    super(
        new Fallback() {
            @Override
            public Opt<Response> route(final RqFallback req)
                throws Exception {
                Opt<Response> rsp = new Opt.Empty<>();
                if (new ListOf<>(check).contains(req.code())) {
                    rsp = fallback.get().route(req);
                }
                return rsp;
            }
        }
    );
}
 
Example 6
Source File: FkParams.java    From takes with MIT License 5 votes vote down vote up
@Override
public Opt<Response> route(final Request req) throws Exception {
    final Iterator<String> params = new RqHref.Base(req).href()
        .param(this.name).iterator();
    final Opt<Response> resp;
    if (params.hasNext()
        && this.pattern.matcher(params.next()).matches()) {
        resp = new Opt.Single<>(this.take.act(req));
    } else {
        resp = new Opt.Empty<>();
    }
    return resp;
}
 
Example 7
Source File: FkAnonymous.java    From takes with MIT License 5 votes vote down vote up
@Override
public Opt<Response> route(final Request req) throws Exception {
    final Identity identity = new RqAuth(req).identity();
    final Opt<Response> resp;
    if (identity.equals(Identity.ANONYMOUS)) {
        resp = new Opt.Single<>(this.take.get().act(req));
    } else {
        resp = new Opt.Empty<>();
    }
    return resp;
}
 
Example 8
Source File: FkEncoding.java    From takes with MIT License 5 votes vote down vote up
@Override
public Opt<Response> route(final Request req) throws IOException {
    final Iterator<String> headers =
        new RqHeaders.Base(req).header("Accept-Encoding").iterator();
    final Opt<Response> resp;
    if (this.encoding.isEmpty()) {
        resp = new Opt.Single<>(this.origin);
    } else if (headers.hasNext()) {
        final Collection<String> items = Arrays.asList(
            FkEncoding.ENCODING_SEP.split(
                new UncheckedText(
                    new Lowered(
                        new Trimmed(
                            new TextOf(headers.next())
                        )
                    )
                ).asString()
            )
        );
        if (items.contains(this.encoding)) {
            resp = new Opt.Single<>(this.origin);
        } else {
            resp = new Opt.Empty<>();
        }
    } else {
        resp = new Opt.Empty<>();
    }
    return resp;
}
 
Example 9
Source File: FkMethods.java    From takes with MIT License 5 votes vote down vote up
@Override
public Opt<Response> route(final Request req) throws Exception {
    final String mtd = new RqMethod.Base(req).method();
    final Opt<Response> resp;
    if (this.methods.contains(mtd)) {
        resp = new Opt.Single<>(this.take.act(req));
    } else {
        resp = new Opt.Empty<>();
    }
    return resp;
}
 
Example 10
Source File: TkAppFallback.java    From jare with MIT License 5 votes vote down vote up
/**
 * Authenticated.
 * @param take Takes
 * @return Authenticated takes
 */
private static Take make(final Take take) {
    return new TkFallback(
        take,
        new FbChain(
            new FbStatus(
                HttpURLConnection.HTTP_NOT_FOUND,
                new RsWithStatus(
                    new RsText("Page not found"),
                    HttpURLConnection.HTTP_NOT_FOUND
                )
            ),
            new FbStatus(
                HttpURLConnection.HTTP_BAD_REQUEST,
                new RsWithStatus(
                    new RsText("Bad request"),
                    HttpURLConnection.HTTP_BAD_REQUEST
                )
            ),
            req -> {
                Sentry.capture(req.throwable());
                return new Opt.Empty<>();
            },
            req -> new Opt.Single<>(TkAppFallback.fatal(req))
        )
    );
}
 
Example 11
Source File: FbEmpty.java    From takes with MIT License 5 votes vote down vote up
/**
 * Ctor.
 */
public FbEmpty() {
    super(
        new Fallback() {
            @Override
            public Opt<Response> route(final RqFallback req) {
                return new Opt.Empty<>();
            }
        }
    );
}
 
Example 12
Source File: RqLive.java    From takes with MIT License 5 votes vote down vote up
/**
 * Parse input stream.
 * @param input Input stream
 * @return Request
 * @throws IOException If fails
 * @checkstyle ExecutableStatementCountCheck (100 lines)
 */
@SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops")
private static Request parse(final InputStream input) throws IOException {
    boolean eof = true;
    final Collection<String> head = new LinkedList<>();
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    Opt<Integer> data = new Opt.Empty<>();
    data = RqLive.data(input, data, false);
    while (data.get() > 0) {
        eof = false;
        if (data.get() == '\r') {
            RqLive.checkLineFeed(input, baos, head.size() + 1);
            if (baos.size() == 0) {
                break;
            }
            data = new Opt.Single<>(input.read());
            final Opt<String> header = RqLive.newHeader(data, baos);
            if (header.has()) {
                head.add(header.get());
            }
            data = RqLive.data(input, data, false);
            continue;
        }
        baos.write(RqLive.legalCharacter(data, baos, head.size() + 1));
        data = RqLive.data(input, new Opt.Empty<>(), true);
    }
    if (eof) {
        throw new IOException("empty request");
    }
    return new RequestOf(head, input);
}
 
Example 13
Source File: FkTypes.java    From takes with MIT License 5 votes vote down vote up
@Override
public Opt<Response> route(final Request req) throws Exception {
    final Opt<Response> resp;
    if (FkTypes.accepted(req).contains(this.types)) {
        if (this.response.has()) {
            resp = new Opt.Single<>(this.response.get());
        } else {
            resp = new Opt.Single<>(this.take.get().act(req));
        }
    } else {
        resp = new Opt.Empty<>();
    }
    return resp;
}
 
Example 14
Source File: FbLog4j.java    From takes with MIT License 5 votes vote down vote up
/**
 * Ctor.
 */
public FbLog4j() {
    super(
        new Fallback() {
            @Override
            public Opt<Response> route(final RqFallback req)
                throws IOException {
                FbLog4j.log(req);
                return new Opt.Empty<>();
            }
        }
    );
}
 
Example 15
Source File: PsChain.java    From takes with MIT License 5 votes vote down vote up
@Override
public Opt<Identity> enter(final Request req) throws Exception {
    Opt<Identity> user = new Opt.Empty<>();
    for (final Pass pass : this.passes) {
        user = pass.enter(req);
        if (user.has()) {
            break;
        }
    }
    return user;
}
 
Example 16
Source File: PsAll.java    From takes with MIT License 5 votes vote down vote up
@Override
public Opt<Identity> enter(final Request request) throws Exception {
    final Opt<Identity> result;
    if (this.allMatch(request)) {
        result = this.all.get(this.index).enter(request);
    } else {
        result = new Opt.Empty<>();
    }
    return result;
}
 
Example 17
Source File: RsWithType.java    From takes with MIT License 2 votes vote down vote up
/**
 * Constructs a {@code Text} that will add text/plain as the content
 * type header to the response.
 * @param res Original response
 */
public Text(final Response res) {
    this(res, new Opt.Empty<>());
}
 
Example 18
Source File: RsWithType.java    From takes with MIT License 2 votes vote down vote up
/**
 * Constructs a {@code HTML} that will add text/html as the content type
 * header to the response.
 * @param res Original response
 */
public Html(final Response res) {
    this(res, new Opt.Empty<>());
}
 
Example 19
Source File: RsWithType.java    From takes with MIT License 2 votes vote down vote up
/**
 * Constructs a {@code JSON} that will add application/json as the
 * content type header to the response.
 * @param res Original response
 */
public Json(final Response res) {
    this(res, new Opt.Empty<>());
}
 
Example 20
Source File: FkTypes.java    From takes with MIT License 2 votes vote down vote up
/**
 * Ctor.
 * @param list List of types
 * @param resp Response to return
 */
public FkTypes(final String list, final Response resp) {
    this(list, new Opt.Single<>(resp), new Opt.Empty<>());
}