Esempio n. 1
0
  /**
   * Looks for the parameter table which matches the center, subcenter and table version from the
   * tables array. If this is the first time asking for this table, then the parameters for this
   * table have not been read in yet, so this is done as well.
   *
   * @param center - integer from PDS octet 5, representing Center.
   * @param subcenter - integer from PDS octet 26, representing Subcenter
   * @param tableVersion - integer from PDS octet 4, representing Parameter Table Version
   * @return GribPDSParamTable matching center, subcenter, and number
   * @throws NotSupportedException no table found
   */
  public static GribPDSParamTable getParameterTable(int center, int subcenter, int tableVersion)
      throws NotSupportedException {

    String key = center + "_" + subcenter + "_" + tableVersion;
    if (center == -1) { // non existent table
      logger.error("GribPDSParamTable: non existent table for center, subcenter, table = " + key);
      return null;
    }

    GribPDSParamTable table = tableMap.get(key);
    if (table != null) return table;

    table = readParameterTable(center, subcenter, tableVersion, true);

    if (table == null) {
      logger.error("GribPDSParamTable: cannot find table for center, subcenter, table " + key);
      throw new NotSupportedException(
          "Could not find a table entry for GRIB file with center: "
              + center
              + " subCenter: "
              + subcenter
              + " number: "
              + tableVersion);
    }

    tableMap.put(key, table);
    return table;
  }
Esempio n. 2
0
 /**
  * Method to get a resource. href:
  * http://howtodoinjava.com/spring/spring-core/how-to-load-external-resources-files-into-spring-context/
  *
  * @param fileNameOrUri the {@link Object} to convert to {@link Resource} must be a {@link
  *     File},{@link URI},{@link URL},{@link Path},{@link String},{@link InputStream}
  * @param clazz the {@link Class} for reference to the resource folder.
  * @param classLoader the {@link ClassLoader} for load the resources.
  * @return the {@link Resource}.
  */
 private static Resource getResourceSpringAsResource(
     Object fileNameOrUri, @Nullable Class<?> clazz, @Nullable ClassLoader classLoader) {
   try {
     Resource yourfile;
     // if File
     if (fileNameOrUri instanceof File && ((File) fileNameOrUri).exists()) {
       yourfile = new org.springframework.core.io.FileSystemResource(((File) fileNameOrUri));
     }
     // if URL
     else if (org.springframework.util.ResourceUtils.isUrl(String.valueOf(fileNameOrUri))
         || fileNameOrUri instanceof URL) {
       if (fileNameOrUri instanceof URL) {
         yourfile = new org.springframework.core.io.UrlResource((URL) fileNameOrUri);
       } else {
         yourfile = new org.springframework.core.io.UrlResource(String.valueOf(fileNameOrUri));
       }
       // if Path or URI
     } else if (fileNameOrUri instanceof Path || fileNameOrUri instanceof URI) {
       if (fileNameOrUri instanceof Path && Files.exists((Path) fileNameOrUri)) {
         yourfile = new org.springframework.core.io.PathResource((Path) fileNameOrUri);
       } else {
         yourfile = new org.springframework.core.io.PathResource((URI) fileNameOrUri);
       }
       /*  }else if(fileNameOrUri instanceof Class){
       org.springframework.core.io.ClassRelativeResourceLoader relativeResourceLoader =
               new org.springframework.core.io.ClassRelativeResourceLoader((Class<?>) fileNameOrUri);
       yourfile = relativeResourceLoader.getResource("")
       */
       // if InputStream
     } else if (fileNameOrUri instanceof InputStream) {
       yourfile = new org.springframework.core.io.InputStreamResource((InputStream) fileNameOrUri);
     } else if (fileNameOrUri instanceof byte[]) {
       yourfile = new org.springframework.core.io.ByteArrayResource((byte[]) fileNameOrUri);
       // if String path toa file or String of a URI
     } else if (fileNameOrUri instanceof String) {
       if (classLoader != null) {
         yourfile =
             new org.springframework.core.io.ClassPathResource(
                 String.valueOf(fileNameOrUri), classLoader);
       } else if (clazz != null) {
         yourfile =
             new org.springframework.core.io.ClassPathResource(
                 String.valueOf(fileNameOrUri), clazz);
       } else {
         yourfile =
             new org.springframework.core.io.ClassPathResource(String.valueOf(fileNameOrUri));
       }
     } else {
       logger.error(
           "Can't load the resource for the Object with Class:"
               + fileNameOrUri.getClass().getName());
       return null;
     }
     return yourfile;
   } catch (IOException e) {
     logger.error(e.getMessage(), e);
     return null;
   }
 }
Esempio n. 3
0
  public boolean load(String abspath) {
    abspath = abspath.replace('\\', '/');
    File rcFile = new File(abspath);
    if (!rcFile.exists() || !rcFile.canRead()) {
      return false;
    }
    if (showlog) log.debug("Loading rc file: " + abspath);
    try (BufferedReader rdr =
        new BufferedReader(new InputStreamReader(new FileInputStream(rcFile), CDM.UTF8))) {
      for (int lineno = 1; ; lineno++) {
        URL url = null;
        String line = rdr.readLine();
        if (line == null) break;
        // trim leading blanks
        line = line.trim();
        if (line.length() == 0) continue; // empty line
        if (line.charAt(0) == '#') continue; // check for comment
        // parse the line
        if (line.charAt(0) == LTAG) {
          int rindex = line.indexOf(RTAG);
          if (rindex < 0) return false;
          if (showlog) log.error("Malformed [url] at " + abspath + "." + lineno);
          String surl = line.substring(1, rindex);
          try {
            url = new URL(surl);
          } catch (MalformedURLException mue) {
            if (showlog) log.error("Malformed [url] at " + abspath + "." + lineno);
          }
          line = line.substring(rindex + 1);
          // trim again
          line = line.trim();
        }
        // Get the key,value part
        String[] pieces = line.split("\\s*=\\s*");
        assert (pieces.length == 1 || pieces.length == 2);
        // Create the triple
        String value = "1";
        if (pieces.length == 2) value = pieces[1].trim();
        Triple triple = new Triple(pieces[0].trim(), value, url);
        List<Triple> list = triplestore.get(triple.key);
        if (list == null) list = new ArrayList<Triple>();
        Triple prev = addtriple(list, triple);
        triplestore.put(triple.key, list);
      }

    } catch (FileNotFoundException fe) {
      if (showlog) log.debug("Loading rc file: " + abspath);
      return false;

    } catch (IOException ioe) {
      if (showlog) log.error("File " + abspath + ": IO exception: " + ioe.getMessage());
      return false;
    }
    return true;
  }
Esempio n. 4
0
 /**
  * Utility to save the string content to a file
  *
  * @param filename String
  * @param content
  */
 public static void saveFileContent(String filename, String content) {
   try (OutputStreamWriter w =
       new OutputStreamWriter(new FileOutputStream(filename), "ISO-8859-1")) {
     w.write(content);
     w.flush();
   } catch (IOException ioe) {
     logger.error("", ioe);
   } catch (Exception ex) {
     logger.error("", ex);
   }
 }
Esempio n. 5
0
  public static boolean saveFile(
      HttpServlet servlet,
      String contentPath,
      String path,
      HttpServletRequest req,
      HttpServletResponse res) {

    // @todo Need to use logServerAccess() below here.
    boolean debugRequest = Debug.isSet("SaveFile");
    if (debugRequest) log.debug(" saveFile(): path= " + path);

    String filename = contentPath + path; // absolute path
    File want = new File(filename);

    // backup current version if it exists
    int version = getBackupVersion(want.getParent(), want.getName());
    String fileSave = filename + "~" + version;
    File file = new File(filename);
    if (file.exists()) {
      try {
        IO.copyFile(filename, fileSave);
      } catch (IOException e) {
        log.error(
            "saveFile(): Unable to save copy of file "
                + filename
                + " to "
                + fileSave
                + "\n"
                + e.getMessage());
        return false;
      }
    }

    // save new file
    try {
      OutputStream out = new BufferedOutputStream(new FileOutputStream(filename));
      IO.copy(req.getInputStream(), out);
      out.close();
      if (debugRequest) log.debug("saveFile(): ok= " + filename);
      res.setStatus(HttpServletResponse.SC_CREATED);
      log.info(UsageLog.closingMessageForRequestContext(HttpServletResponse.SC_CREATED, -1));
      return true;
    } catch (IOException e) {
      log.error(
          "saveFile(): Unable to PUT file " + filename + " to " + fileSave + "\n" + e.getMessage());
      return false;
    }
  }
Esempio n. 6
0
 /**
  * Method use OpenCsv Library for
  *
  * @param fileInputCsv the File CSV to parse.
  * @param separator the char separator.
  * @return the List of Bean parsed from the CSV file.
  */
 public static List<String[]> parseCSVFileAsList(File fileInputCsv, char separator) {
   try {
     List<String[]> records;
     // read all lines at once
     try ( // create CSVReader object
     CSVReader reader = new CSVReader(new FileReader(fileInputCsv), separator)) {
       // read all lines at once
       records = reader.readAll();
       Iterator<String[]> iterator = records.iterator();
       records.clear();
       // skip header row
       iterator.next();
       while (iterator.hasNext()) {
         String[] record = iterator.next();
         records.add(record);
       }
     }
     return records;
   } catch (IOException e) {
     logger.error(
         "Can't parse the CSV file:" + fileInputCsv.getAbsolutePath() + " -> " + e.getMessage(),
         e);
     return new ArrayList<>();
   }
 }
Esempio n. 7
0
  private void initRSTA(String syntaxstyle) {

    rsTextArea.setCodeFoldingEnabled(true);
    rsTextArea.setAntiAliasingEnabled(true);
    // rsTextArea.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_JAVA);
    rsTextArea.setSyntaxEditingStyle(syntaxstyle);

    rTextScrollPane1.setFoldIndicatorEnabled(true);
    rTextScrollPane1.setLineNumbersEnabled(true);

    // Create a toolbar with searching options.
    nextButton.setActionCommand("FindNext");
    nextButton.addActionListener(this);
    searchField.addActionListener(
        new ActionListener() {
          @Override
          public void actionPerformed(ActionEvent e) {
            nextButton.doClick(0);
          }
        });
    prevButton.setActionCommand("FindPrev");
    prevButton.addActionListener(this);

    try {
      //           Theme theme =
      // Theme.load(getClass().getResourceAsStream("/jeplus/gui/themes/eclipse.xml"));
      Theme theme = Theme.load(new FileInputStream("RSyntaxTheme.xml"));
      theme.apply(rsTextArea);
    } catch (IOException ioe) { // Never happens
      logger.error("Failed to apply theme from RSyntaxTheme.xml. Default is used.", ioe);
    }
    setFont(rsTextArea, rsTextArea.getFont().deriveFont(13f));
  }
Esempio n. 8
0
  public void create(Project project) {
    Assert.isNull(
        projectService.findProject(project.getName()),
        "project  [" + project.getName() + "] has exist");
    MongoTemplate template = project.fetchMongoTemplate();
    Assert.notNull(template, "mongo uri is not access");
    Assert.notNull(template.getDb(), "mongo uri is not access");
    Assert.isTrue(
        project.fetchMongoTemplate().collectionExists(project.getLogCollection()),
        " [" + project.getLogCollection() + "] 日志表不存在");
    try {
      List<Task> taskList = Lists.newArrayList();
      logger.debug("init task count:{}", project.getTasks().size());
      for (Task _task : project.getTasks()) {

        Task task = getTemplateTask(_task);
        Task tempTask = renderTemplateTask(task, project);
        if (tempTask != null) taskList.add(tempTask);
      }
      project.setTasks(taskList);
    } catch (Exception e) {
      logger.error("", e);
      throw new IllegalArgumentException("自动添加监控脚本错误:" + e.getMessage());
    }
    projectService.saveProject(project);
  }
Esempio n. 9
0
 /**
  * Utility to load content of a file into a String object
  *
  * @param filename String
  * @return String
  */
 public static String getFileContent(String filename) {
   String content = null;
   try (BufferedReader r =
       new BufferedReader(new InputStreamReader(new FileInputStream(filename), "ISO-8859-1"))) {
     String line = r.readLine();
     StringBuilder buf = new StringBuilder();
     while (line != null) {
       buf.append(line).append('\n');
       line = r.readLine();
     }
     content = buf.toString();
   } catch (IOException ioe) {
     logger.error("", ioe);
   } catch (Exception ex) {
     logger.error("", ex);
   }
   return content;
 }
Esempio n. 10
0
 public static File getResourceSpringAsFile(String pathRelativeToFileOnResourceFolder) {
   try {
     //noinspection ConstantConditions
     return getResourceSpringAsResource(pathRelativeToFileOnResourceFolder, null, null).getFile();
   } catch (IOException e) {
     logger.error(e.getMessage(), e);
     return null;
   }
 }
Esempio n. 11
0
 /**
  * Method to get the String array of the columns of a CSV File.
  *
  * @param contentWithHeaders the {@link List} of {@link String} array of the content of the csv.
  * @return a String Array of the columns.
  */
 public static String[] getHeaders(List<String[]> contentWithHeaders) {
   String[] columns = new String[0];
   try {
     String[] headers = contentWithHeaders.get(0);
     if (headers.length <= 1) {
       throw new Exception("Can't find the delimiter with openCSV try with Univicity method.");
     }
   } catch (Exception e) {
     logger.error("Can't find the Headers on the content", e);
   }
   return columns;
 }
Esempio n. 12
0
  /**
   * Send given content string as the HTTP response.
   *
   * @param contents the string to return as the HTTP response.
   * @param res the HttpServletResponse
   * @throws IOException if an I/O error occurs while writing the response.
   */
  public static void returnString(String contents, HttpServletResponse res) throws IOException {

    try {
      ServletOutputStream out = res.getOutputStream();
      IO.copy(new ByteArrayInputStream(contents.getBytes()), out);
      log.info(
          UsageLog.closingMessageForRequestContext(HttpServletResponse.SC_OK, contents.length()));
    } catch (IOException e) {
      log.error(" IOException sending string: ", e);
      log.info(UsageLog.closingMessageForRequestContext(HttpServletResponse.SC_NOT_FOUND, 0));
      res.sendError(HttpServletResponse.SC_NOT_FOUND, "Problem sending string: " + e.getMessage());
    }
  }
