Example usage for org.apache.commons.fileupload FileUploadException FileUploadException

List of usage examples for org.apache.commons.fileupload FileUploadException FileUploadException

Introduction

In this page you can find the example usage for org.apache.commons.fileupload FileUploadException FileUploadException.

Prototype

public FileUploadException() 

Source Link

Document

Constructs a new FileUploadException without message.

Usage

From source file:edu.um.umflix.stubs.UploadServletStub.java

protected List<FileItem> getFileItems(HttpServletRequest request) throws FileUploadException {
    if (error.equals("fileUpload"))
        new FileUploadException();

    File file = null;//  w w  w.j  a  v  a  2 s .c o  m
    FileItem item1 = null;
    List<FileItem> list = new ArrayList<FileItem>();
    for (int i = 0; i < fields.length; i++) {
        FileItem item = Mockito.mock(FileItem.class);
        Mockito.when(item.isFormField()).thenReturn(true);
        Mockito.when(item.getFieldName()).thenReturn(fields[i]);
        if (fields[i].equals("premiere") || fields[i].equals("endDate") || fields[i].equals("startDate")) {
            Mockito.when(item.getString()).thenReturn("2013-06-01");
        } else {
            Mockito.when(item.getString()).thenReturn("11");
        }
        if (fields[i].equals("premiere") && error.equals("date"))
            Mockito.when(item.getString()).thenReturn("11");
        if (fields[i].equals("clipduration0")) {
            Mockito.when(item.getString()).thenReturn("01:22:01");
        }
        list.add(item);
    }
    try {
        file = File.createTempFile("aaaa", "aaaatest");
        item1 = Mockito.mock(FileItem.class);
        Mockito.when(item1.getInputStream()).thenReturn(new FileInputStream(file));

    } catch (IOException e) {
        e.printStackTrace();
    }

    list.add(item1);
    FileItem token = Mockito.mock(FileItem.class);
    Mockito.when(token.isFormField()).thenReturn(true);
    Mockito.when(token.getFieldName()).thenReturn("token");
    if (error.equals("token")) {
        Mockito.when(token.getString()).thenReturn("invalidToken");
    } else
        Mockito.when(token.getString()).thenReturn("validToken");

    list.add(token);

    return list;
}

From source file:com.epam.wilma.webapp.config.servlet.stub.upload.MultiPartFormUploadServletTest.java

@Test
public void testDoGetShouldSendErrorResponseWhenRequestParsingFailedByTheServletFileUpload()
        throws ServletException, IOException, FileUploadException {
    //GIVEN//from   w  w  w  .  java2s . c  om
    given(request.getMethod()).willReturn("POST");
    given(fileUpload.parseRequest(request)).willThrow(new FileUploadException());
    //WHEN
    underTest.doGet(request, response);
    //THEN
    verify(response).setContentType("text/html");
    verify(response).setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
    verify(writer).write(Matchers.anyString());
}

From source file:com.sifiso.dvs.util.DocFileUtil.java

public ResponseDTO downloadPDF(HttpServletRequest request, PlatformUtil platformUtil)
        throws FileUploadException {
    logger.log(Level.INFO, "######### starting PDF DOWNLOAD process\n\n");
    ResponseDTO resp = new ResponseDTO();
    InputStream stream = null;//  w w  w  .  j  av  a2s .  c  o m
    File rootDir;
    try {
        rootDir = dvsProperties.getDocumentDir();
        logger.log(Level.INFO, "rootDir - {0}", rootDir.getAbsolutePath());
        if (!rootDir.exists()) {
            rootDir.mkdir();
        }
    } catch (Exception ex) {
        logger.log(Level.SEVERE, "Properties file problem", ex);
        resp.setMessage("Server file unavailable. Please try later");
        resp.setStatusCode(114);

        return resp;
    }

    PatientfileDTO dto = null;
    Gson gson = new Gson();
    File clientDir = null, surgeryDir = null, doctorDir = null;
    try {
        ServletFileUpload upload = new ServletFileUpload();
        FileItemIterator iter = upload.getItemIterator(request);
        while (iter.hasNext()) {
            FileItemStream item = iter.next();
            String name = item.getFieldName();
            stream = item.openStream();
            if (item.isFormField()) {
                if (name.equalsIgnoreCase("JSON")) {
                    String json = Streams.asString(stream);
                    if (json != null) {
                        logger.log(Level.INFO, "picture with associated json: {0}", json);
                        dto = gson.fromJson(json, PatientfileDTO.class);
                        if (dto != null) {
                            surgeryDir = createSurgeryFileDirectory(rootDir, surgeryDir,
                                    dto.getDoctor().getSurgeryID());
                            if (dto.getDoctorID() != null) {
                                doctorDir = createDoctorDirectory(surgeryDir, doctorDir, dto.getDoctorID());
                                if (dto.getClientID() != null) {
                                    clientDir = createClientDirectory(doctorDir, clientDir);
                                }
                            }

                        }
                    } else {
                        logger.log(Level.WARNING, "JSON input seems pretty fucked up! is NULL..");
                    }
                }
            } else {
                File imageFile = null;
                if (dto == null) {
                    continue;
                }
                DateTime dt = new DateTime();
                String fileName = "";

                if (dto.getClientID() != null) {
                    fileName = "client" + dto.getClientID() + ".pdf";
                }

                imageFile = new File(clientDir, fileName);

                writeFile(stream, imageFile);
                resp.setStatusCode(0);
                resp.setMessage("Photo downloaded from mobile app ");
                //add database
                System.out.println("filepath: " + imageFile.getAbsolutePath());

            }
        }

    } catch (FileUploadException | IOException | JsonSyntaxException ex) {
        logger.log(Level.SEVERE, "Servlet failed on IOException, images NOT uploaded", ex);
        throw new FileUploadException();
    }

    return resp;
}

