From 4a1ed86ef73754af65841db8578f86dee0747482 Mon Sep 17 00:00:00 2001 From: shanicejones Date: Thu, 7 Mar 2024 19:46:23 -0500 Subject: [PATCH] Excercise --- .../com/bobocode/net/client/ClientUtil.java | 7 +++++-- .../com/bobocode/net/server/ServerUtil.java | 8 ++++--- .../com/bobocode/servlet/DateServlet.java | 21 +++++++++++++++++++ .../bobocode/config/ApplicationConfig.java | 11 ++++++++-- .../java/com/bobocode/dao/FakeAccountDao.java | 4 ++++ .../com/bobocode/service/AccountService.java | 2 ++ 6 files changed, 46 insertions(+), 7 deletions(-) create mode 100644 2-0-servlet-api/2-0-1-hello-servlet-api/src/main/java/com/bobocode/servlet/DateServlet.java diff --git a/1-0-networking-and-http/1-0-0-hello-network-socket/src/main/java/com/bobocode/net/client/ClientUtil.java b/1-0-networking-and-http/1-0-0-hello-network-socket/src/main/java/com/bobocode/net/client/ClientUtil.java index e2cdac5..cdec6ad 100644 --- a/1-0-networking-and-http/1-0-0-hello-network-socket/src/main/java/com/bobocode/net/client/ClientUtil.java +++ b/1-0-networking-and-http/1-0-0-hello-network-socket/src/main/java/com/bobocode/net/client/ClientUtil.java @@ -24,7 +24,7 @@ private ClientUtil() { */ @SneakyThrows public static Socket openSocket(String host, int port) { - throw new ExerciseNotCompletedException(); // todo: implement according to javadoc and verify by ClientUtilTest + return new Socket(host, port); } /** @@ -62,6 +62,9 @@ public static String readMessage(BufferedReader reader) { */ @SneakyThrows public static void writeToSocket(String message, Socket socket) { - throw new ExerciseNotCompletedException(); // todo: implement according to javadoc and verify by ClientUtilTest + OutputStream outputStream = socket.getOutputStream(); + Writer writer = new BufferedWriter(new OutputStreamWriter(outputStream)); + writer.write(message); + writer.flush(); } } diff --git a/1-0-networking-and-http/1-0-0-hello-network-socket/src/main/java/com/bobocode/net/server/ServerUtil.java b/1-0-networking-and-http/1-0-0-hello-network-socket/src/main/java/com/bobocode/net/server/ServerUtil.java index 3778579..4bafb7a 100644 --- a/1-0-networking-and-http/1-0-0-hello-network-socket/src/main/java/com/bobocode/net/server/ServerUtil.java +++ b/1-0-networking-and-http/1-0-0-hello-network-socket/src/main/java/com/bobocode/net/server/ServerUtil.java @@ -41,7 +41,7 @@ public static String getLocalHost() { */ @SneakyThrows public static ServerSocket createServerSocket(int port) { - throw new ExerciseNotCompletedException(); // todo: implement according to javadoc and verify by ServerUtilTest + return new ServerSocket(port); } /** @@ -52,7 +52,7 @@ public static ServerSocket createServerSocket(int port) { */ @SneakyThrows public static Socket acceptClientSocket(ServerSocket serverSocket) { - throw new ExerciseNotCompletedException(); // todo: implement according to javadoc and verify by ServerUtilTest + return serverSocket.accept(); } /** @@ -66,7 +66,9 @@ public static Socket acceptClientSocket(ServerSocket serverSocket) { */ @SneakyThrows public static String readMessageFromSocket(Socket socket) { - throw new ExerciseNotCompletedException(); // todo: implement according to javadoc and verify by ServerUtilTest + InputStream inputStream = socket.getInputStream(); + BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); + return reader.readLine(); } /** diff --git a/2-0-servlet-api/2-0-1-hello-servlet-api/src/main/java/com/bobocode/servlet/DateServlet.java b/2-0-servlet-api/2-0-1-hello-servlet-api/src/main/java/com/bobocode/servlet/DateServlet.java new file mode 100644 index 0000000..413f25d --- /dev/null +++ b/2-0-servlet-api/2-0-1-hello-servlet-api/src/main/java/com/bobocode/servlet/DateServlet.java @@ -0,0 +1,21 @@ +package com.bobocode.servlet; + +import jakarta.servlet.annotation.WebServlet; +import jakarta.servlet.http.HttpServlet; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; + +import java.io.IOException; +import java.io.PrintWriter; +import java.time.LocalDate; + +@WebServlet("/date") +public class DateServlet extends HttpServlet { + + @Override + public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { + PrintWriter out = response.getWriter(); + LocalDate date = LocalDate.now(); + out.println(date); + } +} diff --git a/3-0-spring-framework/3-0-0-hello-spring-framework/src/main/java/com/bobocode/config/ApplicationConfig.java b/3-0-spring-framework/3-0-0-hello-spring-framework/src/main/java/com/bobocode/config/ApplicationConfig.java index f7ebf38..ed14d85 100644 --- a/3-0-spring-framework/3-0-0-hello-spring-framework/src/main/java/com/bobocode/config/ApplicationConfig.java +++ b/3-0-spring-framework/3-0-0-hello-spring-framework/src/main/java/com/bobocode/config/ApplicationConfig.java @@ -1,6 +1,7 @@ package com.bobocode.config; import com.bobocode.TestDataGenerator; +import org.springframework.context.annotation.*; /** * This class specifies application context configuration. It tells Spring to scan "dao" and "service" packages in order @@ -10,7 +11,13 @@ * It also explicitly configures a bean of {@link TestDataGenerator} called "dataGenerator". This beans will be injected * into {@link com.bobocode.dao.FakeAccountDao} in order to generate some fake accounts. */ + +@Configuration +@ComponentScan(basePackages = {"com.bobocode.dao", "com.bobocode.service"}) public class ApplicationConfig { - // todo: configure application context according to javadoc by following tests in ApplicationConfigTest - // todo: verify final implementation by running ApplicationContextTest + + @Bean + public TestDataGenerator dataGenerator() { + return new TestDataGenerator(); + } } diff --git a/3-0-spring-framework/3-0-0-hello-spring-framework/src/main/java/com/bobocode/dao/FakeAccountDao.java b/3-0-spring-framework/3-0-0-hello-spring-framework/src/main/java/com/bobocode/dao/FakeAccountDao.java index bae7264..988a90b 100644 --- a/3-0-spring-framework/3-0-0-hello-spring-framework/src/main/java/com/bobocode/dao/FakeAccountDao.java +++ b/3-0-spring-framework/3-0-0-hello-spring-framework/src/main/java/com/bobocode/dao/FakeAccountDao.java @@ -2,6 +2,8 @@ import com.bobocode.TestDataGenerator; import com.bobocode.model.Account; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; import java.util.List; import java.util.stream.Stream; @@ -16,9 +18,11 @@ * Its bean is called "accountDao". And it uses constructor with explicit autowired annotation in order to inject * {@link TestDataGenerator} instance. */ +@Component("accountDao") public class FakeAccountDao implements AccountDao { private List accounts; + @Autowired public FakeAccountDao(TestDataGenerator testDataGenerator) { this.accounts = Stream.generate(testDataGenerator::generateAccount) .limit(20) diff --git a/3-0-spring-framework/3-0-0-hello-spring-framework/src/main/java/com/bobocode/service/AccountService.java b/3-0-spring-framework/3-0-0-hello-spring-framework/src/main/java/com/bobocode/service/AccountService.java index a46dfd2..d6d69b8 100644 --- a/3-0-spring-framework/3-0-0-hello-spring-framework/src/main/java/com/bobocode/service/AccountService.java +++ b/3-0-spring-framework/3-0-0-hello-spring-framework/src/main/java/com/bobocode/service/AccountService.java @@ -2,6 +2,7 @@ import com.bobocode.dao.AccountDao; import com.bobocode.model.Account; +import org.springframework.stereotype.Service; import static java.util.Comparator.comparing; @@ -12,6 +13,7 @@ * Since it's a service that should be added to the application context, it is marked as Spring service. It order to get * {@link AccountDao} instances, it uses implicit constructor-based injection. */ +@Service public class AccountService { private final AccountDao accountDao;