Esempio n. 13
0
  /**
   * ************************************************************************ Sends an error to the
   * client.
   *
   * @param t The exception that caused the problem.
   * @param res The <code>HttpServletResponse</code> for the client.
   */
  public static void handleException(Throwable t, HttpServletResponse res) {
    try {
      String message = t.getMessage();
      if (message == null) message = "NULL message " + t.getClass().getName();
      if (Debug.isSet("trustedMode")) { // security issue: only show stack if trusted
        ByteArrayOutputStream bs = new ByteArrayOutputStream();
        PrintStream ps = new PrintStream(bs);
        t.printStackTrace(ps);
        message = new String(bs.toByteArray());
      }
      log.info(
          UsageLog.closingMessageForRequestContext(
              HttpServletResponse.SC_BAD_REQUEST, message.length()));
      log.error("handleException", t);
      t.printStackTrace(); // debugging - log.error not showing stack trace !!
      if (!res.isCommitted()) res.sendError(HttpServletResponse.SC_BAD_REQUEST, message);

    } catch (Throwable e) {
      log.error("handleException() had problem reporting Exception", e);
      t.printStackTrace();
    }
  }
  private double getTime(StructureMembers.Member timeVar, StructureData sdata) {
    if (timeVar == null) return 0.0;

    if ((timeVar.getDataType() == DataType.CHAR) || (timeVar.getDataType() == DataType.STRING)) {
      String time = sdata.getScalarString(timeVar);
      CalendarDate date = CalendarDateFormatter.isoStringToCalendarDate(null, time);
      if (date == null) {
        log.error("Cant parse date - not ISO formatted, = " + time);
        return 0.0;
      }
      return date.getMillis() / 1000.0;

    } else {
      return sdata.convertScalarDouble(timeVar);
    }
  }
Esempio n. 15
0
 /**
  * Method to write a CSV List of Array of String to System Console..
  *
  * @param content the List of Array of String to convert.
  * @param separator the char separator.
  */
 public static void writeCSVDataToConsole(List<String[]> content, char separator) {
   try {
     Writer writer = new OutputStreamWriter(System.out, StringUtilities.UTF_8);
     CSVWriter csvWriter;
     if (StringUtilities.NULL_CHAR2 == separator) {
       csvWriter =
           new CSVWriter(writer, CSVWriter.DEFAULT_SEPARATOR, CSVWriter.NO_QUOTE_CHARACTER);
     } else {
       csvWriter = new CSVWriter(writer, separator, CSVWriter.NO_QUOTE_CHARACTER);
     }
     csvWriter.writeAll(content, false);
     csvWriter.close();
   } catch (IOException e) {
     logger.error("Can't write the CSV to Console -> " + e.getMessage(), e);
   }
 }
Esempio n. 16
0
 /**
  * Method to get the String array of the columns of a CSV File.
  *
  * @param fileCSV the File CSV.
  * @param hasFirstLine if true the first line of CSV File contains the columns name.
  * @return a String Array of the columns.
  */
 public static String[] getHeaders(File fileCSV, boolean hasFirstLine) {
   String[] columns = new String[0];
   try {
     CSVReader reader = new CSVReader(new FileReader(fileCSV));
     columns = reader.readNext(); // assuming first read
     if (!hasFirstLine) {
       int columnCount = 0;
       if (columns != null) columnCount = columns.length;
       columns = new String[columnCount];
       for (int i = 0; i < columnCount; i++) {
         columns[i] = "Column#" + i;
       }
     }
   } catch (IOException e) {
     logger.error("Can't find the Headers on the CSV File", e);
   }
   return columns;
 }
Esempio n. 17
0
 /**
  * Method to write a CSV Data List of Beans to a String.
  *
  * @param beans the List of Beans to convert.
  * @param separator the char separator.
  * @param <T> the generic variable.
  * @return the String content of the List of Beans.
  */
 public static <T> String writeCSVDataToStringWithBeans(List<T> beans, char separator) {
   try {
     Writer writer = new StringWriter();
     try (CSVWriter csvWriter = new CSVWriter(writer, separator)) {
       List<String[]> data = toStringArray(beans);
       csvWriter.writeAll(data);
     }
     return writer.toString();
   } catch (IOException e) {
     logger.error(
         "Can't write the CSV String from the Bean:"
             + beans.get(0).getClass().getName()
             + " -> "
             + e.getMessage(),
         e);
     return "";
   }
 }
Esempio n. 18
0
 public static String readResource(Resource resource) {
   try {
     /* org.springframework.core.io.Resource resource =
     new org.springframework.core.io.ClassPathResource(fileLocationInClasspath);*/
     BufferedReader br =
         new BufferedReader(new InputStreamReader(resource.getInputStream()), 1024);
     StringBuilder stringBuilder = new StringBuilder();
     String line;
     while ((line = br.readLine()) != null) {
       stringBuilder.append(line).append('\n');
     }
     br.close();
     return stringBuilder.toString();
   } catch (Exception e) {
     logger.error(e.getMessage(), e);
     return null;
   }
 }
Esempio n. 19
0
 /**
  * Method to write a CSV Data List of Array of String to a String.
  *
  * @param content the List of Array of String to convert.
  * @param separator the char separator.
  * @return the String content of the List of Beans.
  */
 public static String writeCSVDataToString(List<String[]> content, char separator) {
   try {
     Writer writer = new StringWriter();
     CSVWriter csvWriter;
     if (StringUtilities.NULL_CHAR2 == separator) {
       csvWriter =
           new CSVWriter(writer, CSVWriter.DEFAULT_SEPARATOR, CSVWriter.NO_QUOTE_CHARACTER);
     } else {
       csvWriter = new CSVWriter(writer, separator, CSVWriter.NO_QUOTE_CHARACTER);
     }
     csvWriter.writeAll(content);
     csvWriter.close();
     return writer.toString();
   } catch (IOException e) {
     logger.error("Can't write the CSV String -> " + e.getMessage(), e);
     return "";
   }
 }
