001    package com.nimbusds.oauth2.sdk.client;
002    
003    
004    import java.net.URL;
005    
006    import org.apache.commons.lang3.StringUtils;
007    
008    import net.jcip.annotations.Immutable;
009    
010    import com.nimbusds.oauth2.sdk.ParseException;
011    import com.nimbusds.oauth2.sdk.ProtectedResourceRequest;
012    import com.nimbusds.oauth2.sdk.SerializeException;
013    import com.nimbusds.oauth2.sdk.http.HTTPRequest;
014    import com.nimbusds.oauth2.sdk.token.BearerAccessToken;
015    
016    
017    /**
018     * Client read request. This class is immutable.
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-12), section 4.2.
034     * </ul>
035     *
036     * @author Vladimir Dzhuvinov
037     */
038    @Immutable
039    public class ClientReadRequest extends ProtectedResourceRequest {
040    
041    
042            /**
043             * Creates a new client read request.
044             *
045             * @param uri         The URI of the client configuration endpoint. May 
046             *                    be {@code null} if the {@link #toHTTPRequest()}
047             *                    method will not be used.
048             * @param accessToken An OAuth 2.0 Bearer access token for the request. 
049             *                    Must not be {@code null}.
050             */
051            public ClientReadRequest(final URL uri, final BearerAccessToken accessToken) {
052    
053                    super(uri, accessToken);
054    
055                    if (accessToken == null)
056                            throw new IllegalArgumentException("The access token must not be null");
057            }
058    
059    
060            @Override
061            public HTTPRequest toHTTPRequest() 
062                    throws SerializeException {
063                    
064                    if (getURI() == null)
065                            throw new SerializeException("The endpoint URI is not specified");
066            
067                    HTTPRequest httpRequest = new HTTPRequest(HTTPRequest.Method.GET, getURI());
068                    httpRequest.setAuthorization(getAccessToken().toAuthorizationHeader());
069                    return httpRequest;
070            }
071    
072    
073            /**
074             * Parses a client read request from the specified HTTP GET request.
075             *
076             * @param httpRequest The HTTP request. Must not be {@code null}.
077             *
078             * @return The client read request.
079             *
080             * @throws ParseException If the HTTP request couldn't be parsed to a 
081             *                        client read request.
082             */
083            public static ClientReadRequest parse(final HTTPRequest httpRequest)
084                    throws ParseException {
085    
086                    httpRequest.ensureMethod(HTTPRequest.Method.GET);
087    
088                    String authzHeaderValue = httpRequest.getAuthorization();
089                    
090                    if (StringUtils.isBlank(authzHeaderValue))
091                            throw new ParseException("Missing HTTP Authorization header");
092                    
093                    BearerAccessToken accessToken = BearerAccessToken.parse(authzHeaderValue);
094                    
095                    return new ClientReadRequest(httpRequest.getURL(), accessToken);
096            }
097    }