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