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