From source file:com.sifiso.dvs.util.PhotoUtil.java

public ResponseDTO downloadPhotos(HttpServletRequest request, PlatformUtil platformUtil)
        throws FileUploadException {
    logger.log(Level.INFO, "######### starting PHOTO DOWNLOAD process\n\n");
    ResponseDTO resp = new ResponseDTO();
    InputStream stream = null;//  w  w w.j av  a2s.c o m
    File rootDir;
    try {
        rootDir = dvsProperties.getImageDir();
        logger.log(Level.INFO, "rootDir - {0}", rootDir.getAbsolutePath());
        if (!rootDir.exists()) {
            rootDir.mkdir();
        }
    } catch (Exception ex) {
        logger.log(Level.SEVERE, "Properties file problem", ex);
        resp.setMessage("Server file unavailable. Please try later");
        resp.setStatusCode(114);

        return resp;
    }

    PhotoUploadDTO dto = null;
    Gson gson = new Gson();
    File doctorFileDir = null, surgeryDir = null;
    try {
        ServletFileUpload upload = new ServletFileUpload();
        FileItemIterator iter = upload.getItemIterator(request);
        while (iter.hasNext()) {
            FileItemStream item = iter.next();
            String name = item.getFieldName();
            stream = item.openStream();
            if (item.isFormField()) {
                if (name.equalsIgnoreCase("JSON")) {
                    String json = Streams.asString(stream);
                    if (json != null) {
                        logger.log(Level.INFO, "picture with associated json: {0}", json);
                        dto = gson.fromJson(json, PhotoUploadDTO.class);
                        if (dto != null) {
                            surgeryDir = createSurgeryFileDirectory(rootDir, surgeryDir, dto.getSurgeryID());
                            if (dto.getDoctorID() > 0) {
                                doctorFileDir = createDoctorDirectory(surgeryDir, doctorFileDir,
                                        dto.getDoctorID());
                            }

                        }
                    } else {
                        logger.log(Level.WARNING, "JSON input seems pretty fucked up! is NULL..");
                    }
                }
            } else {
                File imageFile = null;
                if (dto == null) {
                    continue;
                }
                DateTime dt = new DateTime();
                String fileName = "";
                if (dto.isIsFullPicture()) {
                    fileName = "f" + dt.getMillis() + ".jpg";
                } else {
                    fileName = "t" + dt.getMillis() + ".jpg";
                }
                if (dto.getPatientfileID() != null) {
                    if (dto.isIsFullPicture()) {
                        fileName = "f" + dto.getPatientfileID() + ".jpg";
                    } else {
                        fileName = "t" + dto.getPatientfileID() + ".jpg";
                    }
                }

                //
                switch (dto.getPictureType()) {
                case PhotoUploadDTO.FILES_DOCTOR:
                    imageFile = new File(doctorFileDir, fileName);
                    break;
                case PhotoUploadDTO.FILES_SURGERY:
                    imageFile = new File(surgeryDir, fileName);
                }

                writeFile(stream, imageFile);
                resp.setStatusCode(0);
                resp.setMessage("Photo downloaded from mobile app ");
                //add database
                System.out.println("filepath: " + imageFile.getAbsolutePath());
                //create uri
                /*int index = imageFile.getAbsolutePath().indexOf("monitor_images");
                 if (index > -1) {
                 String uri = imageFile.getAbsolutePath().substring(index);
                 System.out.println("uri: " + uri);
                 dto.setUri(uri);
                 }
                 dto.setDateUploaded(new Date());
                 if (dto.isIsFullPicture()) {
                 dto.setThumbFlag(null);
                 } else {
                 dto.setThumbFlag(1);
                 }
                 dataUtil.addPhotoUpload(dto);*/

            }
        }

    } catch (FileUploadException | IOException | JsonSyntaxException ex) {
        logger.log(Level.SEVERE, "Servlet failed on IOException, images NOT uploaded", ex);
        throw new FileUploadException();
    }

    return resp;
}

