• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

box / box-java-sdk / #5076

07 Oct 2025 12:35PM UTC coverage: 37.132% (+0.007%) from 37.125%
#5076

push

github

web-flow
test: Change `Event.additionalDetails` field assertion in events test (box/box-codegen#858) (#1491)

18454 of 49699 relevant lines covered (37.13%)

0.37 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

91.73
/src/main/java/com/box/sdkgen/managers/webhooks/WebhooksManager.java
1
package com.box.sdkgen.managers.webhooks;
2

3
import static com.box.sdkgen.internal.utils.UtilsManager.compareSignatures;
4
import static com.box.sdkgen.internal.utils.UtilsManager.computeWebhookSignature;
5
import static com.box.sdkgen.internal.utils.UtilsManager.convertToString;
6
import static com.box.sdkgen.internal.utils.UtilsManager.dateTimeFromString;
7
import static com.box.sdkgen.internal.utils.UtilsManager.dateTimeToEpochSeconds;
8
import static com.box.sdkgen.internal.utils.UtilsManager.entryOf;
9
import static com.box.sdkgen.internal.utils.UtilsManager.getEpochTimeInSeconds;
10
import static com.box.sdkgen.internal.utils.UtilsManager.mapOf;
11
import static com.box.sdkgen.internal.utils.UtilsManager.mergeMaps;
12
import static com.box.sdkgen.internal.utils.UtilsManager.prepareParams;
13

14
import com.box.sdkgen.networking.auth.Authentication;
15
import com.box.sdkgen.networking.fetchoptions.FetchOptions;
16
import com.box.sdkgen.networking.fetchoptions.ResponseFormat;
17
import com.box.sdkgen.networking.fetchresponse.FetchResponse;
18
import com.box.sdkgen.networking.network.NetworkSession;
19
import com.box.sdkgen.schemas.webhook.Webhook;
20
import com.box.sdkgen.schemas.webhooks.Webhooks;
21
import com.box.sdkgen.serialization.json.JsonManager;
22
import java.time.OffsetDateTime;
23
import java.util.Map;
24

25
public class WebhooksManager {
26

27
  public Authentication auth;
28

29
  public NetworkSession networkSession;
30

31
  public WebhooksManager() {
×
32
    this.networkSession = new NetworkSession();
×
33
  }
×
34

35
  protected WebhooksManager(Builder builder) {
1✔
36
    this.auth = builder.auth;
1✔
37
    this.networkSession = builder.networkSession;
1✔
38
  }
1✔
39

40
  /**
41
   * Returns all defined webhooks for the requesting application.
42
   *
43
   * <p>This API only returns webhooks that are applied to files or folders that are owned by the
44
   * authenticated user. This means that an admin can not see webhooks created by a service account
45
   * unless the admin has access to those folders, and vice versa.
46
   */
47
  public Webhooks getWebhooks() {
48
    return getWebhooks(new GetWebhooksQueryParams(), new GetWebhooksHeaders());
1✔
49
  }
50

51
  /**
52
   * Returns all defined webhooks for the requesting application.
53
   *
54
   * <p>This API only returns webhooks that are applied to files or folders that are owned by the
55
   * authenticated user. This means that an admin can not see webhooks created by a service account
56
   * unless the admin has access to those folders, and vice versa.
57
   *
58
   * @param queryParams Query parameters of getWebhooks method
59
   */
60
  public Webhooks getWebhooks(GetWebhooksQueryParams queryParams) {
61
    return getWebhooks(queryParams, new GetWebhooksHeaders());
×
62
  }
63

64
  /**
65
   * Returns all defined webhooks for the requesting application.
66
   *
67
   * <p>This API only returns webhooks that are applied to files or folders that are owned by the
68
   * authenticated user. This means that an admin can not see webhooks created by a service account
69
   * unless the admin has access to those folders, and vice versa.
70
   *
71
   * @param headers Headers of getWebhooks method
72
   */
73
  public Webhooks getWebhooks(GetWebhooksHeaders headers) {
74
    return getWebhooks(new GetWebhooksQueryParams(), headers);
×
75
  }
76

77
  /**
78
   * Returns all defined webhooks for the requesting application.
79
   *
80
   * <p>This API only returns webhooks that are applied to files or folders that are owned by the
81
   * authenticated user. This means that an admin can not see webhooks created by a service account
82
   * unless the admin has access to those folders, and vice versa.
83
   *
84
   * @param queryParams Query parameters of getWebhooks method
85
   * @param headers Headers of getWebhooks method
86
   */
87
  public Webhooks getWebhooks(GetWebhooksQueryParams queryParams, GetWebhooksHeaders headers) {
88
    Map<String, String> queryParamsMap =
1✔
89
        prepareParams(
1✔
90
            mapOf(
1✔
91
                entryOf("marker", convertToString(queryParams.getMarker())),
1✔
92
                entryOf("limit", convertToString(queryParams.getLimit()))));
1✔
93
    Map<String, String> headersMap = prepareParams(mergeMaps(mapOf(), headers.getExtraHeaders()));
1✔
94
    FetchResponse response =
1✔
95
        this.networkSession
96
            .getNetworkClient()
1✔
97
            .fetch(
1✔
98
                new FetchOptions.Builder(
99
                        String.join(
1✔
100
                            "", this.networkSession.getBaseUrls().getBaseUrl(), "/2.0/webhooks"),
1✔
101
                        "GET")
102
                    .params(queryParamsMap)
1✔
103
                    .headers(headersMap)
1✔
104
                    .responseFormat(ResponseFormat.JSON)
1✔
105
                    .auth(this.auth)
1✔
106
                    .networkSession(this.networkSession)
1✔
107
                    .build());
1✔
108
    return JsonManager.deserialize(response.getData(), Webhooks.class);
1✔
109
  }
110

111
  /**
112
   * Creates a webhook.
113
   *
114
   * @param requestBody Request body of createWebhook method
115
   */
116
  public Webhook createWebhook(CreateWebhookRequestBody requestBody) {
117
    return createWebhook(requestBody, new CreateWebhookHeaders());
1✔
118
  }
119

120
  /**
121
   * Creates a webhook.
122
   *
123
   * @param requestBody Request body of createWebhook method
124
   * @param headers Headers of createWebhook method
125
   */
126
  public Webhook createWebhook(CreateWebhookRequestBody requestBody, CreateWebhookHeaders headers) {
127
    Map<String, String> headersMap = prepareParams(mergeMaps(mapOf(), headers.getExtraHeaders()));
1✔
128
    FetchResponse response =
1✔
129
        this.networkSession
130
            .getNetworkClient()
1✔
131
            .fetch(
1✔
132
                new FetchOptions.Builder(
133
                        String.join(
1✔
134
                            "", this.networkSession.getBaseUrls().getBaseUrl(), "/2.0/webhooks"),
1✔
135
                        "POST")
136
                    .headers(headersMap)
1✔
137
                    .data(JsonManager.serialize(requestBody))
1✔
138
                    .contentType("application/json")
1✔
139
                    .responseFormat(ResponseFormat.JSON)
1✔
140
                    .auth(this.auth)
1✔
141
                    .networkSession(this.networkSession)
1✔
142
                    .build());
1✔
143
    return JsonManager.deserialize(response.getData(), Webhook.class);
1✔
144
  }
145

146
  /**
147
   * Retrieves a specific webhook.
148
   *
149
   * @param webhookId The ID of the webhook. Example: "3321123"
150
   */
151
  public Webhook getWebhookById(String webhookId) {
152
    return getWebhookById(webhookId, new GetWebhookByIdHeaders());
1✔
153
  }
154

155
  /**
156
   * Retrieves a specific webhook.
157
   *
158
   * @param webhookId The ID of the webhook. Example: "3321123"
159
   * @param headers Headers of getWebhookById method
160
   */
161
  public Webhook getWebhookById(String webhookId, GetWebhookByIdHeaders headers) {
162
    Map<String, String> headersMap = prepareParams(mergeMaps(mapOf(), headers.getExtraHeaders()));
1✔
163
    FetchResponse response =
1✔
164
        this.networkSession
165
            .getNetworkClient()
1✔
166
            .fetch(
1✔
167
                new FetchOptions.Builder(
168
                        String.join(
1✔
169
                            "",
170
                            this.networkSession.getBaseUrls().getBaseUrl(),
1✔
171
                            "/2.0/webhooks/",
172
                            convertToString(webhookId)),
1✔
173
                        "GET")
174
                    .headers(headersMap)
1✔
175
                    .responseFormat(ResponseFormat.JSON)
1✔
176
                    .auth(this.auth)
1✔
177
                    .networkSession(this.networkSession)
1✔
178
                    .build());
1✔
179
    return JsonManager.deserialize(response.getData(), Webhook.class);
1✔
180
  }
181

182
  /**
183
   * Updates a webhook.
184
   *
185
   * @param webhookId The ID of the webhook. Example: "3321123"
186
   */
187
  public Webhook updateWebhookById(String webhookId) {
188
    return updateWebhookById(
×
189
        webhookId, new UpdateWebhookByIdRequestBody(), new UpdateWebhookByIdHeaders());
190
  }
191

192
  /**
193
   * Updates a webhook.
194
   *
195
   * @param webhookId The ID of the webhook. Example: "3321123"
196
   * @param requestBody Request body of updateWebhookById method
197
   */
198
  public Webhook updateWebhookById(String webhookId, UpdateWebhookByIdRequestBody requestBody) {
199
    return updateWebhookById(webhookId, requestBody, new UpdateWebhookByIdHeaders());
1✔
200
  }
201

202
  /**
203
   * Updates a webhook.
204
   *
205
   * @param webhookId The ID of the webhook. Example: "3321123"
206
   * @param headers Headers of updateWebhookById method
207
   */
208
  public Webhook updateWebhookById(String webhookId, UpdateWebhookByIdHeaders headers) {
209
    return updateWebhookById(webhookId, new UpdateWebhookByIdRequestBody(), headers);
×
210
  }
211

212
  /**
213
   * Updates a webhook.
214
   *
215
   * @param webhookId The ID of the webhook. Example: "3321123"
216
   * @param requestBody Request body of updateWebhookById method
217
   * @param headers Headers of updateWebhookById method
218
   */
219
  public Webhook updateWebhookById(
220
      String webhookId,
221
      UpdateWebhookByIdRequestBody requestBody,
222
      UpdateWebhookByIdHeaders headers) {
223
    Map<String, String> headersMap = prepareParams(mergeMaps(mapOf(), headers.getExtraHeaders()));
1✔
224
    FetchResponse response =
1✔
225
        this.networkSession
226
            .getNetworkClient()
1✔
227
            .fetch(
1✔
228
                new FetchOptions.Builder(
229
                        String.join(
1✔
230
                            "",
231
                            this.networkSession.getBaseUrls().getBaseUrl(),
1✔
232
                            "/2.0/webhooks/",
233
                            convertToString(webhookId)),
1✔
234
                        "PUT")
235
                    .headers(headersMap)
1✔
236
                    .data(JsonManager.serialize(requestBody))
1✔
237
                    .contentType("application/json")
1✔
238
                    .responseFormat(ResponseFormat.JSON)
1✔
239
                    .auth(this.auth)
1✔
240
                    .networkSession(this.networkSession)
1✔
241
                    .build());
1✔
242
    return JsonManager.deserialize(response.getData(), Webhook.class);
1✔
243
  }
244

245
  /**
246
   * Deletes a webhook.
247
   *
248
   * @param webhookId The ID of the webhook. Example: "3321123"
249
   */
250
  public void deleteWebhookById(String webhookId) {
251
    deleteWebhookById(webhookId, new DeleteWebhookByIdHeaders());
1✔
252
  }
1✔
253

254
  /**
255
   * Deletes a webhook.
256
   *
257
   * @param webhookId The ID of the webhook. Example: "3321123"
258
   * @param headers Headers of deleteWebhookById method
259
   */
260
  public void deleteWebhookById(String webhookId, DeleteWebhookByIdHeaders headers) {
261
    Map<String, String> headersMap = prepareParams(mergeMaps(mapOf(), headers.getExtraHeaders()));
1✔
262
    FetchResponse response =
1✔
263
        this.networkSession
264
            .getNetworkClient()
1✔
265
            .fetch(
1✔
266
                new FetchOptions.Builder(
267
                        String.join(
1✔
268
                            "",
269
                            this.networkSession.getBaseUrls().getBaseUrl(),
1✔
270
                            "/2.0/webhooks/",
271
                            convertToString(webhookId)),
1✔
272
                        "DELETE")
273
                    .headers(headersMap)
1✔
274
                    .responseFormat(ResponseFormat.NO_CONTENT)
1✔
275
                    .auth(this.auth)
1✔
276
                    .networkSession(this.networkSession)
1✔
277
                    .build());
1✔
278
  }
1✔
279

280
  /**
281
   * Validate a webhook message by verifying the signature and the delivery timestamp
282
   *
283
   * @param body The request body of the webhook message
284
   * @param headers The headers of the webhook message
285
   * @param primaryKey The primary signature to verify the message with
286
   */
287
  public static boolean validateMessage(
288
      String body, Map<String, String> headers, String primaryKey) {
289
    return validateMessage(body, headers, primaryKey, null, 600);
×
290
  }
291

292
  /**
293
   * Validate a webhook message by verifying the signature and the delivery timestamp
294
   *
295
   * @param body The request body of the webhook message
296
   * @param headers The headers of the webhook message
297
   * @param primaryKey The primary signature to verify the message with
298
   * @param secondaryKey The secondary signature to verify the message with
299
   */
300
  public static boolean validateMessage(
301
      String body, Map<String, String> headers, String primaryKey, String secondaryKey) {
302
    return validateMessage(body, headers, primaryKey, secondaryKey, 600);
1✔
303
  }
304

305
  /**
306
   * Validate a webhook message by verifying the signature and the delivery timestamp
307
   *
308
   * @param body The request body of the webhook message
309
   * @param headers The headers of the webhook message
310
   * @param primaryKey The primary signature to verify the message with
311
   * @param maxAge The maximum age of the message in seconds, defaults to 10 minutes
312
   */
313
  public static boolean validateMessage(
314
      String body, Map<String, String> headers, String primaryKey, Integer maxAge) {
315
    return validateMessage(body, headers, primaryKey, null, maxAge);
×
316
  }
317

318
  /**
319
   * Validate a webhook message by verifying the signature and the delivery timestamp
320
   *
321
   * @param body The request body of the webhook message
322
   * @param headers The headers of the webhook message
323
   * @param primaryKey The primary signature to verify the message with
324
   * @param secondaryKey The secondary signature to verify the message with
325
   * @param maxAge The maximum age of the message in seconds, defaults to 10 minutes
326
   */
327
  public static boolean validateMessage(
328
      String body,
329
      Map<String, String> headers,
330
      String primaryKey,
331
      String secondaryKey,
332
      Integer maxAge) {
333
    OffsetDateTime deliveryTimestamp = dateTimeFromString(headers.get("box-delivery-timestamp"));
1✔
334
    long currentEpoch = getEpochTimeInSeconds();
1✔
335
    if (currentEpoch - maxAge > dateTimeToEpochSeconds(deliveryTimestamp)
1✔
336
        || dateTimeToEpochSeconds(deliveryTimestamp) > currentEpoch) {
1✔
337
      return false;
1✔
338
    }
339
    if (!(primaryKey == null)
1✔
340
        && !(headers.get("box-signature-primary") == null)
1✔
341
        && compareSignatures(
1✔
342
            computeWebhookSignature(body, headers, primaryKey, false),
1✔
343
            headers.get("box-signature-primary"))) {
1✔
344
      return true;
1✔
345
    }
346
    if (!(primaryKey == null)
1✔
347
        && !(headers.get("box-signature-primary") == null)
1✔
348
        && compareSignatures(
1✔
349
            computeWebhookSignature(body, headers, primaryKey, true),
1✔
350
            headers.get("box-signature-primary"))) {
1✔
351
      return true;
1✔
352
    }
353
    if (!(secondaryKey == null)
1✔
354
        && !(headers.get("box-signature-secondary") == null)
1✔
355
        && compareSignatures(
1✔
356
            computeWebhookSignature(body, headers, secondaryKey, false),
1✔
357
            headers.get("box-signature-secondary"))) {
1✔
358
      return true;
1✔
359
    }
360
    if (!(secondaryKey == null)
1✔
361
        && !(headers.get("box-signature-secondary") == null)
1✔
362
        && compareSignatures(
1✔
363
            computeWebhookSignature(body, headers, secondaryKey, true),
1✔
364
            headers.get("box-signature-secondary"))) {
1✔
365
      return true;
1✔
366
    }
367
    return false;
1✔
368
  }
369

370
  public Authentication getAuth() {
371
    return auth;
×
372
  }
373

374
  public NetworkSession getNetworkSession() {
375
    return networkSession;
×
376
  }
377

378
  public static class Builder {
379

380
    protected Authentication auth;
381

382
    protected NetworkSession networkSession;
383

384
    public Builder() {
1✔
385
      this.networkSession = new NetworkSession();
1✔
386
    }
1✔
387

388
    public Builder auth(Authentication auth) {
389
      this.auth = auth;
1✔
390
      return this;
1✔
391
    }
392

393
    public Builder networkSession(NetworkSession networkSession) {
394
      this.networkSession = networkSession;
1✔
395
      return this;
1✔
396
    }
397

398
    public WebhooksManager build() {
399
      return new WebhooksManager(this);
1✔
400
    }
401
  }
402
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc