List of usage examples for java.util.regex Matcher quoteReplacement
public static String quoteReplacement(String s)
From source file:org.openhab.tools.analysis.checkstyle.PackageExportsNameCheck.java
/** * Filter and return the packages from the source directory. Only not excluded packages will be returned. * * @param sourcePath - The full path of the source directory * @return {@link Set } of {@link String }s with the package names. * @throws IOException if an I/O error is thrown while visiting files */// w w w . java 2 s.c o m private Set<String> getFilteredPackagesFromSourceDirectory(Path sourcePath) throws IOException { Set<String> packages = new HashSet<>(); // No symbolic links are expected in the source directory if (Files.exists(sourcePath, LinkOption.NOFOLLOW_LINKS)) { Files.walkFileTree(sourcePath, new SimpleFileVisitor<Path>() { @Override public FileVisitResult visitFile(Path path, BasicFileAttributes attrs) { Path packageRelativePath = sourcePath.relativize(path.getParent()); String packageName = packageRelativePath.toString() .replaceAll(Matcher.quoteReplacement(File.separator), "."); if (!isExcluded(packageName)) { packages.add(packageName); } return FileVisitResult.CONTINUE; } }); } return packages; }
From source file:org.opencastproject.capture.impl.XProperties.java
/** * Wrapper around the actual search and replace functionality. This function will value with all of the instances of * subkey replaced.//from w w w . j ava 2 s . c o m * * @param value * The original string you wish to replace. * @param subkey * The substring you wish to replace. This must be the substring rather than the full variable - M2_REPO * rather than ${M2_REPO} * @return The value string with all instances of subkey replaced, or null in the case of an error. */ private String findReplacement(String value, String subkey) { if (subkey == null) { return null; } Pattern p = Pattern.compile(START_REPLACEMENT + subkey + END_REPLACEMENT, Pattern.LITERAL); String replacement = null; if (System.getProperty(subkey) != null) { replacement = System.getProperty(subkey); } else if (this.getProperty(subkey) != null) { replacement = this.getProperty(subkey); } else if (this.context != null && this.bundle != null && this.bundle.getState() == Bundle.ACTIVE && this.context.getProperty(subkey) != null) { replacement = this.context.getProperty(subkey); } else if (System.getenv(subkey) != null) { replacement = System.getenv(subkey); } if (replacement != null) return p.matcher(value).replaceAll(Matcher.quoteReplacement(replacement)); else return null; }
From source file:hydrograph.engine.spark.datasource.utils.FTPUtil.java
public void upload(RunFileTransferEntity runFileTransferEntity) { log.debug("Start FTPUtil upload"); FTPClient ftpClient = new FTPClient(); ftpClient.enterLocalPassiveMode();/*w ww .ja va2 s. co m*/ ftpClient.setBufferSize(1024000); int retryAttempt = runFileTransferEntity.getRetryAttempt(); int attemptCount = 1; int i = 0; InputStream inputStream = null; boolean login = false; File filecheck = new File(runFileTransferEntity.getInputFilePath()); log.info("input file name" + filecheck.getName()); if (runFileTransferEntity.getFailOnError()) { if (!(filecheck.isFile() || filecheck.isDirectory()) && !(runFileTransferEntity.getInputFilePath().contains("hdfs://"))) { log.error("Invalid input file path. Please provide valid input file path."); throw new FTPUtilException("Invalid input file path"); } } boolean done = false; for (i = 0; i < retryAttempt; i++) { try { log.info("Connection attempt: " + (i + 1)); if (runFileTransferEntity.getTimeOut() != 0) if (runFileTransferEntity.getEncoding() != null) ftpClient.setControlEncoding(runFileTransferEntity.getEncoding()); ftpClient.setConnectTimeout(runFileTransferEntity.getTimeOut()); log.debug("connection details: " + "/n" + "Username: " + runFileTransferEntity.getUserName() + "/n" + "HostName " + runFileTransferEntity.getHostName() + "/n" + "Portno" + runFileTransferEntity.getPortNo()); ftpClient.connect(runFileTransferEntity.getHostName(), runFileTransferEntity.getPortNo()); login = ftpClient.login(runFileTransferEntity.getUserName(), runFileTransferEntity.getPassword()); if (!login) { log.error("Invalid FTP details provided. Please provide correct FTP details."); throw new FTPUtilException("Invalid FTP details"); } ftpClient.enterLocalPassiveMode(); ftpClient.setFileType(FTP.BINARY_FILE_TYPE); if (runFileTransferEntity.getInputFilePath().contains("hdfs://")) { log.debug("Processing for HDFS input file path"); String inputPath = runFileTransferEntity.getInputFilePath(); String s1 = inputPath.substring(7, inputPath.length()); String s2 = s1.substring(0, s1.indexOf("/")); int index = runFileTransferEntity.getInputFilePath() .replaceAll(Matcher.quoteReplacement("\\"), "/").lastIndexOf('/'); String file_name = runFileTransferEntity.getInputFilePath().substring(index + 1); File f = new File("/tmp"); if (!f.exists()) f.mkdir(); Configuration conf = new Configuration(); conf.set("fs.defaultFS", "hdfs://" + s2); FileSystem hdfsFileSystem = FileSystem.get(conf); Path local = new Path("/tmp"); String s = inputPath.substring(7, inputPath.length()); String hdfspath = s.substring(s.indexOf("/"), s.length()); File dir = new File(hdfspath); Random ran = new Random(); String tempFolder = "ftp_sftp_" + System.nanoTime() + "_" + ran.nextInt(1000); File dirs = new File("/tmp/" + tempFolder); boolean success = dirs.mkdirs(); if (hdfsFileSystem.isDirectory(new Path(hdfspath))) { log.debug("Provided HDFS input path is for directory."); InputStream is = null; OutputStream os = null; String localDirectory = hdfspath.substring(hdfspath.lastIndexOf("/") + 1); FileStatus[] fileStatus = hdfsFileSystem .listStatus(new Path(runFileTransferEntity.getInputFilePath())); Path[] paths = FileUtil.stat2Paths(fileStatus); try { String folderName = hdfspath.substring(hdfspath.lastIndexOf("/") + 1); Path hdfs = new Path(hdfspath); for (Path file : paths) { is = hdfsFileSystem.open(file); os = new BufferedOutputStream( new FileOutputStream(dirs + "" + File.separatorChar + file.getName())); IOUtils.copyBytes(is, os, conf); } ftpClient.changeWorkingDirectory(runFileTransferEntity.getOutFilePath() .replaceAll(Matcher.quoteReplacement("\\"), "/")); ftpClient.removeDirectory(folderName); ftpClient.makeDirectory(folderName); ftpClient.changeWorkingDirectory(runFileTransferEntity.getOutFilePath().replaceAll( Matcher.quoteReplacement("\\"), "/") + File.separatorChar + folderName); for (File files : dirs.listFiles()) { if (files.isFile()) ftpClient.storeFile(files.getName().toString(), new BufferedInputStream(new FileInputStream(files))); } } catch (IOException e) { log.error("Failed while doing FTP file", e); //throw e; } finally { IOUtils.closeStream(is); IOUtils.closeStream(os); if (dirs != null) { FileUtils.deleteDirectory(dirs); } } } else { try { Path hdfs = new Path(hdfspath); hdfsFileSystem.copyToLocalFile(false, hdfs, local); inputStream = new FileInputStream(dirs + file_name); ftpClient.storeFile(file_name, new BufferedInputStream(inputStream)); } catch (Exception e) { log.error("Failed while doing FTP file", e); throw new FTPUtilException("Failed while doing FTP file", e); } finally { FileUtils.deleteDirectory(dirs); } } } else { java.nio.file.Path file = new File(runFileTransferEntity.getInputFilePath()).toPath(); if (Files.isDirectory(file)) { log.debug("Provided input file path is for directory"); File dir = new File(runFileTransferEntity.getInputFilePath()); String folderName = new File(runFileTransferEntity.getInputFilePath()).getName(); ftpClient.changeWorkingDirectory(runFileTransferEntity.getOutFilePath() .replaceAll(Matcher.quoteReplacement("\\"), "/")); try { ftpClient.removeDirectory(folderName); } catch (IOException e) { log.error("Failed while doing FTP file", e); throw new FTPUtilException("Failed while doing FTP file", e); } ftpClient.makeDirectory(folderName); ftpClient.changeWorkingDirectory(runFileTransferEntity.getOutFilePath() .replaceAll(Matcher.quoteReplacement("\\"), "/") + "/" + folderName); for (File files : dir.listFiles()) { if (files.isFile()) ftpClient.storeFile(files.getName().toString(), new BufferedInputStream(new FileInputStream(files))); } } else { inputStream = new FileInputStream(runFileTransferEntity.getInputFilePath()); ftpClient.changeWorkingDirectory(runFileTransferEntity.getOutFilePath() .replaceAll(Matcher.quoteReplacement("\\"), "/")); int index = runFileTransferEntity.getInputFilePath() .replaceAll(Matcher.quoteReplacement("\\"), "/").lastIndexOf('/'); String file_name = runFileTransferEntity.getInputFilePath().substring(index + 1); ftpClient.storeFile(file_name, new BufferedInputStream(inputStream)); } } } catch (Exception e) { log.error("Failed while doing FTP file", e); if (!login && runFileTransferEntity.getFailOnError()) { throw new FTPUtilException("Invalid FTP details"); } try { Thread.sleep(runFileTransferEntity.getRetryAfterDuration()); } catch (Exception e1) { log.error("Failed while sleeping for retry duration", e1); } continue; } finally { try { if (inputStream != null) inputStream.close(); } catch (IOException ioe) { } } done = true; break; } try { if (ftpClient != null) { ftpClient.logout(); ftpClient.disconnect(); } } catch (Exception e) { log.error("Failed while clossing the connection", e); } catch (Error e) { log.error("Failed while clossing the connection", e); //throw new RuntimeException(e); } if (runFileTransferEntity.getFailOnError() && !done) { log.error("File transfer failed"); throw new FTPUtilException("File transfer failed"); } else if (!done) { log.error("File transfer failed but mentioned fail on error as false"); } log.debug("Finished FTPUtil upload"); }
From source file:hydrograph.engine.spark.datasource.utils.SFTPUtil.java
public void upload(RunFileTransferEntity runFileTransferEntity) { log.debug("Start SFTPUtil upload"); JSch jsch = new JSch(); Session session = null;/*from w ww . j av a 2 s .c o m*/ Channel channel = null; ChannelSftp sftpChannel = null; ZipInputStream zip = null; FileInputStream fin = null; int retryAttempt = 0; int i; File filecheck = new File(runFileTransferEntity.getInputFilePath()); if (runFileTransferEntity.getFailOnError()) if (!(filecheck.isFile() || filecheck.isDirectory()) && !(runFileTransferEntity.getInputFilePath().contains("hdfs://"))) { log.error("invalid input file path,Please provide valid file path"); throw new SFTPUtilException("Invalid input file path"); } if (runFileTransferEntity.getRetryAttempt() == 0) retryAttempt = 1; else retryAttempt = runFileTransferEntity.getRetryAttempt(); for (i = 0; i < retryAttempt; i++) { try { log.info("connection attempt: " + (i + 1)); if (runFileTransferEntity.getPrivateKeyPath() != null) { jsch.addIdentity(runFileTransferEntity.getPrivateKeyPath()); } log.debug("connection details: " + "/n" + "Username: " + runFileTransferEntity.getUserName() + "/n" + "HostName " + runFileTransferEntity.getHostName() + "/n" + "Portno" + runFileTransferEntity.getPortNo()); session = jsch.getSession(runFileTransferEntity.getUserName(), runFileTransferEntity.getHostName(), runFileTransferEntity.getPortNo()); session.setConfig("PreferredAuthentications", "publickey,keyboard-interactive,password"); session.setConfig("StrictHostKeyChecking", "no"); if (runFileTransferEntity.getPassword() != null) { session.setPassword(runFileTransferEntity.getPassword()); } if (runFileTransferEntity.getTimeOut() > 0) { session.setTimeout(runFileTransferEntity.getTimeOut()); } session.connect(); channel = session.openChannel("sftp"); channel.connect(); sftpChannel = (ChannelSftp) channel; sftpChannel.setFilenameEncoding(runFileTransferEntity.getEncoding()); sftpChannel .cd(runFileTransferEntity.getOutFilePath().replaceAll(Matcher.quoteReplacement("\\"), "/")); if (runFileTransferEntity.getInputFilePath().contains("hdfs://")) { log.debug("in hdfs file system transfer"); String inputPath = runFileTransferEntity.getInputFilePath(); File f = new File("/tmp"); if (!f.exists()) f.mkdir(); String s1 = inputPath.substring(7, inputPath.length()); String s2 = s1.substring(0, s1.indexOf("/")); Configuration conf = new Configuration(); conf.set("fs.defaultFS", "hdfs://" + s2); FileSystem hdfsFileSystem = FileSystem.get(conf); Path local = new Path("/tmp"); String s = inputPath.substring(7, inputPath.length()); String hdfspath = s.substring(s.indexOf("/"), s.length()); File dir = new File(hdfspath); if (hdfsFileSystem.isDirectory(new Path(hdfspath))) { log.debug("in hdfs file system folder path"); InputStream is = null; OutputStream os = null; String localDirectory = hdfspath.substring(hdfspath.lastIndexOf("/") + 1); FileStatus[] fileStatus = hdfsFileSystem .listStatus(new Path(runFileTransferEntity.getInputFilePath())); Path[] paths = FileUtil.stat2Paths(fileStatus); File dirs = null; try { String folderName = hdfspath.substring(hdfspath.lastIndexOf("/") + 1); DateFormat df = new SimpleDateFormat("dd-MM-yyyy"); String dateWithoutTime = df.format(new Date()).toString(); java.util.Random ran = new Random(); String tempFolder = "ftp_sftp_" + System.nanoTime() + "_" + ran.nextInt(1000); dirs = new File("/tmp/" + tempFolder); boolean success = dirs.mkdirs(); for (Path file : paths) { is = hdfsFileSystem.open(file); os = new BufferedOutputStream( new FileOutputStream(dirs + "" + File.separatorChar + file.getName())); IOUtils.copyBytes(is, os, conf); } try { sftpChannel.cd(folderName); } catch (SftpException e) { sftpChannel.mkdir(folderName); sftpChannel.cd(folderName); } for (File files : dirs.listFiles()) { if (files.isFile()) if (files.isFile()) { sftpChannel.put(new BufferedInputStream(new FileInputStream(files)), files.getName()); } } } catch (IOException e) { log.error("error while transferring file", e); throw new SFTPUtilException(e); } finally { IOUtils.closeStream(is); IOUtils.closeStream(os); if (dirs != null) { FileUtils.deleteDirectory(dirs); } } } else { log.debug("File transfer in normal mode"); Path hdfs = new Path(hdfspath); hdfsFileSystem.copyToLocalFile(false, hdfs, local); int index = inputPath.replaceAll(Matcher.quoteReplacement("\\"), "/").lastIndexOf('/'); String file_name = runFileTransferEntity.getInputFilePath().substring(index + 1); fin = new FileInputStream("/tmp/" + file_name); sftpChannel.cd(runFileTransferEntity.getOutFilePath() .replaceAll(Matcher.quoteReplacement("\\"), "/")); sftpChannel.put(new BufferedInputStream(fin), file_name); i = i + 1; fin.close(); } } else { java.nio.file.Path file = new File(runFileTransferEntity.getInputFilePath()).toPath(); if (Files.isDirectory(file)) { log.debug("Folder transfer in SFTP"); File f = new File(file.toAbsolutePath().toString()); String folderName = new File(runFileTransferEntity.getInputFilePath()).getName(); sftpChannel.cd(runFileTransferEntity.getOutFilePath() .replaceAll(Matcher.quoteReplacement("\\"), "/")); try { sftpChannel.cd(folderName); } catch (SftpException e) { log.error( "changing the directory but the folde location not found,so create a new directory"); sftpChannel.cd(runFileTransferEntity.getOutFilePath() .replaceAll(Matcher.quoteReplacement("\\"), "/")); sftpChannel.mkdir(folderName); sftpChannel.cd(folderName); } for (File files : f.listFiles()) { if (files.isFile()) sftpChannel.put(new BufferedInputStream(new FileInputStream(files)), files.getName()); } } else { int index = runFileTransferEntity.getInputFilePath() .replaceAll(Matcher.quoteReplacement("\\"), "/").lastIndexOf('/'); String file_name = runFileTransferEntity.getInputFilePath().substring(index + 1); fin = new FileInputStream(runFileTransferEntity.getInputFilePath()); sftpChannel.cd(runFileTransferEntity.getOutFilePath() .replaceAll(Matcher.quoteReplacement("\\"), "/")); sftpChannel.put(new BufferedInputStream(fin), file_name); fin.close(); } } } catch (JSchException e) { if (e.getMessage().compareTo("Auth fail") == 0) { log.error("authentication error,please provide valid details", e); if (runFileTransferEntity.getFailOnError()) throw new SFTPUtilException(e.getMessage()); } else { log.error("error while transfering the file and retrying ", e); try { Thread.sleep(runFileTransferEntity.getRetryAfterDuration()); } catch (Exception e1) { log.error("sleep duration for re attemp exception", e1); } continue; } } catch (Exception e) { log.error("Error while transfering the file", e); try { Thread.sleep(runFileTransferEntity.getRetryAfterDuration()); } catch (Exception e1) { log.error("exception while sleep thread", e); } continue; } finally { try { if (fin != null) fin.close(); } catch (IOException ioe) { log.error("error while closing input stream "); } } done = true; break; } if (sftpChannel != null) { sftpChannel.disconnect(); } if (channel != null) { channel.disconnect(); } if (session != null) { session.disconnect(); } if (runFileTransferEntity.getFailOnError() && !done) { log.error("File transfer failed"); throw new SFTPUtilException("File transfer failed"); } else if (!done) { log.error("File transfer failed but mentioned fail on error as false"); } log.debug("Fininished SFTPUtil upload"); }
From source file:org.omnaest.utils.codec.EncoderAndDecoderEscaping.java
@Override public String encode(String source) { String retval = source;//from www . ja va 2s . c o m { if (retval != null) { for (String encodedCharacter : this.encodedCharacterToEscapeSequenceMap.keySet()) { final String escapeSequence = this.encodedCharacterToEscapeSequenceMap.get(encodedCharacter); retval = retval.replaceAll(Pattern.quote(encodedCharacter), Matcher.quoteReplacement(escapeSequence)); } } } return retval; }
From source file:org.springframework.integration.sftp.outbound.SftpServerOutboundTests.java
@Test public void testInt2866LocalDirectoryExpressionGET() { Session<?> session = this.sessionFactory.getSession(); String dir = "sftpSource/"; long modified = setModifiedOnSource1(); this.inboundGet.send(new GenericMessage<Object>(dir + " sftpSource1.txt")); Message<?> result = this.output.receive(1000); assertNotNull(result);// ww w . j a v a 2 s . co m File localFile = (File) result.getPayload(); assertThat(localFile.getPath().replaceAll(Matcher.quoteReplacement(File.separator), "/"), containsString(dir.toUpperCase())); assertPreserved(modified, localFile); dir = "sftpSource/subSftpSource/"; this.inboundGet.send(new GenericMessage<Object>(dir + "subSftpSource1.txt")); result = this.output.receive(1000); assertNotNull(result); localFile = (File) result.getPayload(); assertThat(localFile.getPath().replaceAll(Matcher.quoteReplacement(File.separator), "/"), containsString(dir.toUpperCase())); Session<?> session2 = this.sessionFactory.getSession(); assertSame(TestUtils.getPropertyValue(session, "targetSession.jschSession"), TestUtils.getPropertyValue(session2, "targetSession.jschSession")); }
From source file:org.apache.nifi.processors.standard.util.SFTPUtils.java
public static void changeWorkingDirectory(final ChannelSftp sftp, final String dirPath, final boolean createDirs, final Processor proc) throws IOException { final Deque<String> stack = new LinkedList<>(); File dir = new File(dirPath); String currentWorkingDirectory = null; boolean dirExists = false; final String forwardPaths = dir.getPath().replaceAll(Matcher.quoteReplacement("\\"), Matcher.quoteReplacement("/")); try {/* w w w. j a v a 2 s .co m*/ currentWorkingDirectory = sftp.pwd(); logger.debug(proc + " attempting to change directory from " + currentWorkingDirectory + " to " + dir.getPath()); //always use forward paths for long string attempt sftp.cd(forwardPaths); dirExists = true; logger.debug(proc + " changed working directory to '" + forwardPaths + "' from '" + currentWorkingDirectory + "'"); } catch (final SftpException sftpe) { logger.debug(proc + " could not change directory to '" + forwardPaths + "' from '" + currentWorkingDirectory + "' so trying the hard way."); } if (dirExists) { return; } if (!createDirs) { throw new IOException("Unable to change to requested working directory \'" + forwardPaths + "\' but not configured to create dirs."); } do { stack.push(dir.getName()); } while ((dir = dir.getParentFile()) != null); String dirName = null; while ((dirName = stack.peek()) != null) { stack.pop(); //find out if exists, if not make it if configured to do so or throw exception dirName = ("".equals(dirName.trim())) ? "/" : dirName; try { sftp.cd(dirName); } catch (final SftpException sftpe) { logger.debug(proc + " creating new directory and changing to it " + dirName); try { sftp.mkdir(dirName); sftp.cd(dirName); } catch (final SftpException e) { throw new IOException(proc + " could not make/change directory to [" + dirName + "] [" + e.getLocalizedMessage() + "]", e); } } } }
From source file:org.wso2.cep.integration.common.utils.CEPIntegrationTest.java
/** * @param testCaseFolderName Name of the folder created under /artifacts/CEP for the particular test case. * @param configFileName Name of the XML config-file created under above folder. * @return The above XML-configuration, as a string * @throws Exception//from w ww .j a v a 2 s. c o m */ protected String getXMLArtifactConfiguration(String testCaseFolderName, String configFileName) throws Exception { String relativeFilePath = getTestArtifactLocation() + CEPIntegrationTestConstants.RELATIVE_PATH_TO_TEST_ARTIFACTS + testCaseFolderName + "/" + configFileName; relativeFilePath = relativeFilePath.replaceAll("[\\\\/]", Matcher.quoteReplacement(File.separator)); OMElement configElement = loadClasspathResourceXML(relativeFilePath); return configElement.toString(); }
From source file:com.vmware.o11n.plugin.powershell.model.generate.ScriptActionGenerator.java
private String extractParameters(String script, ScriptModuleBuilder builder) { // first escape the PS script script = script.replace("'", "\\'"); //Then look up for {#paramname#} and extract them Pattern pattern = Pattern.compile("\\{#([\\w]*)#\\}"); Matcher matcher = pattern.matcher(script); StringBuffer sb = new StringBuffer(); while (matcher.find()) { String inputName = matcher.group(1); // create input param builder.addParameter(inputName, "String"); matcher.appendReplacement(sb, Matcher.quoteReplacement("' + " + inputName + " + '")); }//from w ww . j av a 2 s . c o m matcher.appendTail(sb); return sb.toString(); }