|
| 1 | +/* |
| 2 | + * Copyright (C) 2023 Dirk Bolte |
| 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 | +package org.wiremock.extensions.state.extensions; |
| 17 | + |
| 18 | +import com.github.tomakehurst.wiremock.admin.AdminTask; |
| 19 | +import com.github.tomakehurst.wiremock.admin.Router; |
| 20 | +import com.github.tomakehurst.wiremock.extension.AdminApiExtension; |
| 21 | +import com.github.tomakehurst.wiremock.extension.WireMockServices; |
| 22 | +import com.github.tomakehurst.wiremock.http.RequestMethod; |
| 23 | +import com.github.tomakehurst.wiremock.http.ResponseDefinition; |
| 24 | +import org.wiremock.extensions.state.internal.ContextManager; |
| 25 | +import org.wiremock.extensions.state.internal.StateExtensionMixin; |
| 26 | +import org.wiremock.extensions.state.internal.model.Context; |
| 27 | + |
| 28 | +import java.util.Optional; |
| 29 | +import java.util.UUID; |
| 30 | + |
| 31 | +/** |
| 32 | + * ADMIN API extension to show and manage context contents. |
| 33 | + * <p> |
| 34 | + * DO NOT REGISTER directly. Use {@link org.wiremock.extensions.state.StateExtension} instead. |
| 35 | + * |
| 36 | + * @see org.wiremock.extensions.state.StateExtension |
| 37 | + */ |
| 38 | +public class StateAdminApiExtension implements AdminApiExtension, StateExtensionMixin { |
| 39 | + |
| 40 | + private final WireMockServices wireMockServices; |
| 41 | + private final ContextManager contextManager; |
| 42 | + |
| 43 | + |
| 44 | + public StateAdminApiExtension(ContextManager contextManager, WireMockServices services) { |
| 45 | + this.contextManager = contextManager; |
| 46 | + this.wireMockServices = services; |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + public String getName() { |
| 51 | + return "stateAdminApi"; |
| 52 | + } |
| 53 | + |
| 54 | + |
| 55 | + @Override |
| 56 | + public void contributeAdminApiRoutes(Router router) { |
| 57 | + router.add(RequestMethod.GET, createPath("contexts"), getContexts()); |
| 58 | + router.add(RequestMethod.GET, createPath("contexts/{context}"), getContext()); |
| 59 | + router.add(RequestMethod.DELETE, createPath("contexts/{context}"), deleteContext()); |
| 60 | + |
| 61 | + } |
| 62 | + |
| 63 | + private AdminTask getContexts() { |
| 64 | + return (admin, serveEvent, pathParams) -> ResponseDefinition.okForJson(contextManager.getAllContextNames()); |
| 65 | + } |
| 66 | + |
| 67 | + private AdminTask getContext() { |
| 68 | + return (admin, serveEvent, pathParams) -> { |
| 69 | + Optional<Context> context = contextManager.getContextCopy(pathParams.get("context")); |
| 70 | + return context.map(ResponseDefinition::okForJson).orElseGet(ResponseDefinition::notFound); |
| 71 | + }; |
| 72 | + } |
| 73 | + |
| 74 | + private AdminTask deleteContext() { |
| 75 | + return (admin, serveEvent, pathParams) -> { |
| 76 | + contextManager.deleteContext(UUID.randomUUID().toString(), pathParams.get("context")); |
| 77 | + return ResponseDefinition.noContent(); |
| 78 | + }; |
| 79 | + } |
| 80 | + |
| 81 | + private String createPath(String path) { |
| 82 | + return String.format("/state-extension/%s", path); |
| 83 | + } |
| 84 | +} |
0 commit comments