001/*
002 * oauth2-oidc-sdk
003 *
004 * Copyright 2012-2020, 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.openid.connect.sdk.federation.api;
019
020
021import net.jcip.annotations.Immutable;
022
023import com.nimbusds.oauth2.sdk.ParseException;
024import com.nimbusds.oauth2.sdk.http.HTTPResponse;
025import com.nimbusds.openid.connect.sdk.federation.entities.EntityStatement;
026
027
028/**
029 * Fetch entity statement success response.
030 *
031 * <p>Related specifications:
032 *
033 * <ul>
034 *     <li>OpenID Connect Federation 1.0, section 7.1.2.
035 * </ul>
036 */
037@Immutable
038public class FetchEntityStatementSuccessResponse extends FetchEntityStatementResponse {
039        
040        
041        /**
042         * The entity statement.
043         */
044        private final EntityStatement entityStatement;
045        
046        
047        /**
048         * Creates a new fetch entity statement success response.
049         *
050         * @param entityStatement The entity statement. Must not be
051         *                        {@code null}.
052         */
053        public FetchEntityStatementSuccessResponse(final EntityStatement entityStatement) {
054                if (entityStatement == null) {
055                        throw new IllegalArgumentException("The federation entity statement must not be null");
056                }
057                this.entityStatement = entityStatement;
058        }
059        
060        
061        /**
062         * Returns the entity statement. No signature or expiration validation
063         * is performed.
064         *
065         * @return The entity statement.
066         */
067        public EntityStatement getEntityStatement() {
068                
069                return entityStatement;
070        }
071        
072        
073        @Override
074        public boolean indicatesSuccess() {
075                return true;
076        }
077        
078        
079        @Override
080        public HTTPResponse toHTTPResponse() {
081                HTTPResponse httpResponse = new HTTPResponse(HTTPResponse.SC_OK);
082                httpResponse.setEntityContentType(EntityStatement.CONTENT_TYPE);
083                httpResponse.setContent(getEntityStatement().getSignedStatement().serialize());
084                return httpResponse;
085        }
086        
087        
088        /**
089         * Parses a fetch entity statement success response from the specified
090         * HTTP response.
091         *
092         * @param httpResponse The HTTP response. Must not be {@code null}.
093         *
094         * @return The fetch entity statement success response.
095         *
096         * @throws ParseException If parsing failed.
097         */
098        public static FetchEntityStatementSuccessResponse parse(final HTTPResponse httpResponse)
099                throws ParseException {
100                
101                httpResponse.ensureStatusCode(HTTPResponse.SC_OK);
102                httpResponse.ensureEntityContentType(EntityStatement.CONTENT_TYPE);
103                return new FetchEntityStatementSuccessResponse(EntityStatement.parse(httpResponse.getContent()));
104        }
105}