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;
022import java.net.URISyntaxException;
023
024import net.jcip.annotations.Immutable;
025
026import com.nimbusds.oauth2.sdk.ParseException;
027import com.nimbusds.oauth2.sdk.ProtectedResourceRequest;
028import com.nimbusds.oauth2.sdk.SerializeException;
029import com.nimbusds.oauth2.sdk.http.HTTPRequest;
030import com.nimbusds.oauth2.sdk.token.BearerAccessToken;
031
032
033/**
034 * Client read request.
035 *
036 * <p>Example HTTP request:
037 *
038 * <pre>
039 * GET /register/s6BhdRkqt3 HTTP/1.1
040 * Accept: application/json
041 * Host: server.example.com
042 * Authorization: Bearer reg-23410913-abewfq.123483
043 * </pre>
044 *
045 * <p>Related specifications:
046 *
047 * <ul>
048 *     <li>OAuth 2.0 Dynamic Client Registration Management Protocol (RFC
049 *         7592), section 2.1.
050 *     <li>OAuth 2.0 Dynamic Client Registration Protocol (RFC 7591), section
051 *         2.
052 * </ul>
053 */
054@Immutable
055public class ClientReadRequest extends ProtectedResourceRequest {
056
057
058        /**
059         * Creates a new client read request.
060         *
061         * @param uri         The URI of the client configuration endpoint. May 
062         *                    be {@code null} if the {@link #toHTTPRequest()}
063         *                    method will not be used.
064         * @param accessToken An OAuth 2.0 Bearer access token for the request. 
065         *                    Must not be {@code null}.
066         */
067        public ClientReadRequest(final URI uri, final BearerAccessToken accessToken) {
068
069                super(uri, accessToken);
070
071                if (accessToken == null)
072                        throw new IllegalArgumentException("The access token must not be null");
073        }
074
075
076        @Override
077        public HTTPRequest toHTTPRequest() {
078                
079                if (getEndpointURI() == null)
080                        throw new SerializeException("The endpoint URI is not specified");
081
082                HTTPRequest httpRequest = new HTTPRequest(HTTPRequest.Method.GET, getEndpointURI());
083                httpRequest.setAuthorization(getAccessToken().toAuthorizationHeader());
084                return httpRequest;
085        }
086
087
088        /**
089         * Parses a client read request from the specified HTTP GET request.
090         *
091         * @param httpRequest The HTTP request. Must not be {@code null}.
092         *
093         * @return The client read request.
094         *
095         * @throws ParseException If the HTTP request couldn't be parsed to a 
096         *                        client read request.
097         */
098        public static ClientReadRequest parse(final HTTPRequest httpRequest)
099                throws ParseException {
100
101                httpRequest.ensureMethod(HTTPRequest.Method.GET);
102
103                BearerAccessToken accessToken = BearerAccessToken.parse(httpRequest.getAuthorization());
104
105                URI endpointURI;
106
107                try {
108                        endpointURI = httpRequest.getURL().toURI();
109
110                } catch (URISyntaxException e) {
111
112                        throw new ParseException(e.getMessage(), e);
113                }
114                
115                return new ClientReadRequest(endpointURI, accessToken);
116        }
117}