Java Code Examples for org.apache.commons.io.FilenameUtils.normalize()

The following are Jave code examples for showing how to use normalize() of the org.apache.commons.io.FilenameUtils class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: cloud-language-servers-container   File: WSSynchronization.java   View Source Code Vote up 5 votes
private void addNewFiles(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
	String artifactRelPath;
	artifactRelPath = request.getRequestURI().substring(request.getServletPath().length() + 1 );
	File destination = new File(FilenameUtils.normalize(this.saveDir + artifactRelPath));
	if (destination.exists()) {
		LOG.info("File to be added already exist: " + destination.getPath());
		response.setContentType("application/json");
		response.getWriter().append(String.format("{ \"error\": \"already exists %s\"}", destination.getPath()));
		response.setStatus(HttpServletResponse.SC_FORBIDDEN);
		return;
	}
	// Expected: one part containing zip
	try{
		Part part = request.getParts().iterator().next();
		WSChangeObserver changeObserver = new WSChangeObserver(ChangeType.CHANGE_CREATED, lspDestPath);
		if (extract(part.getInputStream(), changeObserver)) {
			changeObserver.notifyLSP();
			response.setContentType("application/json");
			response.getWriter().append(String.format("{ \"created\": \"%s\"}", artifactRelPath));
			response.setStatus(HttpServletResponse.SC_CREATED);
		} else {
			response.setContentType("application/json");
			response.getWriter().append(String.format("{ \"error\": \"conflict %s\"}", artifactRelPath));
			response.setStatus(HttpServletResponse.SC_CONFLICT);
		}
	} catch (NoSuchElementException ePart) {
		response.setContentType("application/json");
		response.getWriter().append(String.format("{ \"error\": \"exception for %s\"}", artifactRelPath));
		response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
	}
}
 
Example 2
Project: cloud-language-servers-container   File: WSSynchronization.java   View Source Code Vote up 5 votes
/**
 * @see HttpServlet#doPut(HttpServletRequest request, HttpServletResponse response)
 */
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	// Check if it is LSP registration
	String lspReg = request.getHeader("Register-lsp");
	if ( lspReg != null ) {
		handleLSPDest(Boolean.parseBoolean(lspReg), request.getReader());
		return;
	}

	// Otherwise process data passed from DI
	if( !checkSync() ) {
		response.setStatus(HttpServletResponse.SC_NO_CONTENT);
		return;
	}
	String artifactRelPath = request.getRequestURI().substring(request.getServletPath().length() + 1);
	String artifactPath = this.saveDir + artifactRelPath;

	File destination = new File(FilenameUtils.normalize(artifactPath));
	// Expected: one part containing zip
	try{
		Part part = request.getParts().iterator().next();

		WSChangeObserver changeObserver = new WSChangeObserver(ChangeType.CHANGE_UPDATED, lspDestPath);
		if (destination.exists() && !destination.isDirectory() && extract(part.getInputStream(), changeObserver)) {
			changeObserver.notifyLSP();
			response.setContentType("application/json");
			response.getWriter().append(String.format("{ \"updated\": \"%s\"}", artifactRelPath));
			response.setStatus(HttpServletResponse.SC_OK);
		} else {
			response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
		}
	} catch (NoSuchElementException ePart) {
		response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
	}
}
 
Example 3
Project: alfresco-repository   File: FileContentStore.java   View Source Code Vote up 5 votes
private void ensureFileInContentStore(File file)
{
    String fileNormalizedAbsoultePath = FilenameUtils.normalize(file.getAbsolutePath());
    String rootNormalizedAbsolutePath = FilenameUtils.normalize(rootAbsolutePath);
    
    if (!fileNormalizedAbsoultePath.startsWith(rootNormalizedAbsolutePath))
    {
        throw new ContentIOException("Access to files outside of content store root is not allowed: " + file);
    }
}
 
