Java源码示例:org.eclipse.che.api.core.ServerException
示例1
@Override
public List<BitbucketPullRequest> getRepositoryPullRequests(String owner, String repositorySlug)
throws ServerException, IOException, BitbucketException {
final List<BitbucketPullRequest> pullRequests = new ArrayList<>();
BitbucketServerPullRequestsPage pullRequestsPage = null;
do {
final String url =
urlTemplates.pullRequestUrl(owner, repositorySlug)
+ (pullRequestsPage != null
? "?start=" + valueOf(pullRequestsPage.getNextPageStart())
: "");
pullRequestsPage = getBitbucketPage(this, url, BitbucketServerPullRequestsPage.class);
pullRequests.addAll(
pullRequestsPage
.getValues()
.stream()
.map(BitbucketServerDTOConverter::convertToBitbucketPullRequest)
.collect(Collectors.toList()));
} while (!pullRequestsPage.isIsLastPage());
return pullRequests;
}
示例2
@PUT
@Path("/pullrequests/{account}/{collection}/{project}/{repository}/{pullRequest}")
@Consumes(APPLICATION_JSON)
public MicrosoftPullRequest updatePullRequest(
@PathParam("account") String account,
@PathParam("collection") String collection,
@PathParam("project") String project,
@PathParam("repository") String repository,
@PathParam("pullRequest") String pullRequestId,
MicrosoftPullRequest pullRequest)
throws IOException, ServerException, UnauthorizedException {
final String repositoryId =
microsoftVstsRestClient.getRepository(account, collection, project, repository).getId();
return microsoftVstsRestClient.updatePullRequests(
account, collection, repositoryId, pullRequestId, pullRequest);
}
示例3
@Test(expectedExceptions = ServerException.class)
public void shouldNotRepeatContainerInspectionOnCheckContainerStatusIfInspectionExceptionDiffers()
throws Exception {
// given
when(dockerConnector.inspectContainer(anyString()))
.thenThrow(new DockerException("test exception", 500))
.thenReturn(containerInfo);
// when
try {
createInstanceFromRecipe();
} finally {
// then
verify(dockerConnector).inspectContainer(anyString());
}
}
示例4
/**
* Execute workspace cleanUp script.
*
* @param workspace to cleanUp files.
* @throws IOException in case I/O error.
* @throws ServerException in case internal server error.
*/
@Override
public void clear(Workspace workspace) throws IOException, ServerException {
File wsFolder =
workspaceIdHashLocationFinder.calculateDirPath(backupsRootDir, workspace.getId());
CommandLine commandLine = new CommandLine(workspaceCleanUpScript, wsFolder.getAbsolutePath());
try {
execute(commandLine.asArray(), cleanUpTimeOut);
} catch (InterruptedException | TimeoutException e) {
throw new ServerException(
format(
"Failed to delete workspace files by path: '%s' for workspace with id: '%s'",
wsFolder.getAbsolutePath(), workspace.getId()),
e);
}
}
示例5
@Override
public List<BitbucketPullRequest> getRepositoryPullRequests(String owner, String repositorySlug)
throws ServerException, IOException, BitbucketException {
final List<BitbucketPullRequest> pullRequests = new ArrayList<>();
BitbucketPullRequestsPage pullRequestsPage = newDto(BitbucketPullRequestsPage.class);
do {
final String nextPageUrl = pullRequestsPage.getNext();
final String url =
nextPageUrl == null ? urlTemplates.pullRequestUrl(owner, repositorySlug) : nextPageUrl;
pullRequestsPage = getBitbucketPage(this, url, BitbucketPullRequestsPage.class);
pullRequests.addAll(pullRequestsPage.getValues());
} while (pullRequestsPage.getNext() != null);
return pullRequests;
}
示例6
@Override
protected void checkContainerIsRunning(String container) throws IOException, ServerException {
// Sometimes Swarm doesn't see newly created containers for several seconds.
// Here we retry operation to ensure that such behavior of Swarm doesn't affect the product.
try {
super.checkContainerIsRunning(container);
} catch (ContainerNotFoundException e) {
try {
Thread.sleep(SWARM_WAIT_BEFORE_REPEAT_WORKAROUND_TIME_MS);
super.checkContainerIsRunning(container);
} catch (InterruptedException ignored) {
// throw original error
throw e;
}
}
}
示例7
@Override
public List<BitbucketRepository> getRepositoryForks(String owner, String repositorySlug)
throws IOException, BitbucketException, ServerException {
final List<BitbucketRepository> repositories = new ArrayList<>();
BitbucketRepositoriesPage repositoryPage = newDto(BitbucketRepositoriesPage.class);
do {
final String nextPageUrl = repositoryPage.getNext();
final String url =
nextPageUrl == null ? urlTemplates.forksUrl(owner, repositorySlug) : nextPageUrl;
repositoryPage = getBitbucketPage(this, url, BitbucketRepositoriesPage.class);
repositories.addAll(repositoryPage.getValues());
} while (repositoryPage.getNext() != null);
return repositories;
}
示例8
@Test(
expectedExceptions = ServerException.class,
expectedExceptionsMessageRegExp =
"Sync port is not exposed in ws-machine. Workspace projects syncing is not possible"
)
public void backupShouldThrowErrorIfMachineDoesNotContainServerWithSshPort() throws Exception {
// given
when(machineRuntimeInfo.getServers())
.thenReturn(
singletonMap(
"23/tcp",
new ServerImpl("ref", "proto", "127.0.0.1:" + PUBLISHED_SSH_PORT, null, null)));
backupManager.backupWorkspace(WORKSPACE_ID);
// when
backupManager.backupWorkspace(WORKSPACE_ID);
}
示例9
/**
* Update a given factory
*
* @param factory the factory to update
* @return the updated factory or null if an error occurred during the call to 'updateFactory'
* @throws ServerException
*/
public FactoryDto updateFactory(final FactoryDto factory) throws ServerException {
final String factoryId = factory.getId();
final String url =
fromUri(baseUrl)
.path(FactoryService.class)
.path(FactoryService.class, "updateFactory")
.build(factoryId)
.toString();
FactoryDto newFactory;
HttpJsonRequest httpJsonRequest =
httpJsonRequestFactory.fromUrl(url).usePutMethod().setBody(factory);
try {
HttpJsonResponse response = httpJsonRequest.request();
newFactory = response.asDto(FactoryDto.class);
} catch (IOException | ApiException e) {
LOG.error(e.getLocalizedMessage(), e);
throw new ServerException(e.getLocalizedMessage());
}
return newFactory;
}
示例10
@Override
public void onEvent(InviteCreatedEvent event) {
if (event.getInitiatorId() != null) {
Invite invite = event.getInvite();
try {
sendEmail(event.getInitiatorId(), invite);
} catch (ServerException e) {
LOG.warn(
"Error while processing email invite to {} with id {} for user. Cause: {}",
invite.getDomainId(),
invite.getInstanceId(),
invite.getEmail(),
e.getLocalizedMessage());
}
}
}
示例11
@Test(
expectedExceptions = ServerException.class,
expectedExceptionsMessageRegExp =
"Sync port is not exposed in ws-machine. Workspace projects syncing is not possible"
)
public void restoreShouldThrowErrorIfSshPortIsNotPublishedInContainer() throws Exception {
// given
NetworkSettings networkSettings = new NetworkSettings();
networkSettings.setPorts(
singletonMap("23/tcp", singletonList(new PortBinding().withHostPort(PUBLISHED_SSH_PORT))));
ContainerInfo containerInfo = new ContainerInfo();
containerInfo.setNetworkSettings(networkSettings);
ContainerState containerState = new ContainerState();
containerState.setRunning(true);
containerInfo.setState(containerState);
when(docker.inspectContainer(eq(CONTAINER_ID))).thenReturn(containerInfo);
// when
backupManager.restoreWorkspaceBackup(WORKSPACE_ID, CONTAINER_ID, NODE_HOST);
}
示例12
@POST
@Consumes(APPLICATION_JSON)
@ApiOperation(
value = "Invite unregistered user by email " + "or update permissions for already invited user",
notes = "Invited user will receive email notification only on invitation creation"
)
@ApiResponses({
@ApiResponse(code = 204, message = "The invitation successfully created/updated"),
@ApiResponse(code = 400, message = "Missed required parameters, parameters are not valid"),
@ApiResponse(code = 409, message = "User with specified email is already registered"),
@ApiResponse(code = 500, message = "Internal server error occurred")
})
public void invite(@ApiParam(value = "The invite to store", required = true) InviteDto inviteDto)
throws BadRequestException, NotFoundException, ConflictException, ServerException {
checkArgument(inviteDto != null, "Invite required");
checkArgument(!isNullOrEmpty(inviteDto.getEmail()), "Email required");
checkArgument(!isNullOrEmpty(inviteDto.getDomainId()), "Domain id required");
checkArgument(!isNullOrEmpty(inviteDto.getInstanceId()), "Instance id required");
checkArgument(!inviteDto.getActions().isEmpty(), "One or more actions required");
emailValidator.validateUserMail(inviteDto.getEmail());
inviteManager.store(inviteDto);
}
示例13
/**
* Returns the list of active pull request in given repository. Generates html url for each pull
* requests
*
* @param repositoryId the id of the repository
* @throws IOException when any io error occurs
* @throws ServerException when server responds with unexpected code
* @throws UnauthorizedException when user in not authorized to call this method
*/
public List<MicrosoftPullRequest> getPullRequests(
String account, String collection, String project, String repository, String repositoryId)
throws IOException, ServerException, UnauthorizedException {
return doGet(
templates.pullRequestsUrl(account, collection, repositoryId),
MicrosoftPullRequestList.class)
.getValue()
.stream()
.peek(
pr ->
pr.setHtmlUrl(
templates.pullRequestHtmlUrl(
account,
collection,
project,
repository,
String.valueOf(pr.getPullRequestId()))))
.collect(Collectors.toList());
}
示例14
/**
* Stores (create or updates) invite.
*
* <p>It also send email invite on initial invite creation.
*
* @param invite invite to store
* @throws ConflictException when user is specified email is already registered
* @throws ServerException when any other error occurs during invite storing
*/
@Transactional(rollbackOn = {RuntimeException.class, ServerException.class})
public void store(Invite invite) throws NotFoundException, ConflictException, ServerException {
requireNonNull(invite, "Required non-null invite");
String domainId = invite.getDomainId();
if (!OrganizationDomain.DOMAIN_ID.equals(domainId)
&& !WorkspaceDomain.DOMAIN_ID.equals(domainId)) {
throw new ConflictException("Invitations for specified domain are not supported");
}
permissionsManager.checkActionsSupporting(domainId, invite.getActions());
try {
userManager.getByEmail(invite.getEmail());
throw new ConflictException("User with specified id is already registered");
} catch (NotFoundException ignored) {
}
Optional<InviteImpl> existingInvite = inviteDao.store(new InviteImpl(invite));
if (!existingInvite.isPresent()) {
Subject currentSubject = EnvironmentContext.getCurrent().getSubject();
eventService.publish(
new InviteCreatedEvent(
currentSubject.isAnonymous() ? null : currentSubject.getUserId(), invite));
}
}
示例15
/**
* Get configured 'work item created' webhook for given account, host and collection
*
* @param host the VSTS host
* @param account the VSTS account
* @param collection the VSTS collection
* @return the webhook configured for given account, host and collection or null if no webhook is
* configured
* @throws ServerException
*/
private Optional<WorkItemCreatedWebhook> getWorkItemCreatedWebhook(
final String host, final String account, final String collection) throws ServerException {
final List webhooks = getVSTSWebhooks(WORK_ITEM_CREATED_WEBHOOK);
WorkItemCreatedWebhook webhook = null;
for (Object o : webhooks) {
final WorkItemCreatedWebhook w = (WorkItemCreatedWebhook) o;
final String webhookHost = w.getHost();
final String webhookAccount = w.getAccount();
final String webhookCollection = w.getCollection();
if (host.equals(webhookHost)
&& account.equals(webhookAccount)
&& collection.equals(webhookCollection)) {
webhook = w;
break;
}
}
return Optional.ofNullable(webhook);
}
示例16
@Transactional
protected Optional<InviteImpl> doCreate(InviteImpl invite) throws ServerException {
EntityManager manager = managerProvider.get();
InviteImpl existing = null;
try {
final InviteImpl result =
getEntity(invite.getDomainId(), invite.getInstanceId(), invite.getEmail());
existing = new InviteImpl(result);
result.getActions().clear();
result.getActions().addAll(invite.getActions());
} catch (NotFoundException n) {
manager.persist(invite);
}
manager.flush();
return Optional.ofNullable(existing);
}
示例17
private Optional<Factory> findExistingFailedFactory(String repositoryUrl, String commitId)
throws NotFoundException, ServerException {
return getUserFactories()
.stream()
.filter(
f ->
f.getWorkspace()
.getProjects()
.stream()
.anyMatch(
project ->
repositoryUrl.equals(project.getSource().getLocation())
&& commitId.equals(
project.getSource().getParameters().get("commitId"))))
.findAny();
}
示例18
/**
* Copy files of workspace into backup storage and cleanup them in container.
*
* @param workspaceId id of workspace to backup
* @param containerId id of container that contains data
* @param nodeHost host of a node where container is running
* @throws EnvironmentException if backup failed due to abnormal state of machines in environment
* @throws ServerException if any other error occurs
*/
public void backupWorkspaceAndCleanup(String workspaceId, String containerId, String nodeHost)
throws ServerException, EnvironmentException {
try {
String destPath =
workspaceIdHashLocationFinder.calculateDirPath(backupsRootDir, workspaceId).toString();
// if sync agent is not in machine port parameter is not used
int syncPort = getSyncPort(containerId);
String srcUserName = getUserInfo(workspaceId, containerId).name;
backupAndCleanupInsideLock(
workspaceId, projectFolderPath, nodeHost, syncPort, srcUserName, destPath);
} catch (IOException e) {
throw new ServerException(e.getLocalizedMessage(), e);
} finally {
// remove lock in case exception prevent removing it in regular place to prevent resources
// leak
// and blocking further WS start
workspacesBackupLocks.remove(workspaceId);
// clear user info cache
workspacesMachinesUsersInfo.remove(workspaceId);
}
}
示例19
/**
* Returns user id, group id and username in container. This method caches info about users and on
* second and subsequent calls cached value will be returned.
*
* @param workspaceId id of workspace
* @param containerId id of container
* @return {@code User} object with id, groupId and username filled
* @throws IOException if connection to container fails
* @throws ServerException if other error occurs
*/
private User getUserInfo(String workspaceId, String containerId)
throws IOException, ServerException {
Map<String, User> workspaceMachinesUserInfo = workspacesMachinesUsersInfo.get(workspaceId);
if (workspaceMachinesUserInfo == null) {
workspaceMachinesUserInfo = new HashMap<>();
workspacesMachinesUsersInfo.put(workspaceId, workspaceMachinesUserInfo);
}
User user = workspaceMachinesUserInfo.get(containerId);
if (user == null) {
user = getUserInfoWithinContainer(workspaceId, containerId);
workspaceMachinesUserInfo.put(containerId, user);
}
return user;
}
示例20
/**
* Find a factory
*
* @param factoryName the name of the factory
* @param userId the id of the user that owns the factory
* @return the expected factory or null if an error occurred during the call to 'getFactory'
* @throws ServerException
*/
public List<FactoryDto> findFactory(final String factoryName, final String userId)
throws ServerException {
String url =
fromUri(baseUrl)
.path(FactoryService.class)
.path(FactoryService.class, "getFactoryByAttribute")
.build()
.toString();
List<FactoryDto> factories;
HttpJsonRequest httpJsonRequest =
httpJsonRequestFactory
.fromUrl(url)
.useGetMethod()
.addQueryParam("name", factoryName)
.addQueryParam("creator.userId", userId);
try {
HttpJsonResponse response = httpJsonRequest.request();
factories = response.asList(FactoryDto.class);
} catch (IOException | ApiException e) {
LOG.error(e.getLocalizedMessage(), e);
throw new ServerException(e.getLocalizedMessage());
}
return factories;
}
示例21
/**
* One of the checks in {@link #checkSystemRamLimitAndPropagateStart(WorkspaceCallback)} is needed
* to deny starting workspace, if system RAM limit exceeded. This check may be slow because it is
* based on request to swarm for memory amount allocated on all nodes, but it can't be performed
* more than specified times at the same time, and the semaphore is used to control that. The
* semaphore is a trade off between speed and risk to exceed system RAM limit. In the worst case
* specified number of permits to start workspace can happen at the same time after the actually
* system limit allows to start only one workspace, all permits will be allowed to start
* workspace. If more than specified number of permits to start workspace happens, they will wait
* in a queue. limits.workspace.start.throughput property configures how many permits can be
* handled at the same time.
*/
@VisibleForTesting
<T extends WorkspaceImpl> T checkSystemRamLimitAndPropagateLimitedThroughputStart(
WorkspaceCallback<T> callback) throws ServerException, NotFoundException, ConflictException {
if (startSemaphore == null) {
return checkSystemRamLimitAndPropagateStart(callback);
} else {
try {
startSemaphore.acquire();
return checkSystemRamLimitAndPropagateStart(callback);
} catch (InterruptedException e) {
currentThread().interrupt();
throw new ServerException(e.getMessage(), e);
} finally {
startSemaphore.release();
}
}
}
示例22
@Override
protected long getIdleTimeout(String workspaceId) throws NotFoundException, ServerException {
WorkspaceImpl workspace = workspaceManager.getWorkspace(workspaceId);
Account account = accountManager.getByName(workspace.getNamespace());
List<? extends Resource> availableResources =
resourceUsageManager.getAvailableResources(account.getId());
Optional<? extends Resource> timeoutOpt =
availableResources
.stream()
.filter(resource -> TimeoutResourceType.ID.equals(resource.getType()))
.findAny();
if (timeoutOpt.isPresent()) {
return timeoutOpt.get().getAmount() * 60 * 1000;
} else {
return -1;
}
}
示例23
private Factory createFailedFactory(String factoryId, String repositoryUrl, String commitId)
throws ConflictException, ServerException, NotFoundException {
FactoryImpl factory = (FactoryImpl) factoryManager.getById(factoryId);
factory.setName(factory.getName() + "_" + commitId);
factory
.getWorkspace()
.getProjects()
.stream()
.filter(project -> project.getSource().getLocation().equals(repositoryUrl))
.forEach(
project -> {
Map<String, String> parameters = project.getSource().getParameters();
parameters.remove("branch");
parameters.put("commitId", commitId);
});
return factoryManager.saveFactory(factory);
}
示例24
@Test
public void shouldRespond500IfOtherErrorOccursOnSetupPass() throws Exception {
when(recoveryStorage.isValid(UUID)).thenReturn(true);
when(recoveryStorage.get(UUID)).thenReturn(USER_EMAIL);
when(userManager.getByEmail(USER_EMAIL)).thenReturn(user);
doThrow(new ServerException("test")).when(userManager).update(any(User.class));
Response response =
given()
.formParam("uuid", UUID)
.formParam("password", NEW_PASSWORD)
.when()
.post(SERVICE_PATH + "/setup");
assertEquals(response.statusCode(), 500);
assertEquals(
unwrapDto(response, ServiceError.class).getMessage(),
"Unable to setup password. Please contact support.");
verify(recoveryStorage, times(1)).remove(UUID);
verify(userManager).update(any(User.class));
}
示例25
@Override
public void launch(Instance machine, Agent agent) throws ServerException, AgentStartException {
super.launch(machine, agent);
DockerNode node = (DockerNode) machine.getNode();
DockerInstance dockerMachine = (DockerInstance) machine;
try {
node.bindWorkspace();
} catch (EnvironmentException e) {
throw new AgentStartException(
format(
"Agent '%s' start failed because of an error with underlying environment. Error: %s",
agent.getId(), e.getLocalizedMessage()));
}
LOG.info(
"Docker machine has been deployed. "
+ "ID '{}'. Workspace ID '{}'. "
+ "Container ID '{}'. RAM '{}'. Node host '{}'. Node IP '{}'",
machine.getId(),
machine.getWorkspaceId(),
dockerMachine.getContainer(),
dockerMachine.getConfig().getLimits().getRam(),
node.getHost(),
node.getIp());
}
示例26
private String getResponseBody(final String endpoint, final String keycloakToken)
throws ServerException, UnauthorizedException, ForbiddenException, NotFoundException,
ConflictException, BadRequestException, IOException {
HttpJsonResponse response =
httpJsonRequestFactory
.fromUrl(endpoint)
.setMethod("GET")
.setAuthorizationHeader("Bearer " + keycloakToken)
.request();
return response.asString();
}
示例27
/** Return user's {@link GithubToken} from the fabric8 auth github endpoint. */
public GithubToken getGithubToken()
throws ServerException, ForbiddenException, NotFoundException, UnauthorizedException,
BadRequestException, IOException {
String response = doRequest(githubTokenEndpoint, HttpMethod.GET, null);
return gson.fromJson(response, GithubToken.class);
}
示例28
@Test(
expectedExceptions = {ServerException.class},
expectedExceptionsMessageRegExp = SERVER_EXCEPTION_MESSAGE_WHEN_SPEC_IS_NULL)
public void throwWhenEditorSpecIsNull() throws Exception {
when(pluginFQNParser.parsePluginFQN(any())).thenReturn(PLUGIN_FQN);
doReturn(pluginMeta).when(yamlDownloader).getYamlResponseAndParse(any());
doReturn(null).when(pluginMeta).getSpec();
service =
new CdnSupportService(
commandRunner, pluginFQNParser, yamlDownloader, DEFAULT_REGISTRY_URL, EDITOR_REF);
service.getPaths();
}
示例29
@GET
@Path("/repository/{account}/{collection}/{project}/{repository}")
@Produces(APPLICATION_JSON)
public MicrosoftRepository getRepository(
@PathParam("account") String account,
@PathParam("collection") String collection,
@PathParam("project") String project,
@PathParam("repository") String repository)
throws IOException, ServerException, UnauthorizedException {
return microsoftVstsRestClient.getRepository(account, collection, project, repository);
}
示例30
@Override
public BitbucketRepository getRepository(String owner, String repositorySlug)
throws IOException, BitbucketException, ServerException {
final String response = getJson(this, urlTemplates.repositoryUrl(owner, repositorySlug));
return convertToBitbucketRepository(
parseJsonResponse(response, BitbucketServerRepository.class));
}