Example of a basic authentication servlet
The following Gosu servlet runs in the context of the PolicyCenter application, and uses HTTP basic authentication. This example servlet responds to URL substrings that start with the string /test. If an incoming URL matches that pattern, the servlet displays the PathInfo property of the response object, which contains the path. This servlet also sends debug messages to the Studio console when you run the Quickstart server.
package mycompany.test.servlets
uses gw.servlet.Servlet
uses gw.servlet.AbstractBasicAuthenticationServlet
uses javax.servlet.http.HttpServletRequest
uses javax.servlet.http.HttpServletResponse
@Servlet( \ path : String ->path.matches("/test(/.*)?"))
class TestingServlet extends gw.servlet.AbstractBasicAuthenticationServlet {
override function doGet(req: HttpServletRequest, resp : HttpServletResponse) {
print("servlet test url: " + req.RequestURI)
print("query string: " + req.QueryString)
resp.ContentType = "text/plain"
resp.setStatus(HttpServletResponse.SC_OK)
resp.Writer.append("I am the page " + req.PathInfo)
}
override function isAuthenticationRequired( req: HttpServletRequest ) : boolean {
// -- TODO -------------
// Read the headers and return false if user is already authenticated
// ---------------------
return true
}
}
This servlet responds to URLs with the word test in the service query path, such as the URL:
http://localhost:8180/pc/service/test/is/this/working