List of usage examples for java.nio.file Files lines
public static Stream<String> lines(Path path) throws IOException
From source file:no.ntnu.okse.web.controller.LogController.java
/** * This method returns the log given the request parameters * * @param logID The log file to return (represented as a integer) * @param logLevel The log level to show * @param length How many lines to return * @return A Log model containing the correct log. Will be serialized as JSON *///from w ww .j a va2 s . c o m @RequestMapping(method = RequestMethod.GET) public Log log(@RequestParam(value = "logID", defaultValue = "0") Integer logID, @RequestParam(value = "logLevel", defaultValue = "DEBUG") String logLevel, @RequestParam(value = "length", defaultValue = "250") int length) { try { Stream<String> l = Files.lines(Paths.get("logs/", fileNames.get(logID))); List<String> lines = l.filter(x -> isWithinLogLevel(logLevel, x)).collect(Collectors.toList()); Collections.reverse(lines); Log log; if (lines.size() > length) { log = new Log(fileNames.get(logID), lines.subList(0, length)); } else { log = new Log(fileNames.get(logID), lines); } return log; } catch (IOException e) { Log log = new Log("Does not exists", new ArrayList<String>() { { add("The logfile you requested do not exist."); } }); return log; } }
From source file:org.apache.nifi.properties.NiFiPropertiesLoader.java
/** * Returns the key (if any) used to encrypt sensitive properties, extracted from {@code $NIFI_HOME/conf/bootstrap.conf}. * * @param bootstrapPath the path to the bootstrap file * @return the key in hexadecimal format * @throws IOException if the file is not readable *///from ww w .ja v a 2 s. c om public static String extractKeyFromBootstrapFile(String bootstrapPath) throws IOException { File expectedBootstrapFile; if (StringUtils.isBlank(bootstrapPath)) { // Guess at location of bootstrap.conf file from nifi.properties file String defaultNiFiPropertiesPath = getDefaultFilePath(); File propertiesFile = new File(defaultNiFiPropertiesPath); File confDir = new File(propertiesFile.getParent()); if (confDir.exists() && confDir.canRead()) { expectedBootstrapFile = new File(confDir, "bootstrap.conf"); } else { logger.error( "Cannot read from bootstrap.conf file at {} to extract encryption key -- conf/ directory is missing or permissions are incorrect", confDir.getAbsolutePath()); throw new IOException("Cannot read from bootstrap.conf"); } } else { expectedBootstrapFile = new File(bootstrapPath); } if (expectedBootstrapFile.exists() && expectedBootstrapFile.canRead()) { try (Stream<String> stream = Files.lines(Paths.get(expectedBootstrapFile.getAbsolutePath()))) { Optional<String> keyLine = stream.filter(l -> l.startsWith(BOOTSTRAP_KEY_PREFIX)).findFirst(); if (keyLine.isPresent()) { return keyLine.get().split("=", 2)[1]; } else { logger.warn("No encryption key present in the bootstrap.conf file at {}", expectedBootstrapFile.getAbsolutePath()); return ""; } } catch (IOException e) { logger.error("Cannot read from bootstrap.conf file at {} to extract encryption key", expectedBootstrapFile.getAbsolutePath()); throw new IOException("Cannot read from bootstrap.conf", e); } } else { logger.error( "Cannot read from bootstrap.conf file at {} to extract encryption key -- file is missing or permissions are incorrect", expectedBootstrapFile.getAbsolutePath()); throw new IOException("Cannot read from bootstrap.conf"); } }
From source file:org.springframework.security.config.doc.XsdDocumentedTests.java
/** * This uses a naming convention for the ids of the appendix to ensure that the entire appendix is documented. * The naming convention for the ids is documented in {@link Element#getIds()}. * @return/*from w w w. j av a2 s . com*/ */ @Test public void countReferencesWhenReviewingDocumentationThenEntireSchemaIsIncluded() throws IOException { Map<String, Element> elementsByElementName = this.xml.elementsByElementName(this.schemaDocumentLocation); List<String> documentIds = Files.lines(Paths.get(this.referenceLocation)) .filter(line -> line.matches("\\[\\[(nsa-.*)\\]\\]")) .map(line -> line.substring(2, line.length() - 2)).collect(Collectors.toList()); Set<String> expectedIds = elementsByElementName.values().stream() .flatMap(element -> element.getIds().stream()).collect(Collectors.toSet()); documentIds.removeAll(this.ignoredIds); expectedIds.removeAll(this.ignoredIds); assertThat(documentIds).containsAll(expectedIds); assertThat(expectedIds).containsAll(documentIds); }
From source file:org.apache.gobblin.elasticsearch.ElasticsearchTestServer.java
private void configure() throws IOException { File configFile = new File(_testInstallDirectory + ELASTICSEARCH_CONFIG_FILE); FileOutputStream configFileStream = new FileOutputStream(configFile); try {//from w w w. j a v a 2s . c o m configFileStream.write(("cluster.name: " + _testId + "\n").getBytes("UTF-8")); configFileStream.write(("http.port: " + _httpPort + "\n").getBytes("UTF-8")); configFileStream.write(("transport.tcp.port: " + _tcpPort + "\n").getBytes("UTF-8")); } finally { configFileStream.close(); } File jvmConfigFile = new File(_testInstallDirectory + ELASTICSEARCH_JVMOPTS_FILE); try (Stream<String> lines = Files.lines(jvmConfigFile.toPath())) { List<String> newLines = lines.map(line -> line.replaceAll("^\\s*(-Xm[s,x]).*$", "$1128m")) .collect(Collectors.toList()); Files.write(jvmConfigFile.toPath(), newLines); } }
From source file:org.fcrepo.http.api.ExternalContentPathValidator.java
/** * Loads the allowed list./* w w w. j a v a 2s. c o m*/ * * @throws IOException thrown if the allowed list configuration file cannot be read. */ private synchronized void loadAllowedPaths() throws IOException { try (final Stream<String> stream = Files.lines(Paths.get(configPath))) { allowedList = stream.map(line -> normalizePath(line.trim().toLowerCase())).filter(line -> { final Matcher schemeMatcher = SCHEME_PATTERN.matcher(line); final boolean schemeMatches = schemeMatcher.matches(); if (!schemeMatches || RELATIVE_MOD_PATTERN.matcher(line).matches()) { LOGGER.error("Invalid path {} specified in external path configuration {}", line, configPath); return false; } if (schemeMatches && "file".equals(schemeMatcher.group(1))) { // If a file uri ends with / it must be a directory, otherwise it must be a file. final File allowing = new File(URI.create(line).getPath()); if ((line.endsWith("/") && !allowing.isDirectory()) || (!line.endsWith("/") && !allowing.isFile())) { LOGGER.error("Invalid path {} in configuration {}, directories must end with a '/'", line, configPath); return false; } } return true; }).collect(Collectors.toList()); } }
From source file:org.hawkular.metrics.clients.ptrans.fullstack.CollectdITest.java
private void assertCollectdConfIsValid() throws Exception { collectdOut = temporaryFolder.newFile(); collectdErr = temporaryFolder.newFile(); collectdProcessBuilder = new ProcessBuilder(); collectdProcessBuilder.directory(temporaryFolder.getRoot()); collectdProcessBuilder.redirectOutput(collectdOut); collectdProcessBuilder.redirectError(collectdErr); collectdProcessBuilder.command(COLLECTD_PATH, "-C", collectdConfFile.getAbsolutePath(), "-t", "-T", "-f"); collectdProcess = collectdProcessBuilder.start(); int exitCode = collectdProcess.waitFor(); assertEquals("Collectd configuration doesn't seem to be valid", 0, exitCode); boolean hasErrorInLog = Stream.concat(Files.lines(collectdOut.toPath()), Files.lines(collectdErr.toPath())) .anyMatch(l -> l.contains("[error]")); assertFalse("Collectd configuration doesn't seem to be valid", hasErrorInLog); }
From source file:org.pentaho.di.job.entries.job.JobEntryJobIT.java
@Test public void testLogfileWritesFromRemote() throws Exception { JobEntryJob job = spy(new JobEntryJob(JOB_ENTRY_JOB_NAME)); doCallRealMethod().when(job).execute(any(Result.class), anyInt()); Job parentJob = mock(Job.class); JobMeta parentJobMeta = mock(JobMeta.class); JobMeta jobMeta = mock(JobMeta.class); SlaveServer slaveServer = mock(SlaveServer.class); LogChannelInterface log = mock(LogChannelInterface.class); SlaveServerJobStatus status = mock(SlaveServerJobStatus.class); when(parentJob.getLogLevel()).thenReturn(LogLevel.BASIC); when(parentJobMeta.getRepositoryDirectory()).thenReturn(null); when(jobMeta.getRepositoryDirectory()).thenReturn(mock(RepositoryDirectoryInterface.class)); when(jobMeta.getName()).thenReturn(JOB_META_NAME); when(parentJob.getJobMeta()).thenReturn(parentJobMeta); when(parentJobMeta.findSlaveServer(REMOTE_SLAVE_SERVER_NAME)).thenReturn(slaveServer); when(slaveServer.getLogChannel()).thenReturn(log); when(log.getLogLevel()).thenReturn(LogLevel.BASIC); when(slaveServer.sendXML(anyString(), anyString())).thenReturn(REPLY); when(slaveServer.execService(anyString())).thenReturn(REPLY); when(slaveServer.getJobStatus(anyString(), anyString(), anyInt())).thenReturn(status); when(status.getResult()).thenReturn(mock(Result.class)); when(status.getLoggingString()).thenReturn(LOG); file = Files.createTempFile("file", ""); doReturn(LOG_FILE_NAME).when(job).getLogFilename(); doReturn(file.toString()).when(job).environmentSubstitute(LOG_FILE_NAME); doReturn(REMOTE_SLAVE_SERVER_NAME).when(job).environmentSubstitute(REMOTE_SLAVE_SERVER_NAME); doReturn(jobMeta).when(job).getJobMeta(any(Repository.class), any(VariableSpace.class)); doNothing().when(job).copyVariablesFrom(anyObject()); doNothing().when(job).setParentVariableSpace(anyObject()); job.setLogfile = true;//from ww w. j a va 2s. c o m job.createParentFolder = false; job.logFileLevel = LogLevel.BASIC; job.execPerRow = false; job.paramsFromPrevious = false; job.argFromPrevious = false; job.waitingToFinish = true; job.setSpecificationMethod(ObjectLocationSpecificationMethod.FILENAME); job.setRemoteSlaveServerName(REMOTE_SLAVE_SERVER_NAME); job.setParentJob(parentJob); job.setParentJobMeta(parentJobMeta); job.execute(new Result(), 0); String result = Files.lines(file).collect(Collectors.joining("")); assertTrue(result.contains(LOG)); }
From source file:org.omegat.filters.XLIFFFilterTest.java
@Test public void testTags() throws Exception { String f = "test/data/filters/xliff/file-XLIFFFilter-tags.xlf"; IProject.FileInfo fi = loadSourceFiles(filter, f); SourceTextEntry ste;//from w w w . j a va 2 s . c o m checkMultiStart(fi, f); checkMultiNoPrevNext("Link to <m0>http://localhost</m0>.", null, null, null); // #1988732 checkMultiNoPrevNext("About <b0>Gandalf</b0>", null, null, "7"); // #1988732 checkMultiNoPrevNext("<i0>Tags</i0> translation zz<i1>2</i1>z <b2>-NONTRANSLATED", null, null, null); checkMultiNoPrevNext("one <a0> two </b1> three <c2> four </d3> five", null, null, null); ste = checkMultiNoPrevNext("About <m0>Gandalf</m0> and <m1>other</m1>.", null, null, null); assertEquals(3, ste.getProtectedParts().length); assertEquals("<m0>Gandalf</m0>", ste.getProtectedParts()[0].getTextInSourceSegment()); assertEquals("<mrk mtype=\"protected\">Gandalf</mrk>", ste.getProtectedParts()[0].getDetailsFromSourceFile()); assertEquals("Gandalf", ste.getProtectedParts()[0].getReplacementMatchCalculation()); assertEquals("<m1>", ste.getProtectedParts()[1].getTextInSourceSegment()); assertEquals("<mrk mtype=\"other\">", ste.getProtectedParts()[1].getDetailsFromSourceFile()); assertEquals(StaticUtils.TAG_REPLACEMENT, ste.getProtectedParts()[1].getReplacementMatchCalculation()); assertEquals("</m1>", ste.getProtectedParts()[2].getTextInSourceSegment()); assertEquals("</mrk>", ste.getProtectedParts()[2].getDetailsFromSourceFile()); assertEquals(StaticUtils.TAG_REPLACEMENT, ste.getProtectedParts()[2].getReplacementMatchCalculation()); checkMultiNoPrevNext("one <o0>two</o0> three", null, null, null); checkMultiNoPrevNext("one <t0/> three", null, null, null); checkMultiNoPrevNext("one <w0/> three", null, null, null); checkMultiNoPrevNext("Nested tags: before <g0><g1><x2/></g1></g0> after", null, null, null); checkMultiNoPrevNext("<m0>Check protected-only tag reading</m0>", null, null, null); checkMultiEnd(); File inFile = new File("test/data/filters/xliff/file-XLIFFFilter-tags.xlf"); filter.translateFile(inFile, outFile, new TreeMap<String, String>(), context, new ITranslateCallback() { public String getTranslation(String id, String source, String path) { return source.replace("NONTRANSLATED", "TRANSLATED"); } public String getTranslation(String id, String source) { return source.replace("NONTRANSLATED", "TRANSLATED"); } public void linkPrevNextSegments() { } public void setPass(int pass) { } }); File trFile = new File(outFile.getPath() + "-translated"); List<String> lines = Files.lines(inFile.toPath()).map(line -> line.replace("NONTRANSLATED", "TRANSLATED")) .collect(Collectors.toList()); Files.write(trFile.toPath(), lines); compareXML(trFile, outFile); }
From source file:edumsg.core.PostgresConnection.java
public static void readConfFile() throws Exception { String file = System.getProperty("user.dir") + "/Postgres.conf"; java.util.List<String> lines = new ArrayList<String>(); //extract string between square brackets and compile regex for faster performance Pattern pattern = Pattern.compile("\\[(.+)\\]"); Matcher matcher;//w w w .j ava2s . c o m Exception e; Stream<String> stream = Files.lines(Paths.get(file)); lines = stream.filter(line -> !line.startsWith("#")).collect(Collectors.toList()); //set variables based on matches for (int i = 0; i < lines.size(); i++) { if (lines.get(i).contains("user")) { matcher = pattern.matcher(lines.get(i)); if (matcher.find()) setDBUser(matcher.group(1)); else throw e = new Exception("empty user in Postgres.conf"); } if (lines.get(i).contains("database")) { matcher = pattern.matcher(lines.get(i)); if (matcher.find()) setDBName(matcher.group(1)); else throw e = new Exception("empty database name in Postgres.conf"); } if (lines.get(i).contains("pass")) { matcher = pattern.matcher(lines.get(i)); matcher.find(); setDBPassword(matcher.group(1)); } if (lines.get(i).contains("host")) { matcher = pattern.matcher(lines.get(i)); if (matcher.find()) setDBHost(matcher.group(1)); else setDBHost("localhost"); } if (lines.get(i).contains("port")) { matcher = pattern.matcher(lines.get(i)); if (matcher.find()) setDBPort(matcher.group(1)); else setDBPort("5432"); } } if (!formatURL()) { e = new Exception("Wrong Format in Postgres.conf"); throw e; } }
From source file:it.serverSystem.ClusterTest.java
private static void expectLog(Orchestrator orchestrator, String expectedLog) throws IOException { File logFile = orchestrator.getServer().getWebLogs(); try (Stream<String> lines = Files.lines(logFile.toPath())) { assertThat(lines.anyMatch(s -> StringUtils.containsIgnoreCase(s, expectedLog))).isTrue(); }//from w w w .j a v a 2s . c om }