/**
 *    Copyright 2016 Sven Loesekann

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 */
package ch.xxx.trader.adapter.config;

import java.io.IOException;
import java.util.Collections;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;

import com.mongodb.MongoTimeoutException;

import io.netty.handler.timeout.TimeoutException;

@ControllerAdvice
public class GlobalExceptionHandler extends ResponseEntityExceptionHandler {
	private static final Logger LOGGER = LoggerFactory.getLogger(GlobalExceptionHandler.class);

	@ExceptionHandler({ MongoTimeoutException.class, TimeoutException.class })
	@ResponseStatus(value = HttpStatus.BAD_REQUEST)
	public Object handleException(final Exception exception, final HttpServletRequest request) {
		record MyEntry(String key, Object value) {
		}
		LOGGER.info(String.format("Execption: %s", exception.getMessage()), exception);
		LOGGER.info("Remote Ip: {}", request.getRemoteAddr());
		LOGGER.info("Request URL: {}", request.getRequestURL());
		Map<String, Object> attributeMap = Collections.list(request.getAttributeNames()).stream()
				.flatMap(attName -> Stream.of(new MyEntry(attName, request.getAttribute(attName))))
				.collect(Collectors.toMap(myEntry -> myEntry.key, myEntry -> myEntry.value));
		LOGGER.debug("Request Attributes: {}", this.createStringFromMap(attributeMap));
		Map<String, Object> headerMap = Collections.list(request.getHeaderNames()).stream()
				.flatMap(headerName -> Stream.of(new MyEntry(headerName, request.getHeader(headerName))))
				.collect(Collectors.toMap(myEntry -> myEntry.key, myEntry -> myEntry.value));
		LOGGER.info("Request Headers: {}", this.createStringFromMap(headerMap));
		LOGGER.info("Request Body length: {}", request.getContentLength());
		try {
			LOGGER.debug("Request Body content: {}", new String(request.getInputStream().readAllBytes()));
		} catch (IOException e) {
			LOGGER.warn("Failed to display body.", e);
		}
		return new Object();
	}

	private <K, V> String createStringFromMap(Map<K, V> myMap) {
		return myMap.entrySet().stream()
				.map(entry -> String.format("%s: %s", entry.getKey(), entry.getValue() == null ? "" : entry.getValue()))
				.collect(Collectors.joining(" | "));
	}
}
@@ -632,157 +632,8 @@
 g;%0A%0A
-import java.io.IOException;%0Aimport java.util.Collections;%0Aimport java.util.Map;%0Aimport java.util.stream.Collectors;%0Aimport java.util.stream.Stream;%0A%0A
 impo
@@ -1561,24 +1561,26 @@
  request) %7B%0A
+//
 %09%09record MyE
@@ -1612,16 +1612,18 @@
 alue) %7B%0A
+//
 %09%09%7D%0A%09%09LO
@@ -1815,17 +1815,21 @@
 tURL());
-%0A
+%09%09%0A//
 %09%09Map%3CSt
@@ -1904,32 +1904,34 @@
 mes()).stream()%0A
+//
 %09%09%09%09.flatMap(att
@@ -1994,32 +1994,34 @@
 ute(attName))))%0A
+//
 %09%09%09%09.collect(Col
@@ -2078,32 +2078,34 @@
 yEntry.value));%0A
+//
 %09%09LOGGER.debug(%22
@@ -2170,16 +2170,18 @@
 eMap));%0A
+//
 %09%09Map%3CSt
@@ -2254,24 +2254,26 @@
 )).stream()%0A
+//
 %09%09%09%09.flatMap
@@ -2350,24 +2350,26 @@
 derName))))%0A
+//
 %09%09%09%09.collect
@@ -2434,24 +2434,26 @@
 ry.value));%0A
+//
 %09%09LOGGER.inf
@@ -2511,24 +2511,26 @@
 eaderMap));%0A
+//
 %09%09LOGGER.inf
@@ -2591,16 +2591,18 @@
 ));%0A
+//
 %09%09try %7B%0A
 %09%09%09L
@@ -2597,16 +2597,18 @@
 %09%09try %7B%0A
+//
 %09%09%09LOGGE
@@ -2697,16 +2697,18 @@
 es()));%0A
+//
 %09%09%7D catc
@@ -2727,16 +2727,18 @@
 on e) %7B%0A
+//
 %09%09%09LOGGE
@@ -2775,16 +2775,18 @@
 .%22, e);%0A
+//
 %09%09%7D%0A%09%09re
@@ -2808,16 +2808,18 @@
 ();%0A%09%7D%0A%0A
+//
 %09private
@@ -2872,16 +2872,18 @@
 yMap) %7B%0A
+//
 %09%09return
@@ -2909,16 +2909,18 @@
 tream()%0A
+//
 %09%09%09%09.map
@@ -3020,16 +3020,18 @@
 lue()))%0A
+//
 %09%09%09%09.col
@@ -3063,12 +3063,14 @@
  %7C %22));%0A
+//
 %09%7D%0A%7D

clean up