From ee5395152ef677d93d398d404888ca54610b3b40 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Olov=20Ylinenp=C3=A4=C3=A4?= Date: Tue, 5 Nov 2024 22:53:28 +0100 Subject: [PATCH 1/3] refactor: move HTTP util stuff from rest to server-common --- .../whelk/rest/api/BulkChangePreviewAPI.java | 8 +++- .../groovy/whelk/rest/api/ConverterAPI.groovy | 2 +- .../whelk/rest/api/ConverterUtils.groovy | 7 +-- .../main/groovy/whelk/rest/api/Crud.groovy | 45 ++++--------------- .../whelk/rest/api/CrudGetRequest.groovy | 6 ++- .../groovy/whelk/rest/api/CrudUtils.groovy | 8 +++- .../whelk/rest/api/MarcframeData.groovy | 2 +- .../whelk/rest/api/RemoteSearchAPI.groovy | 1 + .../groovy/whelk/rest/api/SearchUtils2.java | 5 ++- .../whelk/rest/api/TransliterationAPI.groovy | 2 +- .../groovy/whelk/rest/api/UserDataAPI.groovy | 1 + .../groovy/whelk/rest/api/CrudSpec.groovy | 4 +- .../whelk/rest/api/CrudUtilsSpec.groovy | 5 ++- server-common/build.gradle | 2 + .../util/http}/BadRequestException.groovy | 4 +- .../groovy/whelk/util/http}/HttpTools.groovy | 8 ++-- .../groovy/whelk/util/http}/MimeTypes.groovy | 2 +- .../util/http/NoStackTraceException.groovy | 16 +++++++ .../whelk/util/http/NotFoundException.groovy | 7 +++ .../util/http/OtherStatusException.groovy | 10 +++++ .../whelk/util/http/RedirectException.groovy | 7 +++ .../UnsupportedContentTypeException.groovy | 2 +- .../main/java/whelk/AuthenticationFilter.java | 0 23 files changed, 93 insertions(+), 61 deletions(-) rename {rest/src/main/groovy/whelk/rest/api => server-common/src/main/groovy/whelk/util/http}/BadRequestException.groovy (72%) rename {rest/src/main/groovy/whelk/rest/api => server-common/src/main/groovy/whelk/util/http}/HttpTools.groovy (96%) rename {rest/src/main/groovy/whelk/rest/api => server-common/src/main/groovy/whelk/util/http}/MimeTypes.groovy (91%) create mode 100644 server-common/src/main/groovy/whelk/util/http/NoStackTraceException.groovy create mode 100644 server-common/src/main/groovy/whelk/util/http/NotFoundException.groovy create mode 100644 server-common/src/main/groovy/whelk/util/http/OtherStatusException.groovy create mode 100644 server-common/src/main/groovy/whelk/util/http/RedirectException.groovy rename {rest/src/main/groovy/whelk/rest/api => server-common/src/main/groovy/whelk/util/http}/UnsupportedContentTypeException.groovy (88%) rename {rest => server-common}/src/main/java/whelk/AuthenticationFilter.java (100%) diff --git a/rest/src/main/groovy/whelk/rest/api/BulkChangePreviewAPI.java b/rest/src/main/groovy/whelk/rest/api/BulkChangePreviewAPI.java index 6eed2c7b70..f2e74b8d86 100644 --- a/rest/src/main/groovy/whelk/rest/api/BulkChangePreviewAPI.java +++ b/rest/src/main/groovy/whelk/rest/api/BulkChangePreviewAPI.java @@ -11,6 +11,10 @@ import whelk.history.History; import whelk.util.DocumentUtil; import whelk.util.WhelkFactory; +import whelk.util.http.BadRequestException; +import whelk.util.http.HttpTools; +import whelk.util.http.MimeTypes; +import whelk.util.http.NotFoundException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; @@ -49,7 +53,7 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response) t throw new BadRequestException("@id parameter is required"); } if (!id.startsWith(Document.getBASE_URI().toString())) { - throw new Crud.NotFoundException("Document not found"); + throw new NotFoundException("Document not found"); } var systemId = stripPrefix(id, Document.getBASE_URI().toString()); @@ -215,7 +219,7 @@ private static List slice(List l, int from, int to) { private BulkJobDocument load(String id) { Document doc = whelk.getDocument(id); if (doc == null) { - throw new Crud.NotFoundException("Document not found"); + throw new NotFoundException("Document not found"); } try { return new BulkJobDocument(doc.data); diff --git a/rest/src/main/groovy/whelk/rest/api/ConverterAPI.groovy b/rest/src/main/groovy/whelk/rest/api/ConverterAPI.groovy index 1057d18b1e..6d953315da 100644 --- a/rest/src/main/groovy/whelk/rest/api/ConverterAPI.groovy +++ b/rest/src/main/groovy/whelk/rest/api/ConverterAPI.groovy @@ -5,9 +5,9 @@ import org.apache.http.entity.ContentType import whelk.Document import whelk.Whelk import whelk.converter.marc.MarcFrameConverter -import whelk.util.Tools import whelk.util.Unicode import whelk.util.WhelkFactory +import whelk.util.http.HttpTools import javax.servlet.http.HttpServlet import javax.servlet.http.HttpServletRequest diff --git a/rest/src/main/groovy/whelk/rest/api/ConverterUtils.groovy b/rest/src/main/groovy/whelk/rest/api/ConverterUtils.groovy index 7805511bd0..76d8f018d6 100644 --- a/rest/src/main/groovy/whelk/rest/api/ConverterUtils.groovy +++ b/rest/src/main/groovy/whelk/rest/api/ConverterUtils.groovy @@ -8,6 +8,7 @@ import whelk.converter.JsonLD2N3Converter import whelk.converter.JsonLD2RdfXml import whelk.converter.JsonLDTrigConverter import whelk.converter.JsonLDTurtleConverter +import whelk.util.http.MimeTypes @Log class ConverterUtils { @@ -18,10 +19,10 @@ class ConverterUtils { this.whelk = whelk converters = [ - (MimeTypes.RDF): new JsonLD2RdfXml(whelk), + (MimeTypes.RDF) : new JsonLD2RdfXml(whelk), (MimeTypes.TURTLE): new JsonLDTurtleConverter(null, whelk), - (MimeTypes.TRIG): new JsonLDTrigConverter(null, whelk), - (MimeTypes.N3): new JsonLD2N3Converter(whelk), + (MimeTypes.TRIG) : new JsonLDTrigConverter(null, whelk), + (MimeTypes.N3) : new JsonLD2N3Converter(whelk), ] } diff --git a/rest/src/main/groovy/whelk/rest/api/Crud.groovy b/rest/src/main/groovy/whelk/rest/api/Crud.groovy index d66b4eb1e4..a7f2117f9c 100644 --- a/rest/src/main/groovy/whelk/rest/api/Crud.groovy +++ b/rest/src/main/groovy/whelk/rest/api/Crud.groovy @@ -21,6 +21,12 @@ import whelk.history.History import whelk.rest.api.CrudGetRequest.Lens import whelk.rest.security.AccessControl import whelk.util.WhelkFactory +import whelk.util.http.BadRequestException +import whelk.util.http.HttpTools +import whelk.util.http.MimeTypes +import whelk.util.http.NotFoundException +import whelk.util.http.OtherStatusException +import whelk.util.http.RedirectException import javax.servlet.http.HttpServlet import javax.servlet.http.HttpServletRequest @@ -28,8 +34,8 @@ import javax.servlet.http.HttpServletResponse import java.lang.management.ManagementFactory import static whelk.rest.api.CrudUtils.ETag -import static whelk.rest.api.HttpTools.getBaseUri -import static whelk.rest.api.HttpTools.sendResponse +import static whelk.util.http.HttpTools.getBaseUri +import static whelk.util.http.HttpTools.sendResponse import static whelk.util.Jackson.mapper /** @@ -620,7 +626,7 @@ class Crud extends HttpServlet { String location = docAndLoc.v2 if (!existingDoc && !location) { - throw new Crud.NotFoundException("Document not found.") + throw new NotFoundException("Document not found.") } else if (!existingDoc && location) { sendRedirect(request, response, location) return @@ -847,38 +853,5 @@ class Crud extends HttpServlet { HttpTools.sendError(response, code, e.getMessage(), e) } - static class NotFoundException extends NoStackTraceException { - NotFoundException(String msg) { - super(msg) - } - } - - static class OtherStatusException extends NoStackTraceException { - int code - OtherStatusException(String msg, int code, Throwable cause = null) { - super(msg, cause) - this.code = code - } - } - - /** "Don't use exceptions for flow control" in part comes from that exceptions in Java are - * expensive to create because building the stack trace is expensive. But in the context of - * sending error responses in this API exceptions are pretty useful for flow control. - * This is a base class for stack trace-less exceptions for common error flows. - */ - static class NoStackTraceException extends RuntimeException { - protected NoStackTraceException(String msg) { - super(msg, null, true, false) - } - - protected NoStackTraceException(String msg, Throwable cause) { - super(msg, cause, true, false) - } - } - static class RedirectException extends NoStackTraceException { - RedirectException(String msg) { - super(msg) - } - } } diff --git a/rest/src/main/groovy/whelk/rest/api/CrudGetRequest.groovy b/rest/src/main/groovy/whelk/rest/api/CrudGetRequest.groovy index 9df21391a1..befc4f10ce 100644 --- a/rest/src/main/groovy/whelk/rest/api/CrudGetRequest.groovy +++ b/rest/src/main/groovy/whelk/rest/api/CrudGetRequest.groovy @@ -1,5 +1,9 @@ package whelk.rest.api +import whelk.util.http.BadRequestException +import whelk.util.http.MimeTypes +import whelk.util.http.NotFoundException + import javax.servlet.http.HttpServletRequest import static whelk.rest.api.CrudUtils.* @@ -90,7 +94,7 @@ class CrudGetRequest { dataLeaf = matcher[0][2] view = View.fromString(matcher[0][3]) } else { - throw new Crud.NotFoundException("Not found:" + path) + throw new NotFoundException("Not found:" + path) } } diff --git a/rest/src/main/groovy/whelk/rest/api/CrudUtils.groovy b/rest/src/main/groovy/whelk/rest/api/CrudUtils.groovy index f8933a38f3..06183cc875 100644 --- a/rest/src/main/groovy/whelk/rest/api/CrudUtils.groovy +++ b/rest/src/main/groovy/whelk/rest/api/CrudUtils.groovy @@ -8,6 +8,10 @@ import org.apache.commons.io.FilenameUtils import org.apache.http.HeaderElement import org.apache.http.NameValuePair import org.apache.http.message.BasicHeaderValueParser +import whelk.util.http.BadRequestException +import whelk.util.http.MimeTypes +import whelk.util.http.NotFoundException +import whelk.util.http.UnsupportedContentTypeException import javax.servlet.http.HttpServletRequest import java.lang.management.ManagementFactory @@ -75,9 +79,9 @@ class CrudUtils { return ALLOWED_MEDIA_TYPES_BY_EXT.get(extension) } else { if (extension) { - throw new Crud.NotFoundException('.' + extension) + throw new NotFoundException('.' + extension) } else { - throw new Crud.NotFoundException("${mediaTypeIntersect}") + throw new NotFoundException("${mediaTypeIntersect}") } } } diff --git a/rest/src/main/groovy/whelk/rest/api/MarcframeData.groovy b/rest/src/main/groovy/whelk/rest/api/MarcframeData.groovy index ed959727ed..8297a7a8be 100644 --- a/rest/src/main/groovy/whelk/rest/api/MarcframeData.groovy +++ b/rest/src/main/groovy/whelk/rest/api/MarcframeData.groovy @@ -7,7 +7,7 @@ import javax.servlet.http.HttpServlet import javax.servlet.http.HttpServletRequest import javax.servlet.http.HttpServletResponse -import static whelk.rest.api.HttpTools.sendResponse +import static whelk.util.http.HttpTools.sendResponse @CompileStatic class MarcframeData extends HttpServlet { diff --git a/rest/src/main/groovy/whelk/rest/api/RemoteSearchAPI.groovy b/rest/src/main/groovy/whelk/rest/api/RemoteSearchAPI.groovy index ec8dfba701..f49fcb1c35 100644 --- a/rest/src/main/groovy/whelk/rest/api/RemoteSearchAPI.groovy +++ b/rest/src/main/groovy/whelk/rest/api/RemoteSearchAPI.groovy @@ -16,6 +16,7 @@ import whelk.filter.LinkFinder import whelk.util.LegacyIntegrationTools import whelk.util.PropertyLoader import whelk.util.WhelkFactory +import whelk.util.http.HttpTools import javax.servlet.http.HttpServlet import javax.servlet.http.HttpServletRequest diff --git a/rest/src/main/groovy/whelk/rest/api/SearchUtils2.java b/rest/src/main/groovy/whelk/rest/api/SearchUtils2.java index e5d32787f9..80d73f8ea9 100644 --- a/rest/src/main/groovy/whelk/rest/api/SearchUtils2.java +++ b/rest/src/main/groovy/whelk/rest/api/SearchUtils2.java @@ -16,6 +16,7 @@ import whelk.search2.Spell; import whelk.search2.Stats; import whelk.search2.querytree.QueryTree; +import whelk.util.http.RedirectException; import java.io.IOException; import java.util.*; @@ -43,7 +44,7 @@ Map doSearch(Map queryParameters) throws Inval if (queryParams.object == null) { throw new InvalidQueryException("Missing required query parameter: _q"); } else { - throw new Crud.RedirectException(QueryUtil.makeFindUrl("", "*", queryParams.getNonQueryParams())); + throw new RedirectException(QueryUtil.makeFindUrl("", "*", queryParams.getNonQueryParams())); } } @@ -54,7 +55,7 @@ Map doSearch(Map queryParameters) throws Inval QueryTree iTree = new QueryTree(queryParams.i, disambiguate, appParams.siteFilters.aliasToFilter()); if (!iTree.isEmpty() && !iTree.isFreeText()) { - throw new Crud.RedirectException(QueryUtil.makeFindUrl(qTree, queryParams.getNonQueryParams())); + throw new RedirectException(QueryUtil.makeFindUrl(qTree, queryParams.getNonQueryParams())); } qTree.addFilters(queryParams, appParams); diff --git a/rest/src/main/groovy/whelk/rest/api/TransliterationAPI.groovy b/rest/src/main/groovy/whelk/rest/api/TransliterationAPI.groovy index 1c24b44fce..08628384fc 100644 --- a/rest/src/main/groovy/whelk/rest/api/TransliterationAPI.groovy +++ b/rest/src/main/groovy/whelk/rest/api/TransliterationAPI.groovy @@ -1,9 +1,9 @@ package whelk.rest.api import groovy.util.logging.Log4j2 as Log -import whelk.Whelk import whelk.util.Romanizer import whelk.util.WhelkFactory +import whelk.util.http.HttpTools import javax.servlet.http.HttpServlet import javax.servlet.http.HttpServletRequest diff --git a/rest/src/main/groovy/whelk/rest/api/UserDataAPI.groovy b/rest/src/main/groovy/whelk/rest/api/UserDataAPI.groovy index 7dd0c0165a..c1bd8142d3 100644 --- a/rest/src/main/groovy/whelk/rest/api/UserDataAPI.groovy +++ b/rest/src/main/groovy/whelk/rest/api/UserDataAPI.groovy @@ -5,6 +5,7 @@ import groovy.util.logging.Log4j2 as Log import groovy.json.JsonSlurper import whelk.Whelk import whelk.util.WhelkFactory +import whelk.util.http.HttpTools import javax.servlet.http.HttpServlet import javax.servlet.http.HttpServletRequest diff --git a/rest/src/test/groovy/whelk/rest/api/CrudSpec.groovy b/rest/src/test/groovy/whelk/rest/api/CrudSpec.groovy index 4d26745f8c..b31f17d716 100644 --- a/rest/src/test/groovy/whelk/rest/api/CrudSpec.groovy +++ b/rest/src/test/groovy/whelk/rest/api/CrudSpec.groovy @@ -25,8 +25,8 @@ import static javax.servlet.http.HttpServletResponse.SC_BAD_REQUEST import static javax.servlet.http.HttpServletResponse.SC_NOT_FOUND import static javax.servlet.http.HttpServletResponse.SC_NOT_MODIFIED import static javax.servlet.http.HttpServletResponse.SC_OK -import static whelk.rest.api.MimeTypes.JSON -import static whelk.rest.api.MimeTypes.JSONLD +import static whelk.util.http.MimeTypes.JSON +import static whelk.util.http.MimeTypes.JSONLD import static whelk.util.Jackson.mapper /** diff --git a/rest/src/test/groovy/whelk/rest/api/CrudUtilsSpec.groovy b/rest/src/test/groovy/whelk/rest/api/CrudUtilsSpec.groovy index afcf7e1e2d..56960ab877 100644 --- a/rest/src/test/groovy/whelk/rest/api/CrudUtilsSpec.groovy +++ b/rest/src/test/groovy/whelk/rest/api/CrudUtilsSpec.groovy @@ -2,7 +2,8 @@ package whelk.rest.api import com.google.common.net.MediaType import spock.lang.Specification -import whelk.rest.api.CrudUtils +import whelk.util.http.BadRequestException +import whelk.util.http.NotFoundException class CrudUtilsSpec extends Specification { @@ -55,7 +56,7 @@ class CrudUtilsSpec extends Specification { CrudUtils.getBestContentType(acceptHeader, requestPath) then: - thrown(Crud.NotFoundException) + thrown(NotFoundException) where: suffix << ['.invalid'] diff --git a/server-common/build.gradle b/server-common/build.gradle index a4baaf5e2c..190476448f 100755 --- a/server-common/build.gradle +++ b/server-common/build.gradle @@ -28,6 +28,8 @@ dependencies { implementation "org.eclipse.jetty:jetty-server:${jettyVersion}" implementation "org.eclipse.jetty.ee8:jetty-ee8-servlet:${jettyVersion}" + implementation "org.apache.httpcomponents:httpclient:${httpComponentsClientVersion}" + // Logging implementation group: 'org.apache.logging.log4j', name: 'log4j-api', version: "${log4jVersion}" implementation group: 'org.apache.logging.log4j', name: 'log4j-core', version: "${log4jVersion}" diff --git a/rest/src/main/groovy/whelk/rest/api/BadRequestException.groovy b/server-common/src/main/groovy/whelk/util/http/BadRequestException.groovy similarity index 72% rename from rest/src/main/groovy/whelk/rest/api/BadRequestException.groovy rename to server-common/src/main/groovy/whelk/util/http/BadRequestException.groovy index 9979113949..1fede53142 100644 --- a/rest/src/main/groovy/whelk/rest/api/BadRequestException.groovy +++ b/server-common/src/main/groovy/whelk/util/http/BadRequestException.groovy @@ -1,6 +1,6 @@ -package whelk.rest.api +package whelk.util.http -class BadRequestException extends Crud.NoStackTraceException { +class BadRequestException extends NoStackTraceException { Map extraInfo BadRequestException(String msg, Map extraInfo = null) { super(msg) diff --git a/rest/src/main/groovy/whelk/rest/api/HttpTools.groovy b/server-common/src/main/groovy/whelk/util/http/HttpTools.groovy similarity index 96% rename from rest/src/main/groovy/whelk/rest/api/HttpTools.groovy rename to server-common/src/main/groovy/whelk/util/http/HttpTools.groovy index f153751d13..4eb83f6923 100644 --- a/rest/src/main/groovy/whelk/rest/api/HttpTools.groovy +++ b/server-common/src/main/groovy/whelk/util/http/HttpTools.groovy @@ -1,4 +1,4 @@ -package whelk.rest.api +package whelk.util.http import groovy.util.logging.Log4j2 as Log import org.apache.commons.lang3.exception.ExceptionUtils @@ -110,7 +110,7 @@ class HttpTools { case LinkValidationException: return HttpServletResponse.SC_BAD_REQUEST - case Crud.NotFoundException: + case NotFoundException: return HttpServletResponse.SC_NOT_FOUND case UnsupportedContentTypeException: @@ -123,8 +123,8 @@ class HttpTools { case StorageCreateFailedException: return HttpServletResponse.SC_CONFLICT - case Crud.OtherStatusException: - return ((Crud.OtherStatusException) e).code + case OtherStatusException: + return ((OtherStatusException) e).code default: return HttpServletResponse.SC_INTERNAL_SERVER_ERROR diff --git a/rest/src/main/groovy/whelk/rest/api/MimeTypes.groovy b/server-common/src/main/groovy/whelk/util/http/MimeTypes.groovy similarity index 91% rename from rest/src/main/groovy/whelk/rest/api/MimeTypes.groovy rename to server-common/src/main/groovy/whelk/util/http/MimeTypes.groovy index 0386423893..1e298b806c 100644 --- a/rest/src/main/groovy/whelk/rest/api/MimeTypes.groovy +++ b/server-common/src/main/groovy/whelk/util/http/MimeTypes.groovy @@ -1,4 +1,4 @@ -package whelk.rest.api +package whelk.util.http class MimeTypes { static final JSONLD = "application/ld+json" diff --git a/server-common/src/main/groovy/whelk/util/http/NoStackTraceException.groovy b/server-common/src/main/groovy/whelk/util/http/NoStackTraceException.groovy new file mode 100644 index 0000000000..07cb12e432 --- /dev/null +++ b/server-common/src/main/groovy/whelk/util/http/NoStackTraceException.groovy @@ -0,0 +1,16 @@ +package whelk.util.http + +/** "Don't use exceptions for flow control" in part comes from that exceptions in Java are + * expensive to create because building the stack trace is expensive. But in the context of + * sending error responses in this API exceptions are pretty useful for flow control. + * This is a base class for stack trace-less exceptions for common error flows. + */ +class NoStackTraceException extends RuntimeException { + protected NoStackTraceException(String msg) { + super(msg, null, true, false) + } + + protected NoStackTraceException(String msg, Throwable cause) { + super(msg, cause, true, false) + } +} diff --git a/server-common/src/main/groovy/whelk/util/http/NotFoundException.groovy b/server-common/src/main/groovy/whelk/util/http/NotFoundException.groovy new file mode 100644 index 0000000000..4ba3f24e5d --- /dev/null +++ b/server-common/src/main/groovy/whelk/util/http/NotFoundException.groovy @@ -0,0 +1,7 @@ +package whelk.util.http + +class NotFoundException extends NoStackTraceException { + NotFoundException(String msg) { + super(msg) + } +} diff --git a/server-common/src/main/groovy/whelk/util/http/OtherStatusException.groovy b/server-common/src/main/groovy/whelk/util/http/OtherStatusException.groovy new file mode 100644 index 0000000000..62b4583d21 --- /dev/null +++ b/server-common/src/main/groovy/whelk/util/http/OtherStatusException.groovy @@ -0,0 +1,10 @@ +package whelk.util.http + +class OtherStatusException extends NoStackTraceException { + int code + + OtherStatusException(String msg, int code, Throwable cause = null) { + super(msg, cause) + this.code = code + } +} diff --git a/server-common/src/main/groovy/whelk/util/http/RedirectException.groovy b/server-common/src/main/groovy/whelk/util/http/RedirectException.groovy new file mode 100644 index 0000000000..ee3e1057b7 --- /dev/null +++ b/server-common/src/main/groovy/whelk/util/http/RedirectException.groovy @@ -0,0 +1,7 @@ +package whelk.util.http + +class RedirectException extends NoStackTraceException { + RedirectException(String msg) { + super(msg) + } +} diff --git a/rest/src/main/groovy/whelk/rest/api/UnsupportedContentTypeException.groovy b/server-common/src/main/groovy/whelk/util/http/UnsupportedContentTypeException.groovy similarity index 88% rename from rest/src/main/groovy/whelk/rest/api/UnsupportedContentTypeException.groovy rename to server-common/src/main/groovy/whelk/util/http/UnsupportedContentTypeException.groovy index 56c6227372..b997211250 100644 --- a/rest/src/main/groovy/whelk/rest/api/UnsupportedContentTypeException.groovy +++ b/server-common/src/main/groovy/whelk/util/http/UnsupportedContentTypeException.groovy @@ -1,4 +1,4 @@ -package whelk.rest.api +package whelk.util.http /** * Created by markus on 2015-10-15. diff --git a/rest/src/main/java/whelk/AuthenticationFilter.java b/server-common/src/main/java/whelk/AuthenticationFilter.java similarity index 100% rename from rest/src/main/java/whelk/AuthenticationFilter.java rename to server-common/src/main/java/whelk/AuthenticationFilter.java From eba766a25b9bc4d428fb0ba236613c375b83059c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Olov=20Ylinenp=C3=A4=C3=A4?= Date: Tue, 5 Nov 2024 22:56:39 +0100 Subject: [PATCH 2/3] chore: fix some warnings in AuthenticationFilter --- .../main/java/whelk/AuthenticationFilter.java | 28 +++++++++---------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/server-common/src/main/java/whelk/AuthenticationFilter.java b/server-common/src/main/java/whelk/AuthenticationFilter.java index 91634db289..89f598bf56 100644 --- a/server-common/src/main/java/whelk/AuthenticationFilter.java +++ b/server-common/src/main/java/whelk/AuthenticationFilter.java @@ -64,15 +64,15 @@ public void doFilter(ServletRequest request, ServletResponse response, FilterCha try { String token = httpRequest.getHeader("Authorization"); if (token == null) { - httpResponse.sendError(httpResponse.SC_UNAUTHORIZED, "No access token (Authorization header) in request"); - response_code = httpResponse.SC_UNAUTHORIZED; + httpResponse.sendError(HttpServletResponse.SC_UNAUTHORIZED, "No access token (Authorization header) in request"); + response_code = HttpServletResponse.SC_UNAUTHORIZED; return; } log.debug("Verifying token " + token); json = verifyToken(token.replace("Bearer ", "")); if (json == null || json.isEmpty()) { - httpResponse.sendError(httpResponse.SC_UNAUTHORIZED, "Access token has expired"); - response_code = httpResponse.SC_UNAUTHORIZED; + httpResponse.sendError(HttpServletResponse.SC_UNAUTHORIZED, "Access token has expired"); + response_code = HttpServletResponse.SC_UNAUTHORIZED; return; } @@ -80,13 +80,13 @@ public void doFilter(ServletRequest request, ServletResponse response, FilterCha Object message = result.get("message"); if (message != null && message.toString().equals("Bearer token is expired.")) { - httpResponse.sendError(httpResponse.SC_UNAUTHORIZED, "Access token has expired"); - response_code = httpResponse.SC_UNAUTHORIZED; + httpResponse.sendError(HttpServletResponse.SC_UNAUTHORIZED, "Access token has expired"); + response_code = HttpServletResponse.SC_UNAUTHORIZED; return; } if (message != null && message.toString().equals("Bearer token not found.")) { - httpResponse.sendError(httpResponse.SC_UNAUTHORIZED, "Bad access token."); - response_code = httpResponse.SC_UNAUTHORIZED; + httpResponse.sendError(HttpServletResponse.SC_UNAUTHORIZED, "Bad access token."); + response_code = HttpServletResponse.SC_UNAUTHORIZED; return; } @@ -100,17 +100,17 @@ public void doFilter(ServletRequest request, ServletResponse response, FilterCha request.setAttribute("user", user); chain.doFilter(request, response); } else { - httpResponse.sendError(httpResponse.SC_UNAUTHORIZED); - response_code = httpResponse.SC_UNAUTHORIZED; + httpResponse.sendError(HttpServletResponse.SC_UNAUTHORIZED); + response_code = HttpServletResponse.SC_UNAUTHORIZED; } } catch (org.codehaus.jackson.JsonParseException jpe) { log.error("JsonParseException. Failed to parse:" + json, jpe); - httpResponse.sendError(httpResponse.SC_INTERNAL_SERVER_ERROR); - response_code = httpResponse.SC_INTERNAL_SERVER_ERROR; + httpResponse.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); + response_code = HttpServletResponse.SC_INTERNAL_SERVER_ERROR; } catch (Exception e) { log.error("Exception: " + e, e); - httpResponse.sendError(httpResponse.SC_INTERNAL_SERVER_ERROR); - response_code = httpResponse.SC_INTERNAL_SERVER_ERROR; + httpResponse.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); + response_code = HttpServletResponse.SC_INTERNAL_SERVER_ERROR; e.printStackTrace(); } finally { if (response_code != 0) { From 14d2e1980560c976c16979adfb321122c77dea25 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Olov=20Ylinenp=C3=A4=C3=A4?= Date: Tue, 5 Nov 2024 23:22:58 +0100 Subject: [PATCH 3/3] refactor: move BulkChangePreviewAPI from rest to housekeeping --- housekeeping/src/main/groovy/whelk/HouseKeepingServer.java | 2 ++ .../main/groovy/whelk/housekeeping}/BulkChangePreviewAPI.java | 2 +- rest/src/main/java/whelk/RestServer.java | 3 --- 3 files changed, 3 insertions(+), 4 deletions(-) rename {rest/src/main/groovy/whelk/rest/api => housekeeping/src/main/groovy/whelk/housekeeping}/BulkChangePreviewAPI.java (99%) diff --git a/housekeeping/src/main/groovy/whelk/HouseKeepingServer.java b/housekeeping/src/main/groovy/whelk/HouseKeepingServer.java index 7d7c45080e..b8f43a6bfe 100644 --- a/housekeeping/src/main/groovy/whelk/HouseKeepingServer.java +++ b/housekeeping/src/main/groovy/whelk/HouseKeepingServer.java @@ -3,6 +3,7 @@ import org.eclipse.jetty.ee8.servlet.ServletContextHandler; import org.eclipse.jetty.ee8.servlet.ServletHolder; import org.eclipse.jetty.server.Server; +import whelk.housekeeping.BulkChangePreviewAPI; import whelk.housekeeping.WebInterface; public class HouseKeepingServer extends XlServer { @@ -16,6 +17,7 @@ protected void configureHandlers(Server server) { ServletHolder holder = new ServletHolder(WebInterface.class); holder.setInitOrder(0); context.addServlet(holder, "/"); + context.addServlet(BulkChangePreviewAPI.class, "/_bulk-change/*"); serveStaticContent(context); } diff --git a/rest/src/main/groovy/whelk/rest/api/BulkChangePreviewAPI.java b/housekeeping/src/main/groovy/whelk/housekeeping/BulkChangePreviewAPI.java similarity index 99% rename from rest/src/main/groovy/whelk/rest/api/BulkChangePreviewAPI.java rename to housekeeping/src/main/groovy/whelk/housekeeping/BulkChangePreviewAPI.java index f2e74b8d86..56d438f489 100644 --- a/rest/src/main/groovy/whelk/rest/api/BulkChangePreviewAPI.java +++ b/housekeeping/src/main/groovy/whelk/housekeeping/BulkChangePreviewAPI.java @@ -1,4 +1,4 @@ -package whelk.rest.api; +package whelk.housekeeping; import whelk.Document; import whelk.JsonLd; diff --git a/rest/src/main/java/whelk/RestServer.java b/rest/src/main/java/whelk/RestServer.java index 320c69d302..649dee5fef 100644 --- a/rest/src/main/java/whelk/RestServer.java +++ b/rest/src/main/java/whelk/RestServer.java @@ -10,7 +10,6 @@ import org.eclipse.jetty.rewrite.handler.RewriteHandler; import org.eclipse.jetty.server.HttpConnectionFactory; import org.eclipse.jetty.server.Server; -import whelk.rest.api.BulkChangePreviewAPI; import whelk.rest.api.ConverterAPI; import whelk.rest.api.Crud; import whelk.rest.api.DuplicatesAPI; @@ -102,8 +101,6 @@ protected void configureHandlers(Server server) { context.addServlet(RecordRelationAPI.class, "/_dependencies"); context.addServlet(DuplicatesAPI.class, "/_duplicates"); - context.addServlet(BulkChangePreviewAPI.class, "/_bulk-change/*"); - context.addServlet(se.kb.libris.digi.DigitalReproductionAPI.class, "/_reproduction"); serveStaticContent(context);