Line data Source code
1 : // Copyright (C) 2014 The Android Open Source Project 2 : // 3 : // Licensed under the Apache License, Version 2.0 (the "License"); 4 : // you may not use this file except in compliance with the License. 5 : // 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 10 : // distributed under the License is distributed on an "AS IS" BASIS, 11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 : // See the License for the specific language governing permissions and 13 : // limitations under the License. 14 : 15 : package com.google.gerrit.server.restapi.config; 16 : 17 : import static com.google.gerrit.common.data.GlobalCapability.MAINTAIN_SERVER; 18 : import static com.google.gerrit.common.data.GlobalCapability.VIEW_CACHES; 19 : 20 : import com.google.common.cache.Cache; 21 : import com.google.gerrit.extensions.annotations.RequiresAnyCapability; 22 : import com.google.gerrit.extensions.registration.DynamicMap; 23 : import com.google.gerrit.extensions.registration.PluginName; 24 : import com.google.gerrit.extensions.restapi.AuthException; 25 : import com.google.gerrit.extensions.restapi.ChildCollection; 26 : import com.google.gerrit.extensions.restapi.IdString; 27 : import com.google.gerrit.extensions.restapi.ResourceNotFoundException; 28 : import com.google.gerrit.extensions.restapi.RestView; 29 : import com.google.gerrit.server.config.CacheResource; 30 : import com.google.gerrit.server.config.ConfigResource; 31 : import com.google.gerrit.server.permissions.GlobalPermission; 32 : import com.google.gerrit.server.permissions.PermissionBackend; 33 : import com.google.gerrit.server.permissions.PermissionBackendException; 34 : import com.google.inject.Inject; 35 : import com.google.inject.Provider; 36 : import com.google.inject.Singleton; 37 : 38 : @RequiresAnyCapability({VIEW_CACHES, MAINTAIN_SERVER}) 39 : @Singleton 40 : public class CachesCollection implements ChildCollection<ConfigResource, CacheResource> { 41 : 42 : private final DynamicMap<RestView<CacheResource>> views; 43 : private final Provider<ListCaches> list; 44 : private final PermissionBackend permissionBackend; 45 : private final DynamicMap<Cache<?, ?>> cacheMap; 46 : 47 : @Inject 48 : CachesCollection( 49 : DynamicMap<RestView<CacheResource>> views, 50 : Provider<ListCaches> list, 51 : PermissionBackend permissionBackend, 52 138 : DynamicMap<Cache<?, ?>> cacheMap) { 53 138 : this.views = views; 54 138 : this.list = list; 55 138 : this.permissionBackend = permissionBackend; 56 138 : this.cacheMap = cacheMap; 57 138 : } 58 : 59 : @Override 60 : public RestView<ConfigResource> list() { 61 3 : return list.get(); 62 : } 63 : 64 : @Override 65 : public CacheResource parse(ConfigResource parent, IdString id) 66 : throws AuthException, ResourceNotFoundException, PermissionBackendException { 67 2 : permissionBackend.currentUser().check(GlobalPermission.VIEW_CACHES); 68 : 69 2 : String cacheName = id.get(); 70 2 : String pluginName = PluginName.GERRIT; 71 2 : int i = cacheName.lastIndexOf('-'); 72 2 : if (i != -1) { 73 1 : pluginName = cacheName.substring(0, i); 74 1 : cacheName = cacheName.length() > i + 1 ? cacheName.substring(i + 1) : ""; 75 : } 76 : 77 2 : Provider<Cache<?, ?>> cacheProvider = cacheMap.byPlugin(pluginName).get(cacheName); 78 2 : if (cacheProvider == null) { 79 1 : throw new ResourceNotFoundException(id); 80 : } 81 2 : return new CacheResource(pluginName, cacheName, cacheProvider); 82 : } 83 : 84 : @Override 85 : public DynamicMap<RestView<CacheResource>> views() { 86 2 : return views; 87 : } 88 : }