001/*
002 * Copyright 2012-2018 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.boot.actuate.cache;
018
019import org.springframework.boot.actuate.cache.CachesEndpoint.CacheEntry;
020import org.springframework.boot.actuate.endpoint.annotation.DeleteOperation;
021import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
022import org.springframework.boot.actuate.endpoint.annotation.Selector;
023import org.springframework.boot.actuate.endpoint.web.WebEndpointResponse;
024import org.springframework.boot.actuate.endpoint.web.annotation.EndpointWebExtension;
025import org.springframework.lang.Nullable;
026
027/**
028 * {@link EndpointWebExtension} for the {@link CachesEndpoint}.
029 *
030 * @author Stephane Nicoll
031 * @since 2.1.0
032 */
033@EndpointWebExtension(endpoint = CachesEndpoint.class)
034public class CachesEndpointWebExtension {
035
036        private final CachesEndpoint delegate;
037
038        public CachesEndpointWebExtension(CachesEndpoint delegate) {
039                this.delegate = delegate;
040        }
041
042        @ReadOperation
043        public WebEndpointResponse<CacheEntry> cache(@Selector String cache,
044                        @Nullable String cacheManager) {
045                try {
046                        CacheEntry entry = this.delegate.cache(cache, cacheManager);
047                        int status = (entry != null) ? WebEndpointResponse.STATUS_OK
048                                        : WebEndpointResponse.STATUS_NOT_FOUND;
049                        return new WebEndpointResponse<>(entry, status);
050                }
051                catch (NonUniqueCacheException ex) {
052                        return new WebEndpointResponse<>(WebEndpointResponse.STATUS_BAD_REQUEST);
053                }
054        }
055
056        @DeleteOperation
057        public WebEndpointResponse<Void> clearCache(@Selector String cache,
058                        @Nullable String cacheManager) {
059                try {
060                        boolean cleared = this.delegate.clearCache(cache, cacheManager);
061                        int status = (cleared ? WebEndpointResponse.STATUS_NO_CONTENT
062                                        : WebEndpointResponse.STATUS_NOT_FOUND);
063                        return new WebEndpointResponse<>(status);
064                }
065                catch (NonUniqueCacheException ex) {
066                        return new WebEndpointResponse<>(WebEndpointResponse.STATUS_BAD_REQUEST);
067                }
068        }
069
070}