Search in sources :

Example 66 with ServerException

use of org.eclipse.che.api.core.ServerException in project che by eclipse.

the class SshManager method generatePair.

/**
     * Generates and stores ssh pair for specified user.
     *
     * @param owner
     *         the id of the user who will be the owner of the ssh pair
     * @param service
     *         service name pf ssh pair
     * @param name
     *         name of pair
     * @return instance of generated ssh pair
     * @throws ConflictException
     *         when given ssh pair cannot be generated or created
     * @throws ServerException
     *         when any other error occurs during ssh pair generating or creating
     */
public SshPairImpl generatePair(String owner, String service, String name) throws ServerException, ConflictException {
    KeyPair keyPair;
    try {
        keyPair = KeyPair.genKeyPair(genJSch, 2, 2048);
    } catch (JSchException e) {
        throw new ServerException("Failed to generate ssh pair.", e);
    }
    ByteArrayOutputStream privateBuff = new ByteArrayOutputStream();
    keyPair.writePrivateKey(privateBuff);
    ByteArrayOutputStream publicBuff = new ByteArrayOutputStream();
    keyPair.writePublicKey(publicBuff, null);
    final SshPairImpl generatedSshPair = new SshPairImpl(owner, service, name, publicBuff.toString(), privateBuff.toString());
    sshDao.create(generatedSshPair);
    return generatedSshPair;
}
Also used : JSchException(com.jcraft.jsch.JSchException) KeyPair(com.jcraft.jsch.KeyPair) ServerException(org.eclipse.che.api.core.ServerException) SshPairImpl(org.eclipse.che.api.ssh.server.model.impl.SshPairImpl) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 67 with ServerException

use of org.eclipse.che.api.core.ServerException in project che by eclipse.

the class JpaSshDao method remove.

@Override
public void remove(String owner, String service, String name) throws ServerException, NotFoundException {
    requireNonNull(owner);
    requireNonNull(service);
    requireNonNull(name);
    try {
        doRemove(owner, service, name);
    } catch (RuntimeException e) {
        throw new ServerException(e);
    }
}
Also used : ServerException(org.eclipse.che.api.core.ServerException)

Example 68 with ServerException

use of org.eclipse.che.api.core.ServerException in project che by eclipse.

the class UserManager method create.

/**
     * Creates new user and his profile.
     *
     * @param newUser
     *         created user
     * @throws NullPointerException
     *         when {@code newUser} is null
     * @throws ConflictException
     *         when user with such name/email/alias already exists
     * @throws ServerException
     *         when any other error occurs
     */
public User create(User newUser, boolean isTemporary) throws ConflictException, ServerException {
    requireNonNull(newUser, "Required non-null user");
    if (reservedNames.contains(newUser.getName().toLowerCase())) {
        throw new ConflictException(String.format("Username '%s' is reserved", newUser.getName()));
    }
    final String userId = newUser.getId() != null ? newUser.getId() : generate("user", ID_LENGTH);
    final UserImpl user = new UserImpl(userId, newUser.getEmail(), newUser.getName(), firstNonNull(newUser.getPassword(), generate("", PASSWORD_LENGTH)), newUser.getAliases());
    try {
        userDao.create(user);
        profileDao.create(new ProfileImpl(user.getId()));
        preferencesDao.setPreferences(user.getId(), ImmutableMap.of("temporary", Boolean.toString(isTemporary), "codenvy:created", Long.toString(currentTimeMillis())));
    } catch (ConflictException | ServerException x) {
        // NOTE: this logic must be replaced with transaction management
        try {
            userDao.remove(user.getId());
            profileDao.remove(user.getId());
            preferencesDao.remove(user.getId());
        } catch (ServerException rollbackEx) {
            LOG.error(format("An attempt to clean up resources due to user creation failure was unsuccessful." + "Now the system may be in inconsistent state. " + "User with id '%s' must not exist", user.getId()), rollbackEx);
        }
        throw x;
    }
    return user;
}
Also used : ServerException(org.eclipse.che.api.core.ServerException) ConflictException(org.eclipse.che.api.core.ConflictException) ProfileImpl(org.eclipse.che.api.user.server.model.impl.ProfileImpl) UserImpl(org.eclipse.che.api.user.server.model.impl.UserImpl)

Example 69 with ServerException

use of org.eclipse.che.api.core.ServerException in project che by eclipse.

the class JpaPreferenceDao method setPreferences.

@Override
public void setPreferences(String userId, Map<String, String> preferences) throws ServerException {
    requireNonNull(userId);
    requireNonNull(preferences);
    final PreferenceEntity prefs = new PreferenceEntity(userId, preferences);
    if (preferences.isEmpty()) {
        remove(userId);
    } else {
        try {
            doSetPreference(prefs);
        } catch (RuntimeException ex) {
            throw new ServerException(ex.getLocalizedMessage(), ex);
        }
    }
}
Also used : ServerException(org.eclipse.che.api.core.ServerException)

Example 70 with ServerException

use of org.eclipse.che.api.core.ServerException in project che by eclipse.

the class RecipeDaoTest method shouldNotRemoveRecipeWhenSubscriberThrowsExceptionOnRecipeRemoving.

@Test(dependsOnMethods = "shouldGetRecipeById")
public void shouldNotRemoveRecipeWhenSubscriberThrowsExceptionOnRecipeRemoving() throws Exception {
    final RecipeImpl recipe = recipes.get(0);
    CascadeEventSubscriber<BeforeRecipeRemovedEvent> subscriber = mockCascadeEventSubscriber();
    doThrow(new ServerException("error")).when(subscriber).onCascadeEvent(any());
    eventService.subscribe(subscriber, BeforeRecipeRemovedEvent.class);
    try {
        recipeDao.remove(recipe.getId());
        fail("RecipeDao#remove had to throw server exception");
    } catch (ServerException ignored) {
    }
    assertEquals(recipeDao.getById(recipe.getId()), recipe);
    eventService.unsubscribe(subscriber, BeforeRecipeRemovedEvent.class);
}
Also used : ServerException(org.eclipse.che.api.core.ServerException) RecipeImpl(org.eclipse.che.api.machine.server.recipe.RecipeImpl) BeforeRecipeRemovedEvent(org.eclipse.che.api.machine.server.event.BeforeRecipeRemovedEvent) Test(org.testng.annotations.Test)

Aggregations

ServerException (org.eclipse.che.api.core.ServerException)143 IOException (java.io.IOException)51 ForbiddenException (org.eclipse.che.api.core.ForbiddenException)37 NotFoundException (org.eclipse.che.api.core.NotFoundException)37 ConflictException (org.eclipse.che.api.core.ConflictException)32 File (java.io.File)22 Test (org.testng.annotations.Test)20 ArrayList (java.util.ArrayList)19 VirtualFile (org.eclipse.che.api.vfs.VirtualFile)19 List (java.util.List)15 WorkspaceImpl (org.eclipse.che.api.workspace.server.model.impl.WorkspaceImpl)15 Map (java.util.Map)13 Transactional (com.google.inject.persist.Transactional)12 BadRequestException (org.eclipse.che.api.core.BadRequestException)12 InputStream (java.io.InputStream)11 Unlocker (org.eclipse.che.commons.lang.concurrent.Unlocker)10 String.format (java.lang.String.format)9 Path (javax.ws.rs.Path)9 Produces (javax.ws.rs.Produces)9 HashMap (java.util.HashMap)8