From 51d96710a46c460f50d3ac7b4d15ff4e3ff643fe Mon Sep 17 00:00:00 2001 From: Patrick Dowler Date: Sun, 10 Nov 2024 18:09:12 -0800 Subject: [PATCH 1/2] make Entity.MessageDigestWrapper public for use by caom2 --- cadc-util/build.gradle | 2 +- cadc-util/src/main/java/org/opencadc/persist/Entity.java | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/cadc-util/build.gradle b/cadc-util/build.gradle index 8b445b0a..5f8589ce 100644 --- a/cadc-util/build.gradle +++ b/cadc-util/build.gradle @@ -16,7 +16,7 @@ sourceCompatibility = 1.8 group = 'org.opencadc' -version = '1.11.5' +version = '1.11.6' description = 'OpenCADC core utility library' def git_url = 'https://github.com/opencadc/core' diff --git a/cadc-util/src/main/java/org/opencadc/persist/Entity.java b/cadc-util/src/main/java/org/opencadc/persist/Entity.java index 1034da4e..42152fef 100644 --- a/cadc-util/src/main/java/org/opencadc/persist/Entity.java +++ b/cadc-util/src/main/java/org/opencadc/persist/Entity.java @@ -374,7 +374,7 @@ protected final void calcMetaChecksum(Class c, Object o, MessageDigestWrapper di } } - private static class MessageDigestWrapper { + public static class MessageDigestWrapper { private MessageDigest digest; private int numBytes = 0; @@ -473,7 +473,7 @@ public static boolean isChildCollection(Field f) throws IllegalAccessException { } return false; } - + protected byte[] primitiveValueToBytes(Object o, String name) { byte[] ret = null; if (o instanceof Byte) { From ea6f36c37a8ba8b97b2de0b50affed207a784b5b Mon Sep 17 00:00:00 2001 From: Patrick Dowler Date: Fri, 29 Nov 2024 13:23:24 -0800 Subject: [PATCH 2/2] use 405 response code for unconfigured http actions add http allow header in 405 response --- cadc-rest/build.gradle | 2 +- .../java/ca/nrc/cadc/rest/RestServlet.java | 33 ++++++++++++------- 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/cadc-rest/build.gradle b/cadc-rest/build.gradle index 2cafe774..bff5893f 100644 --- a/cadc-rest/build.gradle +++ b/cadc-rest/build.gradle @@ -16,7 +16,7 @@ sourceCompatibility = 1.8 group = 'org.opencadc' -version = '1.4.0' +version = '1.4.1' description = 'OpenCADC REST server library' def git_url = 'https://github.com/opencadc/core' diff --git a/cadc-rest/src/main/java/ca/nrc/cadc/rest/RestServlet.java b/cadc-rest/src/main/java/ca/nrc/cadc/rest/RestServlet.java index 30113e9e..9f62bb76 100644 --- a/cadc-rest/src/main/java/ca/nrc/cadc/rest/RestServlet.java +++ b/cadc-rest/src/main/java/ca/nrc/cadc/rest/RestServlet.java @@ -111,7 +111,7 @@ public class RestServlet extends HttpServlet { private static final Map SEC_METHOD_CHALLENGES = new TreeMap<>(); - private static final List CITEMS = new ArrayList(); + private static final List CITEMS = new ArrayList<>(); static final String AUGMENT_SUBJECT_PARAM = "augmentSubject"; static final String AUTH_HEADERS_PARAM = "authHeaders"; @@ -143,15 +143,18 @@ public class RestServlet extends HttpServlet { protected String componentID; protected boolean augmentSubject = true; protected boolean setAuthHeaders = true; + + private String allowHeaderValue; @Override public void init(ServletConfig config) throws ServletException { super.init(config); - this.getAction = loadAction(config, "get"); - this.postAction = loadAction(config, "post"); - this.putAction = loadAction(config, "put"); - this.deleteAction = loadAction(config, "delete"); - this.headAction = loadAction(config, "head"); + this.getAction = loadAction(config, "get", true); + this.postAction = loadAction(config, "post", true); + this.putAction = loadAction(config, "put", true); + this.deleteAction = loadAction(config, "delete", true); + this.headAction = loadAction(config, "head", true); + StringBuilder sb = new StringBuilder(); // appName: war file foo#bar.war, context path /foo/bar -> foo-bar this.appName = config.getServletContext().getContextPath().substring(1).replaceAll("/", "-"); @@ -166,14 +169,14 @@ public void init(ServletConfig config) throws ServletException { } // application specific config - for (String name : new Enumerator(config.getInitParameterNames())) { + for (String name : new Enumerator<>(config.getInitParameterNames())) { if (!CITEMS.contains(name)) { initParams.put(name, config.getInitParameter(name)); } } try { - Class initActionClass = loadAction(config, "init"); + Class initActionClass = loadAction(config, "init", false); if (initActionClass != null) { initAction = initActionClass.getDeclaredConstructor().newInstance(); initAction.setServletContext(getServletContext()); @@ -199,12 +202,19 @@ public void destroy() { } } - private Class loadAction(ServletConfig config, String method) { + private Class loadAction(ServletConfig config, String method, boolean httpAction) { String cname = config.getInitParameter(method); if (cname != null) { try { Class ret = (Class) Class.forName(cname); log.info(method + ": " + cname + " [loaded]"); + if (httpAction) { + if (allowHeaderValue == null) { + allowHeaderValue = method.toUpperCase(); + } else { + allowHeaderValue += ", " + method.toUpperCase(); + } + } return ret; } catch (Exception ex) { log.error(method + ": " + cname + " [FAILED]", ex); @@ -224,7 +234,8 @@ private Class loadAction(ServletConfig config, String method) { */ protected void handleUnsupportedAction(String action, HttpServletResponse response) throws IOException { - response.setStatus(400); + response.setStatus(405); + response.setHeader("Allow", allowHeaderValue); response.setHeader("Content-Type", "text/plain"); PrintWriter w = response.getWriter(); w.println("unsupported: HTTP " + action); @@ -311,7 +322,7 @@ protected void doit(HttpServletRequest request, HttpServletResponse response, C nae = ex; } - RestAction action = actionClass.newInstance(); + RestAction action = actionClass.getDeclaredConstructor().newInstance(); action.setServletContext(getServletContext()); action.setAppName(appName); action.setComponentID(componentID);