1 /* 2 * Copyright 2017 Google LLC 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.google.cloud.notification; 18 19 import static com.google.cloud.BaseService.EXCEPTION_HANDLER; 20 import static com.google.cloud.RetryHelper.runWithRetries; 21 22 import com.google.cloud.RetryHelper.RetryHelperException; 23 import com.google.cloud.storage.Storage; 24 import com.google.cloud.storage.StorageException; 25 import com.google.cloud.storage.spi.v1.StorageRpc; 26 import com.google.common.collect.Lists; 27 import java.util.List; 28 import java.util.concurrent.Callable; 29 30 public class NotificationImpl implements Notification { 31 32 private final Storage storage; 33 NotificationImpl(Storage storage)34 private NotificationImpl(Storage storage) { 35 this.storage = storage; 36 } 37 38 @Override create(Storage storage)39 public Notification create(Storage storage) { 40 return new NotificationImpl(storage); 41 } 42 43 public static class DefaultNotificationFactory { create(Storage storage)44 public Notification create(Storage storage) { 45 return new NotificationImpl(storage); 46 } 47 } 48 49 @Override deleteNotification(final String bucket, final String notification)50 public boolean deleteNotification(final String bucket, final String notification) { 51 try { 52 return runWithRetries( 53 new Callable<Boolean>() { 54 @Override 55 public Boolean call() { 56 return ((StorageRpc) storage.getOptions().getRpc()) 57 .deleteNotification(bucket, notification); 58 } 59 }, 60 storage.getOptions().getRetrySettings(), 61 EXCEPTION_HANDLER, 62 storage.getOptions().getClock()); 63 } catch (RetryHelperException e) { 64 throw StorageException.translateAndThrow(e); 65 } 66 } 67 68 @Override 69 public List<NotificationInfo> listNotifications(final String bucket) { 70 try { 71 List<com.google.api.services.storage.model.Notification> answer = 72 runWithRetries( 73 new Callable<List<com.google.api.services.storage.model.Notification>>() { 74 @Override 75 public List<com.google.api.services.storage.model.Notification> call() { 76 return ((StorageRpc) storage.getOptions().getRpc()).listNotifications(bucket); 77 } 78 }, 79 storage.getOptions().getRetrySettings(), 80 EXCEPTION_HANDLER, 81 storage.getOptions().getClock()); 82 if (answer == null) { 83 return null; 84 } 85 return Lists.transform(answer, NotificationInfo.FROM_PB_FUNCTION); 86 } catch (RetryHelperException e) { 87 throw StorageException.translateAndThrow(e); 88 } 89 } 90 91 @Override 92 public NotificationInfo createNotification(final String bucket, NotificationInfo notification) { 93 final com.google.api.services.storage.model.Notification notificationPb = notification.toPb(); 94 try { 95 return NotificationInfo.fromPb( 96 runWithRetries( 97 new Callable<com.google.api.services.storage.model.Notification>() { 98 @Override 99 public com.google.api.services.storage.model.Notification call() { 100 return ((StorageRpc) storage.getOptions().getRpc()) 101 .createNotification(bucket, notificationPb); 102 } 103 }, 104 storage.getOptions().getRetrySettings(), 105 EXCEPTION_HANDLER, 106 storage.getOptions().getClock())); 107 } catch (RetryHelperException e) { 108 throw StorageException.translateAndThrow(e); 109 } 110 } 111 } 112