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 com.nimbusds.oauth2.sdk.ParseException;
022import com.nimbusds.oauth2.sdk.ProtectedResourceRequest;
023import com.nimbusds.oauth2.sdk.SerializeException;
024import com.nimbusds.oauth2.sdk.http.HTTPRequest;
025import com.nimbusds.oauth2.sdk.token.BearerAccessToken;
026import net.jcip.annotations.Immutable;
027
028import java.net.URI;
029import java.util.Objects;
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 7592)
048 *     <li>OAuth 2.0 Dynamic Client Registration Protocol (RFC 7591)
049 * </ul>
050 */
051@Immutable
052public class ClientReadRequest extends ProtectedResourceRequest {
053
054
055        /**
056         * Creates a new client read request.
057         *
058         * @param endpoint    The URI of the client configuration endpoint. May
059         *                    be {@code null} if the {@link #toHTTPRequest()}
060         *                    method is not going to be used.
061         * @param accessToken An OAuth 2.0 Bearer access token for the request. 
062         *                    Must not be {@code null}.
063         */
064        public ClientReadRequest(final URI endpoint, final BearerAccessToken accessToken) {
065
066                super(endpoint, Objects.requireNonNull(accessToken));
067        }
068
069
070        @Override
071        public HTTPRequest toHTTPRequest() {
072                
073                if (getEndpointURI() == null)
074                        throw new SerializeException("The endpoint URI is not specified");
075
076                HTTPRequest httpRequest = new HTTPRequest(HTTPRequest.Method.GET, getEndpointURI());
077                httpRequest.setAuthorization(getAccessToken().toAuthorizationHeader());
078                return httpRequest;
079        }
080
081
082        /**
083         * Parses a client read request from the specified HTTP GET request.
084         *
085         * @param httpRequest The HTTP request. Must not be {@code null}.
086         *
087         * @return The client read request.
088         *
089         * @throws ParseException If the HTTP request couldn't be parsed to a 
090         *                        client read request.
091         */
092        public static ClientReadRequest parse(final HTTPRequest httpRequest)
093                throws ParseException {
094
095                httpRequest.ensureMethod(HTTPRequest.Method.GET);
096
097                return new ClientReadRequest(
098                        httpRequest.getURI(),
099                        BearerAccessToken.parse(httpRequest.getAuthorization())
100                );
101        }
102}