Ejemplo n.º 1
0
  @RolesAllowed(SecurityRoles.JOB_EDITOR)
  @RequestMapping(method = RequestMethod.POST, produces = "application/json")
  @ResponseBody
  public Map<String, String> uploadAnalysisJob(
      @PathVariable("tenant") final String tenant,
      @PathVariable("job") String jobName,
      @RequestParam("file") final MultipartFile file) {
    if (file == null) {
      throw new IllegalArgumentException(
          "No file upload provided. Please provide a multipart file using the 'file' HTTP parameter.");
    }

    jobName = jobName.replaceAll("\\+", " ");

    final Action<OutputStream> writeCallback =
        new Action<OutputStream>() {
          @Override
          public void run(OutputStream out) throws Exception {
            final InputStream in = file.getInputStream();
            try {
              FileHelper.copy(in, out);
            } finally {
              FileHelper.safeClose(in);
            }
          }
        };

    final TenantContext context = _contextFactory.getContext(tenant);
    final JobContext existingJob = context.getJob(jobName);
    final RepositoryFile jobFile;
    if (existingJob == null) {
      final RepositoryFolder jobsFolder = context.getJobFolder();

      final String filename;
      if (jobName.endsWith(EXTENSION)) {
        filename = jobName;
      } else {
        filename = jobName + EXTENSION;
      }

      logger.info("Creating new job from uploaded file: {}", filename);
      jobFile = jobsFolder.createFile(filename, writeCallback);
    } else {
      jobFile = existingJob.getJobFile();
      logger.info("Overwriting job from uploaded file: {}", jobFile.getName());
      jobFile.writeFile(writeCallback);
    }

    final Map<String, String> result = new HashMap<String, String>();
    result.put("status", "Success");
    result.put("file_type", jobFile.getType().toString());
    result.put("filename", jobFile.getName());
    result.put("repository_path", jobFile.getQualifiedPath());

    return result;
  }
  @Override
  public RepositoryFile createFile(String name, Action<OutputStream> writeCallback) {
    if (name.indexOf('/') != -1 || name.indexOf('\\') != -1) {
      throw new IllegalArgumentException("File name cannot contain slashes");
    }

    final File file = new File(_file, name);
    if (file.exists()) {
      throw new IllegalArgumentException("A file with the name '" + name + "' already exists");
    }

    RepositoryFile repositoryFile = (RepositoryFile) getChildCache().getUnchecked(file);
    repositoryFile.writeFile(writeCallback);

    return repositoryFile;
  }