001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.camel.model.rest;
018
019import java.util.ArrayList;
020import java.util.List;
021
022import javax.xml.bind.annotation.XmlAccessType;
023import javax.xml.bind.annotation.XmlAccessorType;
024import javax.xml.bind.annotation.XmlElement;
025import javax.xml.bind.annotation.XmlElements;
026import javax.xml.bind.annotation.XmlRootElement;
027import javax.xml.bind.annotation.XmlTransient;
028
029import org.apache.camel.spi.Metadata;
030
031/**
032 * To configure rest security definitions.
033 */
034@Metadata(label = "rest,security", title = "Security Definitions")
035@XmlRootElement(name = "securityDefinitions")
036@XmlAccessorType(XmlAccessType.FIELD)
037public class RestSecuritiesDefinition {
038
039    @XmlTransient
040    private RestDefinition rest;
041
042    @XmlElements({@XmlElement(name = "apiKey", type = RestSecurityApiKey.class), @XmlElement(name = "basicAuth", type = RestSecurityBasicAuth.class),
043                  @XmlElement(name = "oauth2", type = RestSecurityOAuth2.class)})
044    private List<RestSecurityDefinition> securityDefinitions = new ArrayList<>();
045
046    public RestSecuritiesDefinition() {
047    }
048
049    public RestSecuritiesDefinition(RestDefinition rest) {
050        this.rest = rest;
051    }
052
053    public List<RestSecurityDefinition> getSecurityDefinitions() {
054        return securityDefinitions;
055    }
056
057    /**
058     * Security definitions
059     */
060    public void setSecurityDefinitions(List<RestSecurityDefinition> securityDefinitions) {
061        this.securityDefinitions = securityDefinitions;
062    }
063
064    public RestSecurityApiKey apiKey(String key) {
065        return apiKey(key, null);
066    }
067
068    public RestSecurityApiKey apiKey(String key, String description) {
069        RestSecurityApiKey auth = new RestSecurityApiKey(rest);
070        auth.setKey(key);
071        auth.setDescription(description);
072        securityDefinitions.add(auth);
073        return auth;
074    }
075
076    public RestSecuritiesDefinition basicAuth(String key) {
077        return basicAuth(key, null);
078    }
079
080    public RestSecuritiesDefinition basicAuth(String key, String description) {
081        RestSecurityBasicAuth auth = new RestSecurityBasicAuth(rest);
082        securityDefinitions.add(auth);
083        auth.setKey(key);
084        auth.setDescription(description);
085        return this;
086    }
087
088    public RestSecurityOAuth2 oauth2(String key) {
089        return oauth2(key, null);
090    }
091
092    public RestSecurityOAuth2 oauth2(String key, String description) {
093        RestSecurityOAuth2 auth = new RestSecurityOAuth2(rest);
094        auth.setKey(key);
095        auth.setDescription(description);
096        securityDefinitions.add(auth);
097        return auth;
098    }
099
100    public RestDefinition end() {
101        return rest;
102    }
103
104}