Esempio n. 20
0
 /**
  * Method to write a CSV File from List of Array of String.
  *
  * @param content the List of Array of String to convert.
  * @param separator the char separator.
  * @param fileOutputCsv the output File Csv to create.
  * @return the File Csv created.
  */
 public static File writeCSVDataToFile(
     List<String[]> content, char separator, File fileOutputCsv) {
   try {
     Writer writer =
         new FileWriter(fileOutputCsv, true); // the true value make append the result...
     CSVWriter csvWriter;
     if (StringUtilities.NULL_CHAR2 == separator) {
       csvWriter =
           new CSVWriter(writer, CSVWriter.DEFAULT_SEPARATOR, CSVWriter.NO_QUOTE_CHARACTER);
     } else {
       csvWriter = new CSVWriter(writer, separator, CSVWriter.NO_QUOTE_CHARACTER);
     }
     csvWriter.writeAll(content);
     csvWriter.close();
     return fileOutputCsv;
   } catch (IOException e) {
     logger.error("Can't write the CSV to File:" + fileOutputCsv + " -> " + e.getMessage(), e);
     return null;
   }
 }
Esempio n. 21
0
  public static File getResourceAsFile(String name, Class<?> thisClass) {
    ClassLoader classLoader = thisClass.getClassLoader();
    try {
      //noinspection ConstantConditions
      // String path = classLoader.getResource("").getPath();
      // ///C:/Users/tenti/Desktop/Projects/gate-basic/target/test-classes/
      return new File(classLoader.getResource(name).getFile());
    } catch (NullPointerException e) {
      try {
        // return new
        // File(Thread.currentThread().getContextClassLoader().getResource(name).getFile());
        // ClassLoader classLoader = Config.class.getClassLoader();
        // URL resource = classLoader.getResource(name);
        // String path = Paths.get(resource.toURI()).toAbsolutePath().toString();
        File file = null;
        URL url = null;

        if (classLoader != null) {
          url = classLoader.getResource(name);
        }
        if (url == null) {
          url = ClassLoader.getSystemResource(name);
        }
        if (url != null) {
          try {
            file = new File(url.toURI());
          } catch (URISyntaxException e5) {
            file = new File(url.getPath());
          }
        }
        if (file == null) {
          file = getResourceAsFileFromBuildFolder(name);
        }
        // return new File(path);
        return file;
      } catch (NullPointerException | IOException e2) {
        logger.error(e2.getMessage(), e2);
        return new File("");
      }
    }
  }
Esempio n. 22
0
 /**
  * Method use OpenCsv Library for
  *
  * @param columnMapping Map allow the user to pass the column Names to a Field Names of the Class.
  * @param fileInputCsv the File CSV to parse.
  * @param <T> the generic variable.
  * @return the List of Bean parsed from the CSV file.
  */
 public static <T> List<T> parseCSVFileAsList(
     Map<String, String> columnMapping, File fileInputCsv) {
   try {
     HeaderColumnNameTranslateMappingStrategy<T> beanStrategy =
         new HeaderColumnNameTranslateMappingStrategy<>();
     // beanStrategy.setType(clazz); //deprecated
     /*Map<String, String> columnMapping = new HashMap<>();
     columnMapping.put("ID", "id");
     columnMapping.put("Name", "name");
     columnMapping.put("Role", "role");*/
     beanStrategy.setColumnMapping(columnMapping);
     CsvToBean<T> csvToBean = new CsvToBean<>();
     CSVReader reader = new CSVReader(new FileReader(fileInputCsv));
     return csvToBean.parse(beanStrategy, reader);
   } catch (IOException e) {
     logger.error(
         "Can't parse the CSV file:" + fileInputCsv.getAbsolutePath() + " -> " + e.getMessage(),
         e);
     return new ArrayList<>();
   }
 }
Esempio n. 23
0
  public Task renderTemplateTask(Task task, Project project) {
    try {

      Task task1 = new Task();
      task1.setCron(task.getCron());
      task1.setName(task.getName());
      task1.setTimeout(task.getTimeout());
      Writer out = new StringWriter();
      File file = getTemplateFile(task1.getName());

      String temp = FileUtils.readFileToString(file, "utf-8");
      Map map = Maps.newHashMap();
      map.put("project", project);

      Context context = new VelocityContext(map);
      velocityEngine.evaluate(context, out, this.getClass().getName(), temp);
      task1.setScript(out.toString());
      return task1;
    } catch (Exception e) {
      logger.error("init task from template fail:", e);
      return null;
    }
  }