From source file:br.com.caelum.vraptor.observer.upload.CommonsUploadMultipartObserverTest.java

@Test
public void doNothingWhenFileUploadExceptionOccurs() throws Exception {

    when(observer.createServletFileUpload(config)).thenReturn(servletFileUpload);
    when(servletFileUpload.parseRequest(request)).thenThrow(new FileUploadException());

    observer.upload(event, request, config, validator);
}

From source file:com.sifiso.yazisa.util.PhotoUtil.java

public ResponseDTO downloadPhotos(HttpServletRequest request, PlatformUtil platformUtil)
        throws FileUploadException {
    logger.log(Level.INFO, "######### starting PHOTO DOWNLOAD process\n\n");
    ResponseDTO resp = new ResponseDTO();
    InputStream stream = null;// www.j  av a2  s.c om
    File rootDir;
    try {
        rootDir = YazisaProperties.getImageDir();
        logger.log(Level.INFO, "rootDir - {0}", rootDir.getAbsolutePath());
        if (!rootDir.exists()) {
            rootDir.mkdir();
        }
    } catch (Exception ex) {
        logger.log(Level.SEVERE, "Properties file problem", ex);
        resp.setMessage("Server file unavailable. Please try later");
        resp.setStatusCode(114);

        return resp;
    }

    PhotoUploadDTO dto = null;
    Gson gson = new Gson();
    File schoolDir = null, classDir = null, parentDir = null, teacherDir = null, studentDir = null;
    try {
        ServletFileUpload upload = new ServletFileUpload();
        FileItemIterator iter = upload.getItemIterator(request);
        while (iter.hasNext()) {
            FileItemStream item = iter.next();
            String name = item.getFieldName();
            stream = item.openStream();
            if (item.isFormField()) {
                if (name.equalsIgnoreCase("JSON")) {
                    String json = Streams.asString(stream);
                    if (json != null) {
                        logger.log(Level.INFO, "picture with associated json: {0}", json);
                        dto = gson.fromJson(json, PhotoUploadDTO.class);
                        if (dto != null) {
                            if (dto.getSchoolID() > 0) {
                                schoolDir = createSchoolDirectory(rootDir, schoolDir, dto.getSchoolID());
                            }

                            if (dto.getClassID() > 0) {
                                classDir = createClassDirectory(schoolDir, classDir, dto.getClassID());
                            }
                            if (dto.getParentID() > 0) {
                                parentDir = createParentDirectory(schoolDir, parentDir);
                            }
                            if (dto.getTeacherID() > 0) {
                                teacherDir = createTeacherDirectory(schoolDir, teacherDir);
                            }
                            if (dto.getStudentID() > 0) {
                                studentDir = createStudentDirectory(rootDir, studentDir);
                            }
                        }
                    } else {
                        logger.log(Level.WARNING, "JSON input seems pretty fucked up! is NULL..");
                    }
                }
            } else {
                File imageFile = null;
                if (dto == null) {
                    continue;
                }
                DateTime dt = new DateTime();
                String fileName = "";
                if (dto.isIsFullPicture()) {
                    fileName = "f" + dt.getMillis() + ".jpg";
                } else {
                    fileName = "t" + dt.getMillis() + ".jpg";
                }
                if (dto.getSchoolID() != null) {
                    if (dto.isIsFullPicture()) {
                        fileName = "f" + dto.getSchoolID() + ".jpg";
                    } else {
                        fileName = "t" + dto.getSchoolID() + ".jpg";
                    }
                }
                if (dto.getSchoolID() != null) {
                    if (dto.getTeacherID() != null) {
                        if (dto.isIsFullPicture()) {
                            fileName = "f" + dto.getTeacherID() + ".jpg";
                        } else {
                            fileName = "t" + dto.getTeacherID() + ".jpg";
                        }
                    }
                }
                if (dto.getSchoolID() != null) {
                    if (dto.getClassID() != null) {
                        if (dto.isIsFullPicture()) {
                            fileName = "f" + dto.getClassID() + "-" + new Date().getYear() + ".jpg";
                        } else {
                            fileName = "t" + dto.getClassID() + "-" + new Date().getYear() + ".jpg";
                        }
                    }
                }
                if (dto.getSchoolID() != null) {
                    if (dto.getParentID() != null) {
                        if (dto.isIsFullPicture()) {
                            fileName = "f" + dto.getParentID() + ".jpg";
                        } else {
                            fileName = "t" + dto.getParentID() + ".jpg";
                        }
                    }
                }

                if (dto.getStudentID() != null) {
                    if (dto.isIsFullPicture()) {
                        fileName = "f" + dto.getStudentID() + ".jpg";
                    } else {
                        fileName = "t" + dto.getStudentID() + ".jpg";
                    }
                }

                //
                switch (dto.getPictureType()) {
                case PhotoUploadDTO.SCHOOL_IMAGE:
                    imageFile = new File(schoolDir, fileName);
                    break;
                case PhotoUploadDTO.CLASS_IMAGE:
                    imageFile = new File(classDir, fileName);
                    break;
                case PhotoUploadDTO.PARENT_IMAGE:
                    imageFile = new File(parentDir, fileName);
                    break;
                case PhotoUploadDTO.STUDENT_IMAGE:
                    imageFile = new File(studentDir, fileName);
                    break;
                }

                writeFile(stream, imageFile);
                resp.setStatusCode(0);
                resp.setMessage("Photo downloaded from mobile app ");
                //add database
                System.out.println("filepath: " + imageFile.getAbsolutePath());
                //create uri
                /*int index = imageFile.getAbsolutePath().indexOf("monitor_images");
                 if (index > -1) {
                 String uri = imageFile.getAbsolutePath().substring(index);
                 System.out.println("uri: " + uri);
                 dto.setUri(uri);
                 }
                 dto.setDateUploaded(new Date());
                 if (dto.isIsFullPicture()) {
                 dto.setThumbFlag(null);
                 } else {
                 dto.setThumbFlag(1);
                 }
                 dataUtil.addPhotoUpload(dto);*/

            }
        }

    } catch (FileUploadException | IOException | JsonSyntaxException ex) {
        logger.log(Level.SEVERE, "Servlet failed on IOException, images NOT uploaded", ex);
        throw new FileUploadException();
    }

    return resp;
}

