Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions lecture04/src/main/java/ru/atom/chat/client/ChatClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,22 @@ public static Response viewChat() throws IOException {
//POST host:port/chat/say?name=my_name
//Body: "msg='my_message'"
public static Response say(String name, String msg) throws IOException {
throw new UnsupportedOperationException();
MediaType mediaType = MediaType.parse(msg);
Request request = new Request.Builder()
.post(RequestBody.create(mediaType, ""))
.url(PROTOCOL + HOST + PORT + "/chat/say?name=" + name)
.addHeader("host", HOST + PORT)
.build();
return client.newCall(request).execute();
}

//GET host:port/chat/online
public static Response viewOnline() throws IOException {
throw new UnsupportedOperationException();
Request request = new Request.Builder()
.get()
.url(PROTOCOL + HOST + PORT + "/chat/online")
.addHeader("host", HOST + PORT)
.build();
return client.newCall(request).execute();
}
}
36 changes: 33 additions & 3 deletions lecture04/src/main/java/ru/atom/chat/server/ChatController.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,46 @@ public ResponseEntity online() {
/**
* curl -X POST -i localhost:8080/chat/logout -d "name=I_AM_STUPID"
*/
//TODO
@RequestMapping(
path = "logout",
method = RequestMethod.POST,
consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
@ResponseStatus(HttpStatus.OK)
public ResponseEntity<String> logout(@RequestParam("name") String name) {
if (!usersOnline.containsKey(name)) {
return ResponseEntity.badRequest().body("There's not such user");
}
usersOnline.remove(name);
messages.add("[" + name + "] logged out");
return ResponseEntity.ok().build();
}

/**
* curl -X POST -i localhost:8080/chat/say -d "name=I_AM_STUPID&msg=Hello everyone in this chat"
*/
//TODO
@RequestMapping(
path = "say",
method = RequestMethod.POST,
consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
@ResponseStatus(HttpStatus.OK)
public ResponseEntity<String> say(@RequestParam("name") String name, @RequestParam("msg") String message) {
if (!usersOnline.containsKey(name)) {
return ResponseEntity.badRequest().body("There's no such user");
}
messages.add("[" + name + "]: " + message);
return ResponseEntity.ok().build();
}


/**
* curl -i localhost:8080/chat/chat
*/
//TODO
@RequestMapping(
path = "chat",
method = RequestMethod.GET,
produces = MediaType.TEXT_PLAIN_VALUE)
public ResponseEntity chat() {
String responseBody = String.join("\n", messages.stream().sorted().collect(Collectors.toList()));
return ResponseEntity.ok(responseBody);
}
}