|
| 1 | +/* |
| 2 | + * Copyright 2013 Google Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
| 5 | + * in compliance with the License. You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software distributed under the License |
| 10 | + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
| 11 | + * or implied. See the License for the specific language governing permissions and limitations under |
| 12 | + * the License. |
| 13 | + */ |
| 14 | + |
| 15 | +package com.google.api.client.googleapis.extensions.servlet.notifications.jakarta; |
| 16 | + |
| 17 | +import com.google.api.client.googleapis.extensions.servlet.notifications.WebhookHeaders; |
| 18 | +import com.google.api.client.googleapis.notifications.StoredChannel; |
| 19 | +import com.google.api.client.googleapis.notifications.UnparsedNotification; |
| 20 | +import com.google.api.client.googleapis.notifications.UnparsedNotificationCallback; |
| 21 | +import com.google.api.client.util.Beta; |
| 22 | +import com.google.api.client.util.LoggingInputStream; |
| 23 | +import com.google.api.client.util.Preconditions; |
| 24 | +import com.google.api.client.util.StringUtils; |
| 25 | +import com.google.api.client.util.store.DataStore; |
| 26 | +import com.google.api.client.util.store.DataStoreFactory; |
| 27 | +import jakarta.servlet.ServletException; |
| 28 | +import jakarta.servlet.http.HttpServlet; |
| 29 | +import jakarta.servlet.http.HttpServletRequest; |
| 30 | +import jakarta.servlet.http.HttpServletResponse; |
| 31 | +import java.io.IOException; |
| 32 | +import java.io.InputStream; |
| 33 | +import java.util.Enumeration; |
| 34 | +import java.util.logging.Level; |
| 35 | +import java.util.logging.Logger; |
| 36 | + |
| 37 | +/** |
| 38 | + * {@link Beta} <br> |
| 39 | + * Utilities for Webhook notifications using the {@code jakarta.servlet} namespace. |
| 40 | + * |
| 41 | + * @since 2.6.0 |
| 42 | + */ |
| 43 | +@Beta |
| 44 | +public final class WebhookUtils { |
| 45 | + |
| 46 | + static final Logger LOGGER = Logger.getLogger(WebhookUtils.class.getName()); |
| 47 | + |
| 48 | + /** Webhook notification channel type to use in the watch request. */ |
| 49 | + public static final String TYPE = "web_hook"; |
| 50 | + |
| 51 | + /** |
| 52 | + * Utility method to process the webhook notification from {@link HttpServlet#doPost} by finding |
| 53 | + * the notification channel in the given data store factory. |
| 54 | + * |
| 55 | + * <p>It is a wrapper around {@link #processWebhookNotification(HttpServletRequest, |
| 56 | + * HttpServletResponse, DataStore)} that uses the data store from {@link |
| 57 | + * StoredChannel#getDefaultDataStore(DataStoreFactory)}. |
| 58 | + * |
| 59 | + * @param req an {@link HttpServletRequest} object that contains the request the client has made |
| 60 | + * of the servlet |
| 61 | + * @param resp an {@link HttpServletResponse} object that contains the response the servlet sends |
| 62 | + * to the client |
| 63 | + * @param dataStoreFactory data store factory |
| 64 | + * @exception IOException if an input or output error is detected when the servlet handles the |
| 65 | + * request |
| 66 | + * @exception ServletException if the request for the POST could not be handled |
| 67 | + */ |
| 68 | + public static void processWebhookNotification( |
| 69 | + HttpServletRequest req, HttpServletResponse resp, DataStoreFactory dataStoreFactory) |
| 70 | + throws ServletException, IOException { |
| 71 | + processWebhookNotification(req, resp, StoredChannel.getDefaultDataStore(dataStoreFactory)); |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Utility method to process the webhook notification from {@link HttpServlet#doPost}. |
| 76 | + * |
| 77 | + * <p>The {@link HttpServletRequest#getInputStream()} is closed in a finally block inside this |
| 78 | + * method. If it is not detected to be a webhook notification, an {@link |
| 79 | + * HttpServletResponse#SC_BAD_REQUEST} error will be displayed. If the notification channel is |
| 80 | + * found in the given notification channel data store, it will call {@link |
| 81 | + * UnparsedNotificationCallback#onNotification} for the registered notification callback method. |
| 82 | + * |
| 83 | + * @param req an {@link HttpServletRequest} object that contains the request the client has made |
| 84 | + * of the servlet |
| 85 | + * @param resp an {@link HttpServletResponse} object that contains the response the servlet sends |
| 86 | + * to the client |
| 87 | + * @param channelDataStore notification channel data store |
| 88 | + * @exception IOException if an input or output error is detected when the servlet handles the |
| 89 | + * request |
| 90 | + * @exception ServletException if the request for the POST could not be handled |
| 91 | + */ |
| 92 | + public static void processWebhookNotification( |
| 93 | + HttpServletRequest req, HttpServletResponse resp, DataStore<StoredChannel> channelDataStore) |
| 94 | + throws ServletException, IOException { |
| 95 | + Preconditions.checkArgument("POST".equals(req.getMethod())); |
| 96 | + InputStream contentStream = req.getInputStream(); |
| 97 | + try { |
| 98 | + // log headers |
| 99 | + if (LOGGER.isLoggable(Level.CONFIG)) { |
| 100 | + StringBuilder builder = new StringBuilder(); |
| 101 | + Enumeration<?> e = req.getHeaderNames(); |
| 102 | + if (e != null) { |
| 103 | + while (e.hasMoreElements()) { |
| 104 | + Object nameObj = e.nextElement(); |
| 105 | + if (nameObj instanceof String) { |
| 106 | + String name = (String) nameObj; |
| 107 | + Enumeration<?> ev = req.getHeaders(name); |
| 108 | + if (ev != null) { |
| 109 | + while (ev.hasMoreElements()) { |
| 110 | + builder |
| 111 | + .append(name) |
| 112 | + .append(": ") |
| 113 | + .append(ev.nextElement()) |
| 114 | + .append(StringUtils.LINE_SEPARATOR); |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | + LOGGER.config(builder.toString()); |
| 121 | + contentStream = new LoggingInputStream(contentStream, LOGGER, Level.CONFIG, 0x4000); |
| 122 | + // TODO(yanivi): allow to override logging content limit |
| 123 | + } |
| 124 | + // parse the relevant headers, and create a notification |
| 125 | + Long messageNumber; |
| 126 | + try { |
| 127 | + messageNumber = Long.valueOf(req.getHeader(WebhookHeaders.MESSAGE_NUMBER)); |
| 128 | + } catch (NumberFormatException e) { |
| 129 | + messageNumber = null; |
| 130 | + } |
| 131 | + String resourceState = req.getHeader(WebhookHeaders.RESOURCE_STATE); |
| 132 | + String resourceId = req.getHeader(WebhookHeaders.RESOURCE_ID); |
| 133 | + String resourceUri = req.getHeader(WebhookHeaders.RESOURCE_URI); |
| 134 | + String channelId = req.getHeader(WebhookHeaders.CHANNEL_ID); |
| 135 | + String channelExpiration = req.getHeader(WebhookHeaders.CHANNEL_EXPIRATION); |
| 136 | + String channelToken = req.getHeader(WebhookHeaders.CHANNEL_TOKEN); |
| 137 | + String changed = req.getHeader(WebhookHeaders.CHANGED); |
| 138 | + if (messageNumber == null |
| 139 | + || resourceState == null |
| 140 | + || resourceId == null |
| 141 | + || resourceUri == null |
| 142 | + || channelId == null) { |
| 143 | + resp.sendError( |
| 144 | + HttpServletResponse.SC_BAD_REQUEST, |
| 145 | + "Notification did not contain all required information."); |
| 146 | + return; |
| 147 | + } |
| 148 | + UnparsedNotification notification = |
| 149 | + new UnparsedNotification(messageNumber, resourceState, resourceId, resourceUri, channelId) |
| 150 | + .setChannelExpiration(channelExpiration) |
| 151 | + .setChannelToken(channelToken) |
| 152 | + .setChanged(changed) |
| 153 | + .setContentType(req.getContentType()) |
| 154 | + .setContentStream(contentStream); |
| 155 | + // check if we know about the channel, hand over the notification to the notification callback |
| 156 | + StoredChannel storedChannel = channelDataStore.get(notification.getChannelId()); |
| 157 | + if (storedChannel != null) { |
| 158 | + storedChannel.getNotificationCallback().onNotification(storedChannel, notification); |
| 159 | + } |
| 160 | + } finally { |
| 161 | + contentStream.close(); |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + private WebhookUtils() {} |
| 166 | +} |
0 commit comments