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.AbstractMap;
022import java.util.Map;
023
024import com.nimbusds.oauth2.sdk.ParseException;
025import com.nimbusds.oauth2.sdk.util.JSONUtils;
026import com.nimbusds.openid.connect.sdk.federation.policy.language.*;
027
028
029/**
030 * Default (default) value operation.
031 *
032 * <p>Example policy:
033 *
034 * <pre>
035 * "tos_uri" : { "essential" : true }
036 * </pre>
037 *
038 * <p>Related specifications:
039 *
040 * <ul>
041 *     <li>OpenID Connect Federation 1.0, section 5.1.2.
042 * </ul>
043 */
044public class EssentialOperation implements PolicyOperation, BooleanConfiguration, UntypedOperation {
045        
046        
047        public static final OperationName NAME = new OperationName("essential");
048        
049        
050        private boolean enable = false;
051        
052        
053        @Override
054        public OperationName getOperationName() {
055                return NAME;
056        }
057        
058        
059        @Override
060        public void configure(final boolean enable) {
061                this.enable = enable;
062        }
063        
064        
065        @Override
066        public void parseConfiguration(final Object jsonEntity) throws ParseException {
067                
068                configure(JSONUtils.toBoolean(jsonEntity));
069        }
070        
071        
072        @Override
073        public Map.Entry<String,Object> toJSONObjectEntry() {
074                return new AbstractMap.SimpleImmutableEntry<>(getOperationName().getValue(), (Object) getBooleanConfiguration());
075        }
076        
077        
078        @Override
079        public boolean getBooleanConfiguration() {
080                return enable;
081        }
082        
083        
084        @Override
085        public PolicyOperation merge(final PolicyOperation other)
086                throws PolicyViolationException {
087                
088                EssentialOperation otherTyped = Utils.castForMerge(other, EssentialOperation.class);
089                
090                if (getBooleanConfiguration() == otherTyped.getBooleanConfiguration()) {
091                        EssentialOperation copy = new EssentialOperation();
092                        copy.configure(getBooleanConfiguration());
093                        return copy;
094                }
095                
096                throw new PolicyViolationException("Essential value mismatch");
097        }
098        
099        
100        @Override
101        public Object apply(final Object value) throws PolicyViolationException {
102        
103                if (enable && value == null) {
104                        throw new PolicyViolationException("Essential parameter not present");
105                }
106                
107                return value;
108        }
109}