public boolean insert(Target target) { try { String id = target.getIdentifier(); URL url = new URL(id); Path hostPath = getHostPath(url); File hostDirectory = hostPath.toFile(); if (!hostDirectory.exists()) { hostDirectory.mkdirs(); } Path filePath = getFilePath(id, hostPath); try (OutputStream fileStream = new PrintStream(filePath.toFile())) { if (compressData) { try (OutputStream gzipStream = new DeflaterOutputStream(fileStream)) { serializeData(target, url, gzipStream); } } else { serializeData(target, url, fileStream); } } } catch (IOException e) { logger.error("Failed to store object in repository.", e); } return false; }
private void serializeData(Target target, URL url, OutputStream fileStream) throws IOException, JsonGenerationException, JsonMappingException { switch (dataFormat) { case HTML: { fileStream.write(target.getSource().getBytes()); break; } case JSON: { TargetModelJson targetModel = new TargetModelJson((Page) target); jsonMapper.writeValue(fileStream, targetModel); break; } case CBOR: { TargetModel targetModel = new TargetModel("", "", url, target.getSource()); cborMapper.writeValue(fileStream, targetModel); break; } } }