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.MalformedURLException;
022import java.net.URI;
023import java.net.URL;
024import java.util.List;
025import java.util.Map;
026
027import com.nimbusds.oauth2.sdk.AbstractRequest;
028import com.nimbusds.oauth2.sdk.SerializeException;
029import com.nimbusds.oauth2.sdk.http.HTTPRequest;
030import com.nimbusds.oauth2.sdk.util.URLUtils;
031
032
033/**
034 * Federation API request.
035 *
036 * <p>Related specifications:
037 *
038 * <ul>
039 *     <li>OpenID Connect Federation 1.0, section 6.
040 * </ul>
041 */
042public abstract class FederationAPIRequest extends AbstractRequest {
043        
044        
045        /**
046         * The requested operation.
047         */
048        private final OperationType operationType;
049        
050        
051        /**
052         * Creates a new federation API request.
053         *
054         * @param endpoint      The federation API endpoint. Must not be
055         *                      {@code null}.
056         * @param operationType The requested operation type. Must not be
057         *                      {@code null}.
058         */
059        public FederationAPIRequest(final URI endpoint, final OperationType operationType) {
060                super(endpoint);
061                if (operationType == null) {
062                        throw new IllegalArgumentException("The operation type must not be null");
063                }
064                this.operationType = operationType;
065        }
066        
067        
068        /**
069         * Returns the requested operation type.
070         *
071         * @return The operation type.
072         */
073        public OperationType getOperationType() {
074                return operationType;
075        }
076        
077        
078        /**
079         * Returns the request query parameters.
080         *
081         * @return The request query parameters.
082         */
083        public abstract Map<String, List<String>> toParameters();
084        
085        
086        @Override
087        public HTTPRequest toHTTPRequest() {
088                
089                URL url;
090                try {
091                        url = getEndpointURI().toURL();
092                } catch (IllegalArgumentException | MalformedURLException e) {
093                        throw new SerializeException(e.getMessage(), e);
094                }
095                
096                HTTPRequest httpRequest = new HTTPRequest(HTTPRequest.Method.GET, url);
097                httpRequest.setQuery(URLUtils.serializeParameters(toParameters()));
098                return httpRequest;
099        }
100}