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

The following examples show how to use org.takes.misc.Opt#has() . 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: PsGoogle.java    From takes with MIT License 6 votes vote down vote up
/**
 * Make identity from JSON object.
 * @param json JSON received from Google
 * @return Identity found
 */
private static Identity parse(final JsonObject json) {
    final Map<String, String> props = new HashMap<>(json.size());
    final Opt<JsonObject> image = new Opt.Single<>(
        json.getJsonObject("image")
    );
    if (image.has()) {
        props.put(PsGoogle.PICTURE, image.get().getString("url", "#"));
    } else {
        props.put(PsGoogle.PICTURE, "#");
    }
    props.put(
        PsGoogle.NAME, json.getString(PsGoogle.DISPLAY_NAME, "unknown")
    );
    return new Identity.Simple(
        String.format("urn:google:%s", json.getString("id")), props
    );
}
 
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: RsWithType.java    From takes with MIT License 6 votes vote down vote up
/**
 * Factory allowing to create {@code Response} with the corresponding
 * content type and character set.
 * @param res Original response
 * @param type Content type
 * @param charset The character set to add to the content type header. If
 *  absent no character set will be added to the content type header
 * @return Response
 */
private static Response make(final Response res, final CharSequence type,
    final Opt<Charset> charset) {
    final Response response;
    if (charset.has()) {
        response = new RsWithHeader(
            new RsWithoutHeader(res, RsWithType.HEADER),
            RsWithType.HEADER,
            String.format(
                "%s; %s=%s",
                type,
                RsWithType.CHARSET,
                charset.get().name()
            )
        );
    } else {
        response = new RsWithHeader(
            new RsWithoutHeader(res, RsWithType.HEADER),
            RsWithType.HEADER,
            type
        );
    }
    return response;
}
 
Example 5
Source File: FkChain.java    From takes with MIT License 5 votes vote down vote up
@Override
public Opt<Response> route(final Request request) throws Exception {
    Opt<Response> response = new Opt.Empty<>();
    for (final Fork fork : this.forks) {
        final Opt<Response> current = fork.route(request);
        if (current.has()) {
            response = current;
            break;
        }
    }
    return response;
}
 
Example 6
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 7
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 8
Source File: TkAuth.java    From takes with MIT License 5 votes vote down vote up
@Override
public Response act(final Request request) throws Exception {
    final Opt<Identity> user = this.pass.enter(request);
    final Response response;
    if (user.has()) {
        response = this.act(request, user.get());
    } else {
        response = this.origin.act(request);
    }
    return response;
}
 
Example 9
Source File: FbChain.java    From takes with MIT License 5 votes vote down vote up
/**
 * Ctor.
 * @param fallbacks Fallbacks
 */
@SuppressWarnings
    (
        {
            "PMD.CallSuperInConstructor",
            "PMD.ConstructorOnlyInitializesOrCallOtherConstructors"
        }
    )
public FbChain(final Iterable<Fallback> fallbacks) {
    super(
        new Fallback() {
            @Override
            public Opt<Response> route(final RqFallback req)
                throws Exception {
                Opt<Response> rsp = new Opt.Empty<>();
                for (final Fallback fbk : fallbacks) {
                    final Opt<Response> opt = fbk.route(req);
                    if (opt.has()) {
                        rsp = opt;
                        break;
                    }
                }
                return rsp;
            }
        }
    );
}
 
Example 10
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 11
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;
}