001package com.nimbusds.oauth2.sdk.client;
002
003
004import java.net.MalformedURLException;
005import java.net.URI;
006import java.net.URISyntaxException;
007import java.net.URL;
008
009import net.jcip.annotations.Immutable;
010
011import com.nimbusds.oauth2.sdk.ParseException;
012import com.nimbusds.oauth2.sdk.ProtectedResourceRequest;
013import com.nimbusds.oauth2.sdk.SerializeException;
014import com.nimbusds.oauth2.sdk.http.HTTPRequest;
015import com.nimbusds.oauth2.sdk.token.BearerAccessToken;
016
017
018/**
019 * Client delete request.
020 *
021 * <p>Example HTTP request:
022 *
023 * <pre>
024 * DELETE /register/s6BhdRkqt3 HTTP/1.1
025 * Accept: application/json
026 * Host: server.example.com
027 * Authorization: Bearer reg-23410913-abewfq.123483
028 * </pre>
029 *
030 * <p>Related specifications:
031 *
032 * <ul>
033 *     <li>OAuth 2.0 Dynamic Client Registration Management Protocol (RFC
034 *         7592), section 2.3.
035 * </ul>
036 */
037@Immutable
038public class ClientDeleteRequest extends ProtectedResourceRequest {
039
040
041        /**
042         * Creates a new client delete request.
043         *
044         * @param uri         The URI of the client configuration endpoint. May 
045         *                    be {@code null} if the {@link #toHTTPRequest()}
046         *                    method will not be used.
047         * @param accessToken An OAuth 2.0 Bearer access token for the request, 
048         *                    {@code null} if none.
049         */
050        public ClientDeleteRequest(final URI uri, final BearerAccessToken accessToken) {
051
052                super(uri, accessToken);
053                
054                if (accessToken == null)
055                        throw new IllegalArgumentException("The access token must not be null");
056        }
057
058
059        @Override
060        public HTTPRequest toHTTPRequest() {
061                
062                if (getEndpointURI() == null)
063                        throw new SerializeException("The endpoint URI is not specified");
064
065                URL endpointURL;
066
067                try {
068                        endpointURL = getEndpointURI().toURL();
069
070                } catch (MalformedURLException e) {
071
072                        throw new SerializeException(e.getMessage(), e);
073                }
074        
075                HTTPRequest httpRequest = new HTTPRequest(HTTPRequest.Method.DELETE, endpointURL);
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                
097                BearerAccessToken accessToken = BearerAccessToken.parse(httpRequest.getAuthorization());
098
099                URI endpointURI;
100
101                try {
102                        endpointURI = httpRequest.getURL().toURI();
103
104                } catch (URISyntaxException e) {
105
106                        throw new ParseException(e.getMessage(), e);
107                }
108                
109                return new ClientDeleteRequest(endpointURI, accessToken);
110        }
111}