Package org.apache.commons.fileupload

Examples of org.apache.commons.fileupload.FileUploadException


            out.write(uuids.toString().substring(0, uuids.length() - 1));
            out.flush();
        } catch (Exception e) {
            String msg = "File upload FAILED. File may be non-existent or invalid.";
            log.error(msg, e);
            throw new FileUploadException(msg, e);
        } finally {
            if (out != null) {
                out.close();
            }
        }
View Full Code Here


            throws FileUploadException, IOException {
        String axis2Repo = ServerConfiguration.getInstance().
                getFirstProperty(ServerConfiguration.AXIS2_CONFIG_REPO_LOCATION);
        if (CarbonUtils.isURL(axis2Repo)) {
            String msg = "You are not permitted to upload jars to URL repository";
            throw new FileUploadException(msg);
        }

        String tmpDir = (String) configurationContext.getProperty(ServerConstants.WORK_DIR);
        String uuid = String.valueOf(System.currentTimeMillis() + Math.random());
        tmpDir = tmpDir + File.separator + "artifacts" + File.separator + uuid + File.separator;
        File tmpDirFile = new File(tmpDir);
        if (!tmpDirFile.exists() && !tmpDirFile.mkdirs()) {
            log.warn("Could not create " + tmpDirFile.getAbsolutePath());
        }

        response.setContentType("text/html; charset=utf-8");

        ServletRequestContext servletRequestContext = new ServletRequestContext(request);
        boolean isMultipart =
                ServletFileUpload.isMultipartContent(servletRequestContext);
        if (isMultipart) {
            PrintWriter out = null;
            try {
                out = response.getWriter();
                // Create a new file upload handler
                List items = parseRequest(servletRequestContext);
                // Process the uploaded items
                for (Iterator iter = items.iterator(); iter.hasNext();) {
                    FileItem item = (FileItem) iter.next();
                    if (!item.isFormField()) {
                        String fileName = item.getName();
                        String fileExtension = fileName;
                        fileExtension = fileExtension.toLowerCase();

                        String fileNameOnly = getFileName(fileName);
                        File uploadedFile;

                        String fieldName = item.getFieldName();

                        if (fieldName != null && fieldName.equals("jarResource")) {
                            if (fileExtension.endsWith(".jar")) {
                                File servicesDir =
                                        new File(tmpDir + File.separator + uploadDirName, "lib");
                                if (!servicesDir.exists() && !servicesDir.mkdirs()){
                                    log.warn("Could not create " + servicesDir.getAbsolutePath());
                                }
                                uploadedFile = new File(servicesDir, fileNameOnly);
                                item.write(uploadedFile);
                            }
                        } else {
                            File servicesDir = new File(tmpDir, uploadDirName);
                            if (!servicesDir.exists() && !servicesDir.mkdirs()) {
                                log.warn("Could not create " + servicesDir.getAbsolutePath());
                            }
                            uploadedFile = new File(servicesDir, fileNameOnly);
                            item.write(uploadedFile);
                        }
                    }
                }

                //First lets filter for jar resources
                String repo = configurationContext.getAxisConfiguration().getRepository().getPath();

                //Writing the artifacts to the proper location
                String parent = repo + File.separator + uploadDirName;
                File mainDir = new File(tmpDir + File.separator + uploadDirName);
                File libDir = new File(mainDir, "lib");
                File[] resourceLibFile =
                        FileManipulator.getMatchingFiles(libDir.getAbsolutePath(), null, "jar");


                for (File src : resourceLibFile) {
                    File dst = new File(parent, "lib");
                    String[] files = libDir.list();
                    for (String file : files) {
                        copyFile(src, new File(dst, file));
                    }
                }

                for (String extension : extensions) {
                    File[] mainFiles =
                            FileManipulator.getMatchingFiles(mainDir.getAbsolutePath(), null, extension);
                    for (File mainFile : mainFiles) {
                        File dst = new File(parent);
                        String[] files = mainDir.list();
                        for (String file : files) {
                            File f = new File(dst, file);
                            if (!f.isDirectory()) {
                                copyFile(mainFile, f);
                            }
                        }

                    }
                }
                response.sendRedirect(getContextRoot(request) + "/carbon/service-mgt/index.jsp?message=Files have been uploaded "
                                      + "successfully. This page will be auto refreshed shortly with "
                                      + "the status of the created " + utilityString + " service"); //TODO: why do we redirect to service-mgt ???
                return true;
            } catch (RuntimeException e) {
                throw e;
            } catch (Exception e) {
                String msg = "File upload failed";
                log.error(msg, e);
                throw new FileUploadException(msg, e);
            } finally {
                if (out != null) {
                    out.close();
                }
            }
View Full Code Here

                            } else if (item.getSize() > 0) {
                                // process form file field (input type="file").
                                // String filename = FilenameUtils.getName(item
                                // .getName());
                                if (item.getSize() > 1024 * 1024 * 10) {
                                    throw new FileUploadException(
                                            "Current maximum upload size is 10MB");
                                }
                                String name = item.getFieldName();
                                if ("icon".equals(name) || "logo".equals(name)) {
                                    args.add("--" + name);
View Full Code Here

      int result = client.executeMethod(postMethod);
      if (result == HttpServletResponse.SC_OK) {
        return true;
      } else {
        String error = "File Upload Error HttpCode=" + result;
        throw new FileUploadException(error);
      }
    } catch (IOException e) {
      throw new FileUploadException("File Upload Error for:"
          + e.getMessage());
    } finally {
      postMethod.releaseConnection();
      monitor.done();
    }
View Full Code Here

            } else {
                /*
                 * For whatever reason we cannot write the
                 * file to disk.
                 */
                throw new FileUploadException(
                    "Cannot write uploaded file to disk!");
            }
        }
    }
View Full Code Here

            } else {
                /*
                 * For whatever reason we cannot write the
                 * file to disk.
                 */
                throw new FileUploadException(
                    "Cannot write uploaded file to disk!");
            }
        }
    }
View Full Code Here

     *
     * @throws Exception if an error occurs.
     */
    public void write(File file) throws Exception {
      // PAG: we just can't write files
      throw new FileUploadException(
       "Cannot write uploaded file to disk!");
    }
View Full Code Here

 
  @Test
  public void doNothingWhenFileUploadExceptionOccurs() throws Exception {
    interceptor = new CommonsUploadMultipartInterceptor(request, parameters, config, validator, mockCreator);
   
    when(mockUpload.parseRequest(request)).thenThrow(new FileUploadException());
     
    interceptor.intercept(stack, method, instance);
  }
View Full Code Here

  @Test
  public void handleValidatorMessageWhenFileUploadExceptionOccurs() throws Exception {
    interceptor = new CommonsUploadMultipartInterceptor(request, parameters, config, validator, mockCreator);

    when(mockUpload.parseRequest(request)).thenThrow(new FileUploadException());
    interceptor.intercept(stack, method, instance);

    verify(validator).add(any(I18nMessage.class));
  }
View Full Code Here

            } else {
                /*
                 * For whatever reason we cannot write the
                 * file to disk.
                 */
                throw new FileUploadException(
                    "Cannot write uploaded file to disk!");
            }
        }
    }
View Full Code Here

TOP

Related Classes of org.apache.commons.fileupload.FileUploadException

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.