Search in sources :

Example 11 with BadRequestException

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

the class FactoryBaseValidator method validateCurrentTimeBetweenSinceUntil.

/**
     * Validates that factory can be used at present time (used on accept)
     *
     * @param factory
     *         factory to validate
     * @throws BadRequestException
     *         if since date greater than current date<br/>
     * @throws BadRequestException
     *         if until date less than current date<br/>
     */
protected void validateCurrentTimeBetweenSinceUntil(FactoryDto factory) throws BadRequestException {
    final PoliciesDto policies = factory.getPolicies();
    if (policies == null) {
        return;
    }
    final Long since = policies.getSince() == null ? 0L : policies.getSince();
    final Long until = policies.getUntil() == null ? 0L : policies.getUntil();
    if (since != 0 && currentTimeMillis() < since) {
        throw new BadRequestException(FactoryConstants.ILLEGAL_FACTORY_BY_SINCE_MESSAGE);
    }
    if (until != 0 && currentTimeMillis() > until) {
        throw new BadRequestException(FactoryConstants.ILLEGAL_FACTORY_BY_UNTIL_MESSAGE);
    }
}
Also used : BadRequestException(org.eclipse.che.api.core.BadRequestException) PoliciesDto(org.eclipse.che.api.factory.shared.dto.PoliciesDto)

Example 12 with BadRequestException

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

the class FactoryBaseValidator method validateProjects.

/**
     * Validates source parameter of factory.
     *
     * @param factory
     *         factory to validate
     * @throws BadRequestException
     *         when source projects in the factory is invalid
     */
