BrightSide Workbench Full Report + Source Code
PushService.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2022 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 package org.turro.push.service;
19 
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;
40 
45 public class PushService extends AbstractPushService<PushService> {
46 
47  public PushService() {
48  }
49 
50  public PushService(String gcmApiKey) {
51  super(gcmApiKey);
52  }
53 
54  public PushService(KeyPair keyPair) {
55  super(keyPair);
56  }
57 
58  public PushService(KeyPair keyPair, String subject) {
59  super(keyPair, subject);
60  }
61 
62  public PushService(String publicKey, String privateKey) throws GeneralSecurityException {
63  super(publicKey, privateKey);
64  }
65 
66  public PushService(String publicKey, String privateKey, String subject) throws GeneralSecurityException {
67  super(publicKey, privateKey, subject);
68  }
69 
70  public HttpResponse send(Notification notification) throws GeneralSecurityException, IOException, JoseException, ExecutionException, InterruptedException {
71  return send(notification, Encoding.AESGCM);
72  }
73 
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);
78  }
79  }
80 
81  public HttpPost preparePost(Notification notification, Encoding encoding) throws GeneralSecurityException, IOException, JoseException {
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));
87  }
88  return httpPost;
89  }
90 
91  public void sendAsync(Notification notification, Consumer<SimpleHttpResponse> completed,
92  Consumer<Exception> failed, Runnable cancelled) throws GeneralSecurityException, IOException, JoseException, ExecutionException, InterruptedException {
93  sendAsync(notification, Encoding.AESGCM, completed, failed, cancelled);
94  }
95 
96  public void sendAsync(Notification notification, Encoding encoding, Consumer<SimpleHttpResponse> completed,
97  Consumer<Exception> failed, Runnable cancelled) throws GeneralSecurityException, IOException, JoseException {
98  SimpleHttpRequest httpPost = prepareAsyncPost(notification, encoding);
99  final CloseableHttpAsyncClient httpClient = HttpAsyncClientBuilder.create().build();
100  httpClient.start();
101  httpClient.execute(httpPost, new FutureCallback<SimpleHttpResponse>() {
102  @Override
103  public void completed(SimpleHttpResponse result) {
104  if(completed != null) completed.accept(result);
105  httpClient.close(CloseMode.GRACEFUL);
106  }
107 
108  @Override
109  public void failed(Exception ex) {
110  if(failed != null) failed.accept(ex);
111  httpClient.close(CloseMode.GRACEFUL);
112  }
113 
114  @Override
115  public void cancelled() {
116  if(cancelled != null) cancelled.run();
117  httpClient.close(CloseMode.GRACEFUL);
118  }
119  });
120  }
121 
122  public SimpleHttpRequest prepareAsyncPost(Notification notification, Encoding encoding) throws GeneralSecurityException, IOException, JoseException {
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);
127  return httpPost;
128  }
129 
130 }
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 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)
HttpResponse send(Notification notification, Encoding encoding)