001/*
002 * oauth2-oidc-sdk
003 *
004 * Copyright 2012-2016, Connect2id Ltd and contributors.
005 *
006 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use
007 * this file except in compliance with the License. You may obtain a copy of the
008 * License at
009 *
010 *    http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software distributed
013 * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
014 * CONDITIONS OF ANY KIND, either express or implied. See the License for the
015 * specific language governing permissions and limitations under the License.
016 */
017
018package com.nimbusds.oauth2.sdk.client;
019
020
021import java.net.URI;
022
023import net.jcip.annotations.Immutable;
024
025import com.nimbusds.oauth2.sdk.ParseException;
026import com.nimbusds.oauth2.sdk.ProtectedResourceRequest;
027import com.nimbusds.oauth2.sdk.SerializeException;
028import com.nimbusds.oauth2.sdk.http.HTTPRequest;
029import com.nimbusds.oauth2.sdk.token.BearerAccessToken;
030
031
032/**
033 * Client read request.
034 *
035 * <p>Example HTTP request:
036 *
037 * <pre>
038 * GET /register/s6BhdRkqt3 HTTP/1.1
039 * Accept: application/json
040 * Host: server.example.com
041 * Authorization: Bearer reg-23410913-abewfq.123483
042 * </pre>
043 *
044 * <p>Related specifications:
045 *
046 * <ul>
047 *     <li>OAuth 2.0 Dynamic Client Registration Management Protocol (RFC
048 *         7592), section 2.1.
049 *     <li>OAuth 2.0 Dynamic Client Registration Protocol (RFC 7591), section
050 *         2.
051 * </ul>
052 */
053@Immutable
054public class ClientReadRequest extends ProtectedResourceRequest {
055
056
057        /**
058         * Creates a new client read request.
059         *
060         * @param uri         The URI of the client configuration endpoint. May 
061         *                    be {@code null} if the {@link #toHTTPRequest()}
062         *                    method will not be used.
063         * @param accessToken An OAuth 2.0 Bearer access token for the request. 
064         *                    Must not be {@code null}.
065         */
066        public ClientReadRequest(final URI uri, final BearerAccessToken accessToken) {
067
068                super(uri, accessToken);
069
070                if (accessToken == null)
071                        throw new IllegalArgumentException("The access token must not be null");
072        }
073
074
075        @Override
076        public HTTPRequest toHTTPRequest() {
077                
078                if (getEndpointURI() == null)
079                        throw new SerializeException("The endpoint URI is not specified");
080
081                HTTPRequest httpRequest = new HTTPRequest(HTTPRequest.Method.GET, getEndpointURI());
082                httpRequest.setAuthorization(getAccessToken().toAuthorizationHeader());
083                return httpRequest;
084        }
085
086
087        /**
088         * Parses a client read request from the specified HTTP GET request.
089         *
090         * @param httpRequest The HTTP request. Must not be {@code null}.
091         *
092         * @return The client read request.
093         *
094         * @throws ParseException If the HTTP request couldn't be parsed to a 
095         *                        client read request.
096         */
097        public static ClientReadRequest parse(final HTTPRequest httpRequest)
098                throws ParseException {
099
100                httpRequest.ensureMethod(HTTPRequest.Method.GET);
101
102                return new ClientReadRequest(
103                        httpRequest.getURI(),
104                        BearerAccessToken.parse(httpRequest.getAuthorization())
105                );
106        }
107}