001package com.nimbusds.oauth2.sdk.client;
002
003
004import java.net.MalformedURLException;
005import java.net.URI;
006import java.net.URISyntaxException;
007import java.net.URL;
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 Management Protocol
034 *         (draft-ietf-oauth-dyn-reg-management-02), section 2.2.
035 *     <li>OAuth 2.0 Dynamic Client Registration Protocol
036 *         (draft-ietf-oauth-dyn-reg-18), section 2.
037 * </ul>
038 */
039@Immutable
040public class ClientReadRequest extends ProtectedResourceRequest {
041
042
043        /**
044         * Creates a new client read request.
045         *
046         * @param uri         The URI of the client configuration endpoint. May 
047         *                    be {@code null} if the {@link #toHTTPRequest()}
048         *                    method will not be used.
049         * @param accessToken An OAuth 2.0 Bearer access token for the request. 
050         *                    Must not be {@code null}.
051         */
052        public ClientReadRequest(final URI uri, final BearerAccessToken accessToken) {
053
054                super(uri, accessToken);
055
056                if (accessToken == null)
057                        throw new IllegalArgumentException("The access token must not be null");
058        }
059
060
061        @Override
062        public HTTPRequest toHTTPRequest() 
063                throws SerializeException {
064                
065                if (getEndpointURI() == null)
066                        throw new SerializeException("The endpoint URI is not specified");
067
068                URL endpointURL;
069
070                try {
071                        endpointURL = getEndpointURI().toURL();
072
073                } catch (MalformedURLException e) {
074
075                        throw new SerializeException(e.getMessage(), e);
076                }
077        
078                HTTPRequest httpRequest = new HTTPRequest(HTTPRequest.Method.GET, endpointURL);
079                httpRequest.setAuthorization(getAccessToken().toAuthorizationHeader());
080                return httpRequest;
081        }
082
083
084        /**
085         * Parses a client read request from the specified HTTP GET request.
086         *
087         * @param httpRequest The HTTP request. Must not be {@code null}.
088         *
089         * @return The client read request.
090         *
091         * @throws ParseException If the HTTP request couldn't be parsed to a 
092         *                        client read request.
093         */
094        public static ClientReadRequest parse(final HTTPRequest httpRequest)
095                throws ParseException {
096
097                httpRequest.ensureMethod(HTTPRequest.Method.GET);
098
099                BearerAccessToken accessToken = BearerAccessToken.parse(httpRequest.getAuthorization());
100
101                URI endpointURI;
102
103                try {
104                        endpointURI = httpRequest.getURL().toURI();
105
106                } catch (URISyntaxException e) {
107
108                        throw new ParseException(e.getMessage(), e);
109                }
110                
111                return new ClientReadRequest(endpointURI, accessToken);
112        }
113}