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..a622a8b 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,8 @@ 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 + BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())); + 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..239c627 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,8 @@ 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 + BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream())); + return bufferedReader.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..959d3c5 --- /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.ServletException; +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.time.LocalDate; + +@WebServlet("/date") +public class DateServlet extends HttpServlet { + + @Override + protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + var writer = resp.getWriter(); + var date = LocalDate.now(); + writer.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..af3e939 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,16 +1,40 @@ package com.bobocode.config; import com.bobocode.TestDataGenerator; +import com.bobocode.dao.AccountDao; +import com.bobocode.dao.FakeAccountDao; +import com.bobocode.model.Account; +import com.bobocode.service.AccountService; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; + +import java.util.List; /** * This class specifies application context configuration. It tells Spring to scan "dao" and "service" packages in order - * to find and create instances for {@link com.bobocode.dao.FakeAccountDao} and - * {@link com.bobocode.service.AccountService}. + * to find and create instances for {@link FakeAccountDao} and + * {@link AccountService}. *
* 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.
+ * into {@link 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();
+ }
+
+ @Bean
+ public AccountDao accountDao() {
+ return new FakeAccountDao(dataGenerator());
+ }
+
+ @Bean
+ public AccountService accountService() {
+ return new AccountService(accountDao());
+ }
+
}
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
* This controller handles the HTTP GET request that should list all notes. In order to do that, it fetches the list
- * of notes, adds that list as an attribute to the {@link org.springframework.ui.Model}, and returns a corresponding view.
+ * of notes, adds that list as an attribute to the {@link Model}, and returns a corresponding view.
* The view (which is Thymeleaf HTML template) expects to receive an attribute called `noteList`.
* In classical Spring MVC, controller methods return a string value which stores a view name.
* A {@link org.springframework.web.servlet.ViewResolver} is already configured to look for an HTML template in
@@ -27,9 +35,22 @@
* the same controller will look like {@link com.bobocode.mvc.api.NoteRestController}
*/
@RequiredArgsConstructor
+@Controller
+@RequestMapping("/notes")
public class NoteController {
private final Notes notes;
- // TODO: implement controller methods according to the javadoc and verify your impl using NoteControllerTest
+
+ @GetMapping()
+ public String getNotes(Model model){
+ model.addAttribute("noteList", notes.getAll());
+ return "notes";
+ }
+
+ @PostMapping
+ public String addNote(Note note){
+ notes.add(note);
+ return "redirect:/notes";
+ }
}
diff --git a/3-0-spring-framework/3-1-1-dispatcher-servlet-initializer/src/main/java/com/bobocode/config/RootConfig.java b/3-0-spring-framework/3-1-1-dispatcher-servlet-initializer/src/main/java/com/bobocode/config/RootConfig.java
index 6536634..1e33cc6 100644
--- a/3-0-spring-framework/3-1-1-dispatcher-servlet-initializer/src/main/java/com/bobocode/config/RootConfig.java
+++ b/3-0-spring-framework/3-1-1-dispatcher-servlet-initializer/src/main/java/com/bobocode/config/RootConfig.java
@@ -1,8 +1,10 @@
package com.bobocode.config;
+import net.bytebuddy.implementation.bind.annotation.IgnoreForBinding;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.context.annotation.Configuration;
+import org.springframework.context.annotation.FilterType;
import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
@@ -15,5 +17,18 @@
* todo: enable component scanning for all packages in "com.bobocode"
* todo: ignore all web related config and beans (ignore @{@link Controller}, ignore {@link EnableWebMvc}) using exclude filter
*/
+@Configuration
+@ComponentScan(basePackages = "com.bobocode",
+ excludeFilters = {
+ @Filter(
+ type = FilterType.ANNOTATION,
+ classes = EnableWebMvc.class
+ ),
+ @Filter(
+ type = FilterType.ANNOTATION,
+ classes = Controller.class
+ )
+ })
public class RootConfig {
+
}
diff --git a/3-0-spring-framework/3-1-1-dispatcher-servlet-initializer/src/main/java/com/bobocode/config/WebAppInitializer.java b/3-0-spring-framework/3-1-1-dispatcher-servlet-initializer/src/main/java/com/bobocode/config/WebAppInitializer.java
index ee23400..9f001de 100644
--- a/3-0-spring-framework/3-1-1-dispatcher-servlet-initializer/src/main/java/com/bobocode/config/WebAppInitializer.java
+++ b/3-0-spring-framework/3-1-1-dispatcher-servlet-initializer/src/main/java/com/bobocode/config/WebAppInitializer.java
@@ -1,6 +1,5 @@
package com.bobocode.config;
-import com.bobocode.util.ExerciseNotCompletedException;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
/**
@@ -9,16 +8,16 @@
public class WebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class>[] getRootConfigClasses() {
- throw new ExerciseNotCompletedException(); //todo: use {@link RootConfig} as root application config class
+ return new Class>[]{RootConfig.class};
}
@Override
protected Class>[] getServletConfigClasses() {
- throw new ExerciseNotCompletedException(); //todo: use {@link WebConfig} as ServletConfig class
+ return new Class>[]{WebConfig.class};
}
@Override
protected String[] getServletMappings() {
- throw new ExerciseNotCompletedException(); //todo: provide default servlet mapping ("/")
+ return new String[]{"/"};
}
}
diff --git a/3-0-spring-framework/3-1-1-dispatcher-servlet-initializer/src/main/java/com/bobocode/config/WebConfig.java b/3-0-spring-framework/3-1-1-dispatcher-servlet-initializer/src/main/java/com/bobocode/config/WebConfig.java
index 66d3a84..e3f3815 100644
--- a/3-0-spring-framework/3-1-1-dispatcher-servlet-initializer/src/main/java/com/bobocode/config/WebConfig.java
+++ b/3-0-spring-framework/3-1-1-dispatcher-servlet-initializer/src/main/java/com/bobocode/config/WebConfig.java
@@ -14,5 +14,8 @@
* todo: enable component scanning for package "web"
*/
+@Configuration
+@ComponentScan(basePackages = "com.bobocode.web")
+@EnableWebMvc
public class WebConfig {
}
diff --git a/3-0-spring-framework/3-1-1-dispatcher-servlet-initializer/src/main/java/com/bobocode/web/controller/WelcomeController.java b/3-0-spring-framework/3-1-1-dispatcher-servlet-initializer/src/main/java/com/bobocode/web/controller/WelcomeController.java
index 77392cc..12dc892 100644
--- a/3-0-spring-framework/3-1-1-dispatcher-servlet-initializer/src/main/java/com/bobocode/web/controller/WelcomeController.java
+++ b/3-0-spring-framework/3-1-1-dispatcher-servlet-initializer/src/main/java/com/bobocode/web/controller/WelcomeController.java
@@ -1,5 +1,9 @@
package com.bobocode.web.controller;
+import org.springframework.stereotype.Controller;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.ResponseBody;
+
/**
* Welcome controller that consists of one method that handles get request to "/welcome" and respond with a message.
*
@@ -8,8 +12,11 @@
* todo: tell Spring that {@link WelcomeController#welcome()} method provides response body without view
*/
+@Controller
public class WelcomeController {
+ @GetMapping("/welcome")
+ @ResponseBody
public String welcome() {
return "Welcome to Spring MVC!";
}
diff --git a/3-0-spring-framework/3-1-1-dispatcher-servlet-initializer/src/test/java/com/bobocode/WebAppConfigurationTest.java b/3-0-spring-framework/3-1-1-dispatcher-servlet-initializer/src/test/java/com/bobocode/WebAppConfigurationTest.java
index 54710ed..b21b0fa 100644
--- a/3-0-spring-framework/3-1-1-dispatcher-servlet-initializer/src/test/java/com/bobocode/WebAppConfigurationTest.java
+++ b/3-0-spring-framework/3-1-1-dispatcher-servlet-initializer/src/test/java/com/bobocode/WebAppConfigurationTest.java
@@ -13,6 +13,8 @@
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
+import java.util.ArrayList;
+import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@@ -170,6 +172,6 @@ void welcomeControllerMethodIsMarkedAsResponseBody() throws NoSuchMethodExceptio
}
private List
@@ -7,5 +11,8 @@
* todo: 2. Enable web mvc using annotation
* todo: 3. Enable component scanning for package "web" using annotation value
*/
+@Configuration
+@EnableWebMvc
+@ComponentScan(basePackages = "com.bobocode.web")
public class WebConfig {
}
diff --git a/3-0-spring-framework/3-2-1-account-rest-api/src/main/java/com/bobocode/dao/impl/InMemoryAccountDao.java b/3-0-spring-framework/3-2-1-account-rest-api/src/main/java/com/bobocode/dao/impl/InMemoryAccountDao.java
index 5620d5b..d8f76d8 100644
--- a/3-0-spring-framework/3-2-1-account-rest-api/src/main/java/com/bobocode/dao/impl/InMemoryAccountDao.java
+++ b/3-0-spring-framework/3-2-1-account-rest-api/src/main/java/com/bobocode/dao/impl/InMemoryAccountDao.java
@@ -3,6 +3,9 @@
import com.bobocode.dao.AccountDao;
import com.bobocode.exception.EntityNotFountException;
import com.bobocode.model.Account;
+import org.springframework.context.annotation.ComponentScan;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.HashMap;
@@ -10,10 +13,11 @@
import java.util.Map;
/**
- * {@link AccountDao} implementation that is based on {@link java.util.HashMap}.
+ * {@link AccountDao} implementation that is based on {@link HashMap}.
*
* todo: 1. Configure a component with name "accountDao"
*/
+@Component("accountDao")
public class InMemoryAccountDao implements AccountDao {
private Map
@@ -16,6 +34,48 @@
* todo: 7. Implement method that handles DELETE request with id as path variable removes an account by id
* todo: Configure HTTP response status code 204 - NO CONTENT
*/
-public class AccountRestController {
+@RestController()
+@RequestMapping("/accounts")
+public class AccountRestController implements AccountDao {
+ protected AccountDao accountDao = new InMemoryAccountDao();
+
+
+ public AccountRestController(AccountDao accountDao) {
+ this.accountDao = accountDao;
+ }
+
+ @Override
+ @GetMapping
+ public List
- *
+ *
* Note! This bean is not marked as a {@link Component} to avoid automatic scanning, instead it should be created in
* {@link StringTrimmingConfiguration} class which can be imported to a {@link Configuration} class by annotation
* {@link EnableStringTrimming}
*/
-public class TrimmedAnnotationBeanPostProcessor {
-//todo: Implement TrimmedAnnotationBeanPostProcessor according to javadoc
+public class TrimmedAnnotationBeanPostProcessor implements BeanPostProcessor {
+ @Override
+ public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
+ Class> beanClass = bean.getClass();
+ if(beanClass.isAnnotationPresent(Trimmed.class)) {
+ System.out.println(bean.getClass());
+ return createLoggingProxy(beanClass);
+ }
+ return bean;
+ }
+
+ @Override
+ public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
+ Class> beanClass = bean.getClass();
+ if(beanClass.isAnnotationPresent(Trimmed.class)) {
+ System.out.println(bean.getClass());
+ return createLoggingProxy(beanClass);
+ }
+ return bean;
+ }
+
+ public Object createLoggingProxy(Class> beanType) {
+ var enhancer = new Enhancer();
+ enhancer.setSuperclass(beanType);
+ enhancer.setInterfaces(beanType.getInterfaces());
+
+ MethodInterceptor methodInterceptor = (Object obj, Method method, Object[] args, MethodProxy proxy) -> {
+ return null;
+ };
+ return beanType.cast(enhancer.create());
+ }
+
}
diff --git a/3-0-spring-framework/3-3-0-enable-string-trimming/src/main/java/com/bobocode/annotation/EnableStringTrimming.java b/3-0-spring-framework/3-3-0-enable-string-trimming/src/main/java/com/bobocode/annotation/EnableStringTrimming.java
index 8c06d65..b04911f 100644
--- a/3-0-spring-framework/3-3-0-enable-string-trimming/src/main/java/com/bobocode/annotation/EnableStringTrimming.java
+++ b/3-0-spring-framework/3-3-0-enable-string-trimming/src/main/java/com/bobocode/annotation/EnableStringTrimming.java
@@ -1,8 +1,18 @@
package com.bobocode.annotation;
+import com.bobocode.StringTrimmingConfiguration;
+import org.springframework.context.annotation.Import;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
/**
* Annotation that can be placed on configuration class to import {@link StringTrimmingConfiguration}
*/
+@Target(ElementType.TYPE)
+@Retention(RetentionPolicy.RUNTIME)
+@Import(StringTrimmingConfiguration.class)
public @interface EnableStringTrimming {
-//todo: Implement EnableStringTrimming annotation according to javadoc
}