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