Ejemplo n.º 1
0
  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);
      }
    }
  }
Ejemplo n.º 2
0
  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());
  }
Ejemplo n.º 3
0
 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);
     }
   }
 }
Ejemplo n.º 4
0
 @Override
 public void onSubmit() {
   FileUpload upload = uploadField.getFileUpload();
   if (upload != null) {
     try {
       Reader input =
           new InputStreamReader(
               new Base64.InputStream(upload.getInputStream(), Base64.ENCODE), "utf-8");
       FieldInstance fieldInstance = fieldValueModel.getFieldInstanceModel().getFieldInstance();
       StringWriter swriter = new StringWriter();
       StreamUtils.transfer(input, swriter);
       String value = swriter.toString();
       fieldInstance.addValue(value, getLifeCycleListener());
       fieldValueModel.setExistingValue(value);
       uploaded = true;
     } catch (IOException e) {
       e.printStackTrace();
     }
   }
 }
Ejemplo n.º 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;
   }
 }
Ejemplo n.º 6
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);
          }
        }
      }
    }
Ejemplo n.º 7
0
  /** @see Form#onSubmit() */
  @Override
  protected void onSubmit() {
    final String project = identifier.getModel().getObject();
    try {
      final FileUpload upload = file.getFileUpload();
      if (upload != null) {
        File dir = projects.reserveResourceId(project);

        //				File target = new File(dir, upload.getClientFileName());
        String type = upload.getContentType();
        if (checkContentType(type)) {
          //					IOUtils.copy(upload.getInputStream(), new FileOutputStream(target));

          // try extracting the archive
          IOUtils.extract(dir, new BufferedInputStream(upload.getInputStream()));

          // trigger scan after upload
          projects.triggerScan();
          info("Successfully uploaded project");

          onUploadSuccess();
        } else {
          projects.releaseResourceId(project);
          error(getTypeErrorMessage(type));
        }
      } else {
        warn("Please provide a file for upload");
      }
    } catch (ScavengerException e) {
      error(e.getMessage());
    } catch (Exception e) {
      projects.releaseResourceId(project);
      log.error("Error while uploading file", e);
      error("Error saving the file");
    }
  }
Ejemplo n.º 8
0
 /**
  * Upload file from an FileUpload to the desired location with the desired name
  *
  * @param url - the URL
  * @param to - the output directory
  * @param name - the desired name of the file
  * @throws IOException
  */
 public static File uploadFileFromFileUpload(FileUpload fileUpload, String to, String name)
     throws IOException {
   File output = new File(to + "/" + name);
   fileUpload.writeTo(output);
   return output;
 }