001package com.nimbusds.oauth2.sdk.client;
002
003
004import java.net.URL;
005
006import com.nimbusds.oauth2.sdk.token.BearerTokenError;
007import org.apache.commons.lang3.StringUtils;
008
009import net.jcip.annotations.Immutable;
010
011import com.nimbusds.oauth2.sdk.ParseException;
012import com.nimbusds.oauth2.sdk.ProtectedResourceRequest;
013import com.nimbusds.oauth2.sdk.SerializeException;
014import com.nimbusds.oauth2.sdk.http.HTTPRequest;
015import com.nimbusds.oauth2.sdk.token.BearerAccessToken;
016
017
018/**
019 * Client read request.
020 *
021 * <p>Example HTTP request:
022 *
023 * <pre>
024 * GET /register/s6BhdRkqt3 HTTP/1.1
025 * Accept: application/json
026 * Host: server.example.com
027 * Authorization: Bearer reg-23410913-abewfq.123483
028 * </pre>
029 *
030 * <p>Related specifications:
031 *
032 * <ul>
033 *     <li>OAuth 2.0 Dynamic Client Registration Protocol 
034 *         (draft-ietf-oauth-dyn-reg-14), section 4.2.
035 * </ul>
036 */
037@Immutable
038public class ClientReadRequest extends ProtectedResourceRequest {
039
040
041        /**
042         * Creates a new client read request.
043         *
044         * @param uri         The URI of the client configuration endpoint. May 
045         *                    be {@code null} if the {@link #toHTTPRequest()}
046         *                    method will not be used.
047         * @param accessToken An OAuth 2.0 Bearer access token for the request. 
048         *                    Must not be {@code null}.
049         */
050        public ClientReadRequest(final URL uri, final BearerAccessToken accessToken) {
051
052                super(uri, accessToken);
053
054                if (accessToken == null)
055                        throw new IllegalArgumentException("The access token must not be null");
056        }
057
058
059        @Override
060        public HTTPRequest toHTTPRequest() 
061                throws SerializeException {
062                
063                if (getEndpointURI() == null)
064                        throw new SerializeException("The endpoint URI is not specified");
065        
066                HTTPRequest httpRequest = new HTTPRequest(HTTPRequest.Method.GET, getEndpointURI());
067                httpRequest.setAuthorization(getAccessToken().toAuthorizationHeader());
068                return httpRequest;
069        }
070
071
072        /**
073         * Parses a client read request from the specified HTTP GET request.
074         *
075         * @param httpRequest The HTTP request. Must not be {@code null}.
076         *
077         * @return The client read request.
078         *
079         * @throws ParseException If the HTTP request couldn't be parsed to a 
080         *                        client read request.
081         */
082        public static ClientReadRequest parse(final HTTPRequest httpRequest)
083                throws ParseException {
084
085                httpRequest.ensureMethod(HTTPRequest.Method.GET);
086
087                BearerAccessToken accessToken = BearerAccessToken.parse(httpRequest.getAuthorization());
088                
089                return new ClientReadRequest(httpRequest.getURL(), accessToken);
090        }
091}