View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.myfaces.tobago.internal.config;
21  
22  import java.util.Collections;
23  import java.util.HashMap;
24  import java.util.Map;
25  
26  public class ContentSecurityPolicy {
27  
28    private Mode mode;
29    private Map<String, String> directiveMap;
30  
31    private boolean unmodifiable = false;
32  
33    private void checkLocked() throws IllegalStateException {
34      if (unmodifiable) {
35        throw new RuntimeException("The configuration must not be changed after initialization!");
36      }
37    }
38  
39    /**
40     * Lock the configuration, so it cannot be modified any more.
41     */
42    public void lock() {
43      unmodifiable = true;
44      directiveMap = Collections.unmodifiableMap(directiveMap);
45    }
46  
47    public ContentSecurityPolicy(final String mode) {
48      this.mode = Mode.parse(mode);
49      this.directiveMap = new HashMap<String, String>();
50    }
51  
52    public void merge(final ContentSecurityPolicy other) {
53      checkLocked();
54      for (Map.Entry<String, String> entry : other.directiveMap.entrySet()) {
55        addDirective(entry.getKey(), entry.getValue());
56      }
57      mode = other.mode;
58    }
59  
60    public void addDirective(final String name, final String text) {
61      final String old = directiveMap.get(name);
62      if (old != null) {
63        directiveMap.put(name, old + ' ' + text);
64      } else {
65        directiveMap.put(name, text);
66      }
67    }
68  
69    public Map<String, String> getDirectiveMap() {
70      return directiveMap;
71    }
72  
73    public Mode getMode() {
74      return mode;
75    }
76  
77    @Override
78    public String toString() {
79      return "ContentSecurityPolicy{"
80          + "mode=" + mode
81          + ", directiveMap=" + directiveMap
82          + '}';
83    }
84  
85    public enum Mode {
86      ON("on"),
87      OFF("off"),
88      REPORT_ONLY("report-only");
89  
90      private final String value;
91  
92      Mode(final String value) {
93        this.value = value;
94      }
95  
96      public String getValue() {
97        return value;
98      }
99  
100     public static Mode parse(final String string) {
101       if (ON.value.equals(string)) {
102         return ON;
103       } else if (OFF.value.equals(string)) {
104         return OFF;
105       } else if (REPORT_ONLY.value.equals(string)) {
106         return REPORT_ONLY;
107       } else {
108         throw new IllegalArgumentException("Found: " + string);
109       }
110     }
111   }
112 
113 }