Esempio n. 24
0
  private static int getBackupVersion(String dirName, String fileName) {
    int maxN = 0;
    File dir = new File(dirName);
    if (!dir.exists()) return -1;

    String[] files = dir.list();
    if (null == files) return -1;

    for (String name : files) {
      if (name.indexOf(fileName) < 0) continue;
      int pos = name.indexOf('~');
      if (pos < 0) continue;
      String ver = name.substring(pos + 1);
      int n = 0;
      try {
        n = Integer.parseInt(ver);
      } catch (NumberFormatException e) {
        log.error("Format Integer error on backup filename= " + ver);
      }
      maxN = Math.max(n, maxN);
    }
    return maxN + 1;
  }
Esempio n. 25
0
 /**
  * Method to get the content of a comma separated file (.csv,.input,.txt)
  *
  * @param CSV the File comma separated.
  * @param noHeaders if true jump the first line of the content.
  * @return the List of Array of the content of the File comma separated.
  */
 public static List<String[]> parseCSVFileAsList(File CSV, boolean noHeaders) {
   List<String[]> content;
   try {
     CSVReader reader1 = new CSVReader(new FileReader(CSV));
     content = reader1.readAll();
     /* List<String[]> myDatas = reader1.readAll();
     String[] lineI = myDatas.get(i);
     for (String[] line : myDatas) {
         for (String value : line) {
             //do stuff with value
         }
     }*/
     if (noHeaders) content.remove(0);
     if (content.get(0).length <= 1) {
       logger.warn(
           "Attention: You haven't parsed correctly the CSV file with OpenCSV API try with Univocity Method");
     }
     return content;
   } catch (IOException e) {
     logger.error("Can't find the CSV File:" + e.getMessage(), e);
     return null;
   }
 }
Esempio n. 26
0
 /**
  * Method use OpenCsv Library for
  *
  * @param clazz the Class of the Bean.
  * @param fileInputCsv the File CSV to parse.
  * @param separator the char separator.
  * @param <T> the generic variable.
  * @return the List of Bean parsed from the CSV file.
  */
 public static <T> List<T> parseCSVFileAsList(Class<T> clazz, File fileInputCsv, char separator) {
   try {
     List<T> beans;
     try ( // create CSVReader object
     CSVReader reader = new CSVReader(new FileReader(fileInputCsv), separator)) {
       beans = new ArrayList<>();
       // read line by line
       String[] record;
       // skip header row
       String[] headers = reader.readNext();
       // read content
       while ((record = reader.readNext()) != null) {
         T t = ReflectionUtilities.invokeConstructor(clazz);
         for (int i = 0; i < record.length; i++) {
           String nameMethod = "set" + org.apache.commons.lang3.StringUtils.capitalize(headers[i]);
           // invoke setter method
           if (ReflectionUtilities.checkMethod(clazz, nameMethod)) {
             ReflectionUtilities.invokeSetter(t, nameMethod, record[i]);
           } else {
             logger.warn(
                 "Not exists the Method with name:"
                     + nameMethod
                     + " on the Bean:"
                     + t.getClass().getName());
           }
         }
         beans.add(t);
       }
     }
     return beans;
   } catch (IOException e) {
     logger.error(
         "Can't parse the CSV file:" + fileInputCsv.getAbsolutePath() + " -> " + e.getMessage(),
         e);
     return new ArrayList<>();
   }
 }
Esempio n. 27
0
  private static ApplicationContext loadApplicationContextSpring(
      Class<?> thisClass, String... filePaths) {
    ApplicationContext context = new GenericApplicationContext();
    // Clean the path for load the context...
    for (int i = 0; i < filePaths.length; i++) {
      if (filePaths[i].startsWith(File.separator) || filePaths[i].startsWith("/")) {
        filePaths[i] = filePaths[i].substring(1, filePaths[i].length());
      }
    }

    // This container loads the definitions of the beans from an XML file.
    // Here you do not need to provide the full path of the XML file but
    // you need to set CLASSPATH properly because this container will look
    // bean configuration XML file in CLASSPATH.
    // You can force with the fileSystem using "file:" instead of "classpath:".
    try {
      context = new ClassPathXmlApplicationContext(filePaths, true);
    } catch (Exception e0) {
      try {
        if (e0.getCause().getMessage().contains("has already been set")) {
          logger.warn(e0.getMessage() + "->" + e0.getCause());
        }
      } catch (java.lang.NullPointerException e) {
        /*do nothing*/
      }
      try {
        context = new ClassPathXmlApplicationContext(filePaths, true);
      } catch (Exception e1) {
        if (thisClass != null) {
          try {
            context = new ClassPathXmlApplicationContext(filePaths, thisClass);
          } catch (Exception e2) {

            try {
              // This container loads the definitions of the beans from an XML file.
              // Here you need to provide the full path of the XML bean configuration file to the
              // constructor.
              // You can force with file: property to the class file.
              List<String> files = new ArrayList<>();
              for (String spath : filePaths) {
                Path path = getResourceAsFile(spath, thisClass).toPath();
                if (Files.exists(path) && path.toRealPath() != null) {
                  files.add(path.toAbsolutePath().toString());
                } else {
                  logger.warn("The resource with path:" + path.toString() + " not exists");
                }
              }
              if (!files.isEmpty()) {
                context =
                    new FileSystemXmlApplicationContext(
                        files.toArray(new String[files.size()]), true);
              } else {
                logger.warn("The paths used are reference 0 resources return NULL value");
                return null;
              }
            } catch (Exception e3) {
              logger.error(e3.getMessage(), e3);
            }
          }
        } else {
          logger.error(e1.getMessage(), e1);
        }
      }
    }
    return context;
  }
