/
PubSubPush.java
86 lines (77 loc) · 3.05 KB
/
PubSubPush.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/*
* Copyright 2023 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.example.flexible.pubsub;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import java.io.IOException;
import java.util.Base64;
import java.util.stream.Collectors;
import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
// [START gae_flex_pubsub_push]
@WebServlet(value = "/pubsub/push")
@MultipartConfig()
public class PubSubPush extends HttpServlet {
@Override
public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws IOException, ServletException {
String pubsubVerificationToken = System.getenv("PUBSUB_VERIFICATION_TOKEN");
// Do not process message if request token does not match pubsubVerificationToken
if (pubsubVerificationToken == null
|| pubsubVerificationToken.compareTo(req.getParameter("token")) != 0) {
resp.setStatus(HttpServletResponse.SC_BAD_REQUEST);
return;
}
// parse message object from "message" field in the request body json
// decode message data from base64
Message message = getMessage(req);
try {
messageRepository.save(message);
// 200, 201, 204, 102 status codes are interpreted as success by the Pub/Sub system
resp.setStatus(HttpServletResponse.SC_OK);
} catch (Exception e) {
System.out.println(e);
resp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
}
}
// [END gae_flex_pubsub_push]
private Message getMessage(HttpServletRequest request) throws IOException {
String requestBody = request.getReader().lines().collect(Collectors.joining("\n"));
JsonElement jsonRoot = JsonParser.parseString(requestBody).getAsJsonObject();
String messageStr = jsonRoot.getAsJsonObject().get("message").toString();
Message message = gson.fromJson(messageStr, Message.class);
// decode from base64
String decoded = decode(message.getData());
message.setData(decoded);
return message;
}
private String decode(String data) {
return new String(Base64.getDecoder().decode(data));
}
private final Gson gson = new Gson();
private MessageRepository messageRepository;
PubSubPush(MessageRepository messageRepository) {
this.messageRepository = messageRepository;
}
public PubSubPush() {
this.messageRepository = MessageRepositoryImpl.getInstance();
}
}