use of javax.ws.rs.core.MediaType.APPLICATION_JSON_TYPE in project linuxtools by eclipse.
the class AbstractRegistry method retrieveTagsFromRegistryV1.
/**
* Retrieves the list of tags for a given repository, assuming that the
* target registry is a registry v2 instance.
*
* @param client
* the client to use
* @param repository
* the repository to look-up
* @return the list of tags for the given repository
* @throws CancellationException
* - if the computation was cancelled
* @throws ExecutionException
* - if the computation threw an exception
* @throws InterruptedException
* - if the current thread was interrupted while waiting
*/
private List<IRepositoryTag> retrieveTagsFromRegistryV1(final Client client, final String repository) throws InterruptedException, ExecutionException {
final GenericType<Map<String, String>> REPOSITORY_TAGS_RESULT_LIST = new GenericType<Map<String, String>>() {
};
final WebTarget queryTagsResource = client.target(getHTTPServerAddress()).path(// $NON-NLS-1$
"v1").path("repositories").path(repository).path(// $NON-NLS-1$ //$NON-NLS-2$
"tags");
return queryTagsResource.request(APPLICATION_JSON_TYPE).async().method(GET, REPOSITORY_TAGS_RESULT_LIST).get().entrySet().stream().map(e -> new RepositoryTag(e.getKey(), e.getValue())).collect(Collectors.toList());
}
use of javax.ws.rs.core.MediaType.APPLICATION_JSON_TYPE in project linuxtools by eclipse.
the class AbstractRegistry method getImages.
@Override
public List<IDockerImageSearchResult> getImages(String term) throws DockerException {
final Client client = ClientBuilder.newClient(DEFAULT_CONFIG);
List<IDockerImageSearchResult> result = new ArrayList<>();
WebTarget queryImagesResource;
if (isVersion2()) {
final GenericType<ImageSearchResultV2> IMAGE_SEARCH_RESULT_LIST = new GenericType<ImageSearchResultV2>() {
};
ImageSearchResultV2 cisr = null;
queryImagesResource = client.target(getHTTPServerAddress()).path("v2").path(// $NON-NLS-1$ //$NON-NLS-2$
"_catalog");
try {
cisr = queryImagesResource.request(APPLICATION_JSON_TYPE).async().method(GET, IMAGE_SEARCH_RESULT_LIST).get();
} catch (InterruptedException | ExecutionException e) {
throw new DockerException(e);
}
List<ImageSearchResult> tmp = cisr.getRepositories().stream().filter(e -> e.name().contains(term)).collect(Collectors.toList());
result.addAll(tmp.stream().map(r -> new DockerImageSearchResult(r.description(), r.official(), r.automated(), r.name(), r.starCount())).collect(Collectors.toList()));
} else {
ImageSearchResultV1 pisr = null;
final GenericType<ImageSearchResultV1> IMAGE_SEARCH_RESULT_LIST = new GenericType<ImageSearchResultV1>() {
};
int page = 0;
try {
while (pisr == null || pisr.getPage() < pisr.getTotalPages()) {
page++;
queryImagesResource = client.target(getHTTPServerAddress()).path("v1").path(// $NON-NLS-1$ //$NON-NLS-2$
"search").queryParam("q", // $NON-NLS-1$
term).queryParam("page", // $NON-NLS-1$
page);
pisr = queryImagesResource.request(APPLICATION_JSON_TYPE).async().method(GET, IMAGE_SEARCH_RESULT_LIST).get();
List<ImageSearchResult> tmp = pisr.getResult();
result.addAll(tmp.stream().map(r -> new DockerImageSearchResult(r.description(), r.official(), r.automated(), r.name(), r.starCount())).collect(Collectors.toList()));
}
} catch (InterruptedException | ExecutionException e) {
throw new DockerException(e);
}
}
return result;
}
use of javax.ws.rs.core.MediaType.APPLICATION_JSON_TYPE in project component-runtime by Talend.
the class Github method load.
public Collection<Contributor> load() {
final String token = "Basic " + Base64.getEncoder().encodeToString((user + ':' + password).getBytes(StandardCharsets.UTF_8));
final Client client = ClientBuilder.newClient().register(new JsonbJaxrsProvider<>());
final WebTarget gravatarBase = client.target(Gravatars.GRAVATAR_BASE);
final ForkJoinPool pool = new ForkJoinPool(Math.max(4, Runtime.getRuntime().availableProcessors() * 8));
try {
return pool.submit(() -> contributors(client, token, "https://api.github.com/repos/talend/component-runtime/contributors").parallel().collect(toMap(e -> normalizeLogin(e.login), identity(), (c1, c2) -> {
c1.contributions += c2.contributions;
return c1;
})).values().stream().map(contributor -> {
if (contributor.url == null) {
try {
final Contributor gravatar = Gravatars.loadGravatar(gravatarBase, contributor.email);
return Contributor.builder().name(contributor.name).commits(contributor.contributions).description(gravatar.getDescription()).gravatar(gravatar.getGravatar()).build();
} catch (final Exception e) {
log.warn(e.getMessage(), e);
return new Contributor(contributor.email, contributor.email, "", Gravatars.url(contributor.email), contributor.contributions);
}
}
final GithubUser user = client.target(contributor.url).request(APPLICATION_JSON_TYPE).header("Authorization", token).get(GithubUser.class);
return Contributor.builder().id(contributor.login).name(ofNullable(user.name).orElse(contributor.name)).description((user.bio == null ? "" : user.bio) + (user.blog != null && !user.blog.trim().isEmpty() && (user.bio == null || !user.bio.contains(user.blog)) ? "\n\nBlog: " + user.blog : "")).commits(contributor.contributions).gravatar(ofNullable(contributor.avatarUrl).orElseGet(() -> {
final String gravatarId = contributor.gravatarId == null || contributor.gravatarId.isEmpty() ? contributor.email : contributor.gravatarId;
try {
final Contributor gravatar = Gravatars.loadGravatar(gravatarBase, gravatarId);
return gravatar.getGravatar();
} catch (final Exception e) {
log.warn(e.getMessage(), e);
return Gravatars.url(gravatarId);
}
})).build();
}).filter(Objects::nonNull).sorted(comparing(Contributor::getCommits).reversed()).collect(toList())).get(15, MINUTES);
} catch (final ExecutionException ee) {
if (WebApplicationException.class.isInstance(ee.getCause())) {
final Response response = WebApplicationException.class.cast(ee.getCause()).getResponse();
if (response != null && response.getEntity() != null) {
log.error(response.readEntity(String.class));
}
}
throw new IllegalStateException(ee.getCause());
} catch (final InterruptedException | TimeoutException e) {
throw new IllegalStateException(e);
} finally {
client.close();
pool.shutdownNow();
}
}
use of javax.ws.rs.core.MediaType.APPLICATION_JSON_TYPE in project component-runtime by Talend.
the class Gravatars method loadGravatar.
public static Contributor loadGravatar(final WebTarget target, final String input) {
final String hash = gravatarHash(input);
final String gravatarUrl = "https://www.gravatar.com/avatar/" + hash + "?s=140";
final Response gravatar = target.path(hash + ".json").request(APPLICATION_JSON_TYPE).get();
return ofNullable(gravatar).filter(r -> r.getStatus() == HttpURLConnection.HTTP_OK).map(r -> r.readEntity(Gravatar.class).getEntry()).map(e -> e[0]).map(e -> Contributor.builder().id(e.getId()).name(ofNullable(e.getName()).map(n -> ofNullable(n.getFormatted()).orElse(ofNullable(n.getGivenName()).orElse("") + ofNullable(n.getFamilyName()).orElse(""))).orElseGet(() -> ofNullable(e.getDisplayName()).orElse(ofNullable(e.getPreferredUsername()).orElse(input)))).description(ofNullable(e.getAboutMe()).orElse("")).gravatar(gravatarUrl).build()).orElse(Contributor.builder().name(input).id(input).description("").gravatar(gravatarUrl).build());
}
use of javax.ws.rs.core.MediaType.APPLICATION_JSON_TYPE in project component-runtime by Talend.
the class ProjectResource method createZip.
@POST
@Path("zip/form")
@Produces("application/zip")
public Response createZip(@FormParam("project") final String compressedModel, @Context final Providers providers) {
final MessageBodyReader<ProjectModel> jsonReader = providers.getMessageBodyReader(ProjectModel.class, ProjectModel.class, NO_ANNOTATION, APPLICATION_JSON_TYPE);
final ProjectModel model;
try (final InputStream gzipInputStream = new ByteArrayInputStream(Base64.getUrlDecoder().decode(compressedModel))) {
model = jsonReader.readFrom(ProjectModel.class, ProjectModel.class, NO_ANNOTATION, APPLICATION_JSON_TYPE, new MultivaluedHashMap<>(), gzipInputStream);
} catch (final IOException e) {
throw new WebApplicationException(Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(new ErrorMessage(e.getMessage())).type(APPLICATION_JSON_TYPE).build());
}
final String filename = ofNullable(model.getArtifact()).orElse("zip") + ".zip";
return Response.ok().entity((StreamingOutput) out -> {
generator.generate(toRequest(model), out);
out.flush();
}).header("Content-Disposition", "inline; filename=" + filename).build();
}
Aggregations