@Override public List<CSVReportEntry> apply(final GetCredentialReportResult report) { Assert.state(Textcsv.toString().equals(report.getReportFormat()), "unknown credential report format: " + report.getReportFormat()); try (final Reader r = new BufferedReader(new InputStreamReader(new ByteBufferBackedInputStream(report.getContent())))) { final CSVParser parser = new CSVParser(r, CSV_FORMAT); final Map<String, Integer> headers = parser.getHeaderMap(); Assert.state(headers.containsKey("user"), "Header 'user' not found in CSV"); Assert.state(headers.containsKey("arn"), "Header 'arn' not found in CSV"); Assert.state(headers.containsKey("password_enabled"), "Header 'password_enabled' not found in CSV"); Assert.state(headers.containsKey("mfa_active"), "Header 'mfa_active' not found in CSV"); Assert.state(headers.containsKey("access_key_1_active"), "Header 'access_key_1_active' not found in CSV"); Assert.state(headers.containsKey("access_key_2_active"), "Header 'access_key_2_active' not found in CSV"); return stream(parser.spliterator(), false).map(this::toCSVReportEntry).filter(Objects::nonNull).collect(toList()); } catch (final IOException e) { throw new RuntimeException("Could not read csv report", e); } }
/** * Deserializes the given message. * * @param action * @return * @throws Exception */ public T deserialize(WebSocketAction action) throws Exception { // first deserialize T message = null; if (messageClass != null) { message = serDe.deserialize(new ByteBufferBackedInputStream(rawData), messageClass); } // then validate if (message != null && action.shouldValidatePayload()) { SpringValidatorAdapter validatorAdapter = new SpringValidatorAdapter(messageValidator); BeanPropertyBindingResult result = new BeanPropertyBindingResult(message, messageClass.getName()); validatorAdapter.validate(message, result); if (result.hasErrors()) { throw new MethodArgumentNotValidException(new MethodParameter(action.getMethod(), action.getPayloadParameterIndex()), result); } } return message; }
@Override public <U> U deserialize(BinaryData rawPayload, Class<U> targetType) { try { return objectMapper.readValue(new ByteBufferBackedInputStream(rawPayload.toByteBuffer()), targetType); } catch (IOException e) { throw new IllegalArgumentException("Error deserializing payload", e); } }
@Override @SuppressWarnings("unchecked") public JoseHeader deserialize(BinaryData data) throws JwtMalformedTokenException { try { Map<String, Object> headerValues = objectMapper.readValue( new ByteBufferBackedInputStream(data.toByteBuffer()), Map.class); return new DefaultJoseHeader(headerValues); } catch (IOException e) { throw new JwtMalformedTokenException("Error deserializing JWS header", e); } }
@Override public T decode(ByteBuffer bytes) throws DecodeException { try { return mapper.readValue(new ByteBufferBackedInputStream(bytes), objectClass); } catch (IOException e) { throw new DecodeException(bytes, "decode json error", e); } }
@Override public boolean willDecode(ByteBuffer bytes) { try { mapper.readTree(new ByteBufferBackedInputStream(bytes)); } catch (IOException e) { logger.warn("invalidate json", e); return false; } return true; }
/** * Reads the data field of the record. If the record is compressed, this * will involve decompressing it. Currently both bz2 and lz4 compression * are supported. * Do not call this method more than once, and do not call it if the channel * used to create this record has been closed. * @throws BagReaderException */ public void readData() throws BagReaderException { myData = ByteBuffer.allocate(myDataLength); try { myChannel.position(myDataOffset); myChannel.read(myData); // Chunks can have bz2 or lz4-compressed myData in them, which we need to // decompress in order to do anything useful with. if (myHeader.getType() == RecordType.CHUNK) { String compression = myHeader.getValue("compression"); switch (compression) { case "none": // Do nothing here if not compressed break; case "bz2": case "lz4": { int decompressedSize = myHeader.getInt("size"); myData.flip(); try (ByteBufferBackedInputStream inStream = new ByteBufferBackedInputStream(myData); InputStream compressedStream = openCompressedStream(compression, inStream)) { final byte[] buffer = new byte[decompressedSize]; int n = IOUtils.readFully(compressedStream, buffer); if (n != decompressedSize) { throw new BagReaderException("Read " + n + " bytes from a " + "compressed chunk but expected " + decompressedSize + "."); } myData = ByteBuffer.wrap(buffer); } break; } default: myLogger.warn("Unknown compression format: " + compression); break; } } myData.order(ByteOrder.LITTLE_ENDIAN); } catch (IOException e) { throw new BagReaderException(e); } }