Search in sources :

Example 1 with FileItem

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();
}
Also used : FileItem(org.apache.commons.fileupload.FileItem) StackImpl(org.eclipse.che.api.workspace.server.model.impl.stack.StackImpl) StackIcon(org.eclipse.che.api.workspace.server.stack.image.StackIcon) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) ApiOperation(io.swagger.annotations.ApiOperation) GenerateLink(org.eclipse.che.api.core.rest.annotations.GenerateLink) ApiResponses(io.swagger.annotations.ApiResponses)

Example 2 with FileItem

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();
}
Also used : FileItem(org.apache.commons.fileupload.FileItem) SshPairImpl(org.eclipse.che.api.ssh.server.model.impl.SshPairImpl) BadRequestException(org.eclipse.che.api.core.BadRequestException) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) GenerateLink(org.eclipse.che.api.core.rest.annotations.GenerateLink)

Example 3 with FileItem

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;
    }
}
Also used : FileItem(org.apache.commons.fileupload.FileItem) ForbiddenException(org.eclipse.che.api.core.ForbiddenException) ServerException(org.eclipse.che.api.core.ServerException) ConflictException(org.eclipse.che.api.core.ConflictException) IOException(java.io.IOException)

Example 4 with FileItem

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;
}
Also used : FileItem(org.apache.commons.fileupload.FileItem) RestletFileUpload(org.restlet.ext.fileupload.RestletFileUpload) StringRepresentation(org.restlet.representation.StringRepresentation) Representation(org.restlet.representation.Representation) File(java.io.File) DiskFileItemFactory(org.apache.commons.fileupload.disk.DiskFileItemFactory)

Example 5 with FileItem

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;
}
Also used : FileItem(org.apache.commons.fileupload.FileItem) InvalidParameterException(java.security.InvalidParameterException)

Aggregations

FileItem (org.apache.commons.fileupload.FileItem)165 ServletFileUpload (org.apache.commons.fileupload.servlet.ServletFileUpload)78 DiskFileItemFactory (org.apache.commons.fileupload.disk.DiskFileItemFactory)72 FileUploadException (org.apache.commons.fileupload.FileUploadException)59 File (java.io.File)55 IOException (java.io.IOException)51 ArrayList (java.util.ArrayList)40 HashMap (java.util.HashMap)32 ServletException (javax.servlet.ServletException)30 List (java.util.List)27 InputStream (java.io.InputStream)24 FileItemFactory (org.apache.commons.fileupload.FileItemFactory)23 DiskFileItem (org.apache.commons.fileupload.disk.DiskFileItem)16 Map (java.util.Map)15 UnsupportedEncodingException (java.io.UnsupportedEncodingException)12 ServletRequestContext (org.apache.commons.fileupload.servlet.ServletRequestContext)10 Test (org.junit.Test)10 Iterator (java.util.Iterator)9 FileItemWrap (com.github.bordertech.wcomponents.file.FileItemWrap)8 Locale (java.util.Locale)8