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 java.util.LinkedList;
022import java.util.List;
023
024import net.jcip.annotations.Immutable;
025import net.minidev.json.JSONArray;
026
027import com.nimbusds.common.contenttype.ContentType;
028import com.nimbusds.oauth2.sdk.ParseException;
029import com.nimbusds.oauth2.sdk.http.HTTPResponse;
030import com.nimbusds.oauth2.sdk.util.JSONArrayUtils;
031import com.nimbusds.openid.connect.sdk.federation.entities.EntityID;
032
033
034/**
035 * Entity listing success response.
036 *
037 * <p>Related specifications:
038 *
039 * <ul>
040 *     <li>OpenID Connect Federation 1.0, section 6.3.2.
041 * </ul>
042 */
043@Immutable
044public class EntityListingSuccessResponse extends EntityListingResponse {
045        
046        
047        /**
048         * The entity IDs.
049         */
050        private final List<EntityID> entityIDS;
051        
052        
053        /**
054         * Creates a new entity listing success response.
055         *
056         * @param entityIDS The entity IDs. Must not be {@code null}.
057         */
058        public EntityListingSuccessResponse(final List<EntityID> entityIDS) {
059                if (entityIDS == null) {
060                        throw new IllegalArgumentException("The entity listing must not be null");
061                }
062                this.entityIDS = entityIDS;
063        }
064        
065        
066        /**
067         * Returns the entity IDs.
068         *
069         * @return The entity IDs.
070         */
071        public List<EntityID> getEntityListing() {
072                return entityIDS;
073        }
074        
075        
076        @Override
077        public boolean indicatesSuccess() {
078                return true;
079        }
080        
081        
082        @Override
083        public HTTPResponse toHTTPResponse() {
084                HTTPResponse httpResponse = new HTTPResponse(HTTPResponse.SC_OK);
085                httpResponse.setEntityContentType(ContentType.APPLICATION_JSON);
086                JSONArray jsonArray = new JSONArray();
087                for (EntityID entityID: getEntityListing()) {
088                        jsonArray.add(entityID.getValue());
089                }
090                httpResponse.setContent(jsonArray.toJSONString());
091                return httpResponse;
092        }
093        
094        
095        /**
096         * Parses an entity listing success response from the specified JSON
097         * array.
098         *
099         * @param jsonArray The JSON array. Must not be {@code null}.
100         *
101         * @return The entity listing success response.
102         */
103        public static EntityListingSuccessResponse parse(final JSONArray jsonArray) {
104                
105                List<String> values = JSONArrayUtils.toStringList(jsonArray);
106                
107                List<EntityID> entityIDS = new LinkedList<>();
108                for (String v: values) {
109                        entityIDS.add(new EntityID(v));
110                }
111                return new EntityListingSuccessResponse(entityIDS);
112        }
113        
114        
115        /**
116         * Parses an entity listing success response from the specified HTTP
117         * response.
118         *
119         * @param httpResponse The HTTP response. Must not be {@code null}.
120         *
121         * @return The entity listing success response.
122         *
123         * @throws ParseException If parsing failed.
124         */
125        public static EntityListingSuccessResponse parse(final HTTPResponse httpResponse)
126                throws ParseException {
127                
128                httpResponse.ensureStatusCode(HTTPResponse.SC_OK);
129                return EntityListingSuccessResponse.parse(httpResponse.getContentAsJSONArray());
130        }
131}