diff --git a/spring-boot/src/main/java/org/baeldung/main/SpringBootApplication.java b/spring-boot/src/main/java/org/baeldung/main/SpringBootApplication.java index 59955cc990..582d2d9e9c 100644 --- a/spring-boot/src/main/java/org/baeldung/main/SpringBootApplication.java +++ b/spring-boot/src/main/java/org/baeldung/main/SpringBootApplication.java @@ -1,8 +1,5 @@ package org.baeldung.main; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; - import org.baeldung.common.error.SpringHelloServletRegistrationBean; import org.baeldung.common.resources.ExecutorServiceExitCodeGenerator; import org.baeldung.controller.servlet.HelloWorldServlet; @@ -17,6 +14,9 @@ import org.springframework.context.annotation.ComponentScan; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; + @RestController @EnableAutoConfiguration @ComponentScan({ "org.baeldung.common.error", "org.baeldung.common.error.controller", "org.baeldung.common.properties", "org.baeldung.common.resources", "org.baeldung.endpoints", "org.baeldung.service", "org.baeldung.monitor.jmx", "org.baeldung.service" }) @@ -55,28 +55,6 @@ public class SpringBootApplication { return bean; } - /* @Bean - public JettyEmbeddedServletContainerFactory jettyEmbeddedServletContainerFactory() { - JettyEmbeddedServletContainerFactory jettyContainer = new JettyEmbeddedServletContainerFactory(); - jettyContainer.setPort(9000); - jettyContainer.setContextPath("/springbootapp"); - return jettyContainer; - } - - @Bean - public UndertowEmbeddedServletContainerFactory embeddedServletContainerFactory() { - UndertowEmbeddedServletContainerFactory factory = new UndertowEmbeddedServletContainerFactory(); - factory.addBuilderCustomizers(new UndertowBuilderCustomizer() { - - @Override - public void customize(io.undertow.Undertow.Builde builder) { - builder.addHttpListener(8080, "0.0.0.0"); - } - - }); - return factory; - }*/ - @Bean @Autowired public ExecutorServiceExitCodeGenerator executorServiceExitCodeGenerator(ExecutorService executorService) { diff --git a/spring-hibernate4/src/main/java/com/baeldung/hibernate/fetching/model/OrderDetail.java b/spring-hibernate4/src/main/java/com/baeldung/hibernate/fetching/model/OrderDetail.java index 19ed36eceb..91388b107b 100644 --- a/spring-hibernate4/src/main/java/com/baeldung/hibernate/fetching/model/OrderDetail.java +++ b/spring-hibernate4/src/main/java/com/baeldung/hibernate/fetching/model/OrderDetail.java @@ -3,71 +3,80 @@ package com.baeldung.hibernate.fetching.model; import java.io.Serializable; import java.sql.Date; -public class OrderDetail implements Serializable{ +public class OrderDetail implements Serializable { + + private static final long serialVersionUID = 1L; + private Long orderId; + private Date orderDate; + private String orderDesc; + private User user; + + public OrderDetail() { + + } + + public OrderDetail(Date orderDate, String orderDesc) { + super(); + this.orderDate = orderDate; + this.orderDesc = orderDesc; + } + + public Date getOrderDate() { + return orderDate; + } + + public void setOrderDate(Date orderDate) { + this.orderDate = orderDate; + } + + public String getOrderDesc() { + return orderDesc; + } + + public void setOrderDesc(String orderDesc) { + this.orderDesc = orderDesc; + } + + public User getUser() { + return user; + } + + public void setUser(User user) { + this.user = user; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((orderId == null) ? 0 : orderId.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + OrderDetail other = (OrderDetail) obj; + if (orderId == null) { + if (other.orderId != null) + return false; + } else if (!orderId.equals(other.orderId)) + return false; + + return true; + } + + public Long getOrderId() { + return orderId; + } + + public void setOrderId(Long orderId) { + this.orderId = orderId; + } - private static final long serialVersionUID = 1L; - private Long orderId; - private Date orderDate; - private String orderDesc; - private User user; - - public OrderDetail(){ - - } - - public OrderDetail(Date orderDate, String orderDesc) { - super(); - this.orderDate = orderDate; - this.orderDesc = orderDesc; - } - - public Date getOrderDate() { - return orderDate; - } - public void setOrderDate(Date orderDate) { - this.orderDate = orderDate; - } - public String getOrderDesc() { - return orderDesc; - } - public void setOrderDesc(String orderDesc) { - this.orderDesc = orderDesc; - } - public User getUser() { - return user; - } - public void setUser(User user) { - this.user = user; - } - @Override - public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + ((orderId == null) ? 0 : orderId.hashCode()); - return result; - } - @Override - public boolean equals(Object obj) { - if (this == obj) - return true; - if (obj == null) - return false; - if (getClass() != obj.getClass()) - return false; - OrderDetail other = (OrderDetail) obj; - if (orderId == null) { - if (other.orderId != null) - return false; - } else if (!orderId.equals(other.orderId)) - return false; - - return true; - } - public Long getOrderId() { - return orderId; - } - public void setOrderId(Long orderId) { - this.orderId = orderId; - } - } diff --git a/spring-hibernate4/src/main/java/com/baeldung/hibernate/fetching/model/User.java b/spring-hibernate4/src/main/java/com/baeldung/hibernate/fetching/model/User.java index eb98b7d0f9..158855f93e 100644 --- a/spring-hibernate4/src/main/java/com/baeldung/hibernate/fetching/model/User.java +++ b/spring-hibernate4/src/main/java/com/baeldung/hibernate/fetching/model/User.java @@ -5,92 +5,90 @@ import java.util.HashSet; import java.util.Set; public class User implements Serializable { - - private static final long serialVersionUID = 1L; - private Long userId; + + private static final long serialVersionUID = 1L; + private Long userId; private String userName; private String firstName; private String lastName; private Set orderDetail = new HashSet(); public User() { - } public User(final Long userId, final String userName, final String firstName, final String lastName) { - super(); - this.userId = userId; - this.userName = userName; - this.firstName = firstName; - this.lastName = lastName; + super(); + this.userId = userId; + this.userName = userName; + this.firstName = firstName; + this.lastName = lastName; } @Override public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + ((userId == null) ? 0 : userId.hashCode()); - return result; + final int prime = 31; + int result = 1; + result = prime * result + ((userId == null) ? 0 : userId.hashCode()); + return result; } @Override public boolean equals(final Object obj) { - if (this == obj) - return true; - if (obj == null) - return false; - if (getClass() != obj.getClass()) - return false; - final User other = (User) obj; - if (userId == null) { - if (other.userId != null) - return false; - } else if (!userId.equals(other.userId)) - return false; - return true; + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + final User other = (User) obj; + if (userId == null) { + if (other.userId != null) + return false; + } else if (!userId.equals(other.userId)) + return false; + return true; } public Long getUserId() { - return userId; + return userId; } public void setUserId(final Long userId) { - this.userId = userId; + this.userId = userId; } public String getUserName() { - return userName; + return userName; } public void setUserName(final String userName) { - this.userName = userName; + this.userName = userName; } public String getFirstName() { - return firstName; + return firstName; } public void setFirstName(final String firstName) { - this.firstName = firstName; + this.firstName = firstName; } public String getLastName() { - return lastName; + return lastName; } public void setLastName(final String lastName) { - this.lastName = lastName; + this.lastName = lastName; } - public Set getOrderDetail() { - return orderDetail; - } + public Set getOrderDetail() { + return orderDetail; + } - public void setOrderDetail(Set orderDetail) { - this.orderDetail = orderDetail; - } + public void setOrderDetail(Set orderDetail) { + this.orderDetail = orderDetail; + } - } diff --git a/spring-hibernate4/src/main/java/com/baeldung/hibernate/fetching/util/HibernateUtil.java b/spring-hibernate4/src/main/java/com/baeldung/hibernate/fetching/util/HibernateUtil.java index be877fcaba..65ecea2fa4 100644 --- a/spring-hibernate4/src/main/java/com/baeldung/hibernate/fetching/util/HibernateUtil.java +++ b/spring-hibernate4/src/main/java/com/baeldung/hibernate/fetching/util/HibernateUtil.java @@ -1,33 +1,30 @@ package com.baeldung.hibernate.fetching.util; import org.hibernate.Session; -import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; public class HibernateUtil { - private static SessionFactory factory; - @SuppressWarnings("deprecation") - public static Session getHibernateSession(String fetchMethod) { - //two config files are there - //one with lazy loading enabled - //another lazy = false - SessionFactory sf = null; - if ("lazy".equals(fetchMethod)) { - sf = new Configuration().configure("fetchingLazy.cfg.xml").buildSessionFactory(); - } else { - sf = new Configuration().configure("fetching.cfg.xml").buildSessionFactory(); - } - // fetching.cfg.xml is used for this example - final Session session = sf.openSession(); - return session; - } + @SuppressWarnings("deprecation") + public static Session getHibernateSession(String fetchMethod) { + //two config files are there + //one with lazy loading enabled + //another lazy = false - public static Session getHibernateSession() { - SessionFactory sf = null; - sf = new Configuration().configure("fetching.cfg.xml").buildSessionFactory(); - final Session session = sf.openSession(); - return session; - } + final String configFileName = "lazy".equals(fetchMethod) ? + "fetchingLazy.cfg.xml" : + "fetching.cfg.xml"; + + return new Configuration() + .configure(configFileName) + .buildSessionFactory().openSession(); + } + + public static Session getHibernateSession() { + return new Configuration() + .configure("fetching.cfg.xml") + .buildSessionFactory() + .openSession(); + } } diff --git a/spring-hibernate4/src/main/java/com/baeldung/hibernate/fetching/view/FetchingAppView.java b/spring-hibernate4/src/main/java/com/baeldung/hibernate/fetching/view/FetchingAppView.java index 729acde019..90fd302968 100644 --- a/spring-hibernate4/src/main/java/com/baeldung/hibernate/fetching/view/FetchingAppView.java +++ b/spring-hibernate4/src/main/java/com/baeldung/hibernate/fetching/view/FetchingAppView.java @@ -17,97 +17,94 @@ import com.baeldung.hibernate.fetching.model.User; public class FetchingAppView { - - public FetchingAppView(){ - - } - //lazily loaded - public boolean lazyLoaded(){ - final Session sessionLazy = HibernateUtil.getHibernateSession("lazy"); - List users = sessionLazy.createQuery("From User").list(); - User userLazyLoaded = new User(); - userLazyLoaded = users.get(3); - //since data is lazyloaded so data won't be initialized - Set orderDetailSet = userLazyLoaded.getOrderDetail(); - return (Hibernate.isInitialized(orderDetailSet)); - } - - //eagerly loaded - public boolean eagerLoaded(){ - final Session sessionEager = HibernateUtil.getHibernateSession(); - //data should be loaded in the following line - //also note the queries generated - List users =sessionEager.createQuery("From User").list(); - User userEagerLoaded = new User(); - userEagerLoaded = users.get(3); - Set orderDetailSet = userEagerLoaded.getOrderDetail(); - return (Hibernate.isInitialized(orderDetailSet)); - } - - - //creates test data - //call this method to create the data in the database - public void createTestData() { + public FetchingAppView() { - final Session session = HibernateUtil.getHibernateSession(); - Transaction tx = null; - tx = session.beginTransaction(); - final User user1 = new User(); - final User user2 = new User(); - final User user3 = new User(); - - user1.setFirstName("Priyam"); - user1.setLastName("Banerjee"); - user1.setUserName("priyambanerjee"); - session.save(user1); - - user2.setFirstName("Navneeta"); - user2.setLastName("Mukherjee"); - user2.setUserName("nmukh"); - session.save(user2); - - user3.setFirstName("Molly"); - user3.setLastName("Banerjee"); - user3.setUserName("mollyb"); - session.save(user3); + } - final OrderDetail order1 = new OrderDetail(); - final OrderDetail order2 = new OrderDetail(); - final OrderDetail order3 = new OrderDetail(); - final OrderDetail order4 = new OrderDetail(); - final OrderDetail order5 = new OrderDetail(); + public boolean lazyLoaded() { + final Session sessionLazy = HibernateUtil.getHibernateSession("lazy"); + List users = sessionLazy.createQuery("From User").list(); + User userLazyLoaded = users.get(3); + //since data is lazyloaded so data won't be initialized + Set orderDetailSet = userLazyLoaded.getOrderDetail(); - order1.setOrderDesc("First Order"); - order1.setOrderDate(new Date(2014, 10, 12)); - order1.setUser(user1); - - order2.setOrderDesc("Second Order"); - order2.setOrderDate(new Date(2016, 10, 25)); - order2.setUser(user1); - - order3.setOrderDesc("Third Order"); - order3.setOrderDate(new Date(2015, 2, 17)); - order3.setUser(user2); - - order4.setOrderDesc("Fourth Order"); - order4.setOrderDate(new Date(2014, 10, 1)); - order4.setUser(user2); - - order5.setOrderDesc("Fifth Order"); - order5.setOrderDate(new Date(2014, 9, 11)); - order5.setUser(user3); - - - session.saveOrUpdate(order1); - session.saveOrUpdate(order2); - session.saveOrUpdate(order3); - session.saveOrUpdate(order4); - session.saveOrUpdate(order5); + return (Hibernate.isInitialized(orderDetailSet)); + } - // session.saveOrUpdate(user1); - tx.commit(); - session.close(); + public boolean eagerLoaded() { + final Session sessionEager = HibernateUtil.getHibernateSession(); + //data should be loaded in the following line + //also note the queries generated + List users = sessionEager.createQuery("From User").list(); + User userEagerLoaded = users.get(3); + Set orderDetailSet = userEagerLoaded.getOrderDetail(); - } + return (Hibernate.isInitialized(orderDetailSet)); + } + + + //creates test data + //call this method to create the data in the database + public void createTestData() { + + final Session session = HibernateUtil.getHibernateSession(); + Transaction tx = session.beginTransaction(); + + final User user1 = new User(); + final User user2 = new User(); + final User user3 = new User(); + + user1.setFirstName("Priyam"); + user1.setLastName("Banerjee"); + user1.setUserName("priyambanerjee"); + session.save(user1); + + user2.setFirstName("Navneeta"); + user2.setLastName("Mukherjee"); + user2.setUserName("nmukh"); + session.save(user2); + + user3.setFirstName("Molly"); + user3.setLastName("Banerjee"); + user3.setUserName("mollyb"); + session.save(user3); + + final OrderDetail order1 = new OrderDetail(); + final OrderDetail order2 = new OrderDetail(); + final OrderDetail order3 = new OrderDetail(); + final OrderDetail order4 = new OrderDetail(); + final OrderDetail order5 = new OrderDetail(); + + order1.setOrderDesc("First Order"); + order1.setOrderDate(new Date(2014, 10, 12)); + order1.setUser(user1); + + order2.setOrderDesc("Second Order"); + order2.setOrderDate(new Date(2016, 10, 25)); + order2.setUser(user1); + + order3.setOrderDesc("Third Order"); + order3.setOrderDate(new Date(2015, 2, 17)); + order3.setUser(user2); + + order4.setOrderDesc("Fourth Order"); + order4.setOrderDate(new Date(2014, 10, 1)); + order4.setUser(user2); + + order5.setOrderDesc("Fifth Order"); + order5.setOrderDate(new Date(2014, 9, 11)); + order5.setUser(user3); + + + session.saveOrUpdate(order1); + session.saveOrUpdate(order2); + session.saveOrUpdate(order3); + session.saveOrUpdate(order4); + session.saveOrUpdate(order5); + + // session.saveOrUpdate(user1); + tx.commit(); + session.close(); + } } diff --git a/spring-security-rest-full/src/main/java/org/baeldung/web/interceptor/LoggerInterceptor.java b/spring-security-rest-full/src/main/java/org/baeldung/web/interceptor/LoggerInterceptor.java index f4aa2ff4f5..6afbd921ec 100644 --- a/spring-security-rest-full/src/main/java/org/baeldung/web/interceptor/LoggerInterceptor.java +++ b/spring-security-rest-full/src/main/java/org/baeldung/web/interceptor/LoggerInterceptor.java @@ -1,74 +1,78 @@ package org.baeldung.web.interceptor; -import java.util.Enumeration; - -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; - +import com.google.common.base.Strings; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; -import com.google.common.base.Strings; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.util.Enumeration; public class LoggerInterceptor extends HandlerInterceptorAdapter { - private static Logger log = LoggerFactory.getLogger(LoggerInterceptor.class); + private static Logger log = LoggerFactory.getLogger(LoggerInterceptor.class); - /** Executed before actual handler is executed **/ - @Override - public boolean preHandle(final HttpServletRequest request, final HttpServletResponse response, final Object handler) throws Exception { - log.info("[preHandle][" + request + "]" + "[" + request.getMethod() + "]" + request.getRequestURI() + getParameters(request)); - return true; - } + /** + * Executed before actual handler is executed + **/ + @Override + public boolean preHandle(final HttpServletRequest request, final HttpServletResponse response, final Object handler) throws Exception { + log.info("[preHandle][" + request + "]" + "[" + request.getMethod() + "]" + request.getRequestURI() + getParameters(request)); + return true; + } - /** Executed before after handler is executed **/ - @Override - public void postHandle(final HttpServletRequest request, final HttpServletResponse response, final Object handler, - final ModelAndView modelAndView) throws Exception { - log.info("[postHandle][" + request + "]"); - } + /** + * Executed before after handler is executed + **/ + @Override + public void postHandle(final HttpServletRequest request, final HttpServletResponse response, final Object handler, + final ModelAndView modelAndView) throws Exception { + log.info("[postHandle][" + request + "]"); + } - /** Executed after complete request is finished **/ - @Override - public void afterCompletion(final HttpServletRequest request, final HttpServletResponse response, final Object handler, final Exception ex) - throws Exception { - if (ex != null) - ex.printStackTrace(); - log.info("[afterCompletion][" + request + "][exception: " + ex + "]"); - } + /** + * Executed after complete request is finished + **/ + @Override + public void afterCompletion(final HttpServletRequest request, final HttpServletResponse response, final Object handler, final Exception ex) + throws Exception { + if (ex != null) + ex.printStackTrace(); + log.info("[afterCompletion][" + request + "][exception: " + ex + "]"); + } - private String getParameters(final HttpServletRequest request) { - final StringBuffer posted = new StringBuffer(); - final Enumeration e = request.getParameterNames(); - if (e != null) - posted.append("?"); - while (e.hasMoreElements()) { - if (posted.length() > 1) - posted.append("&"); - final String curr = (String) e.nextElement(); - posted.append(curr + "="); - if (curr.contains("password") || curr.contains("answer") || curr.contains("pwd")) { - posted.append("*****"); - } else { - posted.append(request.getParameter(curr)); - } - } + private String getParameters(final HttpServletRequest request) { + final StringBuffer posted = new StringBuffer(); + final Enumeration e = request.getParameterNames(); + if (e != null) + posted.append("?"); + while (e != null && e.hasMoreElements()) { + if (posted.length() > 1) + posted.append("&"); + final String curr = (String) e.nextElement(); + posted.append(curr).append("="); + if (curr.contains("password") || curr.contains("answer") || curr.contains("pwd")) { + posted.append("*****"); + } else { + posted.append(request.getParameter(curr)); + } + } - final String ip = request.getHeader("X-FORWARDED-FOR"); - final String ipAddr = (ip == null) ? getRemoteAddr(request) : ip; - if (!Strings.isNullOrEmpty(ipAddr)) - posted.append("&_psip=" + ipAddr); - return posted.toString(); - } + final String ip = request.getHeader("X-FORWARDED-FOR"); + final String ipAddr = (ip == null) ? getRemoteAddr(request) : ip; + if (!Strings.isNullOrEmpty(ipAddr)) + posted.append("&_psip=" + ipAddr); + return posted.toString(); + } - private String getRemoteAddr(final HttpServletRequest request) { - final String ipFromHeader = request.getHeader("X-FORWARDED-FOR"); - if (ipFromHeader != null && ipFromHeader.length() > 0) { - log.debug("ip from proxy - X-FORWARDED-FOR : " + ipFromHeader); - return ipFromHeader; - } - return request.getRemoteAddr(); - } + private String getRemoteAddr(final HttpServletRequest request) { + final String ipFromHeader = request.getHeader("X-FORWARDED-FOR"); + if (ipFromHeader != null && ipFromHeader.length() > 0) { + log.debug("ip from proxy - X-FORWARDED-FOR : " + ipFromHeader); + return ipFromHeader; + } + return request.getRemoteAddr(); + } }