18 package org.turro.push.service;
20 import java.io.IOException;
21 import java.security.GeneralSecurityException;
22 import java.security.KeyPair;
23 import java.util.concurrent.ExecutionException;
24 import java.util.function.Consumer;
25 import org.apache.hc.client5.http.async.methods.SimpleHttpRequest;
26 import org.apache.hc.client5.http.async.methods.SimpleHttpResponse;
27 import org.apache.hc.client5.http.async.methods.SimpleRequestBuilder;
28 import org.apache.hc.client5.http.classic.methods.HttpPost;
29 import org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient;
30 import org.apache.hc.client5.http.impl.async.HttpAsyncClientBuilder;
31 import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
32 import org.apache.hc.client5.http.impl.classic.HttpClientBuilder;
33 import org.apache.hc.core5.concurrent.FutureCallback;
34 import org.apache.hc.core5.http.ContentType;
35 import org.apache.hc.core5.http.HttpResponse;
36 import org.apache.hc.core5.http.Method;
37 import org.apache.hc.core5.http.io.entity.ByteArrayEntity;
38 import org.apache.hc.core5.io.CloseMode;
39 import org.jose4j.lang.JoseException;
45 public class PushService extends AbstractPushService<PushService> {
59 super(keyPair, subject);
62 public PushService(String publicKey, String privateKey)
throws GeneralSecurityException {
63 super(publicKey, privateKey);
66 public PushService(String publicKey, String privateKey, String subject)
throws GeneralSecurityException {
67 super(publicKey, privateKey, subject);
70 public HttpResponse
send(
Notification notification)
throws GeneralSecurityException, IOException, JoseException, ExecutionException, InterruptedException {
74 public HttpResponse
send(
Notification notification,
Encoding encoding)
throws GeneralSecurityException, IOException, JoseException {
75 HttpPost httpPost =
preparePost(notification, encoding);
76 try(CloseableHttpClient client = HttpClientBuilder.create().build()) {
77 return client.execute(httpPost);
82 HttpRequest request = prepareRequest(notification, encoding);
83 HttpPost httpPost =
new HttpPost(request.
getUrl());
84 request.
getHeaders().forEach(httpPost::addHeader);
85 if (request.
getBody() !=
null) {
86 httpPost.setEntity(
new ByteArrayEntity(request.
getBody(), ContentType.APPLICATION_OCTET_STREAM));
92 Consumer<Exception> failed, Runnable cancelled)
throws GeneralSecurityException, IOException, JoseException, ExecutionException, InterruptedException {
97 Consumer<Exception> failed, Runnable cancelled)
throws GeneralSecurityException, IOException, JoseException {
99 final CloseableHttpAsyncClient httpClient = HttpAsyncClientBuilder.create().build();
101 httpClient.execute(httpPost,
new FutureCallback<SimpleHttpResponse>() {
103 public void completed(SimpleHttpResponse result) {
104 if(completed !=
null) completed.accept(result);
105 httpClient.close(CloseMode.GRACEFUL);
109 public void failed(Exception ex) {
110 if(failed !=
null) failed.accept(ex);
111 httpClient.close(CloseMode.GRACEFUL);
115 public void cancelled() {
116 if(cancelled !=
null) cancelled.run();
117 httpClient.close(CloseMode.GRACEFUL);
123 HttpRequest request = prepareRequest(notification, encoding);
124 SimpleHttpRequest httpPost = SimpleRequestBuilder.create(Method.POST).setUri(request.
getUrl()).build();
125 request.
getHeaders().forEach(httpPost::addHeader);
126 httpPost.setBody(request.
getBody(), ContentType.APPLICATION_OCTET_STREAM);
Map< String, String > getHeaders()
HttpPost preparePost(Notification notification, Encoding encoding)
SimpleHttpRequest prepareAsyncPost(Notification notification, Encoding encoding)
HttpResponse send(Notification notification)
PushService(KeyPair keyPair, String subject)
PushService(String gcmApiKey)
PushService(String publicKey, String privateKey)
PushService(String publicKey, String privateKey, String subject)
void sendAsync(Notification notification, Encoding encoding, Consumer< SimpleHttpResponse > completed, Consumer< Exception > failed, Runnable cancelled)
void sendAsync(Notification notification, Consumer< SimpleHttpResponse > completed, Consumer< Exception > failed, Runnable cancelled)
PushService(KeyPair keyPair)
HttpResponse send(Notification notification, Encoding encoding)