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