Esempio n. 28
0
  public void doGet(HttpServletRequest request, HttpServletResponse response) {
    log.info("doGet(): " + UsageLog.setupRequestContext(request));
    // System.out.printf("opendap doGet: req=%s%n%s%n", ServletUtil.getRequest(request),
    // ServletUtil.showRequestDetail(this, request));

    String path = null;

    ReqState rs = getRequestState(request, response);

    try {
      path = request.getPathInfo();
      log.debug("doGet path={}", path);

      if (thredds.servlet.Debug.isSet("showRequestDetail"))
        log.debug(ServletUtil.showRequestDetail(this, request));

      if (path == null) {
        log.info(
            "doGet(): "
                + UsageLog.closingMessageForRequestContext(HttpServletResponse.SC_NOT_FOUND, -1));
        response.sendError(HttpServletResponse.SC_NOT_FOUND);
        return;
      }

      if (baseURI == null) { // first time, set baseURI
        URI reqURI = ServletUtil.getRequestURI(request);
        // Build base URI from request (rather than hard-coding "/thredds/dodsC/").
        String baseUriString = request.getContextPath() + request.getServletPath() + "/";
        baseURI = reqURI.resolve(baseUriString);
        log.debug("doGet(): baseURI was set = {}", baseURI);
      }

      if (path.endsWith("latest.xml")) {
        DataRootHandler.getInstance().processReqForLatestDataset(this, request, response);
        return;
      }

      // Redirect all catalog requests at the root level.
      if (path.equals("/") || path.equals("/catalog.html") || path.equals("/catalog.xml")) {
        ServletUtil.sendPermanentRedirect(ServletUtil.getContextPath() + path, request, response);
        return;
      }

      // Make sure catalog requests match a dataRoot before trying to handle.
      if (path.endsWith("/") || path.endsWith("/catalog.html") || path.endsWith("/catalog.xml")) {
        if (!DataRootHandler.getInstance().hasDataRootMatch(path)) {
          log.info(
              "doGet(): "
                  + UsageLog.closingMessageForRequestContext(HttpServletResponse.SC_NOT_FOUND, -1));
          response.sendError(HttpServletResponse.SC_NOT_FOUND);
          return;
        }

        if (!DataRootHandler.getInstance().processReqForCatalog(request, response))
          log.error(
              "doGet(): "
                  + UsageLog.closingMessageForRequestContext(
                      ServletUtil.STATUS_FORWARD_FAILURE, -1));

        return;
      }

      if (rs != null) {
        String dataSet = rs.getDataSet();
        String requestSuffix = rs.getRequestSuffix();

        if ((dataSet == null) || dataSet.equals("/") || dataSet.equals("")) {
          doGetDIR(rs);
        } else if (requestSuffix.equalsIgnoreCase("blob")) {
          doGetBLOB(rs);
        } else if (requestSuffix.equalsIgnoreCase("close")) {
          doClose(rs);
        } else if (requestSuffix.equalsIgnoreCase("dds")) {
          doGetDDS(rs);
        } else if (requestSuffix.equalsIgnoreCase("das")) {
          doGetDAS(rs);
        } else if (requestSuffix.equalsIgnoreCase("ddx")) {
          doGetDDX(rs);
        } else if (requestSuffix.equalsIgnoreCase("dods")) {
          doGetDAP2Data(rs);
        } else if (requestSuffix.equalsIgnoreCase("asc")
            || requestSuffix.equalsIgnoreCase("ascii")) {
          doGetASC(rs);
        } else if (requestSuffix.equalsIgnoreCase("info")) {
          doGetINFO(rs);
        } else if (requestSuffix.equalsIgnoreCase("html")
            || requestSuffix.equalsIgnoreCase("htm")) {
          doGetHTML(rs);
        } else if (requestSuffix.equalsIgnoreCase("ver")
            || requestSuffix.equalsIgnoreCase("version")
            || dataSet.equalsIgnoreCase("/version")
            || dataSet.equalsIgnoreCase("/version/")) {
          doGetVER(rs);
        } else if (dataSet.equalsIgnoreCase("/help")
            || dataSet.equalsIgnoreCase("/help/")
            || dataSet.equalsIgnoreCase("/" + requestSuffix)
            || requestSuffix.equalsIgnoreCase("help")) {
          doGetHELP(rs);
        } else {
          sendErrorResponse(response, HttpServletResponse.SC_BAD_REQUEST, "Unrecognized request");
          return;
        }

      } else {
        sendErrorResponse(response, HttpServletResponse.SC_BAD_REQUEST, "Unrecognized request");
        return;
      }

      log.info(UsageLog.closingMessageForRequestContext(HttpServletResponse.SC_OK, -1));

      // plain ol' 404
    } catch (FileNotFoundException e) {
      sendErrorResponse(response, HttpServletResponse.SC_NOT_FOUND, e.getMessage());

      // DAP2Exception bad url
    } catch (BadURLException e) {
      log.info(UsageLog.closingMessageForRequestContext(HttpServletResponse.SC_BAD_REQUEST, -1));
      response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
      dap2ExceptionHandler(e, rs);

      // all other DAP2Exception
    } catch (DAP2Exception de) {
      int status =
          (de.getErrorCode() == DAP2Exception.NO_SUCH_FILE)
              ? HttpServletResponse.SC_NOT_FOUND
              : HttpServletResponse.SC_BAD_REQUEST;
      if ((de.getErrorCode() != DAP2Exception.NO_SUCH_FILE) && (de.getErrorMessage() != null))
        log.debug(de.getErrorMessage());
      log.info(UsageLog.closingMessageForRequestContext(status, -1));
      response.setStatus(status);
      dap2ExceptionHandler(de, rs);

      // parsing, usually the CE
    } catch (ParseException pe) {
      log.info(UsageLog.closingMessageForRequestContext(HttpServletResponse.SC_BAD_REQUEST, -1));
      response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
      parseExceptionHandler(pe, response);

      // 403 - request too big
    } catch (UnsupportedOperationException e) {
      sendErrorResponse(response, HttpServletResponse.SC_FORBIDDEN, e.getMessage());

    } catch (java.net.SocketException e) {
      log.info("SocketException: " + e.getMessage(), e);
      log.info(UsageLog.closingMessageForRequestContext(ServletUtil.STATUS_CLIENT_ABORT, -1));

    } catch (IOException e) {
      String eName =
          e.getClass().getName(); // dont want compile time dependency on ClientAbortException
      if (eName.equals("org.apache.catalina.connector.ClientAbortException")) {
        log.debug("ClientAbortException: " + e.getMessage());
        log.info(UsageLog.closingMessageForRequestContext(ServletUtil.STATUS_CLIENT_ABORT, -1));
        return;
      }

      log.error("path= " + path, e);
      sendErrorResponse(response, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.getMessage());

      // everything else
    } catch (Throwable t) {
      log.error("path= " + path, t);
      t.printStackTrace();
      sendErrorResponse(response, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, t.getMessage());
    }
  }
