001package com.nimbusds.openid.connect.sdk;
002
003
004import net.jcip.annotations.Immutable;
005
006import org.apache.commons.lang3.StringUtils;
007
008import com.nimbusds.oauth2.sdk.id.Identifier;
009
010
011/**
012 * OAuth 2.0 response mode.
013 *
014 * <p>Related specifications:
015 *
016 * <ul>
017 *     <li>OAuth 2.0 Multiple Response Type Encoding Practices, section 2.1.
018 * </ul>
019 */
020@Immutable
021public final class ResponseMode extends Identifier {
022
023
024        /**
025         * Query response mode. In this mode, response parameters are encoded
026         * in the query string added to the {@code redirect_uri} when
027         * redirecting back to the client.
028         */
029        public static final ResponseMode QUERY = new ResponseMode("query");
030
031
032        /**
033         * Fragment response mode. In this mode, response parameters are
034         * encoded in the fragment added to the {@code redirect_uri} when
035         * redirecting back to the client.
036         */
037        public static final ResponseMode FRAGMENT = new ResponseMode("fragment");
038
039
040        /**
041         * HTML form post response mode. In this mode, response parameters are
042         * encoded as HTML form values that are auto-submitted in the
043         * user-agent, and thus are transmitted via the HTTP POST method to the
044         * client, with the result parameters being encoded in the response
045         * body using the {@code application/x-www-form-urlencoded} format.
046         * The action attribute of the form must be the client's Redirection
047         * URI. The method of the form attribute must be POST.
048         */
049        public static final ResponseMode FORM_POST = new ResponseMode("form_post");
050
051
052        /**
053         * Creates a new response mode with the specified value.
054         *
055         * @param value The response mode value. Must not be {@code null} or
056         *              empty string.
057         */
058        public ResponseMode(final String value) {
059
060                super(value);
061        }
062
063
064        @Override
065        public boolean equals(final Object object) {
066
067                return object instanceof ResponseMode &&
068                        this.toString().equals(object.toString());
069        }
070
071
072        /**
073         * Parses a response mode from the specified string.
074         *
075         * @param s The string to parse, {@code null} or empty if no response
076         *          mode is specified.
077         *
078         * @return The response mode, {@code null} if the parsed string was
079         *         {@code null} or empty.
080         */
081        public static ResponseMode parse(final String s) {
082
083                if (StringUtils.isBlank(s))
084                        return null;
085
086                if (s.equals(ResponseMode.QUERY.getValue())) {
087                        return ResponseMode.QUERY;
088                } else if (s.equals(ResponseMode.FRAGMENT.getValue())) {
089                        return ResponseMode.FRAGMENT;
090                } else if (s.equals(ResponseMode.FORM_POST.getValue())) {
091                        return ResponseMode.FORM_POST;
092                } else {
093                        return new ResponseMode(s);
094                }
095        }
096}