From source file:br.com.caelum.vraptor.observer.upload.CommonsUploadMultipartObserverTest.java

@Test
public void handleValidatorMessageWhenFileUploadExceptionOccurs() throws Exception {
    when(observer.createServletFileUpload(config)).thenReturn(servletFileUpload);
    when(servletFileUpload.parseRequest(request)).thenThrow(new FileUploadException());

    observer.upload(event, request, config, validator);

    verify(validator).add(any(I18nMessage.class));
}

From source file:org.jbpm.formbuilder.server.RESTFileServiceTest.java

private RESTFileService createSaveFileMockService(final byte[] bstream, final String fileName,
        final Class<?> exceptionType, final boolean isMultipart) {
    RESTFileService restService = new RESTFileService() {
        @Override//ww w  .ja va 2  s .  co m
        protected boolean isMultipart(HttpServletRequest request) {
            return isMultipart;
        }

        @Override
        protected ServletFileUpload createFileUpload() {
            return null;
        }

        @Override
        protected List<?> parseFiles(HttpServletRequest request, ServletFileUpload upload)
                throws FileUploadException {
            if (exceptionType != null && exceptionType.equals(FileUploadException.class)) {
                throw new FileUploadException();
            }
            List<FileItem> retval = new ArrayList<FileItem>();
            retval.add(new DiskFileItem("fieldName", "application/octet-stream", true, fileName, 0, null));
            return retval;
        }

        @Override
        protected byte[] readItem(FileItem item) throws IOException {
            if (exceptionType != null && exceptionType.equals(IOException.class)) {
                throw new IOException("mock io error");
            }
            return bstream;
        }
    };
    return restService;
}