Home     RSSRSS

Archives: Others

Java Utility – Rename Files in Folder

November 1, 2019 by kiranbadi1991 | Comments Off on Java Utility – Rename Files in Folder | Filed in Development, Others
import java.io.File;
import java.io.IOException;

public class FileRename {

	public static void main(String args[]) throws IOException {		
		String DIR_PATH = "MyDirectoryPath";
		String stringToReplace = "MyStringtoReplaceInFileName";
		getFileNames(DIR_PATH, stringToReplace);
	}

	public static void getFileNames(String Path, String stringToReplace)
			throws IOException {
		File folder = new File(Path);
		File[] listOfFiles = folder.listFiles();
		System.out.println("Total Files :" + listOfFiles.length);
		for (int i = 0; i < listOfFiles.length; i++) {
			if (listOfFiles[i].isFile()) {
				System.out.println("Original File name is : " + listOfFiles[i].getName());
			} else if (listOfFiles[i].isDirectory()) {
				System.out.println("Directory " + listOfFiles[i].getName());
			}
			renameFile(listOfFiles[i], stringToReplace);

		}
	}

	public static void renameFile(File fileItem, String stringToReplace)
			throws IOException {
		String fileName = fileItem.getName();
		if (fileName.contains(stringToReplace)) {
			String newName = fileName.replace(stringToReplace, "");
			File fileWithNewName = new File(fileItem.getParent(), newName);
			if (fileWithNewName.exists()) {
				System.out.println("File cannot be renamed as 2 files exist with same name so deleting 1 file " + fileWithNewName.getName());
				fileWithNewName.delete();
			}
			boolean success = fileItem.renameTo(fileWithNewName);
			System.out.println("New FileName is " + fileWithNewName.getName());
			if (!success) {
				System.out.println("File cannot be renamed " + fileWithNewName.getName());
			}
		}

	}

}

Tags:

Special HTTP Verb to request and view Documents online

May 28, 2019 by kiranbadi1991 | Comments Off on Special HTTP Verb to request and view Documents online | Filed in Development, Others, Process, Project Management, Security

First American Financial Corp’s recently exposed good amount of data(885 million files related to real estate data) to the external folks.Probably this leak impacted close 885 million folks assuming each file relates to one property and one person or family.

The leak mainly happened by modifying the URL parameter of the request. I have written quite a bit of code to fetch some files for viewing online, downloading etc. etc..

Most common way of writing these type of functionality is that you write a controller class to get file for viewing(render document on browser for viewing),you have some service layer which populates file template, and then some data access layer which either fetches data based on some id from database or cache store to populate the template.All these steps along document viewing happens most frequently via GET verb. Of Course you can have secured GET requests.

The whole purpose of GET verb is to fetch the resource which it does as described  in specs. However I feel document viewing is special type of use case which I believes requires a special http verb to deal which browsers most of the times can provide inbuilt protection for cases like First American Financial Corp (similar as Browsers prompt for double submits).

This verb will help to solve other issues as well like malware, excessive ads, virus problems, etc. etc.. This verb will make web much cleaner.

PS : Though I know that First American Financial Corp’s leak is due to poor development practices but I believe that generally browser or Web specs should provide first level of defense against these types of lapses.

Façade Pattern

May 1, 2018 by kiranbadi1991 | Comments Off on Façade Pattern | Filed in Others

Façade pattern is used to hide the complexity of the sub system and provide users with the simple and easy to use interface to consume.

Commonly used use cases for Façade are

  • Provide simple and easy to use interface to backend legacy system.
  • Sometimes used to build the public interface for users to consume.
  • To abstract the complexity of the system for simplicity and security
  • Depending on use case, it can also be used to improve performance by reducing frequent call invocation and providing remote client single point of access by grouping related functionality in the subset of classes.

Façade Pattern can be implemented for Pojo, stateful and stateless use cases. However for stateful classes it consumes the server resources and ties up to the client during invocation process. So one needs to careful that client do not take up too much of time for processing else server resources will be consumed. Generally if conversational state needs to be maintained then stateful façade can be used.

Generally complexity of the logic dictates the length and abstraction of the wrapper methods in the façade.

Facades can also be used to create factory methods.

Example of Facade can be.

public class Car {

public void 4CylinderEngine(){

	// Methods the exhibit behavior of 4 Cylinder
	}

public void 6CylinderEngine(){
		// Methods the exhibit behavior of 6 Cylinder
	}

}
 
// Using the facade

new Car.4CyclinderEngine();
new Car.6CyclinderEngine();