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