Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

minor Entity tweak for use in caom2; fix cadc-rest response for unsupported http action #254

Merged
merged 2 commits into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cadc-rest/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
33 changes: 22 additions & 11 deletions cadc-rest/src/main/java/ca/nrc/cadc/rest/RestServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public class RestServlet extends HttpServlet {

private static final Map<URI,String> SEC_METHOD_CHALLENGES = new TreeMap<>();

private static final List<String> CITEMS = new ArrayList<String>();
private static final List<String> CITEMS = new ArrayList<>();

static final String AUGMENT_SUBJECT_PARAM = "augmentSubject";
static final String AUTH_HEADERS_PARAM = "authHeaders";
Expand Down Expand Up @@ -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("/", "-");
Expand All @@ -166,14 +169,14 @@ public void init(ServletConfig config) throws ServletException {
}

// application specific config
for (String name : new Enumerator<String>(config.getInitParameterNames())) {
for (String name : new Enumerator<>(config.getInitParameterNames())) {
if (!CITEMS.contains(name)) {
initParams.put(name, config.getInitParameter(name));
}
}

try {
Class<InitAction> initActionClass = loadAction(config, "init");
Class<InitAction> initActionClass = loadAction(config, "init", false);
if (initActionClass != null) {
initAction = initActionClass.getDeclaredConstructor().newInstance();
initAction.setServletContext(getServletContext());
Expand All @@ -199,12 +202,19 @@ public void destroy() {
}
}

private <T> Class<T> loadAction(ServletConfig config, String method) {
private <T> Class<T> loadAction(ServletConfig config, String method, boolean httpAction) {
String cname = config.getInitParameter(method);
if (cname != null) {
try {
Class<T> ret = (Class<T>) 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);
Expand All @@ -224,7 +234,8 @@ private <T> Class<T> 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);
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion cadc-util/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
4 changes: 2 additions & 2 deletions cadc-util/src/main/java/org/opencadc/persist/Entity.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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) {
Expand Down