Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.19 KB | None | 0 0
  1. package server;
  2.  
  3. import java.io.*;
  4. import java.util.regex.Pattern;
  5.  
  6.  
  7.  
  8. /**
  9.  * @author Piotr Kawa
  10.  * @since 09.06.2017
  11.  */
  12.  
  13. class FileHandler {
  14.  
  15.  
  16.     int option;
  17.     private final Pattern PATTERN = Pattern.compile("^[a-zA-Z0-9_/.]+$");
  18.     String searchedFile;
  19.     File file;
  20.  
  21.  
  22.     public FileHandler(){}
  23.  
  24.     /**
  25.      * Method converts .html file to a String in order to create a response.
  26.      * @param fileName - path to a file that will be converted
  27.      * @return content - HTML file converted to a String
  28.      * */
  29.  
  30.     String prepareFileForSending(String fileName){
  31.  
  32.         String content;
  33.         StringBuilder responseBodyBuilder = new StringBuilder();
  34.  
  35.         try {
  36.  
  37.             BufferedReader fileToConvert = new BufferedReader(new FileReader(fileName));
  38.             String line;
  39.  
  40.             while ((line = fileToConvert.readLine()) != null) {
  41.                 responseBodyBuilder.append(line);
  42.             }
  43.  
  44.             fileToConvert.close();
  45.  
  46.         } catch (IOException e) {
  47.             e.printStackTrace();
  48.         }
  49.  
  50.         content = responseBodyBuilder.toString();
  51.  
  52.         return content;
  53.     }
  54.  
  55.     /**
  56.      *  Method checks whether requested file is valid, there are 3 acceptable options
  57.      *  1) "/"            - requests index from root directory
  58.      *  2) "/abc"         - requests abc file from root directory
  59.      *  3) "/abc/"        - requests index from abc directory
  60.      *
  61.      *  @return true    - requested file is valid, otherwise false
  62.      *
  63.      * */
  64.  
  65.     public boolean validateRequestedFile(String requestedFile){
  66.  
  67.  
  68.         System.out.println("REQUEST = " + requestedFile);
  69.         int length = requestedFile.length()-1;
  70.  
  71.  
  72.         String second = requestedFile.substring(1);
  73.  
  74.  
  75.  
  76.         if (requestedFile.compareTo("/") == 0){
  77.             option = 1;
  78.             return true;
  79.         } else if(requestedFile.charAt(0) == '/' && PATTERN.matcher(second).matches() && requestedFile.charAt(length) != '/'){
  80.             option = 2;
  81.             return true;
  82.         } else if(requestedFile.charAt(0) == '/' && PATTERN.matcher(second).matches() && requestedFile.charAt(length) == '/'){
  83.             option = 3;
  84.             return true;
  85.         } else {
  86.             return false;
  87.         }
  88. //        option = 2;
  89. //        return true;
  90.     }
  91.  
  92.     /**
  93.      * Method that searches for a given file.
  94.      * @param requestedFile - given file, if it contains a '/' at the end - look for a index file in a suitable subdirectory, othwerwise
  95.      *                         look for a given file in root directory
  96.      * @return true if a file is found
  97.      * */
  98.  
  99.     public boolean serverContainsFile(String requestedFile) throws Exception{
  100.  
  101.  
  102.  
  103.         int length = requestedFile.length()-1;
  104.         String second = requestedFile.substring(1);
  105.         String path;
  106.  
  107.         /*TODO: somenthing wrong with slashes*/
  108.         /*TODO: implement using RegEx*/
  109.  
  110.         if(option == 1){
  111.             //searched file is index in root directory
  112.             searchedFile = "index.html";
  113.             file = new File("D:\\PIOTR\\PROGRAMOWANIE\\sem4\\SIECI\\lista5\\SiteContent\\" + searchedFile);
  114.  
  115. //            System.out.println(searchedFile);
  116.  
  117.         } else if(option == 2){
  118.  
  119.             searchedFile = requestedFile.substring(1);
  120.             //searched file is index in subdirectory
  121.             file = new File("D:\\PIOTR\\PROGRAMOWANIE\\sem4\\SIECI\\lista5\\SiteContent\\" + searchedFile);
  122.  
  123. //            System.out.println("looking for " + searchedFile);
  124.  
  125.         } else if (option == 3) {
  126.  
  127.             //erase '/' from requested file
  128.             path = requestedFile.substring(1,length);
  129.             StringBuffer abc = new StringBuffer(path);
  130.             abc.append("\\");
  131.  
  132.             System.out.println("abc = " + abc);
  133.             file = new File("D:\\PIOTR\\PROGRAMOWANIE\\sem4\\SIECI\\lista5\\SiteContent\\" + abc);
  134.  
  135.             searchedFile = "index";
  136.  
  137. //            System.out.println(searchedFile);
  138.  
  139.         } else {
  140.             throw new Exception("Error while processing file!");
  141.         }
  142.  
  143.         return file.exists();
  144.     }
  145.  
  146.     public String getRequestedFilePath(){
  147.         return file.toString();
  148.     }
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement