use of org.apache.cassandra.serializers.MarshalException in project cassandra by apache.
the class Json method parseJson.
/**
* Given a JSON string, return a map of columns to their values for the insert.
*/
public static Map<ColumnIdentifier, Term> parseJson(String jsonString, Collection<ColumnMetadata> expectedReceivers) {
try {
Map<String, Object> valueMap = JSON_OBJECT_MAPPER.readValue(jsonString, Map.class);
if (valueMap == null)
throw new InvalidRequestException("Got null for INSERT JSON values");
handleCaseSensitivity(valueMap);
Map<ColumnIdentifier, Term> columnMap = new HashMap<>(expectedReceivers.size());
for (ColumnSpecification spec : expectedReceivers) {
// explicit null value from no value
if (!valueMap.containsKey(spec.name.toString()))
continue;
Object parsedJsonObject = valueMap.remove(spec.name.toString());
if (parsedJsonObject == null) {
// This is an explicit user null
columnMap.put(spec.name, Constants.NULL_VALUE);
} else {
try {
columnMap.put(spec.name, spec.type.fromJSONObject(parsedJsonObject));
} catch (MarshalException exc) {
throw new InvalidRequestException(String.format("Error decoding JSON value for %s: %s", spec.name, exc.getMessage()));
}
}
}
if (!valueMap.isEmpty()) {
throw new InvalidRequestException(String.format("JSON values map contains unrecognized column: %s", valueMap.keySet().iterator().next()));
}
return columnMap;
} catch (IOException exc) {
throw new InvalidRequestException(String.format("Could not decode JSON string as a map: %s. (String was: %s)", exc.toString(), jsonString));
} catch (MarshalException exc) {
throw new InvalidRequestException(exc.getMessage());
}
}
use of org.apache.cassandra.serializers.MarshalException in project cassandra by apache.
the class Attributes method getTimeToLive.
public int getTimeToLive(QueryOptions options, TableMetadata metadata) throws InvalidRequestException {
if (timeToLive == null) {
ExpirationDateOverflowHandling.maybeApplyExpirationDateOverflowPolicy(metadata, metadata.params.defaultTimeToLive, true);
return metadata.params.defaultTimeToLive;
}
ByteBuffer tval = timeToLive.bindAndGet(options);
if (tval == null)
return 0;
if (tval == ByteBufferUtil.UNSET_BYTE_BUFFER)
return metadata.params.defaultTimeToLive;
try {
Int32Type.instance.validate(tval);
} catch (MarshalException e) {
throw new InvalidRequestException("Invalid timestamp value: " + tval);
}
int ttl = Int32Type.instance.compose(tval);
if (ttl < 0)
throw new InvalidRequestException("A TTL must be greater or equal to 0, but was " + ttl);
if (ttl > MAX_TTL)
throw new InvalidRequestException(String.format("ttl is too large. requested (%d) maximum (%d)", ttl, MAX_TTL));
if (metadata.params.defaultTimeToLive != LivenessInfo.NO_TTL && ttl == LivenessInfo.NO_TTL)
return LivenessInfo.NO_TTL;
ExpirationDateOverflowHandling.maybeApplyExpirationDateOverflowPolicy(metadata, ttl, false);
return ttl;
}
use of org.apache.cassandra.serializers.MarshalException in project cassandra by apache.
the class AbstractCompositeType method validate.
public <V> void validate(V input, ValueAccessor<V> accessor) {
boolean isStatic = readIsStatic(input, accessor);
int offset = startingOffset(isStatic);
int i = 0;
V previous = null;
while (!accessor.isEmptyFromOffset(input, offset)) {
AbstractType<?> comparator = validateComparator(i, input, accessor, offset);
offset += getComparatorSize(i, input, accessor, offset);
if (accessor.sizeFromOffset(input, offset) < 2)
throw new MarshalException("Not enough bytes to read value size of component " + i);
int length = accessor.getShort(input, offset);
offset += 2;
if (accessor.sizeFromOffset(input, offset) < length)
throw new MarshalException("Not enough bytes to read value of component " + i);
V value = accessor.slice(input, offset, length);
offset += length;
comparator.validateCollectionMember(value, previous, accessor);
if (accessor.isEmptyFromOffset(input, offset))
throw new MarshalException("Not enough bytes to read the end-of-component byte of component" + i);
byte b = accessor.getByte(input, offset++);
if (b != 0 && !accessor.isEmptyFromOffset(input, offset))
throw new MarshalException("Invalid bytes remaining after an end-of-component at component" + i);
previous = value;
++i;
}
}
use of org.apache.cassandra.serializers.MarshalException in project cassandra by apache.
the class AsciiType method fromString.
public ByteBuffer fromString(String source) {
// the encoder must be reset each time it's used, hence the thread-local storage
CharsetEncoder theEncoder = encoder.get();
theEncoder.reset();
try {
return theEncoder.encode(CharBuffer.wrap(source));
} catch (CharacterCodingException exc) {
throw new MarshalException(String.format("Invalid ASCII character in string literal: %s", exc));
}
}
use of org.apache.cassandra.serializers.MarshalException in project cassandra by apache.
the class DynamicCompositeTypeTest method testValidate.
@Test
public void testValidate() {
ByteBuffer key = createDynamicCompositeKey("test1", uuids[1], 42, false);
comparator.validate(key);
key = createDynamicCompositeKey("test1", null, -1, false);
comparator.validate(key);
key = createDynamicCompositeKey("test1", uuids[2], -1, true);
comparator.validate(key);
// make sure we're not aligned anymore
key.get();
try {
comparator.validate(key);
fail("Should not validate");
} catch (MarshalException e) {
}
key = ByteBuffer.allocate(5 + "test1".length() + 5 + 14);
key.putShort((short) (0x8000 | 'b'));
key.putShort((short) "test1".length());
key.put(ByteBufferUtil.bytes("test1"));
key.put((byte) 0);
key.putShort((short) (0x8000 | 't'));
key.putShort((short) 14);
key.rewind();
try {
comparator.validate(key);
fail("Should not validate");
} catch (MarshalException e) {
assert e.toString().contains("should be 16 or 0 bytes");
}
key = createDynamicCompositeKey("test1", UUID.randomUUID(), 42, false);
try {
comparator.validate(key);
fail("Should not validate");
} catch (MarshalException e) {
assert e.toString().contains("Invalid version for TimeUUID type");
}
}
Aggregations