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 delete request.
034 *
035 * <p>Example HTTP request:
036 *
037 * <pre>
038 * DELETE /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.3.
049 * </ul>
050 */
051@Immutable
052public class ClientDeleteRequest extends ProtectedResourceRequest {
053
054
055        /**
056         * Creates a new client delete request.
057         *
058         * @param uri         The URI of the client configuration endpoint. May 
059         *                    be {@code null} if the {@link #toHTTPRequest()}
060         *                    method will not be used.
061         * @param accessToken An OAuth 2.0 Bearer access token for the request, 
062         *                    {@code null} if none.
063         */
064        public ClientDeleteRequest(final URI uri, final BearerAccessToken accessToken) {
065
066                super(uri, accessToken);
067                
068                if (accessToken == null)
069                        throw new IllegalArgumentException("The access token must not be null");
070        }
071
072
073        @Override
074        public HTTPRequest toHTTPRequest() {
075                
076                if (getEndpointURI() == null)
077                        throw new SerializeException("The endpoint URI is not specified");
078
079                HTTPRequest httpRequest = new HTTPRequest(HTTPRequest.Method.DELETE, getEndpointURI());
080                httpRequest.setAuthorization(getAccessToken().toAuthorizationHeader());
081                return httpRequest;
082        }
083
084
085        /**
086         * Parses a client delete request from the specified HTTP DELETE 
087         * request.
088         *
089         * @param httpRequest The HTTP request. Must not be {@code null}.
090         *
091         * @return The client add (register) request.
092         *
093         * @throws ParseException If the HTTP request couldn't be parsed to a 
094         *                        client delete request.
095         */
096        public static ClientDeleteRequest parse(final HTTPRequest httpRequest)
097                throws ParseException {
098
099                httpRequest.ensureMethod(HTTPRequest.Method.DELETE);
100                return new ClientDeleteRequest(
101                        httpRequest.getURI(),
102                        BearerAccessToken.parse(httpRequest.getAuthorization())
103                );
104        }
105}