| // Copyright (C) 2015 Ericsson |
| // |
| // 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.ericsson.gerrit.plugins.evictcache; |
| |
| import com.google.common.base.Joiner; |
| import com.google.gerrit.extensions.annotations.PluginName; |
| import com.google.inject.Inject; |
| |
| import org.slf4j.Logger; |
| import org.slf4j.LoggerFactory; |
| |
| import java.io.IOException; |
| |
| class RestSession { |
| private static final Logger log = LoggerFactory.getLogger(RestSession.class); |
| private static final String EVICT = "evict"; |
| private final HttpSession httpSession; |
| private final String pluginName; |
| |
| @Inject |
| RestSession(HttpSession httpClient, |
| @PluginName String pluginName) { |
| this.httpSession = httpClient; |
| this.pluginName = pluginName; |
| } |
| |
| boolean evict(String sourceName, String cacheName, Object key) { |
| try { |
| String json = GsonParser.toJson(cacheName, key); |
| String buildEndpoint = buildEndpoint(pluginName, sourceName, cacheName); |
| return httpSession |
| .post(buildEndpoint, json) |
| .isSuccessful(); |
| } catch (IOException e) { |
| log.error("Error trying to evict for cache " + cacheName, e); |
| return false; |
| } |
| } |
| |
| private String buildEndpoint(String pluginName, String sourceName, |
| String cacheName) { |
| return Joiner.on("/").join("/plugins", pluginName, sourceName, EVICT, cacheName); |
| } |
| } |