001/*
002 * oauth2-oidc-sdk
003 *
004 * Copyright 2012-2016, 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.policy.operations;
019
020
021import java.util.Collections;
022import java.util.LinkedList;
023import java.util.List;
024
025import com.nimbusds.oauth2.sdk.ParseException;
026import com.nimbusds.oauth2.sdk.util.JSONUtils;
027import com.nimbusds.openid.connect.sdk.federation.policy.language.*;
028
029
030/**
031 * Add (add) operation.
032 *
033 * <p>Example policy:
034 *
035 * <pre>
036 * "contacts" : { "add" : "[email protected]" }
037 * </pre>
038 *
039 * <p>Input:
040 *
041 * <pre>
042 * "contacts" : "[email protected]"
043 * </pre>
044 *
045 * <p>Result:
046 *
047 * <pre>
048 * "contacts" : [ "[email protected]", "[email protected]" ]
049 * </pre>
050 *
051 * <p>Related specifications:
052 *
053 * <ul>
054 *     <li>OpenID Connect Federation 1.0, section 4.1.4.
055 * </ul>
056 */
057public class AddOperation extends AbstractSetBasedOperation implements StringConfiguration, StringListOperation {
058        
059        
060        public static final OperationName NAME = new OperationName("add");
061        
062        
063        @Override
064        public OperationName getOperationName() {
065                return NAME;
066        }
067        
068        
069        @Override
070        public void configure(final String parameter) {
071                configure(Collections.singletonList(parameter));
072        }
073        
074        
075        @Override
076        public void parseConfiguration(final Object jsonEntity) throws ParseException {
077                
078                if (jsonEntity instanceof String) {
079                        configure(JSONUtils.toString(jsonEntity));
080                } else {
081                        // String list
082                        super.parseConfiguration(jsonEntity);
083                }
084        }
085        
086        
087        @Override
088        public String getStringConfiguration() {
089                return getStringListConfiguration().get(0);
090        }
091        
092        
093        @Override
094        public PolicyOperation merge(final PolicyOperation other)
095                throws PolicyViolationException {
096                
097                AddOperation otherTyped = Utils.castForMerge(other, AddOperation.class);
098                
099                List<String> combined = new LinkedList<>();
100                combined.addAll(getStringListConfiguration());
101                combined.addAll(otherTyped.getStringListConfiguration());
102                
103                AddOperation mergedPolicy = new AddOperation();
104                mergedPolicy.configure(combined);
105                return mergedPolicy;
106        }
107        
108        
109        @Override
110        public List<String> apply(final List<String> value) {
111                
112                if (setConfig == null) {
113                        throw new IllegalStateException("The policy is not initialized");
114                }
115                
116                if (value == null) {
117                        return Collections.unmodifiableList(new LinkedList<>(setConfig));
118                }
119                
120                List<String> result = new LinkedList<>(value);
121                result.addAll(setConfig);
122                return result;
123        }
124}