Java Code Examples for org.takes.misc.Opt#get()

The following examples show how to use org.takes.misc.Opt#get() . 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: RsFork.java    From takes with MIT License 6 votes vote down vote up
/**
 * Pick the right one.
 * @param req Request
 * @param forks List of forks
 * @return Response
 * @throws IOException If fails
 */
@SuppressWarnings("PMD.AvoidCatchingGenericException")
private static Response pick(final Request req,
    final Iterable<Fork> forks) throws IOException {
    for (final Fork fork : forks) {
        try {
            final Opt<Response> rsps = fork.route(req);
            if (rsps.has()) {
                return rsps.get();
            }
            //@checkstyle IllegalCatch (1 line)
        } catch (final Exception ex) {
            throw new IOException(ex);
        }
    }
    throw new HttpException(HttpURLConnection.HTTP_NOT_FOUND);
}
 
Example 2
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 3
Source File: RqLive.java    From takes with MIT License 6 votes vote down vote up
/**
 * Returns a legal character based n the read character.
 * @param data Character read
 * @param baos Byte stream containing read header
 * @param position Header line number
 * @return A legal character
 * @throws IOException if character is illegal
 */
private static Integer legalCharacter(final Opt<Integer> data,
    final ByteArrayOutputStream baos, final Integer position)
    throws IOException {
    // @checkstyle MagicNumber (1 line)
    if ((data.get() > 0x7f || data.get() < 0x20)
        && data.get() != '\t') {
        throw new HttpException(
            HttpURLConnection.HTTP_BAD_REQUEST,
            String.format(
                "illegal character 0x%02X in HTTP header line #%d: \"%s\"",
                data.get(),
                position,
                new TextOf(baos.toByteArray()).asString()
            )
        );
    }
    return data.get();
}
 
Example 4
Source File: TkFork.java    From takes with MIT License 5 votes vote down vote up
@Override
public Response act(final Request request) throws Exception {
    final Opt<Response> response = new FkChain(this.forks).route(request);
    if (response.has()) {
        return response.get();
    }
    throw new HttpException(HttpURLConnection.HTTP_NOT_FOUND);
}
 
Example 5
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);
}