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