001package com.nimbusds.oauth2.sdk.client;
002
003
004import java.net.URL;
005
006import net.jcip.annotations.Immutable;
007
008import com.nimbusds.oauth2.sdk.ParseException;
009import com.nimbusds.oauth2.sdk.ProtectedResourceRequest;
010import com.nimbusds.oauth2.sdk.SerializeException;
011import com.nimbusds.oauth2.sdk.http.HTTPRequest;
012import com.nimbusds.oauth2.sdk.token.BearerAccessToken;
013
014
015/**
016 * Client delete request.
017 *
018 * <p>Example HTTP request:
019 *
020 * <pre>
021 * DELETE /register/s6BhdRkqt3 HTTP/1.1
022 * Accept: application/json
023 * Host: server.example.com
024 * Authorization: Bearer reg-23410913-abewfq.123483
025 * </pre>
026 *
027 * <p>Related specifications:
028 *
029 * <ul>
030 *     <li>OAuth 2.0 Dynamic Client Registration Protocol 
031 *         (draft-ietf-oauth-dyn-reg-14), section 4.4.
032 * </ul>
033 */
034@Immutable
035public class ClientDeleteRequest extends ProtectedResourceRequest {
036
037
038        /**
039         * Creates a new client delete request.
040         *
041         * @param uri         The URI of the client configuration endpoint. May 
042         *                    be {@code null} if the {@link #toHTTPRequest()}
043         *                    method will not be used.
044         * @param accessToken An OAuth 2.0 Bearer access token for the request, 
045         *                    {@code null} if none.
046         */
047        public ClientDeleteRequest(final URL uri, final BearerAccessToken accessToken) {
048
049                super(uri, accessToken);
050                
051                if (accessToken == null)
052                        throw new IllegalArgumentException("The access token must not be null");
053        }
054
055
056        @Override
057        public HTTPRequest toHTTPRequest() 
058                throws SerializeException {
059                
060                if (getEndpointURI() == null)
061                        throw new SerializeException("The endpoint URI is not specified");
062        
063                HTTPRequest httpRequest = new HTTPRequest(HTTPRequest.Method.DELETE, getEndpointURI());
064                httpRequest.setAuthorization(getAccessToken().toAuthorizationHeader());
065                return httpRequest;
066        }
067
068
069        /**
070         * Parses a client delete request from the specified HTTP DELETE 
071         * request.
072         *
073         * @param httpRequest The HTTP request. Must not be {@code null}.
074         *
075         * @return The client add (register) request.
076         *
077         * @throws ParseException If the HTTP request couldn't be parsed to a 
078         *                        client delete request.
079         */
080        public static ClientDeleteRequest parse(final HTTPRequest httpRequest)
081                throws ParseException {
082
083                httpRequest.ensureMethod(HTTPRequest.Method.DELETE);
084                
085                BearerAccessToken accessToken = BearerAccessToken.parse(httpRequest.getAuthorization());
086                
087                return new ClientDeleteRequest(httpRequest.getURL(), accessToken);
088        }
089}