Skip to content

Commit c6080e4

Browse files
committed
Mods to change HookManager into an interface.
1 parent 27aa5f5 commit c6080e4

File tree

3 files changed

+92
-37
lines changed

3 files changed

+92
-37
lines changed

src/main/java/org/gitlab4j/api/HookManager.java

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,37 +4,23 @@
44
import javax.servlet.http.HttpServletRequest;
55

66
/**
7-
* This class provides a base class handler for processing GitLab Web Hook and System Hook callouts.
7+
* This interface provides a base class handler for processing GitLab Web Hook and System Hook callouts.
88
*/
9-
public abstract class HookManager {
10-
11-
private String secretToken;
12-
13-
/**
14-
* Create a HookManager to handle GitLab hook events.
15-
*/
16-
public HookManager() {
17-
this.secretToken = null;
18-
}
9+
public interface HookManager {
1910

2011
/**
21-
* Create a HookManager to handle GitLab hook events which will be verified
22-
* against the specified secretToken.
23-
*
24-
* @param secretToken the secret token to verify against
12+
* Get the secret token that received hook events should be validated against.
13+
*
14+
* @return the secret token that received hook events should be validated against
2515
*/
26-
public HookManager(String secretToken) {
27-
this.secretToken = secretToken;
28-
}
16+
String getSecretToken();
2917

3018
/**
3119
* Set the secret token that received hook events should be validated against.
3220
*
3321
* @param secretToken the secret token to verify against
3422
*/
35-
public void setSecretToken(String secretToken) {
36-
this.secretToken = secretToken;
37-
}
23+
void setSecretToken(String secretToken);
3824

3925
/**
4026
* Validate the provided secret token against the reference secret token. Returns true if
@@ -44,8 +30,11 @@ public void setSecretToken(String secretToken) {
4430
* @param secretToken the token to validate
4531
* @return true if the secret token is valid or there is no reference secret token to validate against
4632
*/
47-
public boolean isValidSecretToken(String secretToken) {
48-
return (this.secretToken == null || this.secretToken.equals(secretToken) ? true : false);
33+
default public boolean isValidSecretToken(String secretToken) {
34+
String ourSecretToken = getSecretToken();
35+
return (ourSecretToken == null ||
36+
ourSecretToken.trim().isEmpty() ||
37+
ourSecretToken.equals(secretToken) ? true : false);
4938
}
5039

5140
/**
@@ -56,9 +45,9 @@ public boolean isValidSecretToken(String secretToken) {
5645
* @param request the HTTP request to verify the secret token
5746
* @return true if the secret token is valid or there is no reference secret token to validate against
5847
*/
59-
public boolean isValidSecretToken(HttpServletRequest request) {
48+
default public boolean isValidSecretToken(HttpServletRequest request) {
6049

61-
if (this.secretToken != null) {
50+
if (getSecretToken() != null) {
6251
String secretToken = request.getHeader("X-Gitlab-Token");
6352
return (isValidSecretToken(secretToken));
6453
}
@@ -73,5 +62,5 @@ public boolean isValidSecretToken(HttpServletRequest request) {
7362
* @param request the HttpServletRequest to read the Event instance from
7463
* @throws GitLabApiException if the parsed event is not supported
7564
*/
76-
public abstract void handleEvent(HttpServletRequest request) throws GitLabApiException;
65+
public void handleEvent(HttpServletRequest request) throws GitLabApiException;
7766
}

src/main/java/org/gitlab4j/api/systemhooks/SystemHookManager.java

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
/**
2222
* This class provides a handler for processing GitLab System Hook callouts.
2323
*/
24-
public class SystemHookManager extends HookManager {
24+
public class SystemHookManager implements HookManager {
2525

2626
public static final String SYSTEM_HOOK_EVENT = "System Hook";
2727
private final static Logger LOGGER = GitLabApi.getLogger();
@@ -30,37 +30,69 @@ public class SystemHookManager extends HookManager {
3030
// Collection of objects listening for System Hook events.
3131
private final List<SystemHookListener> systemHookListeners = new CopyOnWriteArrayList<SystemHookListener>();
3232

33+
private String secretToken;
34+
3335
/**
3436
* Create a HookManager to handle GitLab system hook events.
3537
*/
3638
public SystemHookManager() {
37-
super();
3839
}
3940

4041
/**
4142
* Create a HookManager to handle GitLab system hook events which will be verified
4243
* against the specified secretToken.
43-
*
44+
*
4445
* @param secretToken the secret token to verify against
4546
*/
4647
public SystemHookManager(String secretToken) {
47-
super(secretToken);
48-
}
48+
this.secretToken = secretToken;
49+
}
50+
51+
/**
52+
* Get the secret token that received hook events should be validated against.
53+
*
54+
* @return the secret token that received hook events should be validated against
55+
*/
56+
public String getSecretToken() {
57+
return (secretToken);
58+
}
59+
60+
/**
61+
* Set the secret token that received hook events should be validated against.
62+
*
63+
* @param secretToken the secret token to verify against
64+
*/
65+
public void setSecretToken(String secretToken) {
66+
this.secretToken = secretToken;
67+
}
4968

5069
/**
5170
* Parses and verifies an SystemHookEvent instance from the HTTP request and
5271
* fires it off to the registered listeners.
53-
*
72+
*
5473
* @param request the HttpServletRequest to read the Event instance from
5574
* @throws GitLabApiException if the parsed event is not supported
5675
*/
5776
public void handleEvent(HttpServletRequest request) throws GitLabApiException {
77+
handleRequest(request);
78+
}
79+
80+
/**
81+
* Parses and verifies an SystemHookEvent instance from the HTTP request and
82+
* fires it off to the registered listeners.
83+
*
84+
* @param request the HttpServletRequest to read the Event instance from
85+
* @return the processed SystemHookEvent instance read from the request,null if the request
86+
* not contain a system hook event
87+
* @throws GitLabApiException if the parsed event is not supported
88+
*/
89+
public SystemHookEvent handleRequest(HttpServletRequest request) throws GitLabApiException {
5890

5991
String eventName = request.getHeader("X-Gitlab-Event");
6092
if (eventName == null || eventName.trim().isEmpty()) {
6193
String message = "X-Gitlab-Event header is missing!";
6294
LOGGER.warning(message);
63-
return;
95+
return (null);
6496
}
6597

6698
if (!isValidSecretToken(request)) {
@@ -126,6 +158,7 @@ public void handleEvent(HttpServletRequest request) throws GitLabApiException {
126158
event.setRequestUrl(requestUrl != null ? requestUrl.toString() : null);
127159
event.setRequestQueryString(request.getQueryString());
128160
fireEvent(event);
161+
return (event);
129162

130163
} catch (Exception e) {
131164
LOGGER.warning("Error processing JSON data, exception=" +

src/main/java/org/gitlab4j/api/webhook/WebHookManager.java

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,20 @@
1818
/**
1919
* This class provides a handler for processing GitLab WebHook callouts.
2020
*/
21-
public class WebHookManager extends HookManager {
21+
public class WebHookManager implements HookManager {
2222

2323
private final static Logger LOGGER = GitLabApi.getLogger();
2424
private final JacksonJson jacksonJson = new JacksonJson();
2525

2626
// Collection of objects listening for WebHook events.
2727
private final List<WebHookListener> webhookListeners = new CopyOnWriteArrayList<WebHookListener>();
2828

29+
private String secretToken;
30+
2931
/**
3032
* Create a HookManager to handle GitLab webhook events.
3133
*/
3234
public WebHookManager() {
33-
super();
3435
}
3536

3637
/**
@@ -40,7 +41,25 @@ public WebHookManager() {
4041
* @param secretToken the secret token to verify against
4142
*/
4243
public WebHookManager(String secretToken) {
43-
super(secretToken);
44+
this.secretToken = secretToken;
45+
}
46+
47+
/**
48+
* Get the secret token that received hook events should be validated against.
49+
*
50+
* @return the secret token that received hook events should be validated against
51+
*/
52+
public String getSecretToken() {
53+
return (secretToken);
54+
}
55+
56+
/**
57+
* Set the secret token that received hook events should be validated against.
58+
*
59+
* @param secretToken the secret token to verify against
60+
*/
61+
public void setSecretToken(String secretToken) {
62+
this.secretToken = secretToken;
4463
}
4564

4665
/**
@@ -51,11 +70,24 @@ public WebHookManager(String secretToken) {
5170
* @throws GitLabApiException if the parsed event is not supported
5271
*/
5372
public void handleEvent(HttpServletRequest request) throws GitLabApiException {
73+
handleRequest(request);
74+
}
75+
76+
/**
77+
* Parses and verifies an Event instance from the HTTP request and
78+
* fires it off to the registered listeners.
79+
*
80+
* @param request the HttpServletRequest to read the Event instance from
81+
* @return the Event instance that was read from the request body, null if the request
82+
* not contain a webhook event
83+
* @throws GitLabApiException if the parsed event is not supported
84+
*/
85+
public Event handleRequest(HttpServletRequest request) throws GitLabApiException {
5486

5587
String eventName = request.getHeader("X-Gitlab-Event");
5688
if (eventName == null || eventName.trim().isEmpty()) {
5789
LOGGER.warning("X-Gitlab-Event header is missing!");
58-
return;
90+
return (null);
5991
}
6092

6193
if (!isValidSecretToken(request)) {
@@ -101,6 +133,7 @@ public void handleEvent(HttpServletRequest request) throws GitLabApiException {
101133
event.setRequestUrl(request.getRequestURL().toString());
102134
event.setRequestQueryString(request.getQueryString());
103135
fireEvent(event);
136+
return (event);
104137

105138
} catch (Exception e) {
106139
LOGGER.warning("Error parsing JSON data, exception=" + e.getClass().getSimpleName() + ", error=" + e.getMessage());

0 commit comments

Comments
 (0)