Commit dc58dcce authored by dingjy's avatar dingjy

MODIFY

parent f0ef22af
......@@ -15,6 +15,8 @@ import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.Map;
@Slf4j
@Controller
......@@ -41,7 +43,8 @@ public class APIService {
@ResponseBody
@RequestMapping(value = "sa")
public String reportLog(HttpServletRequest request) {
public String app(HttpServletRequest request) {
String dataList = request.getParameter("data_list");
if(StringUtils.isNotEmpty(dataList)){
......@@ -52,6 +55,19 @@ public class APIService {
return OK_RESULT;
}
@ResponseBody
@RequestMapping(value = "web")
public String reportLog(HttpServletRequest request) {
String dataList = new InputStreamToString(request).get();
if(StringUtils.isNotEmpty(dataList)){
TRACKING_EVENT.info(dataList);
TRACKING_EVENT.info(dataList);
}
return OK_RESULT;
}
@ResponseBody
@RequestMapping(value = "ping")
public String test() {
......
package com.bayread.sensors.service;
/**
* @author martin.ad
* @Description:
* @date 创建时间: 2022/11/7 14:48
*/
import lombok.extern.slf4j.Slf4j;
import org.apache.catalina.connector.ClientAbortException;
import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream;
import javax.servlet.http.HttpServletRequest;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.Objects;
@Slf4j
public class InputStreamToString {
public static final String GZIP_ENCODE_UTF_8 = "UTF-8";
public static final int BUFFER_SIZE = 1024;
private InputStream in;
private boolean isCompress = false;
public InputStreamToString(HttpServletRequest request){
try {
this.in = request.getInputStream();
} catch (IOException e) {
log.error("request.getInputStream()",e);
}
String encode = request.getHeader("Content-Encoding");
if(!Objects.isNull(encode) && encode.trim().equals("gzip")){
isCompress = true;
}
}
public String get(){
return isCompress?gzipStreamToString():streamToString();
}
private String gzipStreamToString() {
try (ByteArrayOutputStream out = new ByteArrayOutputStream();
GzipCompressorInputStream gin = new GzipCompressorInputStream(in)){
final byte[] buffer = new byte[BUFFER_SIZE];
int n = 0;
while (-1 != (n = gin.read(buffer))) {
out.write(buffer, 0, n);
}
return out.toString(GZIP_ENCODE_UTF_8);
} catch (IOException e) {
log.error("gzipStreamToString error",e);
}
return null;
}
private String streamToString() {
try (ByteArrayOutputStream out = new ByteArrayOutputStream()){
final byte[] buffer = new byte[BUFFER_SIZE];
int n = 0;
while (-1 != (n = in.read(buffer))) {
out.write(buffer, 0, n);
}
return new String(out.toByteArray(), StandardCharsets.UTF_8);
} catch (IOException e) {
log.error("streamToString error",e);
}
return null;
}
}
......@@ -10,6 +10,7 @@ import org.springframework.kafka.core.KafkaTemplate;
import sun.misc.BASE64Decoder;
import java.io.IOException;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.List;
......@@ -30,7 +31,11 @@ public class LogsHandleWorker implements MultipleWorkerEvent<String> {
list.forEach(log ->{
try {
handle(log);
if(log.startsWith("data_list=")){
web(log);
}else{
app(log);
}
}catch (Exception ex){
ADAPTER_ERROR_LOG.info("ERROR LOG:{}",log);
ex.printStackTrace();
......@@ -38,7 +43,7 @@ public class LogsHandleWorker implements MultipleWorkerEvent<String> {
});
}
void handle(String logs) throws IOException {
void app(String logs) throws IOException {
BASE64Decoder decoder = new BASE64Decoder();
byte[] dbyte = decoder.decodeBuffer(logs);
......@@ -49,6 +54,18 @@ public class LogsHandleWorker implements MultipleWorkerEvent<String> {
}
}
void web(String logs) throws IOException{
String params = URLDecoder.decode(logs.substring(10),"UTF-8");
BASE64Decoder decoder = new BASE64Decoder();
byte[] dbyte = decoder.decodeBuffer(params);
JSONArray ja = JSON.parseArray(new String(dbyte, StandardCharsets.UTF_8));
for(int i=0;i<ja.size();i++) {
send(ja.getJSONObject(i).toString());
}
}
public void send(String log){
kafka.send(TOPIC, log);
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment