Skip to content

Commit

Permalink
refactor(home): optimize common moudle of home-alert
Browse files Browse the repository at this point in the history
  • Loading branch information
霄鸿 committed Apr 26, 2024
1 parent cd88abd commit d8dd44b
Show file tree
Hide file tree
Showing 39 changed files with 192 additions and 1,507 deletions.
5 changes: 5 additions & 0 deletions server/common/common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -90,5 +90,10 @@
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
</dependency>

<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpasyncclient</artifactId>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* Copyright 2022 Holoinsight Project Authors. Licensed under Apache-2.0.
*/
package io.holoinsight.server.home.alert.common;
package io.holoinsight.server.common;


import java.text.DecimalFormat;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* Copyright 2022 Holoinsight Project Authors. Licensed under Apache-2.0.
*/
package io.holoinsight.server.home.alert.common;
package io.holoinsight.server.common;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.DateUtils;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

Expand Down Expand Up @@ -57,6 +60,23 @@ public static <T> T fromJson(String json, Type type) {
return get().fromJson(json, type);
}

public static <T> T fromJson(String json, Class<T> classOfT) {
return get().fromJson(json, classOfT);
}


public static <T> List<T> parseList(String json, Class<T> elementType) {
JsonElement element = new JsonParser().parse(json);
JsonArray jsonArray = element.getAsJsonArray();
List<T> list = new ArrayList<>();
Iterator<JsonElement> it = jsonArray.iterator();
while (it.hasNext()) {
T object = get().fromJson(it.next(), elementType);
list.add(object);
}
return list;
}

/**
* <p>
* jsonFormatter.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* Copyright 2022 Holoinsight Project Authors. Licensed under Apache-2.0.
*/
package io.holoinsight.server.home.common.http;
package io.holoinsight.server.common.http;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* Copyright 2022 Holoinsight Project Authors. Licensed under Apache-2.0.
*/
package io.holoinsight.server.home.common.http;
package io.holoinsight.server.common.http;

import lombok.Getter;
import org.apache.http.Consts;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* Copyright 2022 Holoinsight Project Authors. Licensed under Apache-2.0.
*/
package io.holoinsight.server.home.common.http;
package io.holoinsight.server.common.http;

public class HttpException extends Exception {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* Copyright 2022 Holoinsight Project Authors. Licensed under Apache-2.0.
*/
package io.holoinsight.server.home.common.http;
package io.holoinsight.server.common.http;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* Copyright 2022 Holoinsight Project Authors. Licensed under Apache-2.0.
*/
package io.holoinsight.server.home.common.http;
package io.holoinsight.server.common.http;

import org.apache.commons.lang3.StringUtils;
import org.apache.http.Header;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* Copyright 2022 Holoinsight Project Authors. Licensed under Apache-2.0.
*/
package io.holoinsight.server.home.common.http;
package io.holoinsight.server.common.http;

import java.io.Serializable;
import java.util.Map;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* Copyright 2022 Holoinsight Project Authors. Licensed under Apache-2.0.
*/
package io.holoinsight.server.home.common.http;
package io.holoinsight.server.common.http;

import java.io.Serializable;

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ public static Map<String, String> generateObjectToStringMap(Object o) throws Exc
return map;
}

private static String getMethodName(String fildeName) throws Exception {
byte[] items = fildeName.getBytes();
private static String getMethodName(String fieldName) throws Exception {
byte[] items = fieldName.getBytes();
items[0] = (byte) ((char) items[0] - 'a' + 'A');
return new String(items);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
public class PeriodUtil implements Serializable {

public static List<Range> toRanges(PeriodType periodType, List<Long> periods) {
if (periods.size() == 0) {
if (periods.isEmpty()) {
return Collections.emptyList();
}
// 排序
Expand All @@ -45,7 +45,7 @@ public static List<Range> toRanges(PeriodType periodType, List<Long> periods) {
}

public static List<Range> toRanges(PeriodType periodType, List<Long> periods, int tagsSize) {
if (periods.size() == 0) {
if (periods.isEmpty()) {
return Collections.emptyList();
}
if (periods.size() * tagsSize < 86400) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
/*
* Copyright 2022 Holoinsight Project Authors. Licensed under Apache-2.0.
*/
package io.holoinsight.server.home.alert.common.webhook;
package io.holoinsight.server.home.alert.common;


import io.holoinsight.server.home.alert.common.G;
import io.holoinsight.server.home.alert.common.http.HttpProxy;
import io.holoinsight.server.home.alert.common.http.XHttpRequest;
import io.holoinsight.server.home.alert.common.http.XHttpResponse;
import io.holoinsight.server.common.J;
import io.holoinsight.server.common.http.HttpProxy;
import io.holoinsight.server.common.http.XHttpRequest;
import io.holoinsight.server.common.http.XHttpResponse;
import io.holoinsight.server.home.alert.model.event.WebhookInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -38,29 +37,29 @@ public static XHttpResponse sendWebhook(WebhookInfo webhookInfo) {
switch (webhookInfo.getRequestType().toUpperCase()) {
case "POST":
xHttpRequest = XHttpRequest.post(webhookInfo.getRequestUrl(),
G.get().fromJson(webhookInfo.getRequestHeaders(), Map.class), UTF_8.name(), 60000,
J.fromJson(webhookInfo.getRequestHeaders(), Map.class), UTF_8.name(), 60000,
webhookInfo.getWebhookMsg().getBytes(UTF_8), CONTENT_TYPE_UTF_8);
break;
case "GET":
xHttpRequest = XHttpRequest.get(webhookInfo.getRequestUrl(), null,
G.get().fromJson(webhookInfo.getRequestHeaders(), Map.class), UTF_8.name(), 60000);
J.fromJson(webhookInfo.getRequestHeaders(), Map.class), UTF_8.name(), 60000);
break;
case "PUT":
xHttpRequest = XHttpRequest.put(webhookInfo.getRequestUrl(),
G.get().fromJson(webhookInfo.getRequestHeaders(), Map.class), UTF_8.name(), null,
J.fromJson(webhookInfo.getRequestHeaders(), Map.class), UTF_8.name(), null,
CONTENT_TYPE_UTF_8, 60000);
break;
case "DELETE":
xHttpRequest = XHttpRequest.delete(webhookInfo.getRequestUrl(),
G.get().fromJson(webhookInfo.getRequestHeaders(), Map.class), UTF_8.name(), 60000);
J.fromJson(webhookInfo.getRequestHeaders(), Map.class), UTF_8.name(), 60000);
break;
default:
xHttpRequest = XHttpRequest.get(webhookInfo.getRequestUrl(), null,
G.get().fromJson(webhookInfo.getRequestHeaders(), Map.class), UTF_8.name(), 60000);
J.fromJson(webhookInfo.getRequestHeaders(), Map.class), UTF_8.name(), 60000);
}
return HttpProxy.request(xHttpRequest);
} catch (Exception e) {
LOGGER.error("SendWebhook Exception WebhookInfo:{}", G.get().toJson(webhookInfo), e);
LOGGER.error("SendWebhook Exception WebhookInfo:{}", J.toJson(webhookInfo), e);
return null;
}
}
Expand All @@ -70,25 +69,25 @@ public static XHttpResponse sendWebhookWithException(WebhookInfo webhookInfo) th
switch (webhookInfo.getRequestType().toUpperCase()) {
case "POST":
xHttpRequest = XHttpRequest.post(webhookInfo.getRequestUrl(),
G.get().fromJson(webhookInfo.getRequestHeaders(), Map.class), UTF_8.name(), 60000,
J.fromJson(webhookInfo.getRequestHeaders(), Map.class), UTF_8.name(), 60000,
webhookInfo.getWebhookMsg().getBytes(UTF_8), CONTENT_TYPE_UTF_8);
break;
case "GET":
xHttpRequest = XHttpRequest.get(webhookInfo.getRequestUrl(), null,
G.get().fromJson(webhookInfo.getRequestHeaders(), Map.class), UTF_8.name(), 60000);
J.fromJson(webhookInfo.getRequestHeaders(), Map.class), UTF_8.name(), 60000);
break;
case "PUT":
xHttpRequest = XHttpRequest.put(webhookInfo.getRequestUrl(),
G.get().fromJson(webhookInfo.getRequestHeaders(), Map.class), UTF_8.name(), null,
J.fromJson(webhookInfo.getRequestHeaders(), Map.class), UTF_8.name(), null,
CONTENT_TYPE_UTF_8, 60000);
break;
case "DELETE":
xHttpRequest = XHttpRequest.delete(webhookInfo.getRequestUrl(),
G.get().fromJson(webhookInfo.getRequestHeaders(), Map.class), UTF_8.name(), 60000);
J.fromJson(webhookInfo.getRequestHeaders(), Map.class), UTF_8.name(), 60000);
break;
default:
xHttpRequest = XHttpRequest.get(webhookInfo.getRequestUrl(), null,
G.get().fromJson(webhookInfo.getRequestHeaders(), Map.class), UTF_8.name(), 60000);
J.fromJson(webhookInfo.getRequestHeaders(), Map.class), UTF_8.name(), 60000);
}
return HttpProxy.request(xHttpRequest);
}
Expand Down
Loading

0 comments on commit d8dd44b

Please sign in to comment.