io.fabric8.taiga.TaigaClientSupport Maven / Gradle / Ivy
/**
* Copyright 2005-2016 Red Hat, Inc.
*
* Red Hat licenses this file to you under the Apache License, version
* 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
package io.fabric8.taiga;
import io.fabric8.utils.Strings;
import io.fabric8.utils.URLUtils;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
import static io.fabric8.taiga.Projects.addUser;
import static io.fabric8.utils.cxf.WebClients.handle404ByReturningNull;
/**
* Default base class for a TaigaClient implementation
*/
public abstract class TaigaClientSupport {
protected final String address;
protected final String username;
protected final String password;
private TaigaApi api;
private AuthDetailDTO authentication;
private boolean autoCreateProjects = true;
public TaigaClientSupport(String address, String username, String password) {
this.address = address;
this.password = password;
this.username = username;
}
/**
* Returns the project ID for the given slug or null if none could be found
*/
public Long getProjectIdForSlug(String slug) {
ProjectDTO project = getProjectBySlug(slug);
return project != null ? project.getId() : null;
}
/**
* For the given project name try find the project by its slug or create a new project
*/
public ProjectDTO getOrCreateProject(String name) {
return getOrCreateProject(name, null);
}
/**
* Find the project from the given name and optional slug; or create one if its not already existing
*/
public ProjectDTO getOrCreateProject(final String name, final String slugOrNull) {
String slug = validateSlug(slugOrNull, name);
ProjectDTO project = getProjectBySlug(slug);
if (project == null) {
project = new ProjectDTO();
project.setSlug(slug);
project.setName(name);
project.setDescription("Description of project " + name);
// lets default a user
addUser(project, getMe());
return createProject(project);
} else {
return project;
}
}
public ModuleDTO moduleForProject(String slug, TaigaModule module) {
return moduleForProject(slug, module.toModuleKey());
}
public ModuleDTO moduleForProject(String slug, String module) {
Map map = getModulesForProject(slug);
return map.get(module);
}
public ModuleDTO moduleForProject(Long projectId, TaigaModule module) {
return moduleForProject(projectId, module.toModuleKey());
}
public ModuleDTO moduleForProject(Long projectId, String module) {
Map map = getModulesForProject(projectId);
return map.get(module);
}
/**
* Returns the webhook URL for the given module.
*
* The module webhook might not use the correct public host name so lets convert it.
*/
public String getPublicWebhookUrl(ModuleDTO module) {
if (module != null) {
String webhooksUrl = module.getWebhooksUrl();
if (Strings.isNotBlank(webhooksUrl)) {
int idx = webhooksUrl.indexOf("/api/v");
if (idx > 0) {
return URLUtils.pathJoin(getAddress(), webhooksUrl.substring(idx));
}
}
return webhooksUrl;
}
return null;
}
// Delegate of TaigaApi
//-------------------------------------------------------------------------
public ProjectDTO createProject(ProjectDTO dto) {
return getApi().createProject(dto);
}
public List getProjects() {
return getApi().getProjects();
}
public ProjectDTO getProjectById(final String id) {
return handle404ByReturningNull(new Callable() {
@Override
public ProjectDTO call() throws Exception {
return getApi().getProjectById(id);
}
});
}
public ProjectDTO getProjectBySlug(final String slug) {
return handle404ByReturningNull(new Callable() {
@Override
public ProjectDTO call() throws Exception {
return getApi().getProjectBySlug(slug);
}
});
}
public UserDTO getMe() {
return handle404ByReturningNull(new Callable() {
@Override
public UserDTO call() throws Exception {
return getApi().getMe();
}
});
}
public UserDTO getUser(final String id) {
return handle404ByReturningNull(new Callable() {
@Override
public UserDTO call() throws Exception {
return getApi().getUser(id);
}
});
}
public Map getModulesForProject(String slug) {
Long id = getProjectIdForSlug(slug);
if (id != null) {
return getModulesForProject(id);
} else {
return new HashMap<>();
}
}
public Map getModulesForProject(final Long id) {
Map answer = handle404ByReturningNull(new Callable
© 2015 - 2025 Weber Informatics LLC | Privacy Policy