|
| 1 | +/* |
| 2 | + * SPDX-FileCopyrightText: none |
| 3 | + * SPDX-License-Identifier: CC0-1.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package gov.nist.secauto.metaschema.core.datatype.adapter; |
| 7 | + |
| 8 | +import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonFormatTypes; |
| 9 | + |
| 10 | +import gov.nist.secauto.metaschema.core.datatype.AbstractDataTypeAdapter; |
| 11 | +import gov.nist.secauto.metaschema.core.metapath.item.atomic.IAnyAtomicItem; |
| 12 | +import gov.nist.secauto.metaschema.core.metapath.type.AbstractAtomicOrUnionType; |
| 13 | +import gov.nist.secauto.metaschema.core.util.ObjectUtils; |
| 14 | + |
| 15 | +import org.apache.commons.codec.BinaryDecoder; |
| 16 | +import org.apache.commons.codec.BinaryEncoder; |
| 17 | +import org.apache.commons.codec.DecoderException; |
| 18 | +import org.apache.commons.codec.EncoderException; |
| 19 | + |
| 20 | +import java.nio.ByteBuffer; |
| 21 | +import java.nio.charset.StandardCharsets; |
| 22 | +import java.util.Arrays; |
| 23 | + |
| 24 | +import edu.umd.cs.findbugs.annotations.NonNull; |
| 25 | + |
| 26 | +/** |
| 27 | + * Maintains a byte buffer representation of a byte stream. |
| 28 | + * <p> |
| 29 | + * The maintained byte stream is kept in a decoded form. |
| 30 | + * |
| 31 | + * @param <ITEM_TYPE> |
| 32 | + * the metapath item type supported by the adapter |
| 33 | + */ |
| 34 | +public abstract class AbstractBinaryAdapter<ITEM_TYPE extends IAnyAtomicItem> |
| 35 | + extends AbstractDataTypeAdapter<ByteBuffer, ITEM_TYPE> { |
| 36 | + |
| 37 | + /** |
| 38 | + * Construct a new Java type adapter for a provided class. |
| 39 | + * |
| 40 | + * @param itemClass |
| 41 | + * the Java type of the Matepath item this adapter supports |
| 42 | + * @param castExecutor |
| 43 | + * the method to call to cast an item to an item based on this type |
| 44 | + */ |
| 45 | + protected AbstractBinaryAdapter( |
| 46 | + @NonNull Class<ITEM_TYPE> itemClass, |
| 47 | + @NonNull AbstractAtomicOrUnionType.ICastExecutor<ITEM_TYPE> castExecutor) { |
| 48 | + super(ByteBuffer.class, itemClass, castExecutor); |
| 49 | + } |
| 50 | + |
| 51 | + @NonNull |
| 52 | + protected abstract BinaryDecoder getDecoder(); |
| 53 | + |
| 54 | + @NonNull |
| 55 | + protected abstract BinaryEncoder getEncoder(); |
| 56 | + |
| 57 | + @NonNull |
| 58 | + private static byte[] stringToBytes(@NonNull String text) { |
| 59 | + return text.getBytes(StandardCharsets.UTF_8); |
| 60 | + } |
| 61 | + |
| 62 | + @NonNull |
| 63 | + private static String bytesToString(@NonNull byte[] bytes) { |
| 64 | + return new String(bytes, StandardCharsets.UTF_8); |
| 65 | + } |
| 66 | + |
| 67 | + private static String elide(@NonNull String text, int length) { |
| 68 | + return text.length() <= length ? text : text.substring(0, length) + "…"; |
| 69 | + } |
| 70 | + |
| 71 | + @NonNull |
| 72 | + public byte[] encode(@NonNull byte[] decodedBytes) { |
| 73 | + try { |
| 74 | + return ObjectUtils.notNull(getEncoder().encode(decodedBytes)); |
| 75 | + } catch (EncoderException ex) { |
| 76 | + throw new IllegalArgumentException( |
| 77 | + String.format("unable to encode text '%s'", elide(bytesToString(decodedBytes), 64)), |
| 78 | + ex); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + @NonNull |
| 83 | + public ByteBuffer encodeToByteBuffer(@NonNull ByteBuffer decodedBuffer) { |
| 84 | + byte[] decodedBytes = bufferToBytes(decodedBuffer, false); |
| 85 | + return encodeToByteBuffer(decodedBytes); |
| 86 | + } |
| 87 | + |
| 88 | + @NonNull |
| 89 | + public ByteBuffer encodeToByteBuffer(String decodedText) { |
| 90 | + byte[] decodedBytes = stringToBytes(decodedText); |
| 91 | + return encodeToByteBuffer(decodedBytes); |
| 92 | + } |
| 93 | + |
| 94 | + @NonNull |
| 95 | + public ByteBuffer encodeToByteBuffer(byte[] decodedBytes) { |
| 96 | + byte[] encodedBytes = encode(decodedBytes); |
| 97 | + return ObjectUtils.notNull(ByteBuffer.wrap(encodedBytes)); |
| 98 | + } |
| 99 | + |
| 100 | + @NonNull |
| 101 | + public byte[] decode(@NonNull byte[] enodedBytes) { |
| 102 | + try { |
| 103 | + return ObjectUtils.notNull(getDecoder().decode(enodedBytes)); |
| 104 | + } catch (DecoderException ex) { |
| 105 | + throw new IllegalArgumentException( |
| 106 | + String.format("unable to decode text '%s'", elide(bytesToString(enodedBytes), 64)), |
| 107 | + ex); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + @NonNull |
| 112 | + public ByteBuffer decode(@NonNull ByteBuffer encodedBuffer) { |
| 113 | + byte[] encodedBytes = bufferToBytes(encodedBuffer, false); |
| 114 | + byte[] decodedBytes = decode(encodedBytes); |
| 115 | + return ObjectUtils.notNull(ByteBuffer.wrap(decodedBytes)); |
| 116 | + } |
| 117 | + |
| 118 | + @NonNull |
| 119 | + public String decodeToString(@NonNull byte[] encodedBytes) { |
| 120 | + byte[] decodedBytes = decode(encodedBytes); |
| 121 | + return bytesToString(decodedBytes); |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * Decodes the provided string. |
| 126 | + * |
| 127 | + * @return a buffer containing the decoded bytes |
| 128 | + */ |
| 129 | + @Override |
| 130 | + public ByteBuffer parse(String encodedString) { |
| 131 | + byte[] encodedBytes = stringToBytes(encodedString); |
| 132 | + // byte[] decodedBytes = decode(encodedBytes); |
| 133 | + // return ObjectUtils.notNull(ByteBuffer.wrap(decodedBytes)); |
| 134 | + return ObjectUtils.notNull(ByteBuffer.wrap(encodedBytes)); |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * Get the wrapped value as a base64 encoded string. |
| 139 | + * <p> |
| 140 | + * Encodes the wrapped value to produce a string. |
| 141 | + * |
| 142 | + * @return the base64 encoded value |
| 143 | + */ |
| 144 | + @Override |
| 145 | + public String asString(Object encodedBuffer) { |
| 146 | + byte[] encodedBytes = bufferToBytes((ByteBuffer) encodedBuffer, false); |
| 147 | + return new String(encodedBytes, StandardCharsets.UTF_8); |
| 148 | + // return bytesToString(encodedBytes); |
| 149 | + |
| 150 | + // byte[] decodedBytes = bufferToBytes((ByteBuffer) decodedBuffer, false); |
| 151 | + // byte[] encodedBytes = encode(decodedBytes); |
| 152 | + // return bytesToString(encodedBytes); |
| 153 | + } |
| 154 | + |
| 155 | + @Override |
| 156 | + public JsonFormatTypes getJsonRawType() { |
| 157 | + return JsonFormatTypes.STRING; |
| 158 | + } |
| 159 | + |
| 160 | + @Override |
| 161 | + public ByteBuffer copy(Object obj) { |
| 162 | + ByteBuffer buffer = (ByteBuffer) obj; |
| 163 | + ByteBuffer clone = buffer.isDirect() |
| 164 | + ? ByteBuffer.allocateDirect(buffer.capacity()) |
| 165 | + : ByteBuffer.allocate(buffer.capacity()); |
| 166 | + ByteBuffer readOnlyCopy = buffer.asReadOnlyBuffer(); |
| 167 | + readOnlyCopy.flip(); |
| 168 | + clone.put(readOnlyCopy); |
| 169 | + return clone; |
| 170 | + } |
| 171 | + |
| 172 | + @NonNull |
| 173 | + public static byte[] bufferToBytes(@NonNull ByteBuffer buffer, boolean copy) { |
| 174 | + byte[] array; |
| 175 | + if (buffer.hasArray()) { |
| 176 | + array = buffer.array(); |
| 177 | + if (copy) { |
| 178 | + array = Arrays.copyOf(array, array.length); |
| 179 | + } |
| 180 | + } else { |
| 181 | + // Handle direct buffers |
| 182 | + array = new byte[buffer.remaining()]; |
| 183 | + buffer.mark(); |
| 184 | + try { |
| 185 | + buffer.get(array); |
| 186 | + } finally { |
| 187 | + buffer.reset(); |
| 188 | + } |
| 189 | + } |
| 190 | + return ObjectUtils.notNull(array); |
| 191 | + } |
| 192 | +} |
0 commit comments