private InputStream getInputStream(boolean raw) throws Exception {
    if (raw) {
      return IOUtils.toInputStream(xmlEditorModel.getObject(), "utf-8");
    }
    File newFile = null;
    try {
      // Create new file
      MidPointApplication application = getMidpointApplication();
      WebApplicationConfiguration config = application.getWebApplicationConfiguration();
      File folder = new File(config.getImportFolder());
      if (!folder.exists() || !folder.isDirectory()) {
        folder.mkdir();
      }

      FileUpload uploadedFile = getUploadedFile();
      newFile = new File(folder, uploadedFile.getClientFileName());
      // Check new file, delete if it already exists
      if (newFile.exists()) {
        newFile.delete();
      }
      // Save file

      newFile.createNewFile();
      uploadedFile.writeTo(newFile);

      InputStreamReader reader = new InputStreamReader(new FileInputStream(newFile), "utf-8");
      return new ReaderInputStream(reader, reader.getEncoding());
    } finally {
      if (newFile != null) {
        FileUtils.deleteQuietly(newFile);
      }
    }
  }
  private void importReportFromFilePerformed(AjaxRequestTarget target) {
    OperationResult result = new OperationResult(OPERATION_IMPORT_REPORT);

    FileUploadField file =
        (FileUploadField)
            get(createComponentPath(ID_MAIN_FORM, ID_INPUT, ID_INPUT_FILE, ID_FILE_INPUT));
    final FileUpload uploadedFile = file.getFileUpload();
    if (uploadedFile == null) {
      error(getString("PageNewReport.message.nullFile"));
      target.add(getFeedbackPanel());

      return;
    }

    InputStream stream = null;
    File newFile = null;
    try {
      // Create new file
      MidPointApplication application = getMidpointApplication();
      WebApplicationConfiguration config = application.getWebApplicationConfiguration();
      File folder = new File(config.getImportFolder());
      if (!folder.exists() || !folder.isDirectory()) {
        folder.mkdir();
      }

      newFile = new File(folder, uploadedFile.getClientFileName());
      // Check new file, delete if it already exists
      if (newFile.exists()) {
        newFile.delete();
      }
      // Save file
      //            Task task = createSimpleTask(OPERATION_IMPORT_FILE);
      newFile.createNewFile();
      uploadedFile.writeTo(newFile);

      InputStreamReader reader = new InputStreamReader(new FileInputStream(newFile), "utf-8");
      //            reader.
      stream = new ReaderInputStream(reader, reader.getEncoding());
      byte[] reportIn = IOUtils.toByteArray(stream);

      setResponsePage(new PageReport(new ReportDto(Base64.encodeBase64(reportIn))));
    } catch (Exception ex) {
      result.recordFatalError("Couldn't import file.", ex);
      LoggingUtils.logException(LOGGER, "Couldn't import file", ex);
    } finally {
      if (stream != null) {
        IOUtils.closeQuietly(stream);
      }
      if (newFile != null) {
        FileUtils.deleteQuietly(newFile);
      }
    }

    showResult(result);
    target.add(getFeedbackPanel());
  }
 public void validate(IValidatable<List<FileUpload>> pValidatables) {
   for (FileUpload image : pValidatables.getValue()) {
     String extension = FilenameUtils.getExtension(image.getClientFileName());
     if (extension != null && !extensions.contains(extension.toLowerCase())) {
       ValidationError error = new ValidationError();
       error.addKey("WrongExtensionValidator");
       error.setVariable("extensions", extensions.toString());
       pValidatables.error(error);
     }
   }
 }
Exemple #4
0
    /** @see org.apache.wicket.markup.html.form.Form#onSubmit() */
    @Override
    protected void onSubmit() {
      final List<FileUpload> uploads = fileUploadField.getFileUploads();
      if (uploads != null) {
        for (FileUpload upload : uploads) {
          // Create a new file
          File newFile = new File(getUploadFolder(), upload.getClientFileName());

          // Check new file, delete if it already existed
          checkFileExists(newFile);
          try {
            // Save to new file
            newFile.createNewFile();
            upload.writeTo(newFile);

            UploadPage.this.info("saved file: " + upload.getClientFileName());
          } catch (Exception e) {
            throw new IllegalStateException("Unable to write file", e);
          }
        }
      }
    }
Exemple #5
0
 public static Map<String, File> uploadFileFromUploadField(FileUpload fileUpload, int userId) {
   Application.debug("uploadFileFromUploadField", userId);
   Map<String, File> tmpDir = new HashMap<String, File>();
   String fileName = fileUpload.getClientFileName();
   UUID uuid = UUID.randomUUID();
   File dir = new File(Configuration.getTmp_dir() + "/" + uuid);
   if (!dir.mkdir()) {
     Application.error("cannot create directory uploadFileFromUploadField :" + uuid);
     return null;
   } else {
     String name = fileName;
     File tmpFile = new File(Configuration.getTmp_dir() + "/" + uuid + "/" + name);
     try {
       fileUpload.writeTo(tmpFile);
     } catch (IOException e) {
       Application.error(e);
       return null;
     }
     tmpDir.put(Configuration.getTmp_dir() + "/" + uuid, tmpFile);
     return tmpDir;
   }
 }