Skip to content

Commit

Permalink
Use redirect servlet handler for results of a TAP job
Browse files Browse the repository at this point in the history
  • Loading branch information
stvoutsin committed Jun 7, 2024
1 parent 8e141c8 commit 0592073
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 3 deletions.
14 changes: 11 additions & 3 deletions tap/src/main/java/ca/nrc/cadc/sample/ResultStoreImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,11 @@

public class ResultStoreImpl implements ResultStore {
private String filename;
private String jobID;
private static final String bucket = System.getProperty("gcs_bucket");
private static final String bucketURL = System.getProperty("gcs_bucket_url");

private static final String baseURL = System.getProperty("base_url");
private static final String pathPrefix = System.getProperty("path_prefix");

@Override
public URL put(final ResultSet resultSet,
Expand Down Expand Up @@ -135,14 +137,14 @@ public URL put(final ResultSet resultSet,
private OutputStream getOutputStream() {
Storage storage = StorageOptions.getDefaultInstance().getService();
BlobId blobId = BlobId.of(bucket, filename);

BlobInfo blobInfo = BlobInfo.newBuilder(blobId).setContentType("application/x-votable+xml").build();
Blob blob = storage.create(blobInfo);
return Channels.newOutputStream(blob.writer());
}

private URL getURL() throws MalformedURLException {
URL bucket = new URL(bucketURL);
return new URL(bucket, filename);
return new URL(baseURL + pathPrefix + "/results/" + filename);
}

@Override
Expand All @@ -157,4 +159,10 @@ public void setJob(Job _job) {
public void setFilename(String filename) {
this.filename = filename;
}

public void setJobID(String jobID) {
this.jobID = jobID;
}


}
37 changes: 37 additions & 0 deletions tap/src/main/java/ca/nrc/cadc/sample/ResultsServlet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
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;

/**
* A servlet that handles redirecting to specific job results.
* This servlet extracts the VOTable file name from the request path and constructs a URL to redirect the client.
*
* @author stvoutsin
*/
public class ResultsServlet extends HttpServlet {
private static final Logger log = Logger.getLogger(ResultsServlet.class);
private static final String bucketURL = System.getProperty("gcs_bucket_url");

/**
* Processes GET requests by extracting the result filename from the request path and redirecting to the corresponding results URL.
* The filename is assumed to be the path info of the request URL, following the first '/' character.
*
* @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 {
String path = request.getPathInfo();
String resultsFile = path.substring(1);
String redirectUrl = bucketURL + "/" + resultsFile;
log.debug("Redirect URL: " + redirectUrl);
response.sendRedirect(redirectUrl);
}
}
11 changes: 11 additions & 0 deletions tap/src/main/webapp/WEB-INF/web.xml
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,17 @@
<servlet-name>SyncServlet</servlet-name>
<url-pattern>/sync/*</url-pattern>
</servlet-mapping>

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

<servlet-mapping>
<servlet-name>ResultsServlet</servlet-name>
<url-pattern>/results/*</url-pattern>
</servlet-mapping>

<servlet-mapping>
<servlet-name>AsyncServlet</servlet-name>
<url-pattern>/async/*</url-pattern>
Expand Down

0 comments on commit 0592073

Please sign in to comment.