Skip to content

Commit

Permalink
Add new /reg endpoints service resource-caps to remove dependency on …
Browse files Browse the repository at this point in the history
…cadc ws
  • Loading branch information
stvoutsin committed Feb 19, 2025
1 parent fa11f64 commit 732c16a
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 0 deletions.
83 changes: 83 additions & 0 deletions tap/src/main/java/ca/nrc/cadc/sample/RubinRegistryServlet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package ca.nrc.cadc.sample;

import org.apache.log4j.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.BufferedReader;
import java.nio.charset.StandardCharsets;

/**
* A servlet that serves registry content from a webapp resource file.
*
* @author stvoutsin
*/
public class RubinRegistryServlet extends HttpServlet {

private static final Logger log = Logger.getLogger(RubinRegistryServlet.class);
private static final String DEFAULT_REGISTRY_FILE = "/resource-caps";

/**
* Read and return the content of the registry file from the webapp 'resource-caps' resource.
*
* @param request the HttpServletRequest object that contains the request
* @param response the HttpServletResponse object that contains the response
* @throws ServletException if an input or output error is detected when the servlet handles the GET request
* @throws IOException if the request for the GET could not be handled
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
String registryContent = getRegistryContent(request);

response.setContentType("text/plain");
response.setCharacterEncoding("UTF-8");

try (PrintWriter writer = response.getWriter()) {
writer.write(registryContent);
}
} catch (IOException e) {
log.error("Error serving registry content", e);
response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
"An error occurred while processing the request: " + e.getMessage());
}
}

/**
* Reads and returns the content of the registry file from webapp resources.
*
* @param request the HttpServletRequest
* @return the content of the registry file as a string
* @throws IOException if there's an error reading the file
*/
private String getRegistryContent(HttpServletRequest request) throws IOException {
String pathInfo = request.getPathInfo();
String resourcePath = DEFAULT_REGISTRY_FILE;

if (pathInfo != null && !pathInfo.isEmpty()) {
resourcePath = pathInfo;
}

InputStream inputStream = getServletContext().getResourceAsStream(resourcePath);
if (inputStream == null) {
throw new IOException("Registry file not found in webapp: " + resourcePath);
}

StringBuilder content = new StringBuilder();
try (BufferedReader reader = new BufferedReader(
new InputStreamReader(inputStream, StandardCharsets.UTF_8))) {
String line;
while ((line = reader.readLine()) != null) {
content.append(line).append("\n");
}
}

return content.toString();
}
}
10 changes: 10 additions & 0 deletions tap/src/main/webapp/WEB-INF/web.xml
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,16 @@
<url-pattern>/results/*</url-pattern>
</servlet-mapping>

<servlet>
<servlet-name>RubinRegistryServlet</servlet-name>
<servlet-class>ca.nrc.cadc.sample.RubinRegistryServlet</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>RubinRegistryServlet</servlet-name>
<url-pattern>/reg/*</url-pattern>
</servlet-mapping>

<servlet-mapping>
<servlet-name>AsyncServlet</servlet-name>
<url-pattern>/async/*</url-pattern>
Expand Down
6 changes: 6 additions & 0 deletions tap/src/main/webapp/resource-caps
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#
# This file maps resource identifiers to the location of capability
# information.
#


0 comments on commit 732c16a

Please sign in to comment.