mirror of
https://github.com/plan-player-analytics/Plan.git
synced 2025-02-17 21:01:22 +01:00
Restructured for easier reading
parent
6fc1e685e1
commit
005e484f3f
@ -59,8 +59,6 @@ Lets implement a new Resolver one method at a time.
|
||||
|
||||
### `Request`-object
|
||||
|
||||
`Request` object contains everything the HTTP request has, like Request headers, Request method, URI path (`/some/path/somewhere`), URI query (`?some=1&query=example`). In addition it contains Plan specific user information if present.
|
||||
|
||||
Here are the methods of Request:
|
||||
```java
|
||||
public final class Request {
|
||||
@ -75,6 +73,8 @@ public final class Request {
|
||||
|
||||
`URIPath`, `URIQuery` and `WebUser` have methods for reading different parts of the request.
|
||||
|
||||
`Request` object contains everything the HTTP request has, like Request headers, Request method, URI path (`/some/path/somewhere`), URI query (`?some=1&query=example`). In addition it contains Plan specific user information if present.
|
||||
|
||||
> 💡 **Tips**
|
||||
> - headers that can have multiple values (Like `Cookie`) are split with `;` character
|
||||
|
||||
@ -115,17 +115,6 @@ Optional<Response> resolve(Request request) {
|
||||
|
||||
This method should check if the user viewing the URL is allowed to do so.
|
||||
|
||||
> The WebUser permissions **are not Minecraft user permissions**. They are an upcoming feature of Plan and will be managed by the admins via Plan.
|
||||
>
|
||||
> You can check if this feature is available with Capability `PAGE_EXTENSION_USER_PERMISSIONS`
|
||||
>
|
||||
> Before the feature arrives, consider using existing page permissions:
|
||||
>
|
||||
> Permission level - permission
|
||||
> - 0 - page.server / page.network
|
||||
> - 1 - page.players
|
||||
> - 2 - page.player.self
|
||||
|
||||
Here is an example that only checks the user's permission regardless of the accessed URIPath or URIQuery.
|
||||
|
||||
```java
|
||||
@ -142,6 +131,17 @@ public class YourResolver implements Resolver {
|
||||
}
|
||||
```
|
||||
|
||||
> The WebUser permissions **are not Minecraft user permissions**. They are an upcoming feature of Plan and will be managed by the admins via Plan.
|
||||
>
|
||||
> You can check if this feature is available with Capability `PAGE_EXTENSION_USER_PERMISSIONS`
|
||||
>
|
||||
> Before the feature arrives, consider using existing page permissions:
|
||||
>
|
||||
> Permission level - permission
|
||||
> - 0 - page.server / page.network
|
||||
> - 1 - page.players
|
||||
> - 2 - page.player.self
|
||||
|
||||
Rest of the `Request` methods can be used for more granular access control.
|
||||
|
||||
> 📢 **Don't return `false` unless a 403 Forbidden should be shown**
|
||||
@ -154,8 +154,6 @@ Rest of the `Request` methods can be used for more granular access control.
|
||||
|
||||
This method links `Request`s to appropriate `Response` objects.
|
||||
|
||||
You can use `newResponseBuilder()` to begin building a new `Response`. You can learn more about the builder below.
|
||||
|
||||
```java
|
||||
public class YourResolver implements Resolver {
|
||||
@Override
|
||||
@ -169,6 +167,8 @@ public class YourResolver implements Resolver {
|
||||
}
|
||||
```
|
||||
|
||||
You can use `newResponseBuilder()` to begin building a new `Response`. You can learn more about the builder below.
|
||||
|
||||
Following Exceptions can be thrown for automated responses:
|
||||
- `NotFoundException` for a 404 not found page
|
||||
- `BadRequestException` for a 400 bad request `{"status": 400, "error": "message"}` response
|
||||
@ -202,21 +202,6 @@ public class ResponseBuilder {
|
||||
- Response status code is `200` (OK) by default
|
||||
- It is recommended to `setMimeType` before setting content.
|
||||
|
||||
### Building
|
||||
|
||||
Following qualities are **required**:
|
||||
|
||||
- Response status code must be between 100 (exclusive) and 600 (inclusive)
|
||||
- Content must be defined with one of `setContent` or `setJSONContent` methods.
|
||||
- MIME Type must be set with `setMimeType` if Content is not 0 bytes
|
||||
|
||||
If these qualities are not met, an `InvalidResponseException` is thrown on `ResponseBuilder#build`.
|
||||
|
||||
Some methods of the builder set mimetype and content automatically:
|
||||
- `redirectTo`
|
||||
- `setJSONContent`
|
||||
- `setContent(String)` adds `"; charset=utf-8"` to the MimeType if mimetype was set before the call.
|
||||
|
||||
### Examples
|
||||
|
||||
```java
|
||||
@ -261,6 +246,21 @@ Response.builder()
|
||||
Response.builder().redirectTo(location).build();
|
||||
```
|
||||
|
||||
### Building
|
||||
|
||||
Following qualities are **required**:
|
||||
|
||||
- Response status code must be between 100 (exclusive) and 600 (inclusive)
|
||||
- Content must be defined with one of `setContent` or `setJSONContent` methods.
|
||||
- MIME Type must be set with `setMimeType` if Content is not 0 bytes
|
||||
|
||||
If these qualities are not met, an `InvalidResponseException` is thrown on `ResponseBuilder#build`.
|
||||
|
||||
Some methods of the builder set mimetype and content automatically:
|
||||
- `redirectTo`
|
||||
- `setJSONContent`
|
||||
- `setContent(String)` adds `"; charset=utf-8"` to the MimeType if mimetype was set before the call.
|
||||
|
||||
> 💡 **Tips**
|
||||
> - Many Mime types are already defined in [`MimeType`-class](https://github.com/plan-player-analytics/Plan/blob/master/Plan/api/src/main/java/com/djrapitops/plan/delivery/web/resolver/MimeType.java#L19)
|
||||
> - You can find more example uses in [`ResponseFactory`-class](https://github.com/plan-player-analytics/Plan/blob/master/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/ResponseFactory.java#L56)
|
||||
@ -336,12 +336,6 @@ public interface WebResource {
|
||||
|
||||
## Adding javascript to .html resource
|
||||
|
||||
`<script src="./js"></script>` snippets can be added to 3 `Position`s in a .html file:
|
||||
|
||||
- `PRE_CONTENT` - to the `<head>`, loaded before content
|
||||
- `PRE_MAIN_SCRIPT` - before `<script id="mainScript">` (Recommended for libraries and page stucture modifications)
|
||||
- `AFTER_MAIN_SCRIPT` - at the end of the page before `</body>` tag (Recommended for data loading)
|
||||
|
||||
This can be done with the following call:
|
||||
|
||||
```java
|
||||
@ -349,17 +343,17 @@ ResourceService svc = ResourceService.getInstance();
|
||||
svc.addScriptsToResource("PluginName", "customized.html", Position.PRE_CONTENT, "./some.js", "another.js");
|
||||
```
|
||||
|
||||
`<script src="./js"></script>` snippets can be added to 3 `Position`s in a .html file:
|
||||
|
||||
- `PRE_CONTENT` - to the `<head>`, loaded before content
|
||||
- `PRE_MAIN_SCRIPT` - before `<script id="mainScript">` (Recommended for libraries and page stucture modifications)
|
||||
- `AFTER_MAIN_SCRIPT` - at the end of the page before `</body>` tag (Recommended for data loading)
|
||||
|
||||
You can add a query to the URL if your javascript needs parameters, eg `"./some.js?value=some"` and access them with `URIQuery` in `Resolver#resolve`.
|
||||
Some Plan resources use `${placeholder}` replacement - in these cases you can use `"./some.js?player=${playerUUID}"` for example.
|
||||
|
||||
## Adding stylesheet to .html resource
|
||||
|
||||
`<link href="./css" rel="stylesheet"></link>` snippets can be added to 3 `Position`s in a .html file:
|
||||
|
||||
- `PRE_CONTENT` - to the `<head>`, loaded before content
|
||||
- `PRE_MAIN_SCRIPT` - before `<script id="mainScript">` (Recommended for libraries)
|
||||
- `AFTER_MAIN_SCRIPT` - at the end of the page before `</body>` tag
|
||||
|
||||
This can be done with the following call:
|
||||
|
||||
```java
|
||||
@ -367,6 +361,12 @@ ResourceService svc = ResourceService.getInstance();
|
||||
svc.addStylesToResource("PluginName", "customized.html", Position.PRE_MAIN_SCRIPT, "./some.css", "another.css");
|
||||
```
|
||||
|
||||
`<link href="./css" rel="stylesheet"></link>` snippets can be added to 3 `Position`s in a .html file:
|
||||
|
||||
- `PRE_CONTENT` - to the `<head>`, loaded before content
|
||||
- `PRE_MAIN_SCRIPT` - before `<script id="mainScript">` (Recommended for libraries)
|
||||
- `AFTER_MAIN_SCRIPT` - at the end of the page before `</body>` tag
|
||||
|
||||
----
|
||||
|
||||
The PageExtension API will be expanded with javascript methods that allow easier modification of the site and interaction with the Plan webserver. At the moment only following method is quaranteed at position `PRE_MAIN_SCRIPT`:
|
||||
|
Loading…
Reference in New Issue
Block a user