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>Example HTTP response (with line breaks for clarity):
035 *
036 * <pre>
037 * HTTP/1.1 200 OK
038 * Content-Type: application/jose; charset=UTF-8
039 *
040 * eyJraWQiOiI4OHR3SGhGSFNiSk4xQnJ4cEdBT1A1Tk5RY3JEMFNBcEhiU3pVWjJpMjgwIiwiYWxn
041 * IjoiUlMyNTYifQ.eyJzdWIiOiJodHRwczpcL1wvb3AuYzJpZC5jb20iLCJqd2tzIjp7ImtleXMiO
042 * lt7Imt0eSI6IlJTQSIsImUiOiJBUUFCIiwidXNlIjoic2lnIiwia2lkIjoiODh0d0hoRkhTYkpOM
043 * UJyeHBHQU9QNU5OUWNyRDBTQXBIYlN6VVoyaTI4MCIsIm4iOiJqYl8zeFBJWGhDM2JJRnFuVG8xb
044 * nFDRHlwSzd6djBxNUJvUTZmNC1adXlfRWs2UFc2ZFdwQ1hGQ1R3c016YVRZV0M2VGViQnE2aGQ5T
045 * 1A5ZXVSckl3ZjBxNnBOQ3o2NG9uMGNBbXcxbmJVXzNKc21wNzRxRl9HMV9ySTVrdVZ3Z0l1VHJQT
046 * k40MUV3RlFYMGtMa2UyYTNVaHAyRTBOcHdBa2ZJa1B6ZFozTlNZVVd0TTRWTXA4SzBjN1dwRlpHS
047 * EtYcWpXcnRWX1JQajRsV0dvYWRnRFJxVEg2R0kyTF9ESVRNRHJldlk2YzU4VlhBT1VvOHBjbGk4W
048 * VVnV0J2UURqcEtGRFY5aU1IejFOZ2o0bzdRbGg5NjhFSnZNdUNXUjBKRWZhbEtvb3lQbXZGeUYwd
049 * 1NUd2FseVh6M0xsOEFxY3d4Qm1Qb3JlQzA0RnhMVGV6R2Q5U1EifV19LCJpc3MiOiJodHRwczpcL
050 * 1wvYWJjLWZlZGVyYXRpb24uYzJpZC5jb20iLCJleHAiOjIwMDAsImlhdCI6MTAwMH0.JTLM1NREw
051 * OBqwHJin4LPBnzmGbHyx61wSx-CqUNwsd9u8u_PelVwo44X_GjV-7W2iPUHTrtnBZm7TURdzyrd6
052 * M0s5V5g0GhSrQLe4HtX_X2gZbSxAUosQKwVltnwIw0lUDOAw7jk3aQ4URXmu0enBSrNb499sAshB
053 * YWFqkrunUAcjoAGepRwhLJwmRjC21pfd5WB1fJHRkHPngeGJIp8nXbSAqJ_d-ks1Y7y0ddy3NOUX
054 * qoBrIIrXRkXzOv6xyaifginDRVu6gZl8_v4k0rjqhnosWq8yDZCHLSu2YjMkCQ2neGivDGTlnfFE
055 * oKfanrdIKy9uDnkdbgxLkjz8XEavA
056 * </pre>
057 *
058 * <p>Related specifications:
059 *
060 * <ul>
061 *     <li>OpenID Connect Federation 1.0, section 5.2.
062 * </ul>
063 */
064public class FederationEntityConfigurationSuccessResponse extends FederationEntityConfigurationResponse {
065        
066        
067        /**
068         * The content type.
069         */
070        private static final ContentType CONTENT_TYPE = new ContentType("application", "jose", StandardCharsets.UTF_8);
071        
072        
073        /**
074         * The entity statement.
075         */
076        private final EntityStatement entityStatement;
077        
078        
079        /**
080         * Creates a new federation entity configuration success response.
081         *
082         * @param entityStatement The entity statement. Must not be
083         *                        {@code null}.
084         */
085        public FederationEntityConfigurationSuccessResponse(final EntityStatement entityStatement) {
086                
087                if (entityStatement == null) {
088                        throw new IllegalArgumentException("The federation entity statement must not be null");
089                }
090                this.entityStatement = entityStatement;
091        }
092        
093        
094        /**
095         * Returns the entity statement. No signature or expiration validation
096         * is performed.
097         *
098         * @return The entity statement.
099         */
100        public EntityStatement getEntityStatement() {
101                
102                return entityStatement;
103        }
104        
105        
106        @Override
107        public boolean indicatesSuccess() {
108                return true;
109        }
110        
111        
112        @Override
113        public HTTPResponse toHTTPResponse() {
114                
115                HTTPResponse httpResponse = new HTTPResponse(HTTPResponse.SC_OK);
116                httpResponse.setEntityContentType(CONTENT_TYPE);
117                httpResponse.setContent(entityStatement.getSignedStatement().serialize());
118                return httpResponse;
119        }
120        
121        
122        /**
123         * Parses a federation entity configuration success response from the
124         * specified HTTP response.
125         *
126         * @param httpResponse The HTTP response. Must not be {@code null}.
127         *
128         * @return The federation entity configuration success response.
129         *
130         * @throws ParseException If HTTP response couldn't be parsed to a
131         *                        federation entity configuration success
132         *                        response.
133         */
134        public static FederationEntityConfigurationSuccessResponse parse(final HTTPResponse httpResponse)
135                throws ParseException {
136                
137                httpResponse.ensureStatusCode(HTTPResponse.SC_OK);
138                httpResponse.ensureEntityContentType(CONTENT_TYPE);
139                
140                String content = httpResponse.getContent();
141                
142                if (StringUtils.isBlank(content)) {
143                        throw new ParseException("Empty HTTP entity body");
144                }
145                
146                SignedJWT signedJWT;
147                try {
148                        signedJWT = SignedJWT.parse(httpResponse.getContent());
149                } catch (java.text.ParseException e) {
150                        throw new ParseException(e.getMessage(), e);
151                }
152                
153                return new FederationEntityConfigurationSuccessResponse(EntityStatement.parse(signedJWT));
154        }
155}