use of org.apache.commons.fileupload.FileItem in project che by eclipse.
the class StackService method uploadIcon.
@POST
@Path("/{id}/icon")
@Consumes(MULTIPART_FORM_DATA)
@Produces(TEXT_PLAIN)
@GenerateLink(rel = LINK_REL_UPLOAD_ICON)
@ApiOperation(value = "Upload icon for required stack", notes = "This operation can be performed only by authorized stack owner")
@ApiResponses({ @ApiResponse(code = 200, message = "Image was successfully uploaded"), @ApiResponse(code = 400, message = "Missed required parameters, parameters are not valid"), @ApiResponse(code = 403, message = "The user does not have access upload image for stack with required id"), @ApiResponse(code = 404, message = "The stack doesn't exist"), @ApiResponse(code = 500, message = "Internal server error occurred") })
public Response uploadIcon(@ApiParam("The image for stack") final Iterator<FileItem> formData, @ApiParam("The stack id") @PathParam("id") final String id) throws NotFoundException, ServerException, BadRequestException, ForbiddenException, ConflictException {
if (formData.hasNext()) {
FileItem fileItem = formData.next();
StackIcon stackIcon = new StackIcon(fileItem.getName(), fileItem.getContentType(), fileItem.get());
StackImpl stack = stackDao.getById(id);
stack.setStackIcon(stackIcon);
stackDao.update(stack);
}
return Response.ok().build();
}
use of org.apache.commons.fileupload.FileItem in project che by eclipse.
the class SshService method createPair.
@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.TEXT_HTML)
@GenerateLink(rel = Constants.LINK_REL_CREATE_PAIR)
public Response createPair(Iterator<FileItem> formData) throws BadRequestException, ServerException, ConflictException {
String service = null;
String name = null;
String privateKey = null;
String publicKey = null;
while (formData.hasNext()) {
FileItem item = formData.next();
String fieldName = item.getFieldName();
switch(fieldName) {
case "service":
service = item.getString();
break;
case "name":
name = item.getString();
break;
case "privateKey":
privateKey = item.getString();
break;
case "publicKey":
publicKey = item.getString();
break;
default:
}
}
requiredNotNull(service, "Service name required");
requiredNotNull(name, "Name required");
if (privateKey == null && publicKey == null) {
throw new BadRequestException("Key content was not provided.");
}
sshManager.createPair(new SshPairImpl(getCurrentUserId(), service, name, publicKey, privateKey));
// through specific of html form that doesn't invoke complete submit handler
return Response.ok("", MediaType.TEXT_HTML).build();
}
use of org.apache.commons.fileupload.FileItem in project che by eclipse.
the class ProjectService method uploadFile.
/* --------------------------------------------------------------------------- */
/* TODO check "upload" methods below, they were copied from old VFS as is */
/* --------------------------------------------------------------------------- */
private static Response uploadFile(VirtualFile parent, Iterator<FileItem> formData) throws ForbiddenException, ConflictException, ServerException {
try {
FileItem contentItem = null;
String name = null;
boolean overwrite = false;
while (formData.hasNext()) {
FileItem item = formData.next();
if (!item.isFormField()) {
if (contentItem == null) {
contentItem = item;
} else {
throw new ServerException("More then one upload file is found but only one should be. ");
}
} else if ("name".equals(item.getFieldName())) {
name = item.getString().trim();
} else if ("overwrite".equals(item.getFieldName())) {
overwrite = Boolean.parseBoolean(item.getString().trim());
}
}
if (contentItem == null) {
throw new ServerException("Cannot find file for upload. ");
}
if (name == null || name.isEmpty()) {
name = contentItem.getName();
}
try {
try {
parent.createFile(name, contentItem.getInputStream());
} catch (ConflictException e) {
if (!overwrite) {
throw new ConflictException("Unable upload file. Item with the same name exists. ");
}
parent.getChild(org.eclipse.che.api.vfs.Path.of(name)).updateContent(contentItem.getInputStream(), null);
}
} catch (IOException ioe) {
throw new ServerException(ioe.getMessage(), ioe);
}
return Response.ok("", MediaType.TEXT_HTML).build();
} catch (ForbiddenException | ConflictException | ServerException e) {
HtmlErrorFormatter.sendErrorAsHTML(e);
// never thrown
throw e;
}
}
use of org.apache.commons.fileupload.FileItem in project pinot by linkedin.
the class PinotSchemaRestletResource method getUploadContents.
private File getUploadContents() throws Exception {
File dataFile = null;
// 1/ Create a factory for disk-based file items
Representation rep;
final DiskFileItemFactory factory = new DiskFileItemFactory();
// 2/ Create a new file upload handler based on the Restlet
// FileUpload extension that will parse Restlet requests and
// generates FileItems.
final RestletFileUpload upload = new RestletFileUpload(factory);
final List<FileItem> items;
// 3/ Request is parsed by the handler which generates a
// list of FileItems
items = upload.parseRequest(getRequest());
final Iterator<FileItem> it = items.iterator();
while (it.hasNext() && dataFile == null) {
final FileItem fi = it.next();
if (fi.getFieldName() != null) {
dataFile = new File(tempDir, fi.getFieldName() + "-" + System.currentTimeMillis());
fi.write(dataFile);
}
}
return dataFile;
}
use of org.apache.commons.fileupload.FileItem in project JessMA by ldcsaa.
the class FileUploader method parseFileItems.
private Result parseFileItems(List<FileItem> items, FileNameGenerator fnGenerator, String absolutePath, String encoding, List<FileItemInfo> fiis) {
for (FileItem item : items) {
if (item.isFormField())
parseFormField(item, encoding);
else {
if (GeneralHelper.isStrEmpty(item.getName()))
continue;
Result result = parseFileField(item, absolutePath, fnGenerator, fiis);
if (result != Result.SUCCESS) {
reset();
cause = new InvalidParameterException(String.format("file '%s' not accepted", item.getName()));
return result;
}
}
}
return Result.SUCCESS;
}
Aggregations