-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClientHandler.java
More file actions
216 lines (187 loc) · 6.26 KB
/
ClientHandler.java
File metadata and controls
216 lines (187 loc) · 6.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
import java.io.DataInputStream;
import java.io.DataOutput;
import java.io.DataOutputStream;
import java.io.File;
import java.io.IOException;
import java.net.Socket;
import java.net.SocketException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.function.Consumer;
import java.util.stream.Collectors;
import java.util.stream.Stream;
class ClientHandler extends Thread {
private Socket socket;
private String directory;
private Utils util;
private DataOutput dataOut;
private HashMap<String, Consumer<String[]>> command;
public ClientHandler(Socket socket, Utils util) throws IOException {
this.socket = socket;
this.dataOut = new DataOutputStream(socket.getOutputStream());
this.directory = System.getProperty("user.dir");
this.util = util;
command = new HashMap<String, Consumer<String[]>>();
command.put(
"cd",
cmd -> {
// Run command line
try {
// Verify that the directory's name was given
if (cmd.length == 2) {
// Create a new file
File newdir = new File(this.directory + "//" + cmd[1]);
// Verify if the directory exists
Boolean dirExsiste = newdir.exists();
// Send the confirmation that the directory exists
dataOut.writeBoolean(dirExsiste);
if (dirExsiste) {
// New directory name
String newDirectery = newdir.toString();
if (cmd[1].compareTo("..") == 0) {
// Get the parent name of the current directory
newDirectery = Paths.get(this.directory).getParent().toString();
} else if(cmd[1].compareTo(".") == 0){
// Keep same path name
newDirectery = this.directory;
}
// Update path name
this.directory = newDirectery;
// Send the new path name
dataOut.writeUTF(newDirectery);
}
} else {
// Send boolean that there is no arguments
dataOut.writeBoolean(false);
}
} catch (IOException e) {
// Auto-generated catch block
e.printStackTrace();
}
}
);
command.put(
"ls",
cmd -> {
try {
// Verify that no argument was given
if (cmd.length == 1){
// Create a String containing the list of all files and directories
String Filelist = Stream
.of(new File(this.directory).listFiles())
.map(File::getName)
.collect(Collectors.joining("\n"));
// Send the String containing the list
dataOut.writeUTF(Filelist);
} else{
// Help user to find the command
dataOut.writeUTF("Do you mean 'ls' ?");
}
} catch (IOException e) {
// Auto-generated catch block
e.printStackTrace();
}
}
);
command.put(
"mkdir",
cmd -> {
try {
String message = "No argument was given.";
// Verify that the directory name is given
if (cmd.length == 2) {
// Check if the directory exists
File newdir = new File(this.directory + "//" + cmd[1]);
Boolean fileDontExist = !newdir.exists();
if (fileDontExist) {
// Create a directory
Files.createDirectory(newdir.toPath());
}
// Create the message
message = (fileDontExist) ? "This directory " + cmd[1] + " has been created" : "This file already exists" ;
}
// Send the message
this.dataOut.writeUTF(message);
} catch (IOException e) {
// Auto-generated catch block
e.printStackTrace();
}
}
);
command.put(
"upload",
cmd -> {
try {
// Receive file from user
if (cmd.length == 2) {
// Protocol to receive file
this.util.getfile(socket, this.directory);
}
}catch (IllegalArgumentException e) {}
catch (IOException e) {
// Add to logs of error
e.printStackTrace();
}
}
);
command.put(
"download",
cmd -> {
try {
if (cmd.length == 2) {
// Protocol to send file
this.util.sendfile(socket, this.directory, cmd[1]);
}
} catch (IllegalArgumentException e) {}
catch (IOException e) {
// Auto-generated catch block
e.printStackTrace();
}
}
);
command.put("exit", cmd -> { /* Could send the entire activity of the user (warnings) to database */ } );
}
public void logs(String input) {
LocalDateTime myDateObj = LocalDateTime.now();
DateTimeFormatter myFormatObj = DateTimeFormatter.ofPattern(
"yyyy-MM-dd@HH:mm:ss"
);
String formattedDate = myDateObj.format(myFormatObj);
System.out.println(
"[" +
socket.getInetAddress().toString().replace("/", "") +
":" +
socket.getPort() +
"-" +
formattedDate +
"]:" +
input
);
}
public void run() {
try {
String userInput = "";
do {
DataInputStream in = new DataInputStream(socket.getInputStream());
userInput = in.readUTF();
logs(userInput);
String[] key = util.getkey(userInput);
command.get(key[0]).accept(key);
} while (userInput.compareTo("exit") != 0);
} catch (SocketException e) {}
catch (IOException e) {
// Auto-generated catch block
e.printStackTrace();
} finally {
try {
socket.close();
} catch (IOException e) {
// Auto-generated catch block
e.printStackTrace();
}
}
}
}