|
| 1 | +/* |
| 2 | + * Copyright 2018 the original author or authors. |
| 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.springframework.data.rest.core.mapping; |
| 17 | + |
| 18 | +import lombok.AccessLevel; |
| 19 | +import lombok.RequiredArgsConstructor; |
| 20 | + |
| 21 | +import java.util.Arrays; |
| 22 | +import java.util.Collection; |
| 23 | +import java.util.Iterator; |
| 24 | +import java.util.List; |
| 25 | +import java.util.stream.Collectors; |
| 26 | +import java.util.stream.Stream; |
| 27 | + |
| 28 | +import org.springframework.http.HttpMethod; |
| 29 | +import org.springframework.util.Assert; |
| 30 | + |
| 31 | +/** |
| 32 | + * {@link HttpMethods} that expose methods to create different {@link ConfigurableHttpMethods}. |
| 33 | + * |
| 34 | + * @author Oliver Gierke |
| 35 | + * @since 3.1 |
| 36 | + */ |
| 37 | +@RequiredArgsConstructor(staticName = "of", access = AccessLevel.PACKAGE) |
| 38 | +public class ConfigurableHttpMethods implements HttpMethods { |
| 39 | + |
| 40 | + public static final ConfigurableHttpMethods NONE = ConfigurableHttpMethods.of(); |
| 41 | + public static final ConfigurableHttpMethods ALL = ConfigurableHttpMethods.of(HttpMethod.values()); |
| 42 | + |
| 43 | + private final Collection<HttpMethod> methods; |
| 44 | + |
| 45 | + /** |
| 46 | + * Creates a new {@link ConfigurableHttpMethods} of the given {@link HttpMethod}s. |
| 47 | + * |
| 48 | + * @param methods must not be {@literal null}. |
| 49 | + * @return |
| 50 | + */ |
| 51 | + static ConfigurableHttpMethods of(HttpMethod... methods) { |
| 52 | + |
| 53 | + Assert.notNull(methods, "HttpMethods must not be null!"); |
| 54 | + |
| 55 | + return new ConfigurableHttpMethods(Arrays.stream(methods).collect(Collectors.toSet())); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Creates a new {@link ConfigurableHttpMethods} of the given {@link HttpMethods}. |
| 60 | + * |
| 61 | + * @param methods must not be {@literal null}. |
| 62 | + * @return |
| 63 | + */ |
| 64 | + static ConfigurableHttpMethods of(HttpMethods methods) { |
| 65 | + |
| 66 | + Assert.notNull(methods, "HttpMethods must not be null!"); |
| 67 | + |
| 68 | + if (ConfigurableHttpMethods.class.isInstance(methods)) { |
| 69 | + return ConfigurableHttpMethods.class.cast(methods); |
| 70 | + } |
| 71 | + |
| 72 | + return new ConfigurableHttpMethods(methods.stream().collect(Collectors.toSet())); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Disables the given {@link HttpMethod}s. |
| 77 | + * |
| 78 | + * @param methods must not be {@literal null}. |
| 79 | + * @return |
| 80 | + */ |
| 81 | + public ConfigurableHttpMethods disable(HttpMethod... methods) { |
| 82 | + |
| 83 | + Assert.notNull(methods, "HttpMethods must not be null!"); |
| 84 | + |
| 85 | + List<HttpMethod> toRemove = Arrays.asList(methods); |
| 86 | + |
| 87 | + return new ConfigurableHttpMethods(this.methods.stream() // |
| 88 | + .filter(it -> !toRemove.contains(it)) // |
| 89 | + .collect(Collectors.toSet())); |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Enables the given {@link HttpMethod}s. |
| 94 | + * |
| 95 | + * @param methods must not be {@literal null}. |
| 96 | + * @return |
| 97 | + */ |
| 98 | + public HttpMethods enable(HttpMethod... methods) { |
| 99 | + |
| 100 | + Assert.notNull(methods, "HttpMethods must not be null!"); |
| 101 | + |
| 102 | + List<HttpMethod> toAdd = Arrays.asList(methods); |
| 103 | + |
| 104 | + if (this.methods.containsAll(toAdd)) { |
| 105 | + return this; |
| 106 | + } |
| 107 | + |
| 108 | + return ConfigurableHttpMethods.of(Stream.concat(this.methods.stream(), toAdd.stream()).collect(Collectors.toSet())); |
| 109 | + } |
| 110 | + |
| 111 | + /* |
| 112 | + * (non-Javadoc) |
| 113 | + * @see org.springframework.data.rest.core.mapping.HttpMethods#contains(org.springframework.http.HttpMethod) |
| 114 | + */ |
| 115 | + @Override |
| 116 | + public boolean contains(HttpMethod method) { |
| 117 | + |
| 118 | + Assert.notNull(method, "HTTP method must not be null!"); |
| 119 | + |
| 120 | + return methods.contains(method); |
| 121 | + } |
| 122 | + |
| 123 | + /* |
| 124 | + * (non-Javadoc) |
| 125 | + * @see java.lang.Iterable#iterator() |
| 126 | + */ |
| 127 | + @Override |
| 128 | + public Iterator<HttpMethod> iterator() { |
| 129 | + return methods.iterator(); |
| 130 | + } |
| 131 | +} |
0 commit comments