Skip to content

Commit c55d651

Browse files
author
Vladimir Kotal
committed
make webhook async
1 parent 3006c48 commit c55d651

File tree

5 files changed

+77
-5
lines changed

5 files changed

+77
-5
lines changed

plugins/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,12 @@ Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
6767
<artifactId>jersey-hk2</artifactId>
6868
<version>${jersey.version}</version>
6969
</dependency>
70+
<dependency>
71+
<groupId>org.glassfish.jersey.test-framework.providers</groupId>
72+
<artifactId>jersey-test-framework-provider-grizzly2</artifactId>
73+
<version>${jersey.version}</version>
74+
<scope>test</scope>
75+
</dependency>
7076
</dependencies>
7177

7278
<build>

plugins/src/opengrok/auth/plugin/ldap/LdapFacade.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@
4545
import opengrok.auth.plugin.entity.User;
4646
import opengrok.auth.plugin.util.WebHook;
4747
import opengrok.auth.plugin.util.WebHooks;
48-
import opengrok.auth.plugin.util.RestfulClient;
4948

5049
public class LdapFacade extends AbstractLdapProvider {
5150

@@ -332,7 +331,7 @@ private <T> T lookup(String dn, String filter, String[] attributes, AttributeMap
332331
reported = false;
333332
WebHook hook;
334333
if ((hook = webHooks.getFail()) != null) {
335-
RestfulClient.postIt(hook.getURI(), hook.getContent());
334+
hook.post();
336335
}
337336
throw new LdapException("Tried all LDAP servers in a pool but no server works");
338337
}
@@ -353,7 +352,7 @@ private <T> T lookup(String dn, String filter, String[] attributes, AttributeMap
353352
errorTimestamp = 0;
354353
WebHook hook;
355354
if ((hook = webHooks.getRecover()) != null) {
356-
RestfulClient.postIt(hook.getURI(), hook.getContent());
355+
hook.post();
357356
}
358357
}
359358
return processResult(sr, mapper);

plugins/src/opengrok/auth/plugin/util/RestfulClient.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
package opengrok.auth.plugin.util;
2525

26+
import javax.servlet.http.HttpServletResponse;
2627
import javax.ws.rs.client.Client;
2728
import javax.ws.rs.client.ClientBuilder;
2829
import javax.ws.rs.client.Entity;
@@ -45,8 +46,9 @@ private RestfulClient() {
4546
* Perform HTTP PUT request
4647
* @param URI URI
4748
* @param input JSON string contents
49+
* @return HTTP status or -1
4850
*/
49-
public static void postIt(String URI, String input) {
51+
public static int postIt(String URI, String input) {
5052
try {
5153
Client client = ClientBuilder.newClient();
5254

@@ -57,11 +59,14 @@ public static void postIt(String URI, String input) {
5759
.post(Entity.entity(input, MediaType.APPLICATION_JSON));
5860

5961
int status = response.getStatus();
60-
if (status != 201) { // TODO use definition
62+
if (status != HttpServletResponse.SC_CREATED) {
6163
LOGGER.log(Level.WARNING, "REST request failed: HTTP error code : {0}", status);
6264
}
65+
66+
return status;
6367
} catch (Exception e) {
6468
LOGGER.log(Level.WARNING, "REST request failed: {0}", e);
69+
return -1;
6570
}
6671
}
6772
}

plugins/src/opengrok/auth/plugin/util/WebHook.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,40 @@
2424
package opengrok.auth.plugin.util;
2525

2626
import java.io.Serializable;
27+
import java.util.concurrent.CompletableFuture;
28+
import java.util.concurrent.Executors;
29+
import java.util.concurrent.Future;
2730

2831
public class WebHook implements Serializable {
2932
private static final long serialVersionUID = -1;
3033

3134
private String URI;
3235
private String content;
3336

37+
public WebHook() {
38+
}
39+
40+
WebHook(String URI, String content) {
41+
this.URI = URI;
42+
this.content = content;
43+
}
44+
3445
public void setURI(String URI) { this.URI = URI; }
3546
public String getURI() { return URI; }
3647

3748
public void setContent(String content) { this.content = content; }
3849
public String getContent() { return content; }
50+
51+
public Future<String> post() {
52+
CompletableFuture<String> completableFuture
53+
= new CompletableFuture<>();
54+
55+
Executors.newCachedThreadPool().submit(() -> {
56+
int status = RestfulClient.postIt(getURI(), getContent());
57+
completableFuture.complete(String.valueOf(status));
58+
return null;
59+
});
60+
61+
return completableFuture;
62+
}
3963
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package opengrok.auth.plugin.util;
2+
3+
import org.glassfish.jersey.server.ResourceConfig;
4+
import org.glassfish.jersey.test.JerseyTest;
5+
import org.junit.Test;
6+
7+
import javax.ws.rs.POST;
8+
import javax.ws.rs.Path;
9+
import javax.ws.rs.core.Application;
10+
import java.util.concurrent.ExecutionException;
11+
import java.util.concurrent.Future;
12+
13+
import static org.junit.Assert.assertEquals;
14+
15+
public class WebHookTest extends JerseyTest {
16+
private static final String PREFIX = "service";
17+
private static int requests;
18+
19+
@Path(PREFIX)
20+
public static class Service {
21+
@POST
22+
public String handlePost() { requests++; return "posted"; }
23+
}
24+
25+
@Override
26+
protected Application configure() {
27+
return new ResourceConfig(Service.class);
28+
}
29+
30+
@Test
31+
public void testPost() throws ExecutionException, InterruptedException {
32+
assertEquals(0, requests);
33+
WebHook hook = new WebHook(getBaseUri() + PREFIX, "{}");
34+
Future<String> future = hook.post();
35+
future.get();
36+
assertEquals(1, requests);
37+
}
38+
}

0 commit comments

Comments
 (0)