/** * Returns the JSON representation of {@link #content()}. * * @return the {@link JsonNode} parsed from the {@link #content()} * * @throws IllegalStateException if this {@link Entry} is a directory * @throws JsonParseException if failed to parse the {@link #content()} as JSON */ public JsonNode contentAsJson() throws JsonParseException { final T content = content(); if (content instanceof JsonNode) { return (JsonNode) content; } return Jackson.readTree(contentAsText()); }
@SuppressWarnings("unchecked") static <T> T convertWithJackson(Entry<?> entry, Class<T> clazz) { requireNonNull(entry, "entry"); requireNonNull(clazz, "clazz"); try { return Jackson.treeToValue(((Entry<JsonNode>) entry).content(), clazz); } catch (Throwable cause) { return Exceptions.throwUnsafely(cause); } } }
@SuppressWarnings("unchecked") static <T> T convertWithJackson(Entry<?> entry, Class<T> clazz) { requireNonNull(entry, "entry"); requireNonNull(clazz, "clazz"); try { return Jackson.treeToValue(((Entry<JsonNode>) entry).content(), clazz); } catch (Throwable cause) { return Exceptions.throwUnsafely(cause); } } }
/** * Returns the value converted from the JSON representation of {@link #content()}. * * @return the value converted from {@link #content()} * * @throws IllegalStateException if this {@link Entry} is a directory * @throws JsonParseException if failed to parse the {@link #content()} as JSON * @throws JsonMappingException if failed to convert the parsed JSON into {@code valueType} */ public <U> U contentAsJson(Class<U> valueType) throws JsonParseException, JsonMappingException { final T content = content(); if (content instanceof TreeNode) { return Jackson.treeToValue((TreeNode) content, valueType); } return Jackson.readValue(contentAsText(), valueType); }
@SuppressWarnings("unchecked") static <T> T convertWithJackson(Entry<?> entry, Class<T> clazz) { requireNonNull(entry, "entry"); requireNonNull(clazz, "clazz"); try { return Jackson.treeToValue(((Entry<JsonNode>) entry).content(), clazz); } catch (Throwable cause) { return Exceptions.throwUnsafely(cause); } } }
private static void watch(Repository repo, Query<Object> query, Revision lastKnownRev, @Nullable Entry<Object> oldResult, CompletableFuture<Entry<Object>> parentFuture) { final CompletableFuture<Revision> future = repo.watch(lastKnownRev, query.path()); parentFuture.whenComplete((res, cause) -> future.completeExceptionally(CANCELLATION_EXCEPTION)); future.thenCompose(newRev -> repo.getOrNull(newRev, query).thenAccept(newResult -> { if (newResult == null || oldResult != null && Objects.equals(oldResult.content(), newResult.content())) { // Entry does not exist or did not change; watch again for more changes. if (!parentFuture.isDone()) { // ... only when the parent future has not been cancelled. watch(repo, query, newRev, oldResult, parentFuture); } } else { parentFuture.complete(newResult); } })).exceptionally(voidFunction(parentFuture::completeExceptionally)); }
private static void watch(Repository repo, Query<Object> query, Revision lastKnownRev, @Nullable Entry<Object> oldResult, CompletableFuture<Entry<Object>> parentFuture) { final CompletableFuture<Revision> future = repo.watch(lastKnownRev, query.path()); parentFuture.whenComplete((res, cause) -> future.completeExceptionally(CANCELLATION_EXCEPTION)); future.thenCompose(newRev -> repo.getOrNull(newRev, query).thenAccept(newResult -> { if (newResult == null || oldResult != null && Objects.equals(oldResult.content(), newResult.content())) { // Entry does not exist or did not change; watch again for more changes. if (!parentFuture.isDone()) { // ... only when the parent future has not been cancelled. watch(repo, query, newRev, oldResult, parentFuture); } } else { parentFuture.complete(newResult); } })).exceptionally(voidFunction(parentFuture::completeExceptionally)); }
@Override protected CompletableFuture<Latest<T>> doWatch(CentralDogma client, String projectName, String repositoryName, Revision lastKnownRevision, long timeoutMillis) { return client.watchFile(projectName, repositoryName, lastKnownRevision, query, timeoutMillis) .thenApply(result -> { if (result == null) { return null; } return new Latest<>(result.revision(), function.apply(result.content())); }); } }
private static void watch(Repository repo, Query<Object> query, Revision lastKnownRev, @Nullable Entry<Object> oldResult, CompletableFuture<Entry<Object>> parentFuture) { final CompletableFuture<Revision> future = repo.watch(lastKnownRev, query.path()); parentFuture.whenComplete((res, cause) -> future.completeExceptionally(CANCELLATION_EXCEPTION)); future.thenCompose(newRev -> repo.getOrNull(newRev, query).thenAccept(newResult -> { if (newResult == null || oldResult != null && Objects.equals(oldResult.content(), newResult.content())) { // Entry does not exist or did not change; watch again for more changes. if (!parentFuture.isDone()) { // ... only when the parent future has not been cancelled. watch(repo, query, newRev, oldResult, parentFuture); } } else { parentFuture.complete(newResult); } })).exceptionally(voidFunction(parentFuture::completeExceptionally)); }
@Override protected CompletableFuture<Latest<T>> doWatch(CentralDogma client, String projectName, String repositoryName, Revision lastKnownRevision, long timeoutMillis) { return client.watchFile(projectName, repositoryName, lastKnownRevision, query, timeoutMillis) .thenApply(result -> { if (result == null) { return null; } return new Latest<>(result.revision(), function.apply(result.content())); }); } }
@Override protected CompletableFuture<Latest<T>> doWatch(CentralDogma client, String projectName, String repositoryName, Revision lastKnownRevision, long timeoutMillis) { return client.watchFile(projectName, repositoryName, lastKnownRevision, query, timeoutMillis) .thenApply(result -> { if (result == null) { return null; } return new Latest<>(result.revision(), function.apply(result.content())); }); } }
private static List<MirrorCredential> loadCredentials(Map<String, Entry<?>> entries) throws Exception { final Entry<?> e = entries.get(PATH_CREDENTIALS); if (e == null) { return Collections.emptyList(); } final JsonNode credentialsJson = (JsonNode) e.content(); if (!credentialsJson.isArray()) { throw new RepositoryMetadataException( PATH_CREDENTIALS + " must be an array: " + credentialsJson.getNodeType()); } if (credentialsJson.size() == 0) { return Collections.emptyList(); } final ImmutableList.Builder<MirrorCredential> builder = ImmutableList.builder(); for (JsonNode c : credentialsJson) { final MirrorCredential credential = Jackson.treeToValue(c, MirrorCredential.class); if (credential == null) { throw new RepositoryMetadataException(PATH_CREDENTIALS + " contains null."); } builder.add(credential); } return builder.build(); }
public static Entry convert(com.linecorp.centraldogma.common.Entry<?> entry) { final Entry file = new Entry(entry.path(), convertEntryType(entry.type())); switch (entry.type()) { case JSON: // FIXME(trustin): Inefficiency try { file.setContent(Jackson.writeValueAsString(entry.content())); } catch (JsonProcessingException e) { throw new UncheckedIOException(e); } break; case TEXT: file.setContent((String) entry.content()); break; case DIRECTORY: break; default: throw new IllegalArgumentException("unsupported entry type: " + entry.type()); } return file; }
public static <T> EntryDto<T> convert(Repository repository, Entry<T> entry) { requireNonNull(entry, "entry"); if (entry.hasContent()) { return convert(repository, entry.path(), entry.type(), entry.content()); } return convert(repository, entry.path(), entry.type()); }
public static <T> EntryDto<T> convert(Repository repository, Entry<T> entry) { requireNonNull(entry, "entry"); if (entry.hasContent()) { return convert(repository, entry.path(), entry.type(), entry.content()); } return convert(repository, entry.path(), entry.type()); }
public static <T> EntryDto<T> convert(Repository repository, Entry<T> entry) { requireNonNull(entry, "entry"); if (entry.hasContent()) { return convert(repository, entry.path(), entry.type(), entry.content()); } return convert(repository, entry.path(), entry.type()); }
@Test(timeout = 10000) public void testWatchWithIdentityQuery() throws Exception { final Revision rev1 = repo.commit(HEAD, 0L, Author.UNKNOWN, SUMMARY, textUpserts[0]).join(); final CompletableFuture<Entry<String>> f = repo.watch(rev1, Query.ofText(textPaths[0])); final Revision rev2 = repo.commit(HEAD, 0L, Author.UNKNOWN, SUMMARY, textPatches[1]).join(); final Entry<String> res = f.get(3, TimeUnit.SECONDS); assertThat(res.revision()).isEqualTo(rev2); assertThat(res.type()).isEqualTo(EntryType.TEXT); // Text must be sanitized so that the last line ends with \n. assertThat(res.content()).isEqualTo(textUpserts[1].content() + '\n'); }
@Test public void testGetFilesWithDirectory() throws Exception { final String testRootWithoutSlash = TEST_ROOT.substring(0, TEST_ROOT.length() - 1); final Map<String, Entry<?>> files = rule.client().getFiles( rule.project(), rule.repo1(), Revision.HEAD, testRootWithoutSlash + ", " + TEST_ROOT + '*').join(); assertThat(files).hasSize(NUM_FILES + 1); final Entry<?> dir = files.get(testRootWithoutSlash); assertThat(dir.type()).isEqualTo(EntryType.DIRECTORY); assertThat(dir.path()).isEqualTo(testRootWithoutSlash); assertThat(dir.hasContent()).isFalse(); assertThatThrownBy(dir::content).isInstanceOf(IllegalStateException.class); files.values().forEach(f -> { if (f.type() != EntryType.DIRECTORY) { assertThatJson(f.content()).isEqualTo("{}"); } }); }
@Test public void testRename() throws Exception { repo.commit(HEAD, 0L, Author.UNKNOWN, SUMMARY, jsonUpserts[0]).join(); // Rename without content modification. repo.commit(HEAD, 0L, Author.UNKNOWN, SUMMARY, Change.ofRename(jsonPaths[0], jsonPaths[1])).join(); assertThat(repo.exists(HEAD, jsonPaths[0]).join()).isFalse(); assertThat(repo.exists(HEAD, jsonPaths[1]).join()).isTrue(); assertThat(repo.exists(HEAD, jsonPaths[2]).join()).isFalse(); assertThatJson(repo.get(HEAD, jsonPaths[1]).join().content()) .isEqualTo(jsonUpserts[0].content()); // Rename with content modification. repo.commit(HEAD, 0L, Author.UNKNOWN, SUMMARY, Change.ofRename(jsonPaths[1], jsonPaths[2]), Change.ofJsonPatch(jsonPaths[2], jsonPatches[1].content()), Change.ofJsonPatch(jsonPaths[2], jsonPatches[2].content())).join(); assertThat(repo.exists(HEAD, jsonPaths[0]).join()).isFalse(); assertThat(repo.exists(HEAD, jsonPaths[1]).join()).isFalse(); assertThat(repo.exists(HEAD, jsonPaths[2]).join()).isTrue(); assertThatJson(repo.get(HEAD, jsonPaths[2]).join().content()) .isEqualTo(jsonUpserts[2].content()); }
@Test public void testGetFiles() throws Exception { final Revision headRev = rule.client().normalizeRevision( rule.project(), rule.repo1(), Revision.HEAD).join(); final Map<String, Entry<?>> files = rule.client().getFiles( rule.project(), rule.repo1(), Revision.HEAD, TEST_ROOT + "*.json").join(); assertThat(files).hasSize(NUM_FILES); files.values().forEach(f -> { assertThat(f.revision()).isEqualTo(headRev); assertThatJson(f.content()).isEqualTo("{}"); }); }