Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package server;
- import java.io.*;
- import java.util.regex.Pattern;
- /**
- * @author Piotr Kawa
- * @since 09.06.2017
- */
- class FileHandler {
- int option;
- private final Pattern PATTERN = Pattern.compile("^[a-zA-Z0-9_/.]+$");
- String searchedFile;
- File file;
- public FileHandler(){}
- /**
- * Method converts .html file to a String in order to create a response.
- * @param fileName - path to a file that will be converted
- * @return content - HTML file converted to a String
- * */
- String prepareFileForSending(String fileName){
- String content;
- StringBuilder responseBodyBuilder = new StringBuilder();
- try {
- BufferedReader fileToConvert = new BufferedReader(new FileReader(fileName));
- String line;
- while ((line = fileToConvert.readLine()) != null) {
- responseBodyBuilder.append(line);
- }
- fileToConvert.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- content = responseBodyBuilder.toString();
- return content;
- }
- /**
- * Method checks whether requested file is valid, there are 3 acceptable options
- * 1) "/" - requests index from root directory
- * 2) "/abc" - requests abc file from root directory
- * 3) "/abc/" - requests index from abc directory
- *
- * @return true - requested file is valid, otherwise false
- *
- * */
- public boolean validateRequestedFile(String requestedFile){
- System.out.println("REQUEST = " + requestedFile);
- int length = requestedFile.length()-1;
- String second = requestedFile.substring(1);
- if (requestedFile.compareTo("/") == 0){
- option = 1;
- return true;
- } else if(requestedFile.charAt(0) == '/' && PATTERN.matcher(second).matches() && requestedFile.charAt(length) != '/'){
- option = 2;
- return true;
- } else if(requestedFile.charAt(0) == '/' && PATTERN.matcher(second).matches() && requestedFile.charAt(length) == '/'){
- option = 3;
- return true;
- } else {
- return false;
- }
- // option = 2;
- // return true;
- }
- /**
- * Method that searches for a given file.
- * @param requestedFile - given file, if it contains a '/' at the end - look for a index file in a suitable subdirectory, othwerwise
- * look for a given file in root directory
- * @return true if a file is found
- * */
- public boolean serverContainsFile(String requestedFile) throws Exception{
- int length = requestedFile.length()-1;
- String second = requestedFile.substring(1);
- String path;
- /*TODO: somenthing wrong with slashes*/
- /*TODO: implement using RegEx*/
- if(option == 1){
- //searched file is index in root directory
- searchedFile = "index.html";
- file = new File("D:\\PIOTR\\PROGRAMOWANIE\\sem4\\SIECI\\lista5\\SiteContent\\" + searchedFile);
- // System.out.println(searchedFile);
- } else if(option == 2){
- searchedFile = requestedFile.substring(1);
- //searched file is index in subdirectory
- file = new File("D:\\PIOTR\\PROGRAMOWANIE\\sem4\\SIECI\\lista5\\SiteContent\\" + searchedFile);
- // System.out.println("looking for " + searchedFile);
- } else if (option == 3) {
- //erase '/' from requested file
- path = requestedFile.substring(1,length);
- StringBuffer abc = new StringBuffer(path);
- abc.append("\\");
- System.out.println("abc = " + abc);
- file = new File("D:\\PIOTR\\PROGRAMOWANIE\\sem4\\SIECI\\lista5\\SiteContent\\" + abc);
- searchedFile = "index";
- // System.out.println(searchedFile);
- } else {
- throw new Exception("Error while processing file!");
- }
- return file.exists();
- }
- public String getRequestedFilePath(){
- return file.toString();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement