Skip to content
Merged
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
7 changes: 4 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ plugins {
ext {
jdkVersion = 1.9

jettyVersion = "9.3.12.v20160915"
jettyVersion = "9.4.7.v20170914"
junitVersion = "4.12"
jacksonVersion = "2.5.3"
jacksonVersion = "2.9.1"
log4jVersion = "2.7"
jetbrainsAnnotationVersion = "15.0"
okhttpVersion = "3.6.0"
jerseyVersion = "2.25.1"
jerseyVersion = "2.26"
gsonjVersion = "2.7"
postgresVersion = "9.4-1200-jdbc41"
jetbrainsAnnotationVersion = "15.0"
Expand Down Expand Up @@ -66,6 +66,7 @@ ext.libraries = [
jetbrainsAnnotations: "org.jetbrains:annotations:$jetbrainsAnnotationVersion",
okhttp: "com.squareup.okhttp3:okhttp:$okhttpVersion",
jersey_server: "org.glassfish.jersey.core:jersey-server:$jerseyVersion",
jersey_hk2: "org.glassfish.jersey.inject:jersey-hk2:$jerseyVersion",
jersey_containers: "org.glassfish.jersey.containers:jersey-container-servlet:$jerseyVersion",
jersey_test:
"org.glassfish.jersey.test-framework.providers:jersey-test-framework-provider-grizzly2:$jerseyVersion",
Expand Down
1 change: 1 addition & 0 deletions lecture04/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ dependencies {
compile rootProject.libraries.jetty_server
compile rootProject.libraries.jetty_servlet
compile rootProject.libraries.jersey_server
compile rootProject.libraries.jersey_hk2
compile rootProject.libraries.jersey_containers
}

Expand Down
38 changes: 18 additions & 20 deletions lecture04/presentation/PITCHME.md
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ For example, one can extend server functionality by custom logic (e.g. for dynam
#HSLIDE
## HTTP via telnet
```bash
> telnet example.org
> telnet example.org 80
```
request:
```http
Expand Down Expand Up @@ -313,7 +313,7 @@ removes resource
[wiki](https://en.wikipedia.org/wiki/List_of_HTTP_status_codes)

#HSLIDE
##HTTP via browser
## HTTP via browser
When you enter adress line in browser, in creates **GET** request
So we can do previous example just by typing in browser:
> example.org
Expand Down Expand Up @@ -347,18 +347,18 @@ it wraps **libcurl** library, which is available for all major languages
## GET Example
Request from cURL:
```bash
> curl -i -X GET -H "Host: example.org" example.org
> curl -i -X GET example.org
```
Response:
```http
HTTP/1.1 200 OK
Cache-Control: max-age=604800
Content-Type: text/html
Date: Sat, 11 Mar 2017 00:22:28 GMT
Date: Wed, 11 Oct 2017 14:17:54 GMT
Etag: "359670651+ident"
Expires: Sat, 18 Mar 2017 00:22:28 GMT
Expires: Wed, 18 Oct 2017 14:17:54 GMT
Last-Modified: Fri, 09 Aug 2013 23:54:35 GMT
Server: ECS (phl/9D2C)
Server: ECS (dca/24D5)
Vary: Accept-Encoding
X-Cache: HIT
Content-Length: 1270
Expand All @@ -377,22 +377,20 @@ POST /chat/say HTTP/1.1
Content-Type: application/x-www-form-urlencoded
Host: localhost:8080

msg="Привет всем в этом чатике"
msg=Hi everyone in this chat!
```
cURL:
```bash
>curl -X POST \
-H "Content-Type: application/x-www-form-urlencoded" \
-H "Host: localhost:8080" \
-d 'msg="Привет всем в этом чатике"'' \
http://localhost:8080/chat/say
> curl -X POST \
-d 'msg=Hi everyone in this chat!' \
http://localhost:8080/chat/say?name=MY_NAME
```
response:
```http
HTTP/1.1 200 OK
Date: Sat, 11 Mar 2017 13:05:11 GMT
Date: Wed, 11 Oct 2017 14:17:11 GMT
Content-Length: 0
Server: Jetty(9.3.12.v20160915)
Server: Jetty(9.4.z-SNAPSHOT)

```

Expand Down Expand Up @@ -426,7 +424,7 @@ REST API is a common way for services to publish their functionality for other s
## HTTP Client Pracrice
We got a chat REST service open for you on

Implement **chat client** and enjoy!
Implement **chat client** and enjoy!
@see **test.ru.atom.http.ChatClient** and **test.ru.atom.http.ChatClientTest**

#HSLIDE
Expand All @@ -448,7 +446,7 @@ login:
Protocol: HTTP
Path: chat/login
Method: POST
PathParam: name
QueryParam: name
Host: {IP}:8080
Response:
Success code: 200
Expand All @@ -457,11 +455,11 @@ login:
400 - Too long name (longer than 30 symbols)
```
#HSLIDE
## Chat REST API. View Online
## Chat REST API. View chat
online:
```
Protocol: HTTP
Path: chat/online
Path: chat/chat
Method: GET
Host: {IP}:8080
Response:
Expand All @@ -476,7 +474,7 @@ login:
Protocol: HTTP
Path: chat/say
Method: POST
PathParam: name
QueryParam: name
Body:
msg="my message"
Host: {IP}:8080
Expand All @@ -502,7 +500,7 @@ login:
## OkHTTP
We use OkHTTP library as java HTTP Client
[http://square.github.io/okhttp/](http://square.github.io/okhttp/)
###@see ru.atom.http.client
### @see ru.atom.http.client

#HSLIDE
## GET example from Java
Expand Down
9 changes: 4 additions & 5 deletions lecture04/src/main/java/ru/atom/cache/ContactListCache.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package ru.atom.cache;

import sun.reflect.generics.reflectiveObjects.NotImplementedException;

import java.util.List;

Expand All @@ -14,21 +13,21 @@ public ContactListCache(int capacity) {

@Override
public boolean put(Person person, List<? extends Person> people) {
throw new NotImplementedException();
throw new UnsupportedOperationException();
}

@Override
public List<? extends Person> get(Person person) {
throw new NotImplementedException();
throw new UnsupportedOperationException();
}

@Override
public int getSize() {
throw new NotImplementedException();
throw new UnsupportedOperationException();
}

private boolean removeAny() {
throw new NotImplementedException();
throw new UnsupportedOperationException();
}

}
14 changes: 7 additions & 7 deletions lecture04/src/main/java/ru/atom/http/server/ChatResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.eclipse.jetty.util.ConcurrentArrayQueue;

import javax.ws.rs.Consumes;
import javax.ws.rs.FormParam;
Expand All @@ -12,13 +11,14 @@
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Response;
import java.util.concurrent.ConcurrentLinkedQueue;

@Path("/chat")
public class ChatResource {
private static final Logger log = LogManager.getLogger(ChatResource.class);

private static final ConcurrentArrayQueue<String> logined = new ConcurrentArrayQueue<>();
private static final ConcurrentArrayQueue<String> chat = new ConcurrentArrayQueue<>();
private static final ConcurrentLinkedQueue<String> logined = new ConcurrentLinkedQueue<>();
private static final ConcurrentLinkedQueue<String> chat = new ConcurrentLinkedQueue<>();

@POST
@Consumes("application/x-www-form-urlencoded")
Expand All @@ -27,8 +27,8 @@ public Response login(@QueryParam("name") String name) {
if (name.length() > 30) {
return Response.status(Response.Status.BAD_REQUEST).entity("Too long name, sorry :(").build();
}
if (name.toLowerCase().contains("gitler")) {
return Response.status(Response.Status.BAD_REQUEST).entity("Gitler not allowed, sorry :(").build();
if (name.toLowerCase().contains("hitler")) {
return Response.status(Response.Status.BAD_REQUEST).entity("Hitler not allowed, sorry :(").build();
}
if (logined.contains(name)) {
return Response.status(Response.Status.BAD_REQUEST).entity("Already logined").build();
Expand All @@ -40,7 +40,7 @@ public Response login(@QueryParam("name") String name) {
}

@GET
@Produces("text/plain")
@Produces("text/plain;charset=UTF-8")
@Path("/online")
public Response online() {
return Response.ok(String.join("\n", logined)).build();
Expand All @@ -65,7 +65,7 @@ public Response say(@QueryParam("name") String name, @FormParam("msg") String ms
}

@GET
@Produces("text/plain")
@Produces("text/plain;charset=UTF-8")
@Path("/chat")
public Response chat() {
return Response.ok(String.join("\n", chat)).build();
Expand Down
9 changes: 4 additions & 5 deletions lecture04/src/test/java/ru/atom/http/ChatClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,15 @@
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import sun.reflect.generics.reflectiveObjects.NotImplementedException;
import sun.util.resources.cldr.ms.CalendarData_ms_MY;


import java.io.IOException;


public class ChatClient {
private static final OkHttpClient client = new OkHttpClient();
private static final String PROTOCOL = "http://";
private static final String HOST = "wtfis.ru";
private static final String HOST = "34.229.108.81";
private static final String PORT = ":8080";

//GET host:port/chat/online
Expand Down Expand Up @@ -42,11 +41,11 @@ public static Response login(String name) 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 NotImplementedException();
throw new UnsupportedOperationException();
}

//GET host:port/chat/chat
public static Response viewChat() throws IOException {
throw new NotImplementedException();
throw new UnsupportedOperationException();
}
}
2 changes: 0 additions & 2 deletions lecture04/src/test/resources/FileToRead.txt

This file was deleted.

7 changes: 7 additions & 0 deletions lecture04/src/test/resources/log4j2.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
appender.console.type = Console
appender.console.name = STDOUT
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %m%n

rootLogger.level = info
rootLogger.appenderRef.stdout.ref = STDOUT
4 changes: 1 addition & 3 deletions settings.gradle
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
rootProject.name = 'atom'
include 'lecture01'
include 'lecture02'
include 'lecture03'
include 'lecture04'