From 54f9e58bb0435109f40ab8b68e597930dba627e7 Mon Sep 17 00:00:00 2001 From: Azamat Kulbarakov Date: Mon, 11 Apr 2022 08:51:50 +0500 Subject: [PATCH 1/6] 01-web task1 added --- 01_web/http-server/pom.xml | 17 +++ 01_web/http-server/public/app.js | 1 + 01_web/http-server/public/classic.html | 14 +++ 01_web/http-server/public/events.html | 16 +++ 01_web/http-server/public/events.js | 8 ++ 01_web/http-server/public/forms.html | 18 +++ 01_web/http-server/public/index.html | 13 +++ 01_web/http-server/public/links.html | 14 +++ 01_web/http-server/public/resources.html | 16 +++ 01_web/http-server/public/spring.png | Bin 0 -> 9440 bytes 01_web/http-server/public/spring.svg | 1 + 01_web/http-server/public/styles.css | 4 + .../src/main/java/ru/netology/Main.java | 10 ++ .../src/main/java/ru/netology/Server.java | 103 ++++++++++++++++++ .../target/classes/ru/netology/Main.class | Bin 0 -> 490 bytes .../target/classes/ru/netology/Server.class | Bin 0 -> 5879 bytes 16 files changed, 235 insertions(+) create mode 100644 01_web/http-server/pom.xml create mode 100644 01_web/http-server/public/app.js create mode 100644 01_web/http-server/public/classic.html create mode 100644 01_web/http-server/public/events.html create mode 100644 01_web/http-server/public/events.js create mode 100644 01_web/http-server/public/forms.html create mode 100644 01_web/http-server/public/index.html create mode 100644 01_web/http-server/public/links.html create mode 100644 01_web/http-server/public/resources.html create mode 100644 01_web/http-server/public/spring.png create mode 100644 01_web/http-server/public/spring.svg create mode 100644 01_web/http-server/public/styles.css create mode 100644 01_web/http-server/src/main/java/ru/netology/Main.java create mode 100644 01_web/http-server/src/main/java/ru/netology/Server.java create mode 100644 01_web/http-server/target/classes/ru/netology/Main.class create mode 100644 01_web/http-server/target/classes/ru/netology/Server.class diff --git a/01_web/http-server/pom.xml b/01_web/http-server/pom.xml new file mode 100644 index 0000000..f0c2ebd --- /dev/null +++ b/01_web/http-server/pom.xml @@ -0,0 +1,17 @@ + + + 4.0.0 + + ru.netology + http-server + 1.0-SNAPSHOT + + + 11 + 11 + UTF-8 + + + \ No newline at end of file diff --git a/01_web/http-server/public/app.js b/01_web/http-server/public/app.js new file mode 100644 index 0000000..4dd8d1d --- /dev/null +++ b/01_web/http-server/public/app.js @@ -0,0 +1 @@ +console.log('it works!'); \ No newline at end of file diff --git a/01_web/http-server/public/classic.html b/01_web/http-server/public/classic.html new file mode 100644 index 0000000..44daeaa --- /dev/null +++ b/01_web/http-server/public/classic.html @@ -0,0 +1,14 @@ + + + + + + + Document + + +

Classic Demo

+

Current time is: {time}

+ + \ No newline at end of file diff --git a/01_web/http-server/public/events.html b/01_web/http-server/public/events.html new file mode 100644 index 0000000..3f5dd79 --- /dev/null +++ b/01_web/http-server/public/events.html @@ -0,0 +1,16 @@ + + + + + + + Document + + +

You clicked: 0

+ + + + + \ No newline at end of file diff --git a/01_web/http-server/public/events.js b/01_web/http-server/public/events.js new file mode 100644 index 0000000..198ad9d --- /dev/null +++ b/01_web/http-server/public/events.js @@ -0,0 +1,8 @@ +let count = 0; +const counterEl = document.querySelector('[data-id="counter"]'); +const incEl = document.querySelector('[data-action="inc"]'); + +incEl.addEventListener('click', () => { + count++; + counterEl.textContent = `${count}`; +}); diff --git a/01_web/http-server/public/forms.html b/01_web/http-server/public/forms.html new file mode 100644 index 0000000..096a3e2 --- /dev/null +++ b/01_web/http-server/public/forms.html @@ -0,0 +1,18 @@ + + + + + + + Document + + +

Forms Demo

+
+ + + +
+ + \ No newline at end of file diff --git a/01_web/http-server/public/index.html b/01_web/http-server/public/index.html new file mode 100644 index 0000000..95f7f27 --- /dev/null +++ b/01_web/http-server/public/index.html @@ -0,0 +1,13 @@ + + + + + + + HTML Document + + +

HTML Document

+ + \ No newline at end of file diff --git a/01_web/http-server/public/links.html b/01_web/http-server/public/links.html new file mode 100644 index 0000000..250dc6f --- /dev/null +++ b/01_web/http-server/public/links.html @@ -0,0 +1,14 @@ + + + + + + + Document + + + PNG Logo + SVG Logo + + \ No newline at end of file diff --git a/01_web/http-server/public/resources.html b/01_web/http-server/public/resources.html new file mode 100644 index 0000000..3b6dd42 --- /dev/null +++ b/01_web/http-server/public/resources.html @@ -0,0 +1,16 @@ + + + + + + + HTML Document + + + +

HTML Document

+ + + + \ No newline at end of file diff --git a/01_web/http-server/public/spring.png b/01_web/http-server/public/spring.png new file mode 100644 index 0000000000000000000000000000000000000000..f40b61f5a001b019a340f170d357b1eb7d16486d GIT binary patch literal 9440 zcmc(Fg;!NywDkp1KtQ^Y?iP^l?gr`Z{?SNEcQ+Sl1f{#Xkw#iNr39qwZQlDIzK_9x zd+s=6pR@N~Ypyxx+7Zf%QYdc;-a;S{6d7r8RR{zo0elWecmw`Y$IVrNe{e3MGU^E6 z#|ObI99-kON@%&NIa;`S7(1IoEFB!}&6!+Goz2Z1T&x^jPhWKkf}5DF)wNv3oXw40 ztsNam)vfK#!Iu!oWX@zeDH{jrWYuKNWGyKhD;p~*8#^yMJ1^TuAJ>IX5C|zmMqEVQ z6MFpHUvJi}gXJ&oN369k!l7hFq&*qN-DcEPCbG2ujV9|Hk2K#m>3_3pUDULkso-*J zTuNFhEw59o|2#*WCFp)n=FWHDrO!u0%YJs0kMb28y5UgsGk%&h8o7Y<=GUN+%X2PS zJpS&zx5eMb2f2;=tzM6g3txYs&efb?VNx^*Suh%T7xol0_z-_1Arg!x8g7IojzAWS zAo>6C;=2{DFrwnAO4XCGD{2b_^4>sd!Tw_UpAkkA zDT9aCYc)RQp_G{n4_2Y-h1!3ivt*X%aWNMn0rz}wLLo2nM`&=o%5S!1X@QTEDklPi zr9SX|n5e=cpk}byAq|eV3U4LdG!?KqxkHYr5HOMoEzpM|M&naysn;QgEKOVChMv4@U!vyaJ-*^_KZ$TxAx|VvuX-Z;Gf5NN*VcqU9+TZV|88=ko!Qw}y{N82W9ru`QH`6aOQ!dgG`3a-Z?cWe- zD593G!mQ(6^MOvuX}Wg{r;Izkk0?3Gb^dd6O{869D(JG6U_XLGSSg zmiYW)7%(b`?)A;PU!rA$IJd!?{5|!D*K;+$)Ev~*&S_CtXrSUu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%Nam%7rz$sotFcdlWR1|Kr>J+V_U! z@XLKPCg^Q~b{O0tiM{HnrILc|EP&GhosTVV!avLe1Amm`&rwNnu!^eu>x5j;mgAnh z{jV;HtQkBT3^a3BPDub9PT)gDQpr*V`>;?0U|Z57+u0PEqP`B43r530`9LThFtu9_ ztmLg0P0@N((IHoy84*fa^<@u!+KH?IkWIIeFTExOGw!4fM-xRrj}8QfWYD_*-(DO&zk=CL!j>-oK=*RaNJc_Y Kyh79{@P7cMX#}nS literal 0 HcmV?d00001 diff --git a/01_web/http-server/public/spring.svg b/01_web/http-server/public/spring.svg new file mode 100644 index 0000000..740097b --- /dev/null +++ b/01_web/http-server/public/spring.svg @@ -0,0 +1 @@ +spring-icon \ No newline at end of file diff --git a/01_web/http-server/public/styles.css b/01_web/http-server/public/styles.css new file mode 100644 index 0000000..ec4ecde --- /dev/null +++ b/01_web/http-server/public/styles.css @@ -0,0 +1,4 @@ +body { + color: #6EB149; + font-family: Arial, serif; +} \ No newline at end of file diff --git a/01_web/http-server/src/main/java/ru/netology/Main.java b/01_web/http-server/src/main/java/ru/netology/Main.java new file mode 100644 index 0000000..f069e77 --- /dev/null +++ b/01_web/http-server/src/main/java/ru/netology/Main.java @@ -0,0 +1,10 @@ +package ru.netology; + +public class Main { + public static void main(String[] args) { + Server myServer = new Server(); + myServer.listen(); + } +} + + diff --git a/01_web/http-server/src/main/java/ru/netology/Server.java b/01_web/http-server/src/main/java/ru/netology/Server.java new file mode 100644 index 0000000..8949514 --- /dev/null +++ b/01_web/http-server/src/main/java/ru/netology/Server.java @@ -0,0 +1,103 @@ +package ru.netology; + +import java.io.BufferedOutputStream; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.net.ServerSocket; +import java.net.Socket; +import java.nio.file.Files; +import java.nio.file.Path; +import java.time.LocalDateTime; +import java.util.List; +import java.util.concurrent.Executors; + +public class Server { + + private final List validPaths = List.of("/index.html", "/spring.svg", "/spring.png", "/resources.html", "/styles.css", "/app.js", "/links.html", "/forms.html", "/classic.html", "/events.html", "/events.js"); + + public void listen() { + var poolExecutor = Executors.newFixedThreadPool(64); + + try (final var serverSocket = new ServerSocket(9999)) { + log("Server start!"); + while (true) { + Socket socket = serverSocket.accept(); + poolExecutor.submit(() -> handle(socket)); + } + } catch (IOException e) { + e.printStackTrace(System.out); + } + } + + public void handle(Socket socket) { + try (final var in = new BufferedReader(new InputStreamReader(socket.getInputStream())); + final var out = new BufferedOutputStream(socket.getOutputStream()); + ) { + log("client connected: " + socket.getRemoteSocketAddress()); + // read only request line for simplicity + // must be in form GET /path HTTP/1.1 + final var requestLine = in.readLine(); + final var parts = requestLine.split(" "); + + if (parts.length != 3) { + // just close socket + log("Invalid request"); + return; + } + + final var path = parts[1]; + if (!validPaths.contains(path)) { + out.write(( + "HTTP/1.1 404 Not Found\r\n" + + "Content-Length: 0\r\n" + + "Connection: close\r\n" + + "\r\n" + ).getBytes()); + out.flush(); + return; + } + + final var filePath = Path.of("http-server", "public", path); + final var mimeType = Files.probeContentType(filePath); +// final var mimeType = "text/plain"; + // special case for classic + if (path.equals("/classic.html")) { + final var template = Files.readString(filePath); + final var content = template.replace( + "{time}", + LocalDateTime.now().toString() + ).getBytes(); + out.write(( + "HTTP/1.1 200 OK\r\n" + + "Content-Type: " + mimeType + "\r\n" + + "Content-Length: " + content.length + "\r\n" + + "Connection: close\r\n" + + "\r\n" + ).getBytes()); + out.write(content); + out.flush(); + log("classic.html loaded"); + return; + } + + final var length = Files.size(filePath); + out.write(( + "HTTP/1.1 200 OK\r\n" + + "Content-Type: " + mimeType + "\r\n" + + "Content-Length: " + length + "\r\n" + + "Connection: close\r\n" + + "\r\n" + ).getBytes()); + Files.copy(filePath, out); + log(filePath.getFileName() + " loaded"); + out.flush(); + } catch (IOException e) { + e.printStackTrace(System.out); + } + } + + private void log(String message) { + System.out.println("[" + Thread.currentThread().getName() + "] " + message); + } +} diff --git a/01_web/http-server/target/classes/ru/netology/Main.class b/01_web/http-server/target/classes/ru/netology/Main.class new file mode 100644 index 0000000000000000000000000000000000000000..d15df63119b078a5386e025d24178b2439e202da GIT binary patch literal 490 zcmZvYO-sW-5Qg7v+9r+Zhi$E&7jLbCIeMsg5dnAxg-e1s=NYfRt*MA7sqla3_G#(Bj^0|tsxATw9 z;7k|A_zc}&@S;k1H_UQ17MIctS2M$(m}hW^IxWte66mw2Qf!j)DKpBIqb1n$%5G6K zHeg|sqE}o%8y!+Lbg@Mh(|koR1FgTozF(pi9KXO_LmC7iG91Erv}yzE6%CZPNjc>1 NV3%+Vd!$R){{ob>V3Pm< literal 0 HcmV?d00001 diff --git a/01_web/http-server/target/classes/ru/netology/Server.class b/01_web/http-server/target/classes/ru/netology/Server.class new file mode 100644 index 0000000000000000000000000000000000000000..dfb776e6e35db4f87f3d1a3054b5adfee5dd7507 GIT binary patch literal 5879 zcmcIod3+pY8GgQGcW1MiH0h>IyFKXkPSPgZ3uu#4+LVSiP13ZRwuAz8vO7t(+3YN{ zv*{&@ig+uYfES`@QA905dP%D&ihu{+QWceYBHkA&l;@k7O=g?O&wu>om-%+SdB6Ai zp6C7EnMYqZc^bf_VpRwwC{^H7;YUEAYM(Z&#nM`)KenxRpPsM;0&7#5l(kNvw6<=S zf?$zQ+%i*{{vbjE<*`&IsUK(@u!hn>sB$8gl}{UU!<3lhnaIi|RRWbUQ_mTBGok0) zWno*&I+*56B9{wdihvT+vf0Lcys42-)2Ym0L43NL=rhcr!o&=LidZ78<#MTnt8$ir z8qB%jlMoHu?|7;<`Dko3uQvrBlf+%^JutlNV|TVJR9EG^&VU znZQM3&LoUXB5#@uBev;)p2%BlLn7WqiGK{o9UF{aHYZI)L&ZH80 zvw{@@Q*9%3)b%lLBnEXWgymQzQE{<=j|z7M2^Um2VMKG5W?FMYjCYNSCNvWrjFP%) zJ!x{l(OkDn>bgwDI$TaTX^DiMWnQamT_C`*S3$(VzXn! zv(Qy#19wM0lhJz9dUH88p;f_^DmG(_z=D@drkfHvn9KJLrRd;FZ!Duq|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up4XbxC_T2Zz2#Wg0Gj(JFIsDibPqd9K>NRi);yB09gj)`!mPi z^R5JcVE0U45a9KXoH=aY10D6pAnL2iAB56V8ZL|X_`>D!9)GwZ-V+E{#(R|T8=^ZeI zvo(~Q#=i(=VFu@Ca#al_XVKPdT!}fh@2lwFEKI`R@ef;fBmRl!7~gvKNB_dV?MOGf zkp=kL{U1Ef2w$L$OW=PV(-dq{uzaV2w)0%!Q?SUH;RFOP%6W>HIMZ?>&RZUmymF&T z@1gZ_rfl~q?Cm)Pt!K#y6337_hIGd%81nY|W5{>b^T&accsbxC?Zzfw#23D41UEPN zBmUEYI}ol9zmd-xM(}24?=43oe&33wK>ht#7zv!fTT9r9M;gNK7{R+na2uuGKPnl% z!|&kZPU0gH;BJ4UNr@=Oad%TN5(q~|@TuF;DkqLMl|{UNu+>Gro?eQ?q+T%G&IWL;TIZQPDS_IMai+v7R0)gB!fK7?hI zoJXzm>A^yNv#KRr>(GoP1W^M&x9mk1A-tU*J$Cbnj+Gd|Y7BDBU=2(@Kg7>QhjAHS z>DJ*^Y`~pdc{eulJ!KQ_mt+pF=mE!A??g z4}ratfWAhQ;93&%I#GkY%)mUcg5w&FTcI(6y$3!mN}vtJwhej zs>NziAts@OHb?0ucDr;VEo@pkbh{B1=NVAIrczSDUIm5%MG=)CDeL(2c?>AHi$BgY zkq#4GIh%>+>4d*{wL{S$`Oc4E9s)bdTmnA_(L&xTECu|G=MWcL>Aft1)vQa$@xu{3 zwd5pzE`j@t3+trLV%=vav&LCc2#?`cF*Ty9L>|np;wH*S#Q{nA-;U{$r5&tevfes+ z0;75YZkkHDtvn7ywV1+j0sLYrO2sTOoBvS=vBPhm)^j0ZV!2p}$cx%- BECc`m literal 0 HcmV?d00001 From c110e8a92c6b0c1fac598a1dd873a9cf5dafb9d2 Mon Sep 17 00:00:00 2001 From: Azamat Kulbarakov Date: Mon, 11 Apr 2022 10:21:26 +0500 Subject: [PATCH 2/6] 01_web task1 modified --- .../src/main/java/ru/netology/Handler.java | 7 +++++++ .../src/main/java/ru/netology/Main.java | 4 ++-- .../src/main/java/ru/netology/Request.java | 4 ++++ .../src/main/java/ru/netology/Server.java | 5 ++--- .../target/classes/ru/netology/Main.class | Bin 490 -> 489 bytes .../target/classes/ru/netology/Server.class | Bin 5879 -> 5995 bytes 6 files changed, 15 insertions(+), 5 deletions(-) create mode 100644 01_web/http-server/src/main/java/ru/netology/Handler.java create mode 100644 01_web/http-server/src/main/java/ru/netology/Request.java diff --git a/01_web/http-server/src/main/java/ru/netology/Handler.java b/01_web/http-server/src/main/java/ru/netology/Handler.java new file mode 100644 index 0000000..8647059 --- /dev/null +++ b/01_web/http-server/src/main/java/ru/netology/Handler.java @@ -0,0 +1,7 @@ +package ru.netology; + +import java.io.BufferedOutputStream; + +public interface Handler { + public void handle(Request request, BufferedOutputStream responseStream); +} diff --git a/01_web/http-server/src/main/java/ru/netology/Main.java b/01_web/http-server/src/main/java/ru/netology/Main.java index f069e77..98470d4 100644 --- a/01_web/http-server/src/main/java/ru/netology/Main.java +++ b/01_web/http-server/src/main/java/ru/netology/Main.java @@ -2,8 +2,8 @@ public class Main { public static void main(String[] args) { - Server myServer = new Server(); - myServer.listen(); + final var server = new Server(); + server.listen(); } } diff --git a/01_web/http-server/src/main/java/ru/netology/Request.java b/01_web/http-server/src/main/java/ru/netology/Request.java new file mode 100644 index 0000000..a2a6cdd --- /dev/null +++ b/01_web/http-server/src/main/java/ru/netology/Request.java @@ -0,0 +1,4 @@ +package ru.netology; + +public class Request { +} diff --git a/01_web/http-server/src/main/java/ru/netology/Server.java b/01_web/http-server/src/main/java/ru/netology/Server.java index 8949514..359b251 100644 --- a/01_web/http-server/src/main/java/ru/netology/Server.java +++ b/01_web/http-server/src/main/java/ru/netology/Server.java @@ -32,7 +32,7 @@ public void listen() { public void handle(Socket socket) { try (final var in = new BufferedReader(new InputStreamReader(socket.getInputStream())); - final var out = new BufferedOutputStream(socket.getOutputStream()); + final var out = new BufferedOutputStream(socket.getOutputStream()) ) { log("client connected: " + socket.getRemoteSocketAddress()); // read only request line for simplicity @@ -58,9 +58,8 @@ public void handle(Socket socket) { return; } - final var filePath = Path.of("http-server", "public", path); + final var filePath = Path.of(".", "public", path); final var mimeType = Files.probeContentType(filePath); -// final var mimeType = "text/plain"; // special case for classic if (path.equals("/classic.html")) { final var template = Files.readString(filePath); diff --git a/01_web/http-server/target/classes/ru/netology/Main.class b/01_web/http-server/target/classes/ru/netology/Main.class index d15df63119b078a5386e025d24178b2439e202da..820f28b4d312afd32ae256f8f0dd49fdb0da8f28 100644 GIT binary patch delta 24 gcmaFG{E~S>5<6Q?W^qYs-o%XgOl-xIxf$;R0CbQD`2YX_ delta 25 hcmaFK{EB%(5*urANn%mS#LW3D9J!Ujlerl00|0&m319#K diff --git a/01_web/http-server/target/classes/ru/netology/Server.class b/01_web/http-server/target/classes/ru/netology/Server.class index dfb776e6e35db4f87f3d1a3054b5adfee5dd7507..835898f7be40cf52e79d59c68ccf1438bf0a5fce 100644 GIT binary patch delta 1393 zcmb`HZBSHY6vuxDcJJN2d$DO5obF5}VUz+(g-+&5Q)y{0BQ(Qc*vr%kD`E?nEcwAn zoQbR`OT9^HFIt#DB`DekC~EI!WhK2^8NHa*2gho3s+sKE&6??ZU!MPc&iS8n{^xm~ zb5BHmD&A6Vge>l6xyd~?_qwA(d8Y?x6C~Hd!*3>XLEA@i$JJFx#S$r)@eg)G{y8+%!MHGn&6R zK&N0VY@VB_s}9l4vnJ2kjN^H?-K-4t&}*{AW-EQ}akJ{u?KUsat^x7en`4P4iymII z>8DSy7R6&V70E?)4YdIVm9ivRS0BIAVkbi;yKHu|$6XP)Y|Ktxwt0oSgg~l3+&##l|f#w)zv=3U-%R|oTr z175(eI}|Jm^zfn0M||ua3r-1i^Qp~e9CS;p!t5_RQlG^kzA_oH`I>Lsg;wdAhxpFs zu&2kYjVH@u_3y z&cja#q}Oc>m$dXz>G)E#Di)2OzVLl1?vMH%|1M)a;ezl(DK3trSfyoFZ*}~> zl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zv;|tWc5e)Yz8LP$jbX#EZnqWqrR*KZ(4{dvkie&)`?QB+cqE3$xa!IODzz>4U6892 zwXGG^HeIS1#dCgHmq*d(cj%I)$MEuIEU^N^ey8r#c8nqxL-e&6CRxp|_;iP^OpId> za7JBM&h*IE-uj;>*4THElL=nuINtrYUYC8N_+VAGE|1}3;uHAX%2=bsP4?n!zs+*n z3zes}>Gl}P^d#G)HD@o9ODzk From 7599f835240057e820e06ebdfd70ee8798c496b0 Mon Sep 17 00:00:00 2001 From: Azamat Kulbarakov Date: Mon, 23 May 2022 09:36:32 +0500 Subject: [PATCH 3/6] servlets task add --- 04_servlets/servlets/pom.xml | 33 +++++++++++ .../netology/controller/PostController.java | 41 ++++++++++++++ .../netology/exception/NotFoundException.java | 22 ++++++++ .../src/main/java/ru/netology/model/Post.java | 30 ++++++++++ .../netology/repository/PostRepository.java | 25 +++++++++ .../java/ru/netology/service/PostService.java | 32 +++++++++++ .../java/ru/netology/servlet/MainServlet.java | 55 +++++++++++++++++++ .../servlets/src/main/webapp/WEB-INF/web.xml | 20 +++++++ 04_servlets/servlets/src/test/request.http | 11 ++++ 9 files changed, 269 insertions(+) create mode 100644 04_servlets/servlets/pom.xml create mode 100644 04_servlets/servlets/src/main/java/ru/netology/controller/PostController.java create mode 100644 04_servlets/servlets/src/main/java/ru/netology/exception/NotFoundException.java create mode 100644 04_servlets/servlets/src/main/java/ru/netology/model/Post.java create mode 100644 04_servlets/servlets/src/main/java/ru/netology/repository/PostRepository.java create mode 100644 04_servlets/servlets/src/main/java/ru/netology/service/PostService.java create mode 100644 04_servlets/servlets/src/main/java/ru/netology/servlet/MainServlet.java create mode 100644 04_servlets/servlets/src/main/webapp/WEB-INF/web.xml create mode 100644 04_servlets/servlets/src/test/request.http diff --git a/04_servlets/servlets/pom.xml b/04_servlets/servlets/pom.xml new file mode 100644 index 0000000..66de66c --- /dev/null +++ b/04_servlets/servlets/pom.xml @@ -0,0 +1,33 @@ + + + 4.0.0 + + ru.netology + servlets + 1.0-SNAPSHOT + war + + + 11 + 11 + UTF-8 + + + + + javax.servlet + javax.servlet-api + 4.0.1 + provided + + + com.google.code.gson + gson + 2.8.6 + compile + + + + \ No newline at end of file diff --git a/04_servlets/servlets/src/main/java/ru/netology/controller/PostController.java b/04_servlets/servlets/src/main/java/ru/netology/controller/PostController.java new file mode 100644 index 0000000..dcd224a --- /dev/null +++ b/04_servlets/servlets/src/main/java/ru/netology/controller/PostController.java @@ -0,0 +1,41 @@ +package ru.netology.controller; + +import com.google.gson.Gson; +import ru.netology.model.Post; +import ru.netology.service.PostService; + +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; +import java.io.Reader; + +public class PostController { + public static final String APPLICATION_JSON = "application/json"; + private final PostService service; + + public PostController(PostService service) { + this.service = service; + } + + public void all(HttpServletResponse response) throws IOException { + response.setContentType(APPLICATION_JSON); + final var data = service.all(); + final var gson = new Gson(); + response.getWriter().print(gson.toJson(data)); + } + + public void getById(long id, HttpServletResponse response) { + // TODO: deserialize request & serialize response + } + + public void save(Reader body, HttpServletResponse response) throws IOException { + response.setContentType(APPLICATION_JSON); + final var gson = new Gson(); + final var post = gson.fromJson(body, Post.class); + final var data = service.save(post); + response.getWriter().print(gson.toJson(data)); + } + + public void removeById(long id, HttpServletResponse response) { + // TODO: deserialize request & serialize response + } +} diff --git a/04_servlets/servlets/src/main/java/ru/netology/exception/NotFoundException.java b/04_servlets/servlets/src/main/java/ru/netology/exception/NotFoundException.java new file mode 100644 index 0000000..82f9ac7 --- /dev/null +++ b/04_servlets/servlets/src/main/java/ru/netology/exception/NotFoundException.java @@ -0,0 +1,22 @@ +package ru.netology.exception; + +public class NotFoundException extends RuntimeException { + public NotFoundException() { + } + + public NotFoundException(String message) { + super(message); + } + + public NotFoundException(String message, Throwable cause) { + super(message, cause); + } + + public NotFoundException(Throwable cause) { + super(cause); + } + + public NotFoundException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { + super(message, cause, enableSuppression, writableStackTrace); + } +} diff --git a/04_servlets/servlets/src/main/java/ru/netology/model/Post.java b/04_servlets/servlets/src/main/java/ru/netology/model/Post.java new file mode 100644 index 0000000..d459c3e --- /dev/null +++ b/04_servlets/servlets/src/main/java/ru/netology/model/Post.java @@ -0,0 +1,30 @@ +package ru.netology.model; + +public class Post { + private long id; + private String content; + + public Post() { + } + + public Post(long id, String content) { + this.id = id; + this.content = content; + } + + public long getId() { + return id; + } + + public void setId(long id) { + this.id = id; + } + + public String getContent() { + return content; + } + + public void setContent(String content) { + this.content = content; + } +} diff --git a/04_servlets/servlets/src/main/java/ru/netology/repository/PostRepository.java b/04_servlets/servlets/src/main/java/ru/netology/repository/PostRepository.java new file mode 100644 index 0000000..b02cc29 --- /dev/null +++ b/04_servlets/servlets/src/main/java/ru/netology/repository/PostRepository.java @@ -0,0 +1,25 @@ +package ru.netology.repository; + +import ru.netology.model.Post; + +import java.util.Collections; +import java.util.List; +import java.util.Optional; + +// Stub +public class PostRepository { + public List all() { + return Collections.emptyList(); + } + + public Optional getById(long id) { + return Optional.empty(); + } + + public Post save(Post post) { + return post; + } + + public void removeById(long id) { + } +} diff --git a/04_servlets/servlets/src/main/java/ru/netology/service/PostService.java b/04_servlets/servlets/src/main/java/ru/netology/service/PostService.java new file mode 100644 index 0000000..e850c3d --- /dev/null +++ b/04_servlets/servlets/src/main/java/ru/netology/service/PostService.java @@ -0,0 +1,32 @@ +package ru.netology.service; + +import ru.netology.exception.NotFoundException; +import ru.netology.model.Post; +import ru.netology.repository.PostRepository; + +import java.util.List; + +public class PostService { + private final PostRepository repository; + + public PostService(PostRepository repository) { + this.repository = repository; + } + + public List all() { + return repository.all(); + } + + public Post getById(long id) { + return repository.getById(id).orElseThrow(NotFoundException::new); + } + + public Post save(Post post) { + return repository.save(post); + } + + public void removeById(long id) { + repository.removeById(id); + } +} + diff --git a/04_servlets/servlets/src/main/java/ru/netology/servlet/MainServlet.java b/04_servlets/servlets/src/main/java/ru/netology/servlet/MainServlet.java new file mode 100644 index 0000000..03b14ba --- /dev/null +++ b/04_servlets/servlets/src/main/java/ru/netology/servlet/MainServlet.java @@ -0,0 +1,55 @@ +package ru.netology.servlet; + +import ru.netology.controller.PostController; +import ru.netology.repository.PostRepository; +import ru.netology.service.PostService; + +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +public class MainServlet extends HttpServlet { + private PostController controller; + + @Override + public void init() { + final var repository = new PostRepository(); + final var service = new PostService(repository); + controller = new PostController(service); + } + + @Override + protected void service(HttpServletRequest req, HttpServletResponse resp) { + // если деплоились в root context, то достаточно этого + try { + final var path = req.getRequestURI(); + final var method = req.getMethod(); + // primitive routing + if (method.equals("GET") && path.equals("/api/posts")) { + controller.all(resp); + return; + } + if (method.equals("GET") && path.matches("/api/posts/\\d+")) { + // easy way + final var id = Long.parseLong(path.substring(path.lastIndexOf("/"))); + controller.getById(id, resp); + return; + } + if (method.equals("POST") && path.equals("/api/posts")) { + controller.save(req.getReader(), resp); + return; + } + if (method.equals("DELETE") && path.matches("/api/posts/\\d+")) { + // easy way + final var id = Long.parseLong(path.substring(path.lastIndexOf("/"))); + controller.removeById(id, resp); + return; + } + resp.setStatus(HttpServletResponse.SC_NOT_FOUND); + } catch (Exception e) { + e.printStackTrace(); + resp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); + } + } +} + diff --git a/04_servlets/servlets/src/main/webapp/WEB-INF/web.xml b/04_servlets/servlets/src/main/webapp/WEB-INF/web.xml new file mode 100644 index 0000000..2391e30 --- /dev/null +++ b/04_servlets/servlets/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,20 @@ + + + UTF-8 + + + main + ru.netology.servlet.MainServlet + 1 + + + + + main + / + + + diff --git a/04_servlets/servlets/src/test/request.http b/04_servlets/servlets/src/test/request.http new file mode 100644 index 0000000..d99095c --- /dev/null +++ b/04_servlets/servlets/src/test/request.http @@ -0,0 +1,11 @@ +GET http://localhost:8080/api/posts + +### + +POST http://localhost:8080/api/posts +Content-Type: application/json + +{ + "id": 0, + "content": "Last Post" +} From 3b221866bd23b4ab49833043b3b7ef659b5168b0 Mon Sep 17 00:00:00 2001 From: Azamat Kulbarakov Date: Tue, 24 May 2022 12:11:17 +0500 Subject: [PATCH 4/6] servlets task1 added --- 04_servlets/servlets/pom.xml | 27 +++++- .../netology/controller/PostController.java | 67 ++++++++------- .../netology/repository/PostRepository.java | 42 ++++++---- .../java/ru/netology/servlet/MainServlet.java | 82 ++++++++++--------- 04_servlets/servlets/src/test/request.http | 4 + 5 files changed, 139 insertions(+), 83 deletions(-) diff --git a/04_servlets/servlets/pom.xml b/04_servlets/servlets/pom.xml index 66de66c..cb536f8 100644 --- a/04_servlets/servlets/pom.xml +++ b/04_servlets/servlets/pom.xml @@ -25,9 +25,34 @@ com.google.code.gson gson - 2.8.6 + 2.9.0 compile + + + + org.apache.maven.plugins + maven-dependency-plugin + 2.3 + + + package + copy + + + + com.heroku + webapp-runner + ${webapp-runner.version} + webapp-runner.jar + + + + + + + + \ No newline at end of file diff --git a/04_servlets/servlets/src/main/java/ru/netology/controller/PostController.java b/04_servlets/servlets/src/main/java/ru/netology/controller/PostController.java index dcd224a..cf74a32 100644 --- a/04_servlets/servlets/src/main/java/ru/netology/controller/PostController.java +++ b/04_servlets/servlets/src/main/java/ru/netology/controller/PostController.java @@ -9,33 +9,42 @@ import java.io.Reader; public class PostController { - public static final String APPLICATION_JSON = "application/json"; - private final PostService service; - - public PostController(PostService service) { - this.service = service; - } - - public void all(HttpServletResponse response) throws IOException { - response.setContentType(APPLICATION_JSON); - final var data = service.all(); - final var gson = new Gson(); - response.getWriter().print(gson.toJson(data)); - } - - public void getById(long id, HttpServletResponse response) { - // TODO: deserialize request & serialize response - } - - public void save(Reader body, HttpServletResponse response) throws IOException { - response.setContentType(APPLICATION_JSON); - final var gson = new Gson(); - final var post = gson.fromJson(body, Post.class); - final var data = service.save(post); - response.getWriter().print(gson.toJson(data)); - } - - public void removeById(long id, HttpServletResponse response) { - // TODO: deserialize request & serialize response - } + public static final String APPLICATION_JSON = "application/json"; + private final PostService service; + + public PostController(PostService service) { + this.service = service; + } + + public void all(HttpServletResponse response) throws IOException { + response.setContentType(APPLICATION_JSON); + final var data = service.all(); + final var gson = new Gson(); + response.getWriter().print(gson.toJson(data)); + } + + public void getById(long id, HttpServletResponse response) throws IOException { + // TODO: deserialize request & serialize response + response.setContentType(APPLICATION_JSON); + final var gson = new Gson(); + final var post = service.getById(id); + response.getWriter().print(gson.toJson(post)); + } + + public void save(Reader body, HttpServletResponse response) throws IOException { + response.setContentType(APPLICATION_JSON); + final var gson = new Gson(); + final var post = gson.fromJson(body, Post.class); + final var data = service.save(post); + response.getWriter().print(gson.toJson(data)); + } + + public void removeById(long id, HttpServletResponse response) throws IOException { + // TODO: deserialize request & serialize response + response.setContentType(APPLICATION_JSON); + final var gson = new Gson(); + final var post = service.getById(id); + service.removeById(id); + response.getWriter().print(gson.toJson(post)); + } } diff --git a/04_servlets/servlets/src/main/java/ru/netology/repository/PostRepository.java b/04_servlets/servlets/src/main/java/ru/netology/repository/PostRepository.java index b02cc29..32e2b16 100644 --- a/04_servlets/servlets/src/main/java/ru/netology/repository/PostRepository.java +++ b/04_servlets/servlets/src/main/java/ru/netology/repository/PostRepository.java @@ -1,25 +1,39 @@ package ru.netology.repository; +import ru.netology.exception.NotFoundException; import ru.netology.model.Post; -import java.util.Collections; -import java.util.List; -import java.util.Optional; +import java.util.*; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.atomic.AtomicLong; // Stub public class PostRepository { - public List all() { - return Collections.emptyList(); - } + private final ConcurrentHashMap map = new ConcurrentHashMap<>(); + private final AtomicLong counter = new AtomicLong(1); - public Optional getById(long id) { - return Optional.empty(); - } + public List all() { + return new ArrayList<>(map.values()); + } - public Post save(Post post) { - return post; - } + public Optional getById(long id) { + return Optional.of(map.get(id)); + } - public void removeById(long id) { - } + public Post save(Post post) { + long id = post.getId(); + if (id == 0) { + map.put(counter.getAndIncrement(), post); + return post; + } + if (map.containsKey(id)) { + map.put(id, post); + return post; + } + throw new NotFoundException(); + } + + public void removeById(long id) { + map.remove(id); + } } diff --git a/04_servlets/servlets/src/main/java/ru/netology/servlet/MainServlet.java b/04_servlets/servlets/src/main/java/ru/netology/servlet/MainServlet.java index 03b14ba..08ee7a6 100644 --- a/04_servlets/servlets/src/main/java/ru/netology/servlet/MainServlet.java +++ b/04_servlets/servlets/src/main/java/ru/netology/servlet/MainServlet.java @@ -9,47 +9,51 @@ import javax.servlet.http.HttpServletResponse; public class MainServlet extends HttpServlet { - private PostController controller; + private PostController controller; - @Override - public void init() { - final var repository = new PostRepository(); - final var service = new PostService(repository); - controller = new PostController(service); - } + @Override + public void init() { + final var repository = new PostRepository(); + final var service = new PostService(repository); + controller = new PostController(service); + } + + @Override + protected void service(HttpServletRequest req, HttpServletResponse resp) { + // если деплоились в root context, то достаточно этого + try { + final var path = req.getRequestURI(); + final var method = req.getMethod(); + // primitive routing + if (method.equals("GET") && path.equals("/api/posts")) { + controller.all(resp); + return; + } + if (method.equals("GET") && path.matches("/api/posts/\\d+")) { + // easy way + final var id = parseLong(path); + controller.getById(id, resp); + return; + } + if (method.equals("POST") && path.equals("/api/posts")) { + controller.save(req.getReader(), resp); + return; + } + if (method.equals("DELETE") && path.matches("/api/posts/\\d+")) { + // easy way + final var id = parseLong(path); + controller.removeById(id, resp); + return; + } + resp.setStatus(HttpServletResponse.SC_NOT_FOUND); + } catch (Exception e) { + e.printStackTrace(); + resp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); + } + } - @Override - protected void service(HttpServletRequest req, HttpServletResponse resp) { - // если деплоились в root context, то достаточно этого - try { - final var path = req.getRequestURI(); - final var method = req.getMethod(); - // primitive routing - if (method.equals("GET") && path.equals("/api/posts")) { - controller.all(resp); - return; - } - if (method.equals("GET") && path.matches("/api/posts/\\d+")) { - // easy way - final var id = Long.parseLong(path.substring(path.lastIndexOf("/"))); - controller.getById(id, resp); - return; - } - if (method.equals("POST") && path.equals("/api/posts")) { - controller.save(req.getReader(), resp); - return; - } - if (method.equals("DELETE") && path.matches("/api/posts/\\d+")) { - // easy way - final var id = Long.parseLong(path.substring(path.lastIndexOf("/"))); - controller.removeById(id, resp); - return; - } - resp.setStatus(HttpServletResponse.SC_NOT_FOUND); - } catch (Exception e) { - e.printStackTrace(); - resp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); + private Long parseLong(String path) { + return Long.parseLong(path.substring(path.lastIndexOf("/") + 1)); } - } } diff --git a/04_servlets/servlets/src/test/request.http b/04_servlets/servlets/src/test/request.http index d99095c..46ac5b6 100644 --- a/04_servlets/servlets/src/test/request.http +++ b/04_servlets/servlets/src/test/request.http @@ -9,3 +9,7 @@ Content-Type: application/json "id": 0, "content": "Last Post" } + +### + +DELETE http://localhost:8080/api/posts/1 From 990e20a22e12a5bea149035952d7413b93900899 Mon Sep 17 00:00:00 2001 From: Azamat Kulbarakov Date: Tue, 24 May 2022 14:19:16 +0500 Subject: [PATCH 5/6] servlets task2 --- 04_servlets/servlets/pom.xml | 44 ++++++++++++++++++------------------ 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/04_servlets/servlets/pom.xml b/04_servlets/servlets/pom.xml index cb536f8..86d7fcd 100644 --- a/04_servlets/servlets/pom.xml +++ b/04_servlets/servlets/pom.xml @@ -9,32 +9,12 @@ 1.0-SNAPSHOT war - - 11 - 11 - UTF-8 - - - - - javax.servlet - javax.servlet-api - 4.0.1 - provided - - - com.google.code.gson - gson - 2.9.0 - compile - - org.apache.maven.plugins maven-dependency-plugin - 2.3 + 3.3.0 package @@ -44,7 +24,7 @@ com.heroku webapp-runner - ${webapp-runner.version} + 9.0.27.1 webapp-runner.jar @@ -55,4 +35,24 @@ + + 11 + 11 + UTF-8 + + + + + javax.servlet + javax.servlet-api + 4.0.1 + provided + + + com.google.code.gson + gson + 2.9.0 + compile + + \ No newline at end of file From a8488670f521cae751aa8774f26497deec925522 Mon Sep 17 00:00:00 2001 From: Azamat Kulbarakov Date: Mon, 30 May 2022 15:04:04 +0500 Subject: [PATCH 6/6] task1 modified --- 04_servlets/servlets/pom.xml | 30 +++++++++---------- .../netology/controller/PostController.java | 5 +--- .../netology/repository/PostRepository.java | 6 ++-- .../java/ru/netology/servlet/MainServlet.java | 4 +++ 4 files changed, 23 insertions(+), 22 deletions(-) diff --git a/04_servlets/servlets/pom.xml b/04_servlets/servlets/pom.xml index 86d7fcd..064f44f 100644 --- a/04_servlets/servlets/pom.xml +++ b/04_servlets/servlets/pom.xml @@ -8,7 +8,20 @@ servlets 1.0-SNAPSHOT war - + + + javax.servlet + javax.servlet-api + 4.0.1 + provided + + + com.google.code.gson + gson + 2.9.0 + compile + + @@ -40,19 +53,4 @@ 11 UTF-8 - - - - javax.servlet - javax.servlet-api - 4.0.1 - provided - - - com.google.code.gson - gson - 2.9.0 - compile - - \ No newline at end of file diff --git a/04_servlets/servlets/src/main/java/ru/netology/controller/PostController.java b/04_servlets/servlets/src/main/java/ru/netology/controller/PostController.java index cf74a32..cee2be6 100644 --- a/04_servlets/servlets/src/main/java/ru/netology/controller/PostController.java +++ b/04_servlets/servlets/src/main/java/ru/netology/controller/PostController.java @@ -39,12 +39,9 @@ public void save(Reader body, HttpServletResponse response) throws IOException { response.getWriter().print(gson.toJson(data)); } - public void removeById(long id, HttpServletResponse response) throws IOException { + public void removeById(long id, HttpServletResponse response) { // TODO: deserialize request & serialize response response.setContentType(APPLICATION_JSON); - final var gson = new Gson(); - final var post = service.getById(id); service.removeById(id); - response.getWriter().print(gson.toJson(post)); } } diff --git a/04_servlets/servlets/src/main/java/ru/netology/repository/PostRepository.java b/04_servlets/servlets/src/main/java/ru/netology/repository/PostRepository.java index 32e2b16..d8decef 100644 --- a/04_servlets/servlets/src/main/java/ru/netology/repository/PostRepository.java +++ b/04_servlets/servlets/src/main/java/ru/netology/repository/PostRepository.java @@ -10,7 +10,7 @@ // Stub public class PostRepository { private final ConcurrentHashMap map = new ConcurrentHashMap<>(); - private final AtomicLong counter = new AtomicLong(1); + private final AtomicLong counter = new AtomicLong(0); public List all() { return new ArrayList<>(map.values()); @@ -23,7 +23,9 @@ public Optional getById(long id) { public Post save(Post post) { long id = post.getId(); if (id == 0) { - map.put(counter.getAndIncrement(), post); + long postId = counter.getAndIncrement(); + post.setId(postId); + map.put(postId, post); return post; } if (map.containsKey(id)) { diff --git a/04_servlets/servlets/src/main/java/ru/netology/servlet/MainServlet.java b/04_servlets/servlets/src/main/java/ru/netology/servlet/MainServlet.java index 08ee7a6..9cb4f38 100644 --- a/04_servlets/servlets/src/main/java/ru/netology/servlet/MainServlet.java +++ b/04_servlets/servlets/src/main/java/ru/netology/servlet/MainServlet.java @@ -1,6 +1,7 @@ package ru.netology.servlet; import ru.netology.controller.PostController; +import ru.netology.exception.NotFoundException; import ru.netology.repository.PostRepository; import ru.netology.service.PostService; @@ -46,6 +47,9 @@ protected void service(HttpServletRequest req, HttpServletResponse resp) { return; } resp.setStatus(HttpServletResponse.SC_NOT_FOUND); + }catch (NotFoundException nfe) { + nfe.printStackTrace(); + resp.setStatus(HttpServletResponse.SC_NOT_FOUND); } catch (Exception e) { e.printStackTrace(); resp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);