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;
022import net.minidev.json.JSONObject;
023
024import com.nimbusds.common.contenttype.ContentType;
025import com.nimbusds.oauth2.sdk.ParseException;
026import com.nimbusds.oauth2.sdk.http.HTTPResponse;
027
028
029/**
030 * Trust negotiation success response.
031 *
032 * <p>Related specifications:
033 *
034 * <ul>
035 *     <li>OpenID Connect Federation 1.0, section 6.2.2.
036 * </ul>
037 */
038@Immutable
039public class TrustNegotiationSuccessResponse extends TrustNegotiationResponse {
040        
041        
042        /**
043         * The metadata JSON object.
044         */
045        private final JSONObject metadata;
046        
047        
048        /**
049         * Creates a new trust negotiation success response.
050         *
051         * @param metadata The metadata JSON object for the requested {@link
052         *                 com.nimbusds.openid.connect.sdk.federation.entities.FederationMetadataType
053         *                 metadata type}. Must not be {@code null}.
054         */
055        public TrustNegotiationSuccessResponse(final JSONObject metadata) {
056                if (metadata == null) {
057                        throw new IllegalArgumentException("The metadata JSON object must not be null");
058                }
059                this.metadata = metadata;
060        }
061        
062        
063        /**
064         * Returns metadata JSON object for the requested {@link
065         * com.nimbusds.openid.connect.sdk.federation.entities.FederationMetadataType
066         * metadata type}.
067         *
068         * @return The metadata JSON object.
069         */
070        public JSONObject getMetadataJSONObject() {
071                return metadata;
072        }
073        
074        
075        @Override
076        public boolean indicatesSuccess() {
077                return true;
078        }
079        
080        
081        @Override
082        public HTTPResponse toHTTPResponse() {
083                HTTPResponse httpResponse = new HTTPResponse(HTTPResponse.SC_OK);
084                httpResponse.setEntityContentType(ContentType.APPLICATION_JSON);
085                httpResponse.setContent(getMetadataJSONObject().toJSONString());
086                return httpResponse;
087        }
088        
089        
090        /**
091         * Parses a trust negotiation success response from the specified HTTP
092         * response.
093         *
094         * @param httpResponse The HTTP response. Must not be {@code null}.
095         *
096         * @return The trust negotiation success response.
097         *
098         * @throws ParseException If parsing failed.
099         */
100        public static TrustNegotiationSuccessResponse parse(final HTTPResponse httpResponse)
101                throws ParseException {
102                
103                httpResponse.ensureStatusCode(HTTPResponse.SC_OK);
104                JSONObject jsonObject = httpResponse.getContentAsJSONObject();
105                return new TrustNegotiationSuccessResponse(jsonObject);
106        }
107}