/graylog2-server/src/main/java/org/graylog2/storage/SupportedSearchVersionFilter.java

https://github.com/Graylog2/graylog2-server · Java · 80 lines · 54 code · 10 blank · 16 comment · 4 complexity · 5fce7075ee5b0b593cbcbb505000fc3e MD5 · raw file

  1. /*
  2. * Copyright (C) 2020 Graylog, Inc.
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the Server Side Public License, version 1,
  6. * as published by MongoDB, Inc.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * Server Side Public License for more details.
  12. *
  13. * You should have received a copy of the Server Side Public License
  14. * along with this program. If not, see
  15. * <http://www.mongodb.com/licensing/server-side-public-license>.
  16. */
  17. package org.graylog2.storage;
  18. import org.graylog2.configuration.validators.SearchVersionRange;
  19. import org.graylog2.shared.utilities.StringUtils;
  20. import org.graylog2.storage.providers.ElasticsearchVersionProvider;
  21. import org.slf4j.Logger;
  22. import org.slf4j.LoggerFactory;
  23. import javax.inject.Inject;
  24. import javax.ws.rs.InternalServerErrorException;
  25. import javax.ws.rs.container.ContainerRequestContext;
  26. import javax.ws.rs.container.ContainerRequestFilter;
  27. import javax.ws.rs.container.ResourceInfo;
  28. import javax.ws.rs.core.Context;
  29. import javax.ws.rs.ext.Provider;
  30. import java.io.IOException;
  31. import java.util.Arrays;
  32. import java.util.Set;
  33. import java.util.stream.Collectors;
  34. @Provider
  35. public class SupportedSearchVersionFilter implements ContainerRequestFilter {
  36. private final Logger LOG = LoggerFactory.getLogger(SupportedSearchVersionFilter.class);
  37. private final ResourceInfo resourceInfo;
  38. private final ElasticsearchVersionProvider versionProvider;
  39. @Inject
  40. public SupportedSearchVersionFilter(@Context ResourceInfo resourceInfo, ElasticsearchVersionProvider versionProvider) {
  41. this.resourceInfo = resourceInfo;
  42. this.versionProvider = versionProvider;
  43. }
  44. private void checkVersion(final SupportedSearchVersion[] annotations) {
  45. final Set<SearchVersionRange> supportedVersions = Arrays.stream(annotations)
  46. .map(supportedVersion -> SearchVersionRange.of(supportedVersion.distribution(), supportedVersion.version()))
  47. .collect(Collectors.toSet());
  48. final String supportedVersionsString = supportedVersions.stream()
  49. .map(version -> StringUtils.f("%s %s", version.distribution(), version.expression()))
  50. .collect(Collectors.joining(", "));
  51. final SearchVersion currentVersion = versionProvider.get();
  52. LOG.debug("Checking current version {} satisfies required version [{}]", currentVersion, supportedVersionsString);
  53. if (!currentVersion.satisfies(supportedVersions)) {
  54. String errMsg = StringUtils.f("Server search version %s is not compatible with resource. Supported versions: [%s]",
  55. currentVersion, supportedVersionsString);
  56. LOG.error(errMsg);
  57. throw new InternalServerErrorException(errMsg);
  58. }
  59. }
  60. @Override
  61. public void filter(ContainerRequestContext requestContext) throws IOException {
  62. if (resourceInfo.getResourceMethod().isAnnotationPresent(SupportedSearchVersion.class) ||
  63. resourceInfo.getResourceMethod().isAnnotationPresent(SupportedSearchVersions.class)) {
  64. checkVersion(resourceInfo.getResourceMethod().getAnnotationsByType(SupportedSearchVersion.class));
  65. } else if (resourceInfo.getResourceClass().isAnnotationPresent(SupportedSearchVersion.class) ||
  66. resourceInfo.getResourceClass().isAnnotationPresent(SupportedSearchVersions.class)) {
  67. checkVersion(resourceInfo.getResourceClass().getAnnotationsByType(SupportedSearchVersion.class));
  68. }
  69. }
  70. }