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.oceanus.base.OceanusException;
20  import io.github.tonywasher.joceanus.metis.data.MetisDataItem.MetisDataFieldId;
21  import io.github.tonywasher.joceanus.metis.field.MetisFieldItem.MetisFieldDef;
22  import io.github.tonywasher.joceanus.metis.field.MetisFieldItem.MetisFieldSetDef;
23  import io.github.tonywasher.joceanus.metis.field.MetisFieldItem.MetisFieldVersionedDef;
24  import io.github.tonywasher.joceanus.metis.field.MetisFieldVersionValues;
25  
26  import java.util.Iterator;
27  
28  /**
29   * Prometheus Set of versioned Values.
30   */
31  public class PrometheusEncryptedValues
32          extends MetisFieldVersionValues {
33      /**
34       * Constructor.
35       *
36       * @param pItem the associated item
37       */
38      public PrometheusEncryptedValues(final PrometheusEncryptedDataItem pItem) {
39          super(pItem);
40      }
41  
42      @Override
43      public PrometheusEncryptedValues cloneIt() {
44          /* Create the valueSet and initialise to existing values */
45          final PrometheusEncryptedValues mySet = new PrometheusEncryptedValues(getItem());
46          mySet.copyFrom(this);
47          return mySet;
48      }
49  
50      @Override
51      protected PrometheusEncryptedDataItem getItem() {
52          return (PrometheusEncryptedDataItem) super.getItem();
53      }
54  
55      @Override
56      public Object getValue(final MetisFieldDef pField) {
57          /* Access the underlying object */
58          final Object myValue = super.getValue(pField);
59  
60          /* If this is an encrypted value */
61          return myValue instanceof PrometheusEncryptedPair myPair
62                  ? myPair.getValue()
63                  : myValue;
64      }
65  
66      /**
67       * Obtain the encrypted bytes.
68       *
69       * @param pFieldId the fieldId
70       * @return the encrypted bytes
71       */
72      public byte[] getEncryptedBytes(final MetisDataFieldId pFieldId) {
73          /* Access the underlying object */
74          final MetisFieldDef myField = getItem().getDataFieldSet().getField(pFieldId);
75          return getEncryptedBytes(myField);
76      }
77  
78      /**
79       * Obtain the encrypted bytes.
80       *
81       * @param pField the field
82       * @return the encrypted bytes
83       */
84      public byte[] getEncryptedBytes(final MetisFieldDef pField) {
85          /* Access the underlying object */
86          final Object myValue = super.getValue(pField);
87  
88          /* If this is an encrypted value */
89          return myValue instanceof PrometheusEncryptedPair myPair
90                  ? myPair.getBytes()
91                  : null;
92      }
93  
94      /**
95       * Obtain the encrypted pair.
96       *
97       * @param pFieldId the fieldId
98       * @return the encrypted pair
99       */
100     public PrometheusEncryptedPair getEncryptedPair(final MetisDataFieldId pFieldId) {
101         /* Access the underlying object */
102         final MetisFieldDef myField = getItem().getDataFieldSet().getField(pFieldId);
103         return getEncryptedPair(myField);
104     }
105 
106     /**
107      * Obtain the encrypted pair.
108      *
109      * @param pField the field
110      * @return the encrypted pair
111      */
112     public PrometheusEncryptedPair getEncryptedPair(final MetisFieldDef pField) {
113         /* Access the underlying object */
114         final Object myValue = super.getValue(pField);
115 
116         /* If this is an encrypted value */
117         return myValue instanceof PrometheusEncryptedPair myPair
118                 ? myPair
119                 : null;
120     }
121 
122     @Override
123     public void setValue(final MetisFieldDef pField,
124                          final Object pValue) throws OceanusException {
125         /* Reject if not in valueSet */
126         if (!(pField instanceof MetisFieldVersionedDef)) {
127             throw new IllegalArgumentException(ERROR_NOTVERSIONED);
128         }
129 
130         /* check the value type */
131         checkValueType(pField, pValue);
132 
133         /* If this is an encrypted field */
134         Object myValue = pValue;
135         if (pField instanceof PrometheusEncryptedField
136                 //&& theEncryptor != null
137                 && myValue != null) {
138             /* Encrypt the value */
139             final PrometheusEncryptedDataItem myItem = getItem();
140             final PrometheusEncryptor myEncryptor = myItem.getEncryptor();
141             myValue = myEncryptor.encryptValue(pValue, pField);
142         }
143 
144         /* Store the value */
145         setUncheckedValue(pField, myValue);
146     }
147 
148     /**
149      * Update security for the values.
150      *
151      * @throws OceanusException on error
152      */
153     public void updateSecurity() throws OceanusException {
154         /* Loop through the fields */
155         final PrometheusEncryptedDataItem myItem = getItem();
156         final PrometheusEncryptor myEncryptor = myItem.getEncryptor();
157         final MetisFieldSetDef myFieldSet = getFields();
158         final Iterator<MetisFieldDef> myIterator = myFieldSet.fieldIterator();
159         while (myIterator.hasNext()) {
160             final MetisFieldDef myField = myIterator.next();
161 
162             /* Ignore non-encrypted fields */
163             if (!(myField instanceof PrometheusEncryptedField)) {
164                 continue;
165             }
166 
167             /* Access the value */
168             final Object myValue = getValue(myField);
169 
170             /* Encrypt the value */
171             setUncheckedValue(myField, myEncryptor.encryptValue(myValue, myField));
172         }
173     }
174 
175     /**
176      * Adopt security for the values.
177      *
178      * @param pBaseValues the base values
179      * @throws OceanusException on error
180      */
181     public void adoptSecurity(final PrometheusEncryptedValues pBaseValues) throws OceanusException {
182         /* Loop through the fields */
183         final PrometheusEncryptedDataItem myItem = getItem();
184         final PrometheusEncryptor myEncryptor = myItem.getEncryptor();
185         final MetisFieldSetDef myFieldSet = getFields();
186         final Iterator<MetisFieldDef> myIterator = myFieldSet.fieldIterator();
187         while (myIterator.hasNext()) {
188             final MetisFieldDef myField = myIterator.next();
189 
190             /* Ignore non-encrypted fields */
191             if (!(myField instanceof PrometheusEncryptedField)) {
192                 continue;
193             }
194             final PrometheusEncryptedPair myValue = getEncryptedPair(myField);
195             if (myValue == null) {
196                 continue;
197             }
198 
199             /* Access the base object */
200             Object myBaseObj = pBaseValues == null
201                     ? null
202                     : pBaseValues.getValue(myField);
203             if (!(myBaseObj instanceof PrometheusEncryptedPair)) {
204                 myBaseObj = null;
205             }
206 
207             /* Adopt encryption */
208             myEncryptor.adoptEncryption(myValue, (PrometheusEncryptedPair) myBaseObj);
209         }
210     }
211 }