-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
137 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package org.opencadc.tap.impl; | ||
|
||
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 { | ||
try { | ||
String path = request.getPathInfo(); | ||
String redirectUrl = generateRedirectUrl(bucketURL, path); | ||
response.sendRedirect(redirectUrl); | ||
} catch (Exception e) { | ||
response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "An error occurred while processing the request."); | ||
} | ||
} | ||
|
||
/** | ||
* Generates the redirect URL based on a path. | ||
* | ||
* @param path the request path | ||
* @return the redirect URL constructed using the bucket URL and results file | ||
*/ | ||
private String generateRedirectUrl(String bucketUrlString, String path) { | ||
String resultsFile = path.substring(1); | ||
return bucketUrlString + "/" + resultsFile; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
src/test/java/org/opencadc/tap/impl/ResultsServletTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package org.opencadc.tap.impl; | ||
|
||
import ca.nrc.cadc.tap.schema.ColumnDesc; | ||
import ca.nrc.cadc.tap.schema.SchemaDesc; | ||
import ca.nrc.cadc.tap.schema.TableDesc; | ||
import ca.nrc.cadc.tap.schema.TapDataType; | ||
import ca.nrc.cadc.tap.schema.TapSchema; | ||
import ca.nrc.cadc.util.Log4jInit; | ||
import ca.nrc.cadc.uws.Job; | ||
import ca.nrc.cadc.uws.Parameter; | ||
import org.apache.log4j.Level; | ||
import org.apache.log4j.Logger; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
import static org.junit.Assert.assertEquals; | ||
import java.lang.reflect.Method; | ||
|
||
public class ResultsServletTest { | ||
|
||
private static final Logger log = Logger.getLogger(ResultsServletTest.class); | ||
|
||
static | ||
{ | ||
Log4jInit.setLevel("org.opencadc.tap.impl", Level.INFO); | ||
|
||
} | ||
|
||
Job job = new Job() | ||
{ | ||
@Override | ||
public String getID() { return "testJob"; } | ||
}; | ||
|
||
public ResultsServletTest() { } | ||
|
||
@Test | ||
public void testGenerateRedirectUrl() throws Exception { | ||
String bucketUrl = "https://tap-files.lsst.codes"; | ||
String expectedUrl = "https://tap-files.lsst.codes/result_qz4z5hf6qy5509p1.xml"; | ||
ResultsServlet resultsServlet = new ResultsServlet(); | ||
resultsServlet.init(); | ||
String path = "/result_qz4z5hf6qy5509p1.xml"; | ||
|
||
Method method = ResultsServlet.class.getDeclaredMethod("generateRedirectUrl", String.class, String.class); | ||
method.setAccessible(true); | ||
String actualUrl = (String) method.invoke(resultsServlet, bucketUrl, path); | ||
|
||
assertEquals(expectedUrl, actualUrl); | ||
} | ||
|
||
@Test | ||
public void testGenerateRedirectUrlWithBucket() throws Exception { | ||
String bucketUrl = "https://tap-files.lsst.codes"; | ||
String expectedUrl = "https://tap-files.lsst.codes/bucket12345/result_qz4z5hf6qy5509p1.xml"; | ||
ResultsServlet resultsServlet = new ResultsServlet(); | ||
resultsServlet.init(); | ||
String path = "/bucket12345/result_qz4z5hf6qy5509p1.xml"; | ||
|
||
Method method = ResultsServlet.class.getDeclaredMethod("generateRedirectUrl", String.class, String.class); | ||
method.setAccessible(true); | ||
String actualUrl = (String) method.invoke(resultsServlet, bucketUrl, path); | ||
|
||
assertEquals(expectedUrl, actualUrl); | ||
} | ||
|
||
} |