diff --git a/build.gradle b/build.gradle index f4ab86a3b1..c377cdd781 100644 --- a/build.gradle +++ b/build.gradle @@ -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" @@ -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", diff --git a/lecture04/build.gradle b/lecture04/build.gradle index 3856cc3230..ced7e58803 100644 --- a/lecture04/build.gradle +++ b/lecture04/build.gradle @@ -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 } diff --git a/lecture04/presentation/PITCHME.md b/lecture04/presentation/PITCHME.md index 6a273751bb..ce1b8fd222 100644 --- a/lecture04/presentation/PITCHME.md +++ b/lecture04/presentation/PITCHME.md @@ -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 @@ -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 @@ -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 @@ -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) ``` @@ -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 @@ -448,7 +446,7 @@ login: Protocol: HTTP Path: chat/login Method: POST - PathParam: name + QueryParam: name Host: {IP}:8080 Response: Success code: 200 @@ -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: @@ -476,7 +474,7 @@ login: Protocol: HTTP Path: chat/say Method: POST - PathParam: name + QueryParam: name Body: msg="my message" Host: {IP}:8080 @@ -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 diff --git a/lecture04/src/main/java/ru/atom/cache/ContactListCache.java b/lecture04/src/main/java/ru/atom/cache/ContactListCache.java index 71e858456b..5770b33eaf 100644 --- a/lecture04/src/main/java/ru/atom/cache/ContactListCache.java +++ b/lecture04/src/main/java/ru/atom/cache/ContactListCache.java @@ -1,6 +1,5 @@ package ru.atom.cache; -import sun.reflect.generics.reflectiveObjects.NotImplementedException; import java.util.List; @@ -14,21 +13,21 @@ public ContactListCache(int capacity) { @Override public boolean put(Person person, List people) { - throw new NotImplementedException(); + throw new UnsupportedOperationException(); } @Override public List 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(); } } diff --git a/lecture04/src/main/java/ru/atom/http/server/ChatResource.java b/lecture04/src/main/java/ru/atom/http/server/ChatResource.java index 895a5b8135..c0e62e694d 100644 --- a/lecture04/src/main/java/ru/atom/http/server/ChatResource.java +++ b/lecture04/src/main/java/ru/atom/http/server/ChatResource.java @@ -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; @@ -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 logined = new ConcurrentArrayQueue<>(); - private static final ConcurrentArrayQueue chat = new ConcurrentArrayQueue<>(); + private static final ConcurrentLinkedQueue logined = new ConcurrentLinkedQueue<>(); + private static final ConcurrentLinkedQueue chat = new ConcurrentLinkedQueue<>(); @POST @Consumes("application/x-www-form-urlencoded") @@ -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(); @@ -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(); @@ -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(); diff --git a/lecture04/src/test/java/ru/atom/http/ChatClient.java b/lecture04/src/test/java/ru/atom/http/ChatClient.java index f0b34f6c12..c5e4316156 100644 --- a/lecture04/src/test/java/ru/atom/http/ChatClient.java +++ b/lecture04/src/test/java/ru/atom/http/ChatClient.java @@ -5,8 +5,7 @@ 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; @@ -14,7 +13,7 @@ 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 @@ -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(); } } \ No newline at end of file diff --git a/lecture04/src/test/resources/FileToRead.txt b/lecture04/src/test/resources/FileToRead.txt deleted file mode 100644 index 5100168494..0000000000 --- a/lecture04/src/test/resources/FileToRead.txt +++ /dev/null @@ -1,2 +0,0 @@ -First line in FileToRead.txt - Second line in file \ No newline at end of file diff --git a/lecture04/src/test/resources/log4j2.properties b/lecture04/src/test/resources/log4j2.properties new file mode 100644 index 0000000000..35dbbb09e5 --- /dev/null +++ b/lecture04/src/test/resources/log4j2.properties @@ -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 \ No newline at end of file diff --git a/settings.gradle b/settings.gradle index 1d9b7da09e..e1535c034e 100644 --- a/settings.gradle +++ b/settings.gradle @@ -1,4 +1,2 @@ rootProject.name = 'atom' -include 'lecture01' -include 'lecture02' -include 'lecture03' +include 'lecture04' \ No newline at end of file