Example 4
Project: ZooKeeper   File: QuorumKerberosHostBasedAuthTest.java   View Source Code Vote up 5 votes
private static void setupJaasConfigEntries(String hostServerPrincipal,
        String hostLearnerPrincipal, String hostNamedLearnerPrincipal) {
    String keytabFilePath = FilenameUtils.normalize(KerberosTestUtils.getKeytabFile(), true);
    String jaasEntries = new String(""
            + "QuorumServer {\n"
            + "       com.sun.security.auth.module.Krb5LoginModule required\n"
            + "       useKeyTab=true\n"
            + "       keyTab=\"" + keytabFilePath + "\"\n"
            + "       storeKey=true\n"
            + "       useTicketCache=false\n"
            + "       debug=true\n"
            + "       doNotPrompt=true\n"
            + "       refreshKrb5Config=true\n"
            + "       principal=\"" + KerberosTestUtils.replaceHostPattern(hostServerPrincipal) + "\";\n" + "};\n"
            + "QuorumLearner {\n"
            + "       com.sun.security.auth.module.Krb5LoginModule required\n"
            + "       useKeyTab=true\n"
            + "       keyTab=\"" + keytabFilePath + "\"\n"
            + "       storeKey=true\n"
            + "       useTicketCache=false\n"
            + "       debug=true\n"
            + "       doNotPrompt=true\n"
            + "       refreshKrb5Config=true\n"
            + "       isInitiator=true\n"
            + "       principal=\"" + KerberosTestUtils.replaceHostPattern(hostLearnerPrincipal) + "\";\n" + "};\n"
            + "QuorumLearnerMyHost {\n"
            + "       com.sun.security.auth.module.Krb5LoginModule required\n"
            + "       useKeyTab=true\n"
            + "       keyTab=\"" + keytabFilePath + "\"\n"
            + "       storeKey=true\n"
            + "       useTicketCache=false\n"
            + "       debug=true\n"
            + "       doNotPrompt=true\n"
            + "       refreshKrb5Config=true\n"
            + "       isInitiator=true\n"
            + "       principal=\"" + hostNamedLearnerPrincipal + "\";\n" + "};\n");
    setupJaasConfig(jaasEntries);
}
 
Example 5
Project: karate   File: FeatureBrowsePanel.java   View Source Code Vote up 4 votes
private static File normalize(File file) {
    String path = file.getAbsolutePath();
    return new File(FilenameUtils.normalize(path));
}
 
Example 6
Project: careconnect-reference-implementation   File: BaseCommand.java   View Source Code Vote up 4 votes
protected Collection<File> loadFile(FhirContext theCtx, String theSpecUrl, String theFilepath, boolean theCacheFile) throws IOException {
	String userHomeDir = System.getProperty("user.home");

	File applicationDir = new File(userHomeDir + File.separator + "." + "hapi-fhir-cli");
	FileUtils.forceMkdir(applicationDir);

	Collection<File> inputFiles;
	if (isNotBlank(theFilepath)) {
		ourLog.info("Loading from local path: {}", theFilepath);

		if (theFilepath.startsWith("~" + File.separator)) {
			theFilepath = userHomeDir + theFilepath.substring(1);
		}

		File suppliedFile = new File(FilenameUtils.normalize(theFilepath));

		if (suppliedFile.isDirectory()) {
			inputFiles = FileUtils.listFiles(suppliedFile, new String[]{"zip"}, false);
		} else {
			inputFiles = Collections.singletonList(suppliedFile);
		}

	} else {

		File cacheDir = new File(applicationDir, "cache");
		FileUtils.forceMkdir(cacheDir);

		File inputFile = new File(cacheDir, "examples-json-" + theCtx.getVersion().getVersion() + ".zip");

		Date cacheExpiryDate = DateUtils.addHours(new Date(), -12);

		if (!inputFile.exists() | (theCacheFile && FileUtils.isFileOlder(inputFile, cacheExpiryDate))) {

			File exampleFileDownloading = new File(cacheDir, "examples-json-" + theCtx.getVersion().getVersion() + ".zip.partial");

			HttpGet get = new HttpGet(theSpecUrl);
			CloseableHttpClient client = HttpClientBuilder.create().build();
			CloseableHttpResponse result = client.execute(get);

			if (result.getStatusLine().getStatusCode() != 200) {
				throw new CommandFailureException("Got HTTP " + result.getStatusLine().getStatusCode() + " response code loading " + theSpecUrl);
			}

			ourLog.info("Downloading from remote url: {}", theSpecUrl);
			downloadFileFromInternet(result, exampleFileDownloading);

			FileUtils.deleteQuietly(inputFile);
			FileUtils.moveFile(exampleFileDownloading, inputFile);

			if (!theCacheFile) {
				inputFile.deleteOnExit();
			}

			ourLog.info("Successfully Loaded example pack ({})", FileUtils.byteCountToDisplaySize(FileUtils.sizeOf(inputFile)));
			IOUtils.closeQuietly(result.getEntity().getContent());
		}

		inputFiles = Collections.singletonList(inputFile);

	}
	return inputFiles;
}