X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;ds=sidebyside;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fweb%2Fpage%2FPage.java;h=dd54f41b035d7a44697aeb7ae3aeea4b12a05679;hb=0ac0337b61614cf23833f866fd8f0568d0f7bf58;hp=a1125cc6a79f5d48662fb312de715a0f04c8b8ab;hpb=b2bc9337f91c303e2700f001c785e8b14a9990ec;p=Sone.git
diff --git a/src/main/java/net/pterodactylus/sone/web/page/Page.java b/src/main/java/net/pterodactylus/sone/web/page/Page.java
index a1125cc..dd54f41 100644
--- a/src/main/java/net/pterodactylus/sone/web/page/Page.java
+++ b/src/main/java/net/pterodactylus/sone/web/page/Page.java
@@ -30,21 +30,21 @@ import freenet.support.api.HTTPRequest;
/**
* A page is responsible for handling HTTP requests and creating appropriate
* responses.
- *
+ *
* @author David âBombeâ Roden
*/
public interface Page {
/**
* Returns the path of this toadlet.
- *
+ *
* @return The path of this toadlet
*/
public String getPath();
/**
* Handles a request.
- *
+ *
* @param request
* The request to handle
* @return The response
@@ -53,16 +53,47 @@ public interface Page {
/**
* Container for request data.
- *
+ *
* @author David âBombeâ Roden
*/
public class Request {
+ /**
+ * Enumeration for all possible HTTP request methods.
+ *
+ * @author David âBombeâ
+ * Roden
+ */
+ public enum Method {
+
+ /** GET. */
+ GET,
+
+ /** POST. */
+ POST,
+
+ /** PUT. */
+ PUT,
+
+ /** DELETE. */
+ DELETE,
+
+ /** HEAD. */
+ HEAD,
+
+ /** OPTIONS. */
+ OPTIONS,
+
+ /** TRACE. */
+ TRACE,
+
+ }
+
/** The URI that was accessed. */
private final URI uri;
/** The HTTP method that was used. */
- private final String method;
+ private final Method method;
/** The HTTP request. */
private final HTTPRequest httpRequest;
@@ -72,7 +103,7 @@ public interface Page {
/**
* Creates a new request that holds the given data.
- *
+ *
* @param uri
* The URI of the request
* @param method
@@ -82,7 +113,7 @@ public interface Page {
* @param toadletContext
* The toadlet context of the request
*/
- public Request(URI uri, String method, HTTPRequest httpRequest, ToadletContext toadletContext) {
+ public Request(URI uri, Method method, HTTPRequest httpRequest, ToadletContext toadletContext) {
this.uri = uri;
this.method = method;
this.httpRequest = httpRequest;
@@ -91,25 +122,25 @@ public interface Page {
/**
* Returns the URI that was accessed.
- *
+ *
* @return The accessed URI
*/
- public URI getURI() {
+ public URI getUri() {
return uri;
}
/**
* Returns the HTTP method that was used to access the page.
- *
+ *
* @return The HTTP method
*/
- public String getMethod() {
+ public Method getMethod() {
return method;
}
/**
* Returns the HTTP request.
- *
+ *
* @return The HTTP request
*/
public HTTPRequest getHttpRequest() {
@@ -118,7 +149,7 @@ public interface Page {
/**
* Returns the toadlet context.
- *
+ *
* @return The toadlet context
*/
public ToadletContext getToadletContext() {
@@ -129,7 +160,7 @@ public interface Page {
/**
* Container for the HTTP response of a {@link Page}.
- *
+ *
* @author David âBombeâ Roden
*/
public class Response {
@@ -151,7 +182,7 @@ public interface Page {
/**
* Creates a new response.
- *
+ *
* @param statusCode
* The HTTP status code of the response
* @param statusText
@@ -167,7 +198,7 @@ public interface Page {
/**
* Creates a new response.
- *
+ *
* @param statusCode
* The HTTP status code of the response
* @param statusText
@@ -178,12 +209,12 @@ public interface Page {
* The content of the reponse body
*/
public Response(int statusCode, String statusText, String contentType, byte[] content) {
- this(statusCode, statusText, contentType, null, content);
+ this(statusCode, statusText, contentType, new HashMap(), content);
}
/**
* Creates a new response.
- *
+ *
* @param statusCode
* The HTTP status code of the response
* @param statusText
@@ -199,7 +230,7 @@ public interface Page {
/**
* Creates a new response.
- *
+ *
* @param statusCode
* The HTTP status code of the response
* @param statusText
@@ -217,7 +248,7 @@ public interface Page {
/**
* Creates a new response.
- *
+ *
* @param statusCode
* The HTTP status code of the response
* @param statusText
@@ -239,7 +270,7 @@ public interface Page {
/**
* Returns the HTTP status code of the response.
- *
+ *
* @return The HTTP status code
*/
public int getStatusCode() {
@@ -248,7 +279,7 @@ public interface Page {
/**
* Returns the HTTP status text.
- *
+ *
* @return The HTTP status text
*/
public String getStatusText() {
@@ -257,7 +288,7 @@ public interface Page {
/**
* Returns the content type of the response.
- *
+ *
* @return The content type of the reponse
*/
public String getContentType() {
@@ -267,7 +298,7 @@ public interface Page {
/**
* Returns HTTP headers of the response. May be {@code null} if no
* headers are returned.
- *
+ *
* @return The response headers, or {@code null} if there are no
* response headers
*/
@@ -276,9 +307,24 @@ public interface Page {
}
/**
+ * Sets the HTTP header with the given name to the given value. Multiple
+ * headers with the same name are not implemented so that latest call to
+ * {@link #setHeader(String, String)} determines what is sent to the
+ * browser.
+ *
+ * @param name
+ * The name of the header
+ * @param value
+ * The value of the header
+ */
+ public void setHeader(String name, String value) {
+ headers.put(name, value);
+ }
+
+ /**
* Returns the content of the response body. May be {@code null} if the
* response does not have a body.
- *
+ *
* @return The content of the response body
*/
public InputStream getContent() {
@@ -291,7 +337,7 @@ public interface Page {
/**
* Returns the UTF-8 representation of the given text.
- *
+ *
* @param text
* The text to encode
* @return The encoded text
@@ -307,7 +353,7 @@ public interface Page {
/**
* Creates a header map containing a single header.
- *
+ *
* @param name
* The name of the header
* @param value
@@ -324,14 +370,14 @@ public interface Page {
/**
* {@link Response} implementation that performs an HTTP redirect.
- *
+ *
* @author David âBombeâ Roden
*/
public class RedirectResponse extends Response {
/**
* Creates a new redirect response to the new location.
- *
+ *
* @param newLocation
* The new location
*/
@@ -341,7 +387,7 @@ public interface Page {
/**
* Creates a new redirect response to the new location.
- *
+ *
* @param newLocation
* The new location
* @param permanent