View Javadoc
1   /*
2    * Prometheus: Application Framework
3    * Copyright 2012-2026. Tony Washer
4    *
5    * Licensed under the Apache License, Version 2.0 (the "License"); you may not
6    * use this file except in compliance with the License.  You may obtain a copy
7    * of the License at
8    *
9    *   http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
14   * License for the specific language governing permissions and limitations under
15   * the License.
16   */
17  package io.github.tonywasher.joceanus.prometheus.data;
18  
19  import io.github.tonywasher.joceanus.metis.data.MetisDataItem.MetisDataFieldId;
20  import io.github.tonywasher.joceanus.metis.field.MetisFieldItem.MetisFieldDef;
21  import io.github.tonywasher.joceanus.metis.field.MetisFieldItem.MetisFieldSetDef;
22  import io.github.tonywasher.joceanus.metis.field.MetisFieldItem.MetisFieldVersionedDef;
23  import io.github.tonywasher.joceanus.metis.field.MetisFieldVersionValues;
24  import io.github.tonywasher.joceanus.oceanus.base.OceanusException;
25  import io.github.tonywasher.joceanus.prometheus.data.PrometheusEncrypted.PrometheusEncryptedDataItemCtl;
26  
27  import java.util.Iterator;
28  
29  /**
30   * Prometheus Set of versioned Values.
31   */
32  public class PrometheusEncryptedValues
33          extends MetisFieldVersionValues {
34      /**
35       * Constructor.
36       *
37       * @param pItem the associated item
38       */
39      public PrometheusEncryptedValues(final PrometheusEncryptedDataItemCtl pItem) {
40          super(pItem);
41      }
42  
43      @Override
44      public PrometheusEncryptedValues cloneIt() {
45          /* Create the valueSet and initialise to existing values */
46          final PrometheusEncryptedValues mySet = new PrometheusEncryptedValues(getItem());
47          mySet.copyFrom(this);
48          return mySet;
49      }
50  
51      @Override
52      protected PrometheusEncryptedDataItemCtl getItem() {
53          return (PrometheusEncryptedDataItemCtl) super.getItem();
54      }
55  
56      @Override
57      public Object getValue(final MetisFieldDef pField) {
58          /* Access the underlying object */
59          final Object myValue = super.getValue(pField);
60  
61          /* If this is an encrypted value */
62          return myValue instanceof PrometheusEncryptedPair myPair
63                  ? myPair.getValue()
64                  : myValue;
65      }
66  
67      /**
68       * Obtain the encrypted bytes.
69       *
70       * @param pFieldId the fieldId
71       * @return the encrypted bytes
72       */
73      public byte[] getEncryptedBytes(final MetisDataFieldId pFieldId) {
74          /* Access the underlying object */
75          final MetisFieldDef myField = getItem().getDataFieldSet().getField(pFieldId);
76          return getEncryptedBytes(myField);
77      }
78  
79      /**
80       * Obtain the encrypted bytes.
81       *
82       * @param pField the field
83       * @return the encrypted bytes
84       */
85      public byte[] getEncryptedBytes(final MetisFieldDef pField) {
86          /* Access the underlying object */
87          final Object myValue = super.getValue(pField);
88  
89          /* If this is an encrypted value */
90          return myValue instanceof PrometheusEncryptedPair myPair
91                  ? myPair.getBytes()
92                  : null;
93      }
94  
95      /**
96       * Obtain the encrypted pair.
97       *
98       * @param pFieldId the fieldId
99       * @return the encrypted pair
100      */
101     public PrometheusEncryptedPair getEncryptedPair(final MetisDataFieldId pFieldId) {
102         /* Access the underlying object */
103         final MetisFieldDef myField = getItem().getDataFieldSet().getField(pFieldId);
104         return getEncryptedPair(myField);
105     }
106 
107     /**
108      * Obtain the encrypted pair.
109      *
110      * @param pField the field
111      * @return the encrypted pair
112      */
113     public PrometheusEncryptedPair getEncryptedPair(final MetisFieldDef pField) {
114         /* Access the underlying object */
115         final Object myValue = super.getValue(pField);
116 
117         /* If this is an encrypted value */
118         return myValue instanceof PrometheusEncryptedPair myPair
119                 ? myPair
120                 : null;
121     }
122 
123     @Override
124     public void setValue(final MetisFieldDef pField,
125                          final Object pValue) throws OceanusException {
126         /* Reject if not in valueSet */
127         if (!(pField instanceof MetisFieldVersionedDef)) {
128             throw new IllegalArgumentException(ERROR_NOTVERSIONED);
129         }
130 
131         /* check the value type */
132         checkValueType(pField, pValue);
133 
134         /* If this is an encrypted field */
135         Object myValue = pValue;
136         if (pField instanceof PrometheusEncryptedField
137                 //&& theEncryptor != null
138                 && myValue != null) {
139             /* Encrypt the value */
140             final PrometheusEncryptedDataItemCtl myItem = getItem();
141             final PrometheusEncryptor myEncryptor = myItem.getEncryptor();
142             myValue = myEncryptor.encryptValue(pValue, pField);
143         }
144 
145         /* Store the value */
146         setUncheckedValue(pField, myValue);
147     }
148 
149     /**
150      * Update security for the values.
151      *
152      * @throws OceanusException on error
153      */
154     public void updateSecurity() throws OceanusException {
155         /* Loop through the fields */
156         final PrometheusEncryptedDataItemCtl myItem = getItem();
157         final PrometheusEncryptor myEncryptor = myItem.getEncryptor();
158         final MetisFieldSetDef myFieldSet = getFields();
159         final Iterator<MetisFieldDef> myIterator = myFieldSet.fieldIterator();
160         while (myIterator.hasNext()) {
161             final MetisFieldDef myField = myIterator.next();
162 
163             /* Ignore non-encrypted fields */
164             if (!(myField instanceof PrometheusEncryptedField)) {
165                 continue;
166             }
167 
168             /* Access the value */
169             final Object myValue = getValue(myField);
170 
171             /* Encrypt the value */
172             setUncheckedValue(myField, myEncryptor.encryptValue(myValue, myField));
173         }
174     }
175 
176     /**
177      * Adopt security for the values.
178      *
179      * @param pBaseValues the base values
180      * @throws OceanusException on error
181      */
182     public void adoptSecurity(final PrometheusEncryptedValues pBaseValues) throws OceanusException {
183         /* Loop through the fields */
184         final PrometheusEncryptedDataItemCtl myItem = getItem();
185         final PrometheusEncryptor myEncryptor = myItem.getEncryptor();
186         final MetisFieldSetDef myFieldSet = getFields();
187         final Iterator<MetisFieldDef> myIterator = myFieldSet.fieldIterator();
188         while (myIterator.hasNext()) {
189             final MetisFieldDef myField = myIterator.next();
190 
191             /* Ignore non-encrypted fields */
192             if (!(myField instanceof PrometheusEncryptedField)) {
193                 continue;
194             }
195             final PrometheusEncryptedPair myValue = getEncryptedPair(myField);
196             if (myValue == null) {
197                 continue;
198             }
199 
200             /* Access the base object */
201             Object myBaseObj = pBaseValues == null
202                     ? null
203                     : pBaseValues.getValue(myField);
204             if (!(myBaseObj instanceof PrometheusEncryptedPair)) {
205                 myBaseObj = null;
206             }
207 
208             /* Adopt encryption */
209             myEncryptor.adoptEncryption(myValue, (PrometheusEncryptedPair) myBaseObj);
210         }
211     }
212 }