001    package com.nimbusds.oauth2.sdk.client;
002    
003    
004    import java.net.URL;
005    
006    import net.jcip.annotations.Immutable;
007    
008    import com.nimbusds.oauth2.sdk.ParseException;
009    import com.nimbusds.oauth2.sdk.ProtectedResourceRequest;
010    import com.nimbusds.oauth2.sdk.SerializeException;
011    import com.nimbusds.oauth2.sdk.http.HTTPRequest;
012    import com.nimbusds.oauth2.sdk.token.BearerAccessToken;
013    
014    
015    /**
016     * Client delete request. This class is immutable.
017     *
018     * <p>Example HTTP request:
019     *
020     * <pre>
021     * DELETE /register/s6BhdRkqt3 HTTP/1.1
022     * Accept: application/json
023     * Host: server.example.com
024     * Authorization: Bearer reg-23410913-abewfq.123483
025     * </pre>
026     *
027     * <p>Related specifications:
028     *
029     * <ul>
030     *     <li>OAuth 2.0 Dynamic Client Registration Protocol 
031     *         (draft-ietf-oauth-dyn-reg-12), section 4.4.
032     * </ul>
033     *
034     * @author Vladimir Dzhuvinov
035     */
036    @Immutable
037    public class ClientDeleteRequest extends ProtectedResourceRequest {
038    
039    
040            /**
041             * Creates a new client delete request.
042             *
043             * @param uri         The URI of the client configuration endpoint. May 
044             *                    be {@code null} if the {@link #toHTTPRequest()}
045             *                    method will not be used.
046             * @param accessToken An OAuth 2.0 Bearer access token for the request, 
047             *                    {@code null} if none.
048             */
049            public ClientDeleteRequest(final URL uri, final BearerAccessToken accessToken) {
050    
051                    super(uri, accessToken);
052                    
053                    if (accessToken == null)
054                            throw new IllegalArgumentException("The access token must not be null");
055            }
056    
057    
058            @Override
059            public HTTPRequest toHTTPRequest() 
060                    throws SerializeException {
061                    
062                    if (getURI() == null)
063                            throw new SerializeException("The endpoint URI is not specified");
064            
065                    HTTPRequest httpRequest = new HTTPRequest(HTTPRequest.Method.DELETE, getURI());
066                    httpRequest.setAuthorization(getAccessToken().toAuthorizationHeader());
067                    return httpRequest;
068            }
069    
070    
071            /**
072             * Parses a client delete request from the specified HTTP DELETE 
073             * request.
074             *
075             * @param httpRequest The HTTP request. Must not be {@code null}.
076             *
077             * @return The client add (register) request.
078             *
079             * @throws ParseException If the HTTP request couldn't be parsed to a 
080             *                        client delete request.
081             */
082            public static ClientDeleteRequest parse(final HTTPRequest httpRequest)
083                    throws ParseException {
084    
085                    httpRequest.ensureMethod(HTTPRequest.Method.DELETE);
086                    
087                    // Parse the bearer access token
088                    String authzHeaderValue = httpRequest.getAuthorization();
089                    
090                    BearerAccessToken accessToken = BearerAccessToken.parse(authzHeaderValue);
091                    
092                    return new ClientDeleteRequest(httpRequest.getURL(), accessToken);
093            }
094    }