All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.ghunteranderson.nexus.client.ComponentClient Maven / Gradle / Ivy

The newest version!
package com.ghunteranderson.nexus.client;

import java.util.Map.Entry;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Stream;

import javax.ws.rs.NotFoundException;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.GenericType;
import javax.ws.rs.core.MediaType;

import com.ghunteranderson.nexus.model.Component;
import com.ghunteranderson.nexus.model.ComponentQuery;

public class ComponentClient {

	private final WebTarget componentTarget;
	private final WebTarget searchTarget;
	
	public ComponentClient(NexusInstance instance) {
		componentTarget = instance.getWebTarget("/service/rest/v1/components");
		searchTarget = instance.getWebTarget("/service/rest/v1/search");
	}
	
	public Stream findAll(String repository){
		Function> source = token -> {
			return componentTarget
					.queryParam("repository", repository)
					.queryParam("continuationToken", token)
					.request()
					.accept(MediaType.APPLICATION_JSON)
					.get(new PaginatedComponentType());
		};
		
		return new PaginationIterator<>(source).stream();
	}
	
	public Optional findOne(String id){
		try {
			return Optional.of(componentTarget.path(id).request().get(Component.class));
		} catch(NotFoundException ex) {
			return Optional.empty();
		}
	}
	
	public Stream findAll(ComponentQuery query){
		// We will build the web target once and reuse it across each pagination request
		WebTarget target = searchTarget;
		for(Entry param : query.getQueryMap().entrySet()) {
			target = target.queryParam(param.getKey(), param.getValue());
		}
		
		// Adding this because of "effectively final" requirement
		WebTarget finalTarget = target; 
		
		Function> source = token -> {
			return finalTarget.queryParam("continuationToken", token)
					.request()
					.accept(MediaType.APPLICATION_JSON)
					.get(new PaginatedComponentType());
		};
		
		return new PaginationIterator<>(source).stream();
	}
	
	public void delete(String id) {
		componentTarget.path(id).request().delete();
	}
	
	private static class PaginatedComponentType extends GenericType>{}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy