Skip to content

Commit

Permalink
Merge pull request #2745 from weihubeats/WebhookUtil
Browse files Browse the repository at this point in the history
[ISSUE #2730] Method check a map with containsKey(), before using get() [WebhookUtil]
  • Loading branch information
xwm1992 authored Dec 31, 2022
2 parents 450b30d + fc42a01 commit e24a0e1
Showing 1 changed file with 11 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,28 +29,27 @@
import org.apache.http.message.BasicHeader;

import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import lombok.extern.slf4j.Slf4j;

/**
* Utility class for implementing CloudEvents Http Webhook spec
*
* @see <a href="https://github.com/cloudevents/spec/blob/v1.0.2/cloudevents/http-webhook.md">CloudEvents Http Webhook</a>
*/
@Slf4j
public class WebhookUtil {

private static final Logger LOGGER = LoggerFactory.getLogger(WebhookUtil.class);

private static final String CONTENT_TYPE_HEADER = "Content-Type";
private static final String REQUEST_ORIGIN_HEADER = "WebHook-Request-Origin";
private static final String ALLOWED_ORIGIN_HEADER = "WebHook-Allowed-Origin";

private static final Map<String, AuthService> authServices = new ConcurrentHashMap<>();

public static boolean obtainDeliveryAgreement(CloseableHttpClient httpClient, String targetUrl, String requestOrigin) {
LOGGER.info("obtain webhook delivery agreement for url: {}", targetUrl);
log.info("obtain webhook delivery agreement for url: {}", targetUrl);
HttpOptions builder = new HttpOptions(targetUrl);
builder.addHeader(REQUEST_ORIGIN_HEADER, requestOrigin);

Expand All @@ -59,8 +58,8 @@ public static boolean obtainDeliveryAgreement(CloseableHttpClient httpClient, St
return StringUtils.isEmpty(allowedOrigin)
|| allowedOrigin.equals("*") || allowedOrigin.equalsIgnoreCase(requestOrigin);
} catch (Exception e) {
LOGGER.error(String.format("HTTP Options Method is not supported at the Delivery Target: %s,"
+ " unable to obtain the webhook delivery agreement.", targetUrl), e);
log.error("HTTP Options Method is not supported at the Delivery Target: {}, unable to obtain the webhook delivery agreement.", targetUrl,
e);
}
return true;
}
Expand Down Expand Up @@ -89,19 +88,19 @@ private static Map<String, String> getHttpAuthParam(String authType) {
}

private static AuthService getHttpAuthPlugin(String pluginType) {
if (authServices.containsKey(pluginType)) {
return authServices.get(pluginType);
AuthService authService = authServices.get(pluginType);
if (Objects.nonNull(authService)) {
return authService;
}

AuthService authService = EventMeshExtensionFactory.getExtension(AuthService.class, pluginType);
authService = EventMeshExtensionFactory.getExtension(AuthService.class, pluginType);

Validate.notNull(authService, "can't load the authService plugin, please check.");
try {
authService.init();
authServices.put(pluginType, authService);
return authService;
} catch (Exception e) {
LOGGER.error("Error in initializing authService", e);
log.error("Error in initializing authService", e);
}
return null;
}
Expand Down

0 comments on commit e24a0e1

Please sign in to comment.