Esempio n. 29
0
  /** Read parameter table. */
  private void readParameterTable() {
    if (path == null) {
      logger.error("GribPDSParamTable: unknown path for " + this);
      return;
    }

    try {
      InputStream is = GribResourceReader.getInputStream(path);
      if (is == null) {
        logger.error("GribPDSParamTable: error getInputStream on " + this);
        return;
      }
      BufferedReader br = new BufferedReader(new InputStreamReader(is));

      // Read first line that has center, subcenter, and version of table
      String line = br.readLine();
      if (debug) System.out.println(line);
      String[] tableDefArr = line.split(":");

      /*  LOOK - why not test values ?
      center    = Integer.parseInt(tableDefArr[1].trim());
      subcenter = Integer.parseInt(tableDefArr[2].trim());
      number    = Integer.parseInt(tableDefArr[3].trim());
      if ((center != center_id) && (subcenter != subcenter_id)
              && (number != table_number)) {
          throw new java.io.IOException(
              "parameter table header values do not "
              + " match values in GRIB file.  Possible error in lookup table.");
      }
      */

      HashMap<String, GridParameter> tmpParameters =
          new HashMap<String, GridParameter>(); // thread safe - temp hash

      // rdg - added the 0 line length check to cover the case of blank lines at
      //       the end of the parameter table file.
      while ((line = br.readLine()) != null) {
        if ((line.length() == 0) || line.startsWith("#")) {
          continue;
        }
        GridParameter parameter = new GridParameter();
        tableDefArr = line.split(":");
        parameter.setNumber(Integer.parseInt(tableDefArr[0].trim()));
        parameter.setName(tableDefArr[1].trim());
        // check to see if unit defined, if not, parameter is undefined
        if (tableDefArr[2].indexOf('[') == -1) {
          // Undefined unit
          parameter.setDescription(tableDefArr[2].trim());
          parameter.setUnit(tableDefArr[2].trim());
        } else {
          String[] arr2 = tableDefArr[2].split("\\[");
          parameter.setDescription(makeValidDesc(arr2[0].trim()));
          // System.out.println( "Desc ="+ parameter.getDescription());
          // Remove "]"
          parameter.setUnit(arr2[1].substring(0, arr2[1].lastIndexOf(']')).trim());
        }
        tmpParameters.put(Integer.toString(parameter.getNumber()), parameter);
        if (debug)
          System.out.println(
              parameter.getNumber() + " " + parameter.getDescription() + " " + parameter.getUnit());
      }

      this.parameters = tmpParameters; // thread safe

    } catch (IOException ioError) {
      logger.warn(
          "An error occurred in GribPDSParamTable while trying to open the parameter table "
              + filename
              + " : "
              + ioError);
    }
  }
 protected void logServerError(
     String message, Exception exception, ContainerRequestContext requestContext) {
   logger.error(message, exception);
 }