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);
}
}
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));
}
}
}
}
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());
}
}
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);
}
}
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());
}
}
Aggregations