From 4eb3768e12078f185e2ac90f143043defadcbd12 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcin=20Micha=C5=82ek?= Date: Tue, 27 Jan 2015 17:31:04 +0100 Subject: [PATCH] loging partially, fox for sql way id --- src/main/java/hello/Application.java | 1 - .../agh/edu/bazy/dbApi/gis/GisDbApi.java | 6 +-- .../bazy/interceptor/LoggingInterceptor.java | 50 +++++++++++++++++++ .../bazy/interceptor/RequestInterceptor.java | 23 ++++++--- .../java/hello/agh/edu/bazy/model/Log.java | 34 +++++++++++++ .../agh/edu/bazy/path/ModuleServicePaths.java | 2 +- 6 files changed, 103 insertions(+), 13 deletions(-) create mode 100644 src/main/java/hello/agh/edu/bazy/interceptor/LoggingInterceptor.java create mode 100644 src/main/java/hello/agh/edu/bazy/model/Log.java diff --git a/src/main/java/hello/Application.java b/src/main/java/hello/Application.java index cc3eb9d..cce1937 100644 --- a/src/main/java/hello/Application.java +++ b/src/main/java/hello/Application.java @@ -1,6 +1,5 @@ package hello; -import hello.agh.edu.bazy.interceptor.RequestInterceptor; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; diff --git a/src/main/java/hello/agh/edu/bazy/dbApi/gis/GisDbApi.java b/src/main/java/hello/agh/edu/bazy/dbApi/gis/GisDbApi.java index 6f8add5..3f3aaec 100644 --- a/src/main/java/hello/agh/edu/bazy/dbApi/gis/GisDbApi.java +++ b/src/main/java/hello/agh/edu/bazy/dbApi/gis/GisDbApi.java @@ -18,10 +18,10 @@ public class GisDbApi { private static final String GET_LAMPS = "SELECT gis_points_id,tags,st_transform(way,4326) FROM gis_points WHERE highway='street_lamp';"; private static String PUT_LAMP = "INSERT INTO public.gis_points(gis_points_id, way, highway) VALUES (@, st_geomfromtext('POINT(@ @)',4326), 'street_lamp');"; - private static final String GET_LAMP = "SELECT gis_points_id,st_transform(way,900913) FROM gis_points WHERE gis_points_id = ?;"; + private static final String GET_LAMP = "SELECT gis_points_id,st_transform(way,4326) FROM gis_points WHERE gis_points_id = ?;"; private static final String DELETE_LAMP = "DELETE FROM public.gis_points WHERE gis_points_id = ?"; - private static final String GET_POLYGONS = "SELECT st_transform(way,900913) as polygon FROM gis_polygons;"; - private static final String GET_POLYGON = "SELECT st_transform(way,900913) as polygon FROM gis_polygons WHERE gis_polygons_id = ?"; + private static final String GET_POLYGONS = "SELECT st_transform(way,4326) as polygon FROM gis_polygons;"; + private static final String GET_POLYGON = "SELECT st_transform(way,4326) as polygon FROM gis_polygons WHERE gis_polygons_id = ?"; JdbcTemplate gisJdbcTemplate; diff --git a/src/main/java/hello/agh/edu/bazy/interceptor/LoggingInterceptor.java b/src/main/java/hello/agh/edu/bazy/interceptor/LoggingInterceptor.java new file mode 100644 index 0000000..8e62598 --- /dev/null +++ b/src/main/java/hello/agh/edu/bazy/interceptor/LoggingInterceptor.java @@ -0,0 +1,50 @@ +package hello.agh.edu.bazy.interceptor; + +import hello.agh.edu.bazy.path.ModuleServicePaths; +import org.springframework.http.HttpEntity; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpMethod; +import org.springframework.http.MediaType; +import org.springframework.util.LinkedMultiValueMap; +import org.springframework.util.MultiValueMap; +import org.springframework.web.client.RestTemplate; +import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; + +import java.util.Arrays; +import java.util.List; + +/** + * Created by Marcin on 2015-01-27. + */ +public class LoggingInterceptor extends HandlerInterceptorAdapter { + public static final List availableLoglevels = Arrays.asList("info", "debug", "warn", "error"); + protected RestTemplate restTemplate; + protected String permission; + + public LoggingInterceptor() { + } + + public LoggingInterceptor(String permission) { + this.restTemplate = new RestTemplate(); + this.permission = permission; + } + + protected boolean logEvent(String message, String logLevel, String type) { + HttpHeaders headers = new HttpHeaders(); + headers.setContentType(MediaType.APPLICATION_JSON); + + MultiValueMap map = new LinkedMultiValueMap<>(); + map.add("type", type); + map.add("msg", message); + + HttpEntity entity = new HttpEntity(headers); + HttpEntity response = restTemplate.exchange( + ModuleServicePaths.PUT_LOG + .concat("/").concat(logLevel), + HttpMethod.POST, entity, + Object.class, + map); + + return true; + } +} diff --git a/src/main/java/hello/agh/edu/bazy/interceptor/RequestInterceptor.java b/src/main/java/hello/agh/edu/bazy/interceptor/RequestInterceptor.java index d59c0d7..581557b 100644 --- a/src/main/java/hello/agh/edu/bazy/interceptor/RequestInterceptor.java +++ b/src/main/java/hello/agh/edu/bazy/interceptor/RequestInterceptor.java @@ -1,13 +1,11 @@ package hello.agh.edu.bazy.interceptor; -import hello.agh.edu.bazy.path.ModuleServicePaths; import hello.agh.edu.bazy.controllers.LoginController; +import hello.agh.edu.bazy.path.ModuleServicePaths; import org.springframework.http.HttpStatus; import org.springframework.stereotype.Component; -import org.springframework.web.client.RestTemplate; import org.springframework.web.servlet.ModelAndView; -import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @@ -16,16 +14,13 @@ import javax.servlet.http.HttpServletResponse; * Created by lukasgol on 22.01.15. */ @Component -public class RequestInterceptor extends HandlerInterceptorAdapter { - RestTemplate restTemplate; - String permission; +public class RequestInterceptor extends LoggingInterceptor { public RequestInterceptor() { } public RequestInterceptor(String permission) { - this.restTemplate = new RestTemplate(); - this.permission = permission; + super(permission); } @Override @@ -45,14 +40,26 @@ public class RequestInterceptor extends HandlerInterceptorAdapter { if (!correctHeaders(request)) { System.out.println("No token in header."); response.sendError(HttpStatus.BAD_REQUEST.value(), "No token parameter in request header"); + logEvent("No token parameter in request header." + + request.getRequestURI(), + "error", + "rest"); return super.preHandle(request, response, handler); } if (validToken(request.getHeader(LoginController.TOKEN))) { + logEvent("Correct request." + + request.getRequestURI(), + "info", + "rest"); return super.preHandle(request, response, handler); } else { System.out.println("Token or permission not valid."); response.sendError(HttpStatus.UNAUTHORIZED.value(), "Token or permission not valid."); + logEvent("Token or permission not valid." + + request.getRequestURI(), + "error", + "rest"); return super.preHandle(request, response, handler); } } diff --git a/src/main/java/hello/agh/edu/bazy/model/Log.java b/src/main/java/hello/agh/edu/bazy/model/Log.java new file mode 100644 index 0000000..3254b5b --- /dev/null +++ b/src/main/java/hello/agh/edu/bazy/model/Log.java @@ -0,0 +1,34 @@ +package hello.agh.edu.bazy.model; + +import com.fasterxml.jackson.annotation.JsonProperty; + +/** + * Created by Marcin on 2015-01-27. + */ +public class Log { + private String type; + + @JsonProperty("msg") + private String message; + + public Log(String type, String message) { + this.type = type; + this.message = message; + } + + public String getType() { + return type; + } + + public void setType(String type) { + this.type = type; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } +} diff --git a/src/main/java/hello/agh/edu/bazy/path/ModuleServicePaths.java b/src/main/java/hello/agh/edu/bazy/path/ModuleServicePaths.java index 666c732..7a28f6c 100644 --- a/src/main/java/hello/agh/edu/bazy/path/ModuleServicePaths.java +++ b/src/main/java/hello/agh/edu/bazy/path/ModuleServicePaths.java @@ -23,7 +23,7 @@ public final class ModuleServicePaths { //LOGS public static final String LOGS_MODULE = ROOT + "/restlogger/logger"; + public static final String PUT_LOG = ROOT + "/restlogger/logger"; public static final String GET_LOGS = LOGS_MODULE + "/getlog"; -// http://10.156.207.136:8080/restlogger/logger/getlog?moduleType=gis&logLevels=debug,error }