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