Repository navigation
Expand file tree
/
Copy pathExchange.java
More file actions
181 lines (170 loc) · 5.55 KB
/
Copy pathExchange.java
File metadata and controls
181 lines (170 loc) · 5.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package org.javawebstack.httpserver;
import com.google.gson.JsonElement;
import org.javawebstack.httpserver.helper.HttpMethod;
import javax.servlet.MultipartConfigElement;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
public class Exchange {
private final HTTPServer server;
private final HttpMethod method;
private final String path;
private byte[] body = null;
public Map<String, Object> pathVariables;
public final Map<String, String> parameters = new HashMap<>();
private final HttpServletRequest request;
private final HttpServletResponse response;
private final Map<String, Object> attributes = new HashMap<>();
public Exchange(HTTPServer server, HttpServletRequest request, HttpServletResponse response){
this.server = server;
this.request = request;
this.response = response;
this.path = request.getPathInfo();
method = HttpMethod.valueOf(request.getMethod());
}
public <T> T getBody(Class<T> clazz){
if(body == null)
body = read();
if(clazz == byte[].class)
return (T) body;
String body = new String(this.body, StandardCharsets.UTF_8);
if(clazz == String.class)
return (T) body;
return server.getGson().fromJson(body, clazz);
}
public HTTPServer getServer() {
return server;
}
public HttpMethod getMethod() {
return method;
}
public String getPath() {
return path;
}
public String getContentType(){
return request.getContentType();
}
public byte[] read(){
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
InputStream is = request.getInputStream();
byte [] data = new byte[1024];
int r;
while (is.available() > 0){
r = is.read(data);
baos.write(data, 0, r);
}
}catch(IOException ex){
throw new RuntimeException(ex);
}
return baos.toByteArray();
}
public Exchange write(String data){
write(data.getBytes(StandardCharsets.UTF_8));
return this;
}
public Exchange write(byte[] bytes){
try {
response.getOutputStream().write(bytes);
}catch (IOException ex){
throw new RuntimeException(ex);
}
return this;
}
public Exchange write(byte[] bytes, int offset, int length){
try {
response.getOutputStream().write(bytes, offset, length);
} catch (IOException ex){
throw new RuntimeException(ex);
}
return this;
}
public void close() {
try {
response.getOutputStream().close();
} catch (IOException ex){
throw new RuntimeException(ex);
}
}
public Exchange header(String header, String value){
if(header.equalsIgnoreCase("content-type")){
response.setContentType(value);
return this;
}
response.setHeader(header, value);
return this;
}
public Exchange status(int code){
response.setStatus(code);
return this;
}
public String header(String header){
return request.getHeader(header);
}
public Exchange redirect(String url){
response.setStatus(302);
try {
response.sendRedirect(url);
}catch (IOException ex){
throw new RuntimeException(ex);
}
return this;
}
public HttpServletRequest rawRequest(){
return request;
}
public HttpServletResponse rawResponse(){
return response;
}
public <T> T attrib(String key){
if(attributes.get(key) == null)
return null;
return (T) attributes.get(key);
}
public Exchange attrib(String key, Object value){
attributes.put(key, value);
return this;
}
public String bearerAuth(){
String auth = header("Authorization");
if(auth == null)
return null;
if(!auth.startsWith("Bearer "))
return null;
return auth.substring(7);
}
public <T> T getBodyPath(String path, Class<T> clazz){
return server.getGson().fromJson(getBodyPathElement(path), clazz);
}
public JsonElement getBodyPathElement(String path){
return getPathElement(getBody(JsonElement.class), path);
}
protected static JsonElement getPathElement(JsonElement source, String path){
if(source == null || path == null || path.length() == 0)
return source;
if(!path.contains(".")){
if(source.isJsonObject()){
return source.getAsJsonObject().get(path);
}else if(source.isJsonArray()){
return source.getAsJsonArray().get(Integer.parseInt(path));
}else{
return null;
}
}
String[] spl = path.split("\\.");
return getPathElement(getPathElement(source, spl[0]), path.substring(spl[0].length()+1));
}
public Exchange enableMultipart(String location){
enableMultipart(location, -1L);
return this;
}
public Exchange enableMultipart(String location, long maxFileSize){
request.setAttribute("org.eclipse.jetty.multipartConfig", new MultipartConfigElement(location, maxFileSize, -1L, 0));
return this;
}
}