protected void validateProjects(FactoryDto factory) throws BadRequestException {
    for (ProjectConfigDto project : factory.getWorkspace().getProjects()) {
        final String projectName = project.getName();
        if (null != projectName && !PROJECT_NAME_VALIDATOR.matcher(projectName).matches()) {
            throw new BadRequestException("Project name must contain only Latin letters, " + "digits or these following special characters -._.");
        }
        if (project.getPath().indexOf('/', 1) == -1) {
            final String location = project.getSource().getLocation();
            final String parameterLocationName = "project.source.location";
            if (isNullOrEmpty(location)) {
                throw new BadRequestException(format(FactoryConstants.PARAMETRIZED_ILLEGAL_PARAMETER_VALUE_MESSAGE, parameterLocationName, location));
            }
            try {
                URLDecoder.decode(location, "UTF-8");
            } catch (IllegalArgumentException | UnsupportedEncodingException e) {
                throw new BadRequestException(format(FactoryConstants.PARAMETRIZED_ILLEGAL_PARAMETER_VALUE_MESSAGE, parameterLocationName, location));
            }
        }
    }
}
Also used : ProjectConfigDto(org.eclipse.che.api.workspace.shared.dto.ProjectConfigDto) BadRequestException(org.eclipse.che.api.core.BadRequestException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 13 with BadRequestException

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

the class FactoryService method createImage.

/**
     * Creates factory image from input stream.
     * InputStream should be closed manually.
     *
     * @param is
     *         input stream with image data
     * @param mediaType
     *         media type of image
     * @param name
     *         image name
     * @return factory image, if {@param is} has no content then empty factory image will be returned
     * @throws BadRequestException
     *         when factory image exceeded maximum size
     * @throws ServerException
     *         when any server errors occurs
     */
public static FactoryImage createImage(InputStream is, String mediaType, String name) throws BadRequestException, ServerException {
    try {
        final ByteArrayOutputStream out = new ByteArrayOutputStream();
        final byte[] buffer = new byte[1024];
        int read;
        while ((read = is.read(buffer, 0, buffer.length)) != -1) {
            out.write(buffer, 0, read);
            if (out.size() > 1024 * 1024) {
                throw new BadRequestException("Maximum upload size exceeded.");
            }
        }
        if (out.size() == 0) {
            return new FactoryImage();
        }
        out.flush();
        return new FactoryImage(out.toByteArray(), mediaType, name);
    } catch (IOException ioEx) {
        throw new ServerException(ioEx.getLocalizedMessage());
    }
}
Also used : ServerException(org.eclipse.che.api.core.ServerException) BadRequestException(org.eclipse.che.api.core.BadRequestException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Example 14 with BadRequestException

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

the class FactoryService method saveFactory.

@POST
@Consumes(MULTIPART_FORM_DATA)
@Produces(APPLICATION_JSON)
@ApiOperation(value = "Create a new factory based on configuration and factory images", notes = "The field 'factory' is required")
@ApiResponses({ @ApiResponse(code = 200, message = "Factory successfully created"), @ApiResponse(code = 400, message = "Missed required parameters, parameters are not valid"), @ApiResponse(code = 403, message = "The user does not have rights to create factory"), @ApiResponse(code = 409, message = "When factory with given name and creator already exists"), @ApiResponse(code = 500, message = "Internal server error occurred") })
public FactoryDto saveFactory(Iterator<FileItem> formData) throws ForbiddenException, ConflictException, BadRequestException, ServerException {
    try {
        final Set<FactoryImage> images = new HashSet<>();
        FactoryDto factory = null;
        while (formData.hasNext()) {
            final FileItem item = formData.next();
            switch(item.getFieldName()) {
                case ("factory"):
                    {
                        try (InputStream factoryData = item.getInputStream()) {
                            factory = factoryBuilder.build(factoryData);
                        } catch (JsonSyntaxException ex) {
                            throw new BadRequestException("Invalid JSON value of the field 'factory' provided");
                        }
                        break;
                    }
                case ("image"):
                    {
                        try (InputStream imageData = item.getInputStream()) {
                            final FactoryImage image = createImage(imageData, item.getContentType(), NameGenerator.generate(null, 16));
                            if (image.hasContent()) {
                                images.add(image);
                            }
                        }
                        break;
                    }
                default:
            }
        }
        requiredNotNull(factory, "factory configuration");
        processDefaults(factory);
        createValidator.validateOnCreate(factory);
        return injectLinks(asDto(factoryManager.saveFactory(factory, images)), images);
    } catch (IOException ioEx) {
        throw new ServerException(ioEx.getLocalizedMessage(), ioEx);
    }
}
Also used : FileItem(org.apache.commons.fileupload.FileItem) JsonSyntaxException(com.google.gson.JsonSyntaxException) ServerException(org.eclipse.che.api.core.ServerException) InputStream(java.io.InputStream) FactoryDto(org.eclipse.che.api.factory.shared.dto.FactoryDto) BadRequestException(org.eclipse.che.api.core.BadRequestException) IOException(java.io.IOException) HashSet(java.util.HashSet) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Example 15 with BadRequestException

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

the class ProjectManagerWriteTest method shouldThrowBadRequestExceptionAtCreatingBatchProjectsWhenConfigNotContainsPath.

@Test
public void shouldThrowBadRequestExceptionAtCreatingBatchProjectsWhenConfigNotContainsPath() throws Exception {
    //Path is mandatory field for NewProjectConfig
    final SourceStorageDto source = DtoFactory.newDto(SourceStorageDto.class).withLocation("someLocation").withType("importType");
    final NewProjectConfig config = createProjectConfigObject("project", null, BaseProjectType.ID, source);
    final List<NewProjectConfig> configs = new ArrayList<>(1);
    configs.add(config);
    try {
        pm.createBatchProjects(configs, false, new ProjectOutputLineConsumerFactory("ws", 300));
        fail("BadRequestException should be thrown : path field is mandatory");
    } catch (BadRequestException e) {
        assertEquals(0, projectRegistry.getProjects().size());
    }
}
Also used : SourceStorageDto(org.eclipse.che.api.workspace.shared.dto.SourceStorageDto) ArrayList(java.util.ArrayList) BadRequestException(org.eclipse.che.api.core.BadRequestException) NewProjectConfig(org.eclipse.che.api.core.model.project.NewProjectConfig) Test(org.junit.Test)

Aggregations

BadRequestException (org.eclipse.che.api.core.BadRequestException)20 ServerException (org.eclipse.che.api.core.ServerException)9 ConflictException (org.eclipse.che.api.core.ConflictException)7 NotFoundException (org.eclipse.che.api.core.NotFoundException)7 Consumes (javax.ws.rs.Consumes)6 POST (javax.ws.rs.POST)6 ForbiddenException (org.eclipse.che.api.core.ForbiddenException)6 ApiOperation (io.swagger.annotations.ApiOperation)5 ApiResponses (io.swagger.annotations.ApiResponses)5 IOException (java.io.IOException)5 Map (java.util.Map)5 Produces (javax.ws.rs.Produces)5 ArrayList (java.util.ArrayList)4 List (java.util.List)4 GenerateLink (org.eclipse.che.api.core.rest.annotations.GenerateLink)4 Api (io.swagger.annotations.Api)3 ApiParam (io.swagger.annotations.ApiParam)3 ApiResponse (io.swagger.annotations.ApiResponse)3 String.format (java.lang.String.format)3 Collectors.toList (java.util.stream.Collectors.toList)3