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