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.openid.connect.sdk.federation.config;
019
020
021import java.nio.charset.StandardCharsets;
022
023import com.nimbusds.common.contenttype.ContentType;
024import com.nimbusds.jwt.SignedJWT;
025import com.nimbusds.oauth2.sdk.ParseException;
026import com.nimbusds.oauth2.sdk.http.HTTPResponse;
027import com.nimbusds.oauth2.sdk.util.StringUtils;
028import com.nimbusds.openid.connect.sdk.federation.entities.EntityStatement;
029
030
031/**
032 * Federation entity configuration success response.
033 *
034 * <p>Related specifications:
035 *
036 * <ul>
037 *     <li>OpenID Connect Federation 1.0, section 6.2.
038 * </ul>
039 */
040public class FederationEntityConfigurationSuccessResponse extends FederationEntityConfigurationResponse {
041        
042        
043        /**
044         * The content type.
045         */
046        private static final ContentType CONTENT_TYPE = EntityStatement.CONTENT_TYPE;
047        
048        
049        /**
050         * The entity statement.
051         */
052        private final EntityStatement entityStatement;
053        
054        
055        /**
056         * Creates a new federation entity configuration success response.
057         *
058         * @param entityStatement The entity statement. Must not be
059         *                        {@code null}.
060         */
061        public FederationEntityConfigurationSuccessResponse(final EntityStatement entityStatement) {
062                
063                if (entityStatement == null) {
064                        throw new IllegalArgumentException("The federation entity statement must not be null");
065                }
066                this.entityStatement = entityStatement;
067        }
068        
069        
070        /**
071         * Returns the entity statement. No signature or expiration validation
072         * is performed.
073         *
074         * @return The entity statement.
075         */
076        public EntityStatement getEntityStatement() {
077                
078                return entityStatement;
079        }
080        
081        
082        @Override
083        public boolean indicatesSuccess() {
084                return true;
085        }
086        
087        
088        @Override
089        public HTTPResponse toHTTPResponse() {
090                
091                HTTPResponse httpResponse = new HTTPResponse(HTTPResponse.SC_OK);
092                httpResponse.setEntityContentType(CONTENT_TYPE);
093                httpResponse.setContent(entityStatement.getSignedStatement().serialize());
094                return httpResponse;
095        }
096        
097        
098        /**
099         * Parses a federation entity configuration success response from the
100         * specified HTTP response.
101         *
102         * @param httpResponse The HTTP response. Must not be {@code null}.
103         *
104         * @return The federation entity configuration success response.
105         *
106         * @throws ParseException If HTTP response couldn't be parsed to a
107         *                        federation entity configuration success
108         *                        response.
109         */
110        public static FederationEntityConfigurationSuccessResponse parse(final HTTPResponse httpResponse)
111                throws ParseException {
112                
113                httpResponse.ensureStatusCode(HTTPResponse.SC_OK);
114                httpResponse.ensureEntityContentType(CONTENT_TYPE);
115                
116                String content = httpResponse.getContent();
117                
118                if (StringUtils.isBlank(content)) {
119                        throw new ParseException("Empty HTTP entity body");
120                }
121                
122                SignedJWT signedJWT;
123                try {
124                        signedJWT = SignedJWT.parse(httpResponse.getContent());
125                } catch (java.text.ParseException e) {
126                        throw new ParseException(e.getMessage(), e);
127                }
128                
129                return new FederationEntityConfigurationSuccessResponse(EntityStatement.parse(signedJWT));
130        }
131}