Advertisement
Guest User

Untitled

a guest
Mar 31st, 2020
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.98 KB | None | 0 0
  1. import java.io.*;
  2. import java.net.*;
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import javax.imageio.ImageIO;
  6. import java.awt.image.BufferedImage;
  7.  
  8. public class ServerWeb {
  9. public static final String continutPath = "D:\\SCOALA\\PW\\Laborator6\\proiect1-IureaBogdan\\continut\\";
  10. public static int connectedUsers = 0;
  11.  
  12. public static void main(String[] args) throws IOException {
  13. System.out.println("#########################################################################");
  14. System.out.println("Serverul asculta potentiali clienti.");
  15. // pornește un server pe portul 5678
  16. ServerSocket serverSocket = new ServerSocket(5678);
  17. while (true) {
  18. Socket clientSocket = serverSocket.accept();
  19. connectedUsers++;
  20. System.out.println("###CLIENT NOU###");
  21. // socketWriter - wrapper peste fluxul de ieșire folosit pentru a transmite date
  22. // clientului
  23. // PrintWriter socketWriter = new PrintWriter(clientSocket.getOutputStream(),
  24. // true);
  25.  
  26. // socketReader - wrapper peste fluxul de intrare folosit pentru a primi date de
  27. // la client
  28. BufferedReader socketReader = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
  29. // este citită prima linie de text din cerere
  30. String linieDeStart = socketReader.readLine();
  31. System.out.println("##### " + linieDeStart + " #####");
  32. // mesajul citit este transmis la client
  33. // interpretarea sirului de caractere `linieDeStart` pentru a extrage numele resursei cerut
  34. String requestedFile = StringRequestInterpretter(linieDeStart);
  35. boolean isFound = searchFile(requestedFile);
  36.  
  37. // trimiterea răspunsului HTTP
  38. String path = continutPath + requestedFile;
  39. System.out.println("##############");
  40. System.out.println(path);
  41. String contentType = "";
  42. switch (getFileType(linieDeStart)) {
  43. case 1:
  44. contentType = "Content-Type:text/html; charset=UTF-8";
  45. break;
  46. case 2:
  47. contentType = "Content-Type:text/css";
  48. break;
  49. case 3:
  50. contentType = "Content-Type:application/js";
  51. break;
  52. case 4:
  53. contentType = "Content-Type:text/png";
  54. break;
  55. case 5:
  56. contentType = "Content-Type:text/jpeg";
  57. break;
  58. case 6:
  59. contentType = "Content-Type:text/gif";
  60. break;
  61. case 7:
  62. contentType = "Content-Type:image/x-icon";
  63. break;
  64. }
  65. String response = createResponse(isFound, path, contentType);
  66. clientSocket.getOutputStream().write(response.getBytes());
  67.  
  68.  
  69. // închide conexiunea cu clientul
  70. // la apelul metodei close() se închid automat fluxurile de intrare și ieșire
  71. // (socketReader și socketWriter)
  72. clientSocket.close();
  73. System.out.println("S-a terminat comunicarea cu clientul.");
  74. System.out.println();
  75. System.out.println();
  76. System.out.println();
  77.  
  78. //conditie pusa doar ca sa dea eroare la 30 de accesari ca sa se inchida sv
  79. if (connectedUsers > 30)
  80. serverSocket.close();
  81. }
  82. }
  83.  
  84. private static String StringRequestInterpretter(String input) {
  85. int nrOfSl = 0;
  86. for (int i = 0; i < input.length(); i++) {
  87. if (input.charAt(i) == '/') {
  88. nrOfSl++;
  89. }
  90. }
  91. String temp = "";
  92. if (nrOfSl >= 2) {
  93. temp = input.substring(input.indexOf("/") + 1, input.indexOf("HTTP/") - 1);
  94. } else {
  95. temp = "index.html";
  96. }
  97. if (temp.contains("/")) {
  98. temp = temp.replace("/", "\\");
  99. }
  100. return temp;
  101. }
  102.  
  103. private static int getFileType(String input) {
  104. int fileType = 0;
  105. if (input.contains(".html")) {
  106. fileType = 1;
  107. } else if (input.contains(".css")) {
  108. fileType = 2;
  109. } else if (input.contains(".js")) {
  110. fileType = 3;
  111. } else if (input.contains(".png")) {
  112. fileType = 4;
  113. } else if (input.contains(".jpg") || input.contains(".jpeg")) {
  114. fileType = 5;
  115. } else if (input.contains(".gif")) {
  116. fileType = 6;
  117. } else if (input.contains(".ico")) {
  118. fileType = 7;
  119. }
  120. return fileType;
  121. }
  122.  
  123. private static boolean searchFile(String input) {
  124. File dir;
  125. int fileType = getFileType(input);
  126. File[] files;
  127. List<File> retFiles = new ArrayList<>();
  128. if (fileType == 1) {
  129. dir = new File(continutPath);
  130. files = dir.listFiles();
  131. for (File file : files) {
  132. if (file.getName().contains(".html")) {
  133. retFiles.add(file);
  134. }
  135. }
  136. } else if (fileType == 2) {
  137. dir = new File(continutPath + "\\css\\");
  138. files = dir.listFiles();
  139. for (File file : files) {
  140. if (file.getName().contains(".css")) {
  141. retFiles.add(file);
  142. }
  143. }
  144. } else if (fileType == 3) {
  145. dir = new File(continutPath + "\\js\\");
  146. files = dir.listFiles();
  147. for (File file : files) {
  148. if (file.getName().contains(".js")) {
  149. retFiles.add(file);
  150. }
  151. }
  152. } else if (fileType == 4 || fileType == 5 || fileType == 6 || fileType == 7) {
  153. dir = new File(continutPath + "\\imagini\\");
  154. files = dir.listFiles();
  155. for (File file : files) {
  156. if (file.getName().contains(".jpeg") || file.getName().contains(".jpg")
  157. || file.getName().contains(".gif") || file.getName().contains(".ico")
  158. || file.getName().contains(".png")) {
  159. retFiles.add(file);
  160. }
  161. }
  162. }
  163. boolean found = false;
  164. System.out.println("\n\n#######CAUT FISIER###########");
  165. for (File file : retFiles) {
  166. System.out.println("FISIER###" + file.getName() + "#");
  167. if (!input.equals("index.html")) {
  168. String tempReqFile = input.substring(input.lastIndexOf('\\') + 1, input.length());
  169. System.out.println("testez cu ###" + tempReqFile + "###" +file.getName().equals(tempReqFile));
  170. if (file.getName().equals(tempReqFile)) found = true;
  171. }
  172. else found = true;
  173. }
  174. System.out.println("########" + found + "##########\n\n");
  175. return found;
  176. }
  177.  
  178. private static String readFile(String path) {
  179. File file = new File(path);
  180. int filetype = getFileType(path);
  181. String content = "";
  182.  
  183. if (filetype == 1 || filetype == 2 || filetype == 3) {
  184. try (BufferedReader br = new BufferedReader(new FileReader(file))) {
  185. String line;
  186. while ((line = br.readLine()) != null) {
  187. content += line + "\n";
  188. }
  189. } catch (IOException e) {
  190. e.printStackTrace();
  191. }
  192. } else if (filetype == 4) {
  193. try
  194. {
  195. BufferedImage image = ImageIO.read(file);
  196. }
  197. catch (IOException e) {
  198. e.printStackTrace();
  199. }
  200.  
  201. }
  202. else if(filetype==5)
  203. {
  204.  
  205. }
  206. else if(filetype==6)
  207. {
  208.  
  209. }
  210. else if(filetype==7)
  211. {
  212.  
  213. }
  214. return content;
  215. }
  216.  
  217. private static String createResponse(boolean flag ,String path,String contentType)
  218. {
  219. String response,CSSlink, status,length,type,server, CRLF,messageBody;
  220. if(flag){
  221. messageBody=readFile(path)+"\r\n";
  222. int messageLength=messageBody.getBytes().length;
  223. status="HTTP:/1.1 200 OK\r\n";
  224. length="Content-Length:"+messageLength+"\r\n";
  225. type=contentType+"\r\n";
  226. server="Server:IBI\r\n";
  227. CRLF="\r\n";
  228. CSSlink="Link: <D:\\SCOALA\\PW\\Laborator6\\proiect1-IureaBogdan\\continut\\css\\stil.css>; rel=stylesheet\r\n";
  229. response=status+length+CSSlink+type+server+CRLF+messageBody;
  230. }
  231. else{
  232. status="HTTP/1.1 404 Not Found";
  233. messageBody="ERROR\r\n";
  234. response=status+messageBody;
  235. }
  236. System.out.println(status);
  237. return response;
  238. }
  239. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement