[BAEL-16663] - Split or move spring-mvc-java module
This commit is contained in:
@@ -1,54 +0,0 @@
|
||||
package com.baeldung.aop;
|
||||
|
||||
import org.aspectj.lang.JoinPoint;
|
||||
import org.aspectj.lang.annotation.Aspect;
|
||||
import org.aspectj.lang.annotation.Before;
|
||||
import org.aspectj.lang.annotation.Pointcut;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
@Component
|
||||
@Aspect
|
||||
public class LoggingAspect {
|
||||
|
||||
private static Logger logger = Logger.getLogger(LoggingAspect.class.getName());
|
||||
|
||||
private ThreadLocal<SimpleDateFormat> sdf = new ThreadLocal<SimpleDateFormat>() {
|
||||
@Override
|
||||
protected SimpleDateFormat initialValue() {
|
||||
return new SimpleDateFormat("[yyyy-mm-dd hh:mm:ss:SSS]");
|
||||
}
|
||||
};
|
||||
|
||||
@Pointcut("within(com.baeldung..*) && execution(* com.baeldung.dao.FooDao.*(..))")
|
||||
public void repositoryMethods() {
|
||||
}
|
||||
|
||||
@Pointcut("within(com.baeldung..*) && @annotation(com.baeldung.aop.annotations.Loggable)")
|
||||
public void loggableMethods() {
|
||||
}
|
||||
|
||||
@Pointcut("within(com.baeldung..*) && @args(com.baeldung.aop.annotations.Entity)")
|
||||
public void methodsAcceptingEntities() {
|
||||
}
|
||||
|
||||
@Before("repositoryMethods()")
|
||||
public void logMethodCall(JoinPoint jp) {
|
||||
String methodName = jp.getSignature().getName();
|
||||
logger.info(sdf.get().format(new Date()) + methodName);
|
||||
}
|
||||
|
||||
@Before("loggableMethods()")
|
||||
public void logMethod(JoinPoint jp) {
|
||||
String methodName = jp.getSignature().getName();
|
||||
logger.info("Executing method: " + methodName);
|
||||
}
|
||||
|
||||
@Before("methodsAcceptingEntities()")
|
||||
public void logMethodAcceptionEntityAnnotatedBean(JoinPoint jp) {
|
||||
logger.info("Accepting beans with @Entity annotation: " + jp.getArgs()[0]);
|
||||
}
|
||||
}
|
||||
@@ -1,32 +0,0 @@
|
||||
package com.baeldung.aop;
|
||||
|
||||
import org.aspectj.lang.ProceedingJoinPoint;
|
||||
import org.aspectj.lang.annotation.Around;
|
||||
import org.aspectj.lang.annotation.Aspect;
|
||||
import org.aspectj.lang.annotation.Pointcut;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
@Aspect
|
||||
@Component
|
||||
public class PerformanceAspect {
|
||||
|
||||
private static Logger logger = Logger.getLogger(PerformanceAspect.class.getName());
|
||||
|
||||
@Pointcut("within(com.baeldung..*) && execution(* com.baeldung.dao.FooDao.*(..))")
|
||||
public void repositoryClassMethods() {
|
||||
}
|
||||
|
||||
@Around("repositoryClassMethods()")
|
||||
public Object measureMethodExecutionTime(ProceedingJoinPoint pjp) throws Throwable {
|
||||
long start = System.nanoTime();
|
||||
Object retval = pjp.proceed();
|
||||
long end = System.nanoTime();
|
||||
String methodName = pjp.getSignature().getName();
|
||||
logger.info("Execution of " + methodName + " took " + TimeUnit.NANOSECONDS.toMillis(end - start) + " ms");
|
||||
return retval;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,39 +0,0 @@
|
||||
package com.baeldung.aop;
|
||||
|
||||
import com.baeldung.events.FooCreationEvent;
|
||||
import org.aspectj.lang.JoinPoint;
|
||||
import org.aspectj.lang.annotation.AfterReturning;
|
||||
import org.aspectj.lang.annotation.Aspect;
|
||||
import org.aspectj.lang.annotation.Pointcut;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.ApplicationEventPublisher;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
@Aspect
|
||||
public class PublishingAspect {
|
||||
|
||||
private ApplicationEventPublisher eventPublisher;
|
||||
|
||||
@Autowired
|
||||
public void setEventPublisher(ApplicationEventPublisher eventPublisher) {
|
||||
this.eventPublisher = eventPublisher;
|
||||
}
|
||||
|
||||
@Pointcut("within(com.baeldung..*) && execution(* com.baeldung.dao.FooDao.*(..))")
|
||||
public void repositoryMethods() {
|
||||
}
|
||||
|
||||
@Pointcut("within(com.baeldung..*) && execution(* com.baeldung.dao.FooDao.create*(Long,..))")
|
||||
public void firstLongParamMethods() {
|
||||
}
|
||||
|
||||
@Pointcut("repositoryMethods() && firstLongParamMethods()")
|
||||
public void entityCreationMethods() {
|
||||
}
|
||||
|
||||
@AfterReturning(value = "entityCreationMethods()", returning = "entity")
|
||||
public void logMethodCall(JoinPoint jp, Object entity) throws Throwable {
|
||||
eventPublisher.publishEvent(new FooCreationEvent(entity));
|
||||
}
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
package com.baeldung.aop.annotations;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.TYPE)
|
||||
public @interface Entity {
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
package com.baeldung.aop.annotations;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.METHOD)
|
||||
public @interface Loggable {
|
||||
}
|
||||
@@ -1,22 +0,0 @@
|
||||
package com.baeldung.dao;
|
||||
|
||||
import com.baeldung.aop.annotations.Loggable;
|
||||
import com.baeldung.model.Foo;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
public class FooDao {
|
||||
|
||||
public String findById(Long id) {
|
||||
return "Bazz";
|
||||
}
|
||||
|
||||
@Loggable
|
||||
public Foo create(Long id, String name) {
|
||||
return new Foo(id, name);
|
||||
}
|
||||
|
||||
public Foo merge(Foo foo) {
|
||||
return foo;
|
||||
}
|
||||
}
|
||||
@@ -1,10 +0,0 @@
|
||||
package com.baeldung.events;
|
||||
|
||||
import org.springframework.context.ApplicationEvent;
|
||||
|
||||
public class FooCreationEvent extends ApplicationEvent {
|
||||
|
||||
public FooCreationEvent(Object source) {
|
||||
super(source);
|
||||
}
|
||||
}
|
||||
@@ -1,17 +0,0 @@
|
||||
package com.baeldung.events;
|
||||
|
||||
import org.springframework.context.ApplicationListener;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.logging.Logger;
|
||||
|
||||
@Component
|
||||
public class FooCreationEventListener implements ApplicationListener<FooCreationEvent> {
|
||||
|
||||
private static Logger logger = Logger.getLogger(FooCreationEventListener.class.getName());
|
||||
|
||||
@Override
|
||||
public void onApplicationEvent(FooCreationEvent event) {
|
||||
logger.info("Created foo instance: " + event.getSource().toString());
|
||||
}
|
||||
}
|
||||
@@ -1,19 +0,0 @@
|
||||
package com.baeldung.model;
|
||||
|
||||
import com.baeldung.aop.annotations.Entity;
|
||||
|
||||
@Entity
|
||||
public class Foo {
|
||||
private Long id;
|
||||
private String name;
|
||||
|
||||
public Foo(Long id, String name) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Foo{" + "id=" + id + ", name='" + name + '\'' + '}';
|
||||
}
|
||||
}
|
||||
@@ -1,15 +0,0 @@
|
||||
package com.baeldung.model;
|
||||
|
||||
public class Message {
|
||||
|
||||
private String from;
|
||||
private String text;
|
||||
|
||||
public String getText() {
|
||||
return text;
|
||||
}
|
||||
|
||||
public String getFrom() {
|
||||
return from;
|
||||
}
|
||||
}
|
||||
@@ -1,27 +0,0 @@
|
||||
package com.baeldung.model;
|
||||
|
||||
public class OutputMessage {
|
||||
|
||||
private String from;
|
||||
private String text;
|
||||
private String time;
|
||||
|
||||
public OutputMessage(final String from, final String text, final String time) {
|
||||
|
||||
this.from = from;
|
||||
this.text = text;
|
||||
this.time = time;
|
||||
}
|
||||
|
||||
public String getText() {
|
||||
return text;
|
||||
}
|
||||
|
||||
public String getTime() {
|
||||
return time;
|
||||
}
|
||||
|
||||
public String getFrom() {
|
||||
return from;
|
||||
}
|
||||
}
|
||||
@@ -1,25 +0,0 @@
|
||||
package com.baeldung.spring.web.config;
|
||||
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
|
||||
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
|
||||
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
|
||||
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;
|
||||
|
||||
@Configuration
|
||||
@EnableWebSocketMessageBroker
|
||||
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {
|
||||
|
||||
@Override
|
||||
public void configureMessageBroker(final MessageBrokerRegistry config) {
|
||||
config.enableSimpleBroker("/topic");
|
||||
config.setApplicationDestinationPrefixes("/app");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void registerStompEndpoints(final StompEndpointRegistry registry) {
|
||||
registry.addEndpoint("/chat");
|
||||
registry.addEndpoint("/chat").withSockJS();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,42 +0,0 @@
|
||||
package com.baeldung.spring.web.config;
|
||||
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.http.server.ServerHttpRequest;
|
||||
import org.springframework.http.server.ServerHttpResponse;
|
||||
import org.springframework.http.server.ServletServerHttpRequest;
|
||||
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
|
||||
import org.springframework.web.socket.WebSocketHandler;
|
||||
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
|
||||
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
|
||||
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;
|
||||
import org.springframework.web.socket.server.support.DefaultHandshakeHandler;
|
||||
|
||||
import javax.servlet.http.HttpSession;
|
||||
import java.util.Map;
|
||||
|
||||
@Configuration
|
||||
@EnableWebSocketMessageBroker
|
||||
public class WebSocketSendToUserConfig implements WebSocketMessageBrokerConfigurer {
|
||||
|
||||
@Override
|
||||
public void configureMessageBroker(MessageBrokerRegistry config) {
|
||||
config.enableSimpleBroker("/topic/", "/queue/");
|
||||
config.setApplicationDestinationPrefixes("/app");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void registerStompEndpoints(StompEndpointRegistry registry) {
|
||||
registry.addEndpoint("/greeting").setHandshakeHandler(new DefaultHandshakeHandler() {
|
||||
|
||||
//Get sessionId from request and set it in Map attributes
|
||||
public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler,
|
||||
Map attributes) throws Exception {
|
||||
if (request instanceof ServletServerHttpRequest) {
|
||||
ServletServerHttpRequest servletRequest = (ServletServerHttpRequest) request;
|
||||
HttpSession session = servletRequest.getServletRequest().getSession();
|
||||
attributes.put("sessionId", session.getId());
|
||||
}
|
||||
return true;
|
||||
}}).withSockJS();
|
||||
}
|
||||
}
|
||||
@@ -1,23 +0,0 @@
|
||||
package com.baeldung.web.controller;
|
||||
|
||||
import com.baeldung.model.Message;
|
||||
import com.baeldung.model.OutputMessage;
|
||||
import org.springframework.messaging.handler.annotation.MessageMapping;
|
||||
import org.springframework.messaging.handler.annotation.SendTo;
|
||||
import org.springframework.stereotype.Controller;
|
||||
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
|
||||
@Controller
|
||||
public class ChatController {
|
||||
|
||||
@MessageMapping("/chat")
|
||||
@SendTo("/topic/messages")
|
||||
public OutputMessage send(final Message message) throws Exception {
|
||||
|
||||
final String time = new SimpleDateFormat("HH:mm").format(new Date());
|
||||
return new OutputMessage(message.getFrom(), message.getText(), time);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,30 +0,0 @@
|
||||
package com.baeldung.web.controller;
|
||||
|
||||
import java.security.Principal;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.messaging.handler.annotation.MessageExceptionHandler;
|
||||
import org.springframework.messaging.handler.annotation.MessageMapping;
|
||||
import org.springframework.messaging.handler.annotation.Payload;
|
||||
import org.springframework.messaging.simp.annotation.SendToUser;
|
||||
import org.springframework.stereotype.Controller;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
|
||||
@Controller
|
||||
public class WebsocketSendToUserController {
|
||||
|
||||
private Gson gson = new Gson();
|
||||
|
||||
@MessageMapping("/message")
|
||||
@SendToUser("/queue/reply")
|
||||
public String processMessageFromClient(@Payload String message, Principal principal) throws Exception {
|
||||
return gson.fromJson(message, Map.class).get("name").toString();
|
||||
}
|
||||
|
||||
@MessageExceptionHandler
|
||||
@SendToUser("/queue/errors")
|
||||
public String handleException(Throwable exception) {
|
||||
return exception.getMessage();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user