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