Esempio n. 1
0
 private Set<String> loadEntries() {
   Set<String> entries = new HashSet<String>();
   BufferedReader reader = null;
   try {
     reader = FileHelper.getBufferedReader(getFile(), _encoding);
     for (String line = reader.readLine(); line != null; line = reader.readLine()) {
       entries.add(line);
     }
   } catch (Exception e) {
     throw new IllegalStateException(e);
   } finally {
     FileHelper.safeClose(reader);
   }
   return entries;
 }
  @RolesAllowed(SecurityRoles.CONFIGURATION_EDITOR)
  @RequestMapping(
      method = RequestMethod.POST,
      produces = MediaType.APPLICATION_JSON_VALUE,
      consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
  @ResponseBody
  public Map<String, String> uploadConfigurationFileJson(
      @PathVariable("tenant") final String tenant, @RequestParam("file") final MultipartFile file)
      throws Exception {
    if (file == null) {
      logger.warn("No upload file provided, throwing IllegalArgumentException");
      throw new IllegalArgumentException(
          "No file upload provided. Please provide a multipart file using the 'file' HTTP parameter.");
    }

    logger.info("Upload of new configuration file beginning");

    try {
      final TenantContext context = _contextFactory.getContext(tenant);
      final RepositoryFile configurationFile = context.getConfigurationFile();

      final InputStream inputStream = file.getInputStream();
      final WriteUpdatedConfigurationFileAction writeAction =
          new WriteUpdatedConfigurationFileAction(inputStream, configurationFile);

      try {
        configurationFile.writeFile(writeAction);
      } finally {
        FileHelper.safeClose(inputStream);
      }

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

      return result;
    } catch (Exception e) {
      logger.warn(
          "An error occurred while uploading new configuration file for tenant " + tenant, e);
      throw e;
    }
  }
 @Override
 public void close() {
   FileHelper.safeClose(_reader);
   _row = null;
   _rowsRemaining = null;
 }