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