Package gnu.kawa.servlet
Class HttpRequestContext
- java.lang.Object
-
- gnu.kawa.servlet.HttpRequestContext
-
- Direct Known Subclasses:
KawaHttpHandler.Context
,KawaServlet.Context
public abstract class HttpRequestContext extends Object
A representation of an http request as it is being handled. It abstracts over different http server's API - specially, there are are concrete implementations on top of JDK6's com.sun.net.httpserver, javax.servlet.http, and CGI (on top of servlets).
-
-
Field Summary
Fields Modifier and Type Field Description static int
HTTP_NOT_FOUND
static int
HTTP_OK
static int
importServletDefinitions
This is a bit of a kludge, to import servlet functions into Scheme.protected static ThreadLocal<HttpRequestContext>
instance
int
statusCode
String
statusReasonPhrase
-
Constructor Summary
Constructors Constructor Description HttpRequestContext()
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description abstract Object
getAttribute(String name)
Get attribute from the server context.ServletPrinter
getConsumer()
abstract String
getContextPath()
Returns the context path, relative to the server root.static HttpRequestContext
getInstance()
static HttpRequestContext
getInstance(String command)
InetAddress
getLocalHost()
String
getLocalIPAddress()
String
getLocalPath()
Returns the remainder of the request path, relative to the script.abstract int
getLocalPort()
InetSocketAddress
getLocalSocketAddress()
abstract String
getPathTranslated()
abstract String
getQueryString()
abstract InetAddress
getRemoteHost()
abstract String
getRemoteIPAddress()
abstract int
getRemotePort()
InetSocketAddress
getRemoteSocketAddress()
String
getRequestBodyChars()
abstract String
getRequestHeader(String name)
abstract Map<String,List<String>>
getRequestHeaders()
abstract List<String>
getRequestHeaders(String name)
abstract String
getRequestMethod()
String
getRequestParameter(String name)
abstract Map<String,List<String>>
getRequestParameters()
String
getRequestPath()
gnu.kawa.io.InPort
getRequestPort()
String
getRequestScheme()
abstract InputStream
getRequestStream()
abstract URI
getRequestURI()
StringBuffer
getRequestURLBuffer()
abstract URL
getResourceURL(String path)
Returns the URL of a resource.abstract OutputStream
getResponseStream()
Return an OutputStream for the result body.String
getScriptPath()
Returns the path of the script, relative to the context.static void
handleStaticFile(HttpRequestContext hctx, gnu.kawa.io.Path absPath)
abstract void
log(String message)
abstract void
log(String message, Throwable ex)
protected String
normalizeToContext(String path)
abstract boolean
reset(boolean headersAlso)
Try to reset (delete) any response generated so far.void
sendNotFound(String path)
abstract void
sendResponseHeaders(int reasonCode, String reasonPhrase, long responseLength)
Send headers.abstract void
setAttribute(String name, Object value)
Set attribute in the server context.void
setContentType(String type)
static void
setInstance(HttpRequestContext ctx)
abstract void
setResponseHeader(String name, String value)
void
setScriptAndLocalPath(String scriptPath, String localPath)
-
-
-
Field Detail
-
HTTP_OK
public static final int HTTP_OK
- See Also:
- Constant Field Values
-
HTTP_NOT_FOUND
public static final int HTTP_NOT_FOUND
- See Also:
- Constant Field Values
-
statusCode
public int statusCode
-
statusReasonPhrase
public String statusReasonPhrase
-
importServletDefinitions
public static int importServletDefinitions
This is a bit of a kludge, to import servlet functions into Scheme. (The kludge is that we really shouldn't be using a static.) If importServletDefinitions == 1, then we're running in (or compiling for) a web server context; if it is 2 we specifically support servlets.
-
instance
protected static final ThreadLocal<HttpRequestContext> instance
-
-
Method Detail
-
getInstance
public static HttpRequestContext getInstance()
-
getInstance
public static HttpRequestContext getInstance(String command)
-
setInstance
public static void setInstance(HttpRequestContext ctx)
-
getRequestStream
public abstract InputStream getRequestStream()
-
getRequestPort
public gnu.kawa.io.InPort getRequestPort()
-
getRequestBodyChars
public String getRequestBodyChars() throws IOException
- Throws:
IOException
-
getResponseStream
public abstract OutputStream getResponseStream()
Return an OutputStream for the result body. Multiple calls will return the same OutputStream.
-
getConsumer
public ServletPrinter getConsumer() throws IOException
- Throws:
IOException
-
reset
public abstract boolean reset(boolean headersAlso)
Try to reset (delete) any response generated so far.- Parameters:
headersAlso
- if response headers should also be reset.- Returns:
- true on success, false if it's too late.
-
getRequestURI
public abstract URI getRequestURI()
-
getContextPath
public abstract String getContextPath()
Returns the context path, relative to the server root. This is an initial substring of thegetRequestPath()
. LikeServletContext#getContextPath
, but ends with a'/'
. The stringgetRequestURI()
is the same as the concatenation ofgetContextPath()
,getScriptPath()
, andgetLocalPath()
.
-
getScriptPath
public String getScriptPath()
Returns the path of the script, relative to the context. LikeServletRequestt#getServletPath
, but ends with a'/'
, and does not start with one. (The reason for this is to produce URIs that work better with operations like resolve-uri.)
-
getLocalPath
public String getLocalPath()
Returns the remainder of the request path, relative to the script.
-
getPathTranslated
public abstract String getPathTranslated()
-
getRequestPath
public String getRequestPath()
-
getRequestScheme
public String getRequestScheme()
-
getLocalSocketAddress
public InetSocketAddress getLocalSocketAddress()
-
getLocalIPAddress
public String getLocalIPAddress()
-
getLocalHost
public InetAddress getLocalHost()
-
getLocalPort
public abstract int getLocalPort()
-
getRemoteSocketAddress
public InetSocketAddress getRemoteSocketAddress()
-
getRemoteHost
public abstract InetAddress getRemoteHost()
-
getRemoteIPAddress
public abstract String getRemoteIPAddress()
-
getRemotePort
public abstract int getRemotePort()
-
getRequestURLBuffer
public StringBuffer getRequestURLBuffer()
-
getQueryString
public abstract String getQueryString()
-
getRequestMethod
public abstract String getRequestMethod()
-
setContentType
public void setContentType(String type)
-
getResourceURL
public abstract URL getResourceURL(String path)
Returns the URL of a resource. The resource is relative to the script path, if the path is relative; otherwise (if it starts with a'/'
it is relative to the context path.
-
getAttribute
public abstract Object getAttribute(String name)
Get attribute from the server context.
-
setAttribute
public abstract void setAttribute(String name, Object value)
Set attribute in the server context.
-
sendResponseHeaders
public abstract void sendResponseHeaders(int reasonCode, String reasonPhrase, long responseLength) throws IOException
Send headers.- Parameters:
reasonCode
- response code - e.g. 200 for OK.reasonPhrase
- response string - e.g. "OK" or "Not Found".responseLength
- response length in bytes, or -1 (unspecified). Note this is different from HttpExchange.sendResponseHeaders. This method must be called before getResponseStream. Implementations should set statusCode to STATUS_SENT.- Throws:
IOException
-
sendNotFound
public void sendNotFound(String path) throws IOException
- Throws:
IOException
-
log
public abstract void log(String message)
-
handleStaticFile
public static void handleStaticFile(HttpRequestContext hctx, gnu.kawa.io.Path absPath) throws IOException
- Throws:
IOException
-
-