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;
019
020
021import net.jcip.annotations.Immutable;
022
023import com.nimbusds.oauth2.sdk.http.HTTPResponse;
024
025
026/**
027 * Request object POST error response.
028 *
029 * <p>Example request object POST error response indicating an invalid JWS
030 * signature:
031 *
032 * <pre>
033 * HTTP/1.1 401 Unauthorized
034 * Date: Tue, 2 May 2017 15:22:31 GMT
035 * </pre>
036 *
037 * <p>Related specifications:
038 *
039 * <ul>
040 *     <li>Financial-grade API - Part 2: Read and Write API Security Profile,
041 *         section 7.
042 *     <li>The OAuth 2.0 Authorization Framework: JWT Secured Authorization
043 *         Request (JAR) (draft-ietf-oauth-jwsreq-17).
044 * </ul>
045 */
046@Immutable
047public final class RequestObjectPOSTErrorResponse extends RequestObjectPOSTResponse implements ErrorResponse {
048        
049        
050        /**
051         * Holds the HTTP status code.
052         */
053        private final ErrorObject errorObject;
054        
055        
056        /**
057         * Creates a new request object POST error response.
058         *
059         * @param httpStatusCode The HTTP status code. Should be other than
060         *                       2xx.
061         */
062        public RequestObjectPOSTErrorResponse(final int httpStatusCode) {
063                errorObject = new ErrorObject(null, null, httpStatusCode);
064        }
065        
066        
067        public int getHTTPStatusCode() {
068                return errorObject.getHTTPStatusCode();
069        }
070        
071        
072        @Override
073        public ErrorObject getErrorObject() {
074                return errorObject;
075        }
076        
077        
078        @Override
079        public boolean indicatesSuccess() {
080                return false;
081        }
082        
083        
084        @Override
085        public HTTPResponse toHTTPResponse() {
086                return new HTTPResponse(getHTTPStatusCode());
087        }
088        
089        
090        /**
091         * Parses a request object POST error response from the specified
092         * HTTP response.
093         *
094         * @param httpResponse The HTTP response. Must not be {@code null}.
095         *
096         * @return The request object POST error response.
097         *
098         * @throws ParseException If the HTTP response couldn't be parsed to a
099         *                        request object POST error response.
100         */
101        public static RequestObjectPOSTErrorResponse parse(final HTTPResponse httpResponse)
102                throws ParseException {
103                
104                if (httpResponse.getStatusCode() >= 200 && httpResponse.getStatusCode() <= 299) {
105                        throw new ParseException("Unexpected HTTP status code, must not be 2xx");
106                }
107                
108                return new RequestObjectPOSTErrorResponse(httpResponse.getStatusCode());
109        }
110}