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.net.URI;
022import java.util.Collections;
023import java.util.HashMap;
024import java.util.List;
025import java.util.Map;
026
027import net.jcip.annotations.Immutable;
028
029import com.nimbusds.common.contenttype.ContentType;
030import com.nimbusds.oauth2.sdk.ParseException;
031import com.nimbusds.oauth2.sdk.http.HTTPRequest;
032import com.nimbusds.oauth2.sdk.util.MultivaluedMapUtils;
033import com.nimbusds.oauth2.sdk.util.StringUtils;
034import com.nimbusds.oauth2.sdk.util.URLUtils;
035import com.nimbusds.openid.connect.sdk.federation.entities.EntityType;
036
037
038/**
039 * Entity listing request.
040 *
041 * <p>Related specifications:
042 *
043 * <ul>
044 *     <li>OpenID Connect Federation 1.0, section 7.3.1.
045 * </ul>
046 */
047@Immutable
048public class EntityListingRequest extends FederationAPIRequest {
049        
050        
051        /**
052         * Optional entity type.
053         */
054        private final EntityType entityType;
055        
056        
057        /**
058         * Creates a new entity listing request.
059         *
060         * @param endpoint The federation list endpoint. Must not be
061         *                 {@code null}.
062         */
063        public EntityListingRequest(final URI endpoint) {
064                this(endpoint, null);
065        }
066        
067        
068        /**
069         * Creates a new entity listing request.
070         *
071         * @param endpoint   The federation list endpoint. Must not be
072         *                   {@code null}.
073         * @param entityType The type of the entities to list, {@code null} for
074         *                   all.
075         */
076        public EntityListingRequest(final URI endpoint, final EntityType entityType) {
077                super(endpoint);
078                this.entityType = entityType;
079        }
080        
081        
082        /**
083         * Returns the type of the entities to list.
084         *
085         * @return The type of the entities to list, {@code null} for all.
086         */
087        public EntityType getEntityType() {
088                return entityType;
089        }
090        
091        
092        @Override
093        public Map<String, List<String>> toParameters() {
094                Map<String, List<String>> params = new HashMap<>();
095                if (entityType != null) {
096                        params.put("entity_type", Collections.singletonList(entityType.getValue()));
097                }
098                return Collections.unmodifiableMap(params);
099        }
100        
101        
102        @Override
103        public HTTPRequest toHTTPRequest() {
104                HTTPRequest httpRequest = new HTTPRequest(HTTPRequest.Method.GET, getEndpointURI());
105                httpRequest.setEntityContentType(ContentType.APPLICATION_URLENCODED);
106                httpRequest.setQuery(URLUtils.serializeParameters(toParameters()));
107                return httpRequest;
108        }
109        
110        
111        /**
112         * Parses an entity listing request from the specified HTTP request.
113         *
114         * @param httpRequest The HTTP request. Must not be {@code null}.
115         *
116         * @return The entity listing request.
117         *
118         * @throws ParseException If parsing failed.
119         */
120        public static EntityListingRequest parse(final HTTPRequest httpRequest)
121                throws ParseException {
122                
123                httpRequest.ensureMethod(HTTPRequest.Method.GET);
124                
125                EntityType entityType = null;
126                Map<String, List<String>> params = httpRequest.getQueryParameters();
127                String value = MultivaluedMapUtils.getFirstValue(params, "entity_type");
128                if (StringUtils.isNotBlank(value)) {
129                        entityType = new EntityType(value);
130                }
131                return new EntityListingRequest(httpRequest.getURI(), entityType);
132        }
133}