Skip to content

Commit 8f0d287

Browse files
authored
Merge pull request #2 from JWaldecker/reformatCode
linting project
2 parents 00485ce + a6c956b commit 8f0d287

24 files changed

+536
-455
lines changed

src/main/java/org/javawebstack/httpserver/Exchange.java

Lines changed: 69 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package org.javawebstack.httpserver;
22

3-
import com.google.gson.JsonElement;
43
import org.javawebstack.abstractdata.AbstractElement;
54
import org.javawebstack.abstractdata.AbstractNull;
65
import org.javawebstack.httpserver.helper.HttpMethod;
@@ -31,27 +30,27 @@ public class Exchange {
3130
private final HttpServletResponse response;
3231
private final Map<String, Object> attributes = new HashMap<>();
3332

34-
public Exchange(HTTPServer server, HttpServletRequest request, HttpServletResponse response){
33+
public Exchange(HTTPServer server, HttpServletRequest request, HttpServletResponse response) {
3534
this.server = server;
3635
this.request = request;
3736
this.response = response;
3837
this.path = request.getPathInfo();
3938
method = "websocket".equalsIgnoreCase(request.getHeader("Upgrade")) ? HttpMethod.WEBSOCKET : HttpMethod.valueOf(request.getMethod());
4039
}
4140

42-
public <T> T body(Class<T> clazz){
43-
if(body == null)
41+
public <T> T body(Class<T> clazz) {
42+
if (body == null)
4443
body = read();
45-
if(clazz == byte[].class)
44+
if (clazz == byte[].class)
4645
return (T) body;
4746
String body = new String(this.body, StandardCharsets.UTF_8);
48-
if(clazz == String.class)
47+
if (clazz == String.class)
4948
return (T) body;
5049
MimeType type = MimeType.byMimeType(getContentType());
51-
if(type == null)
50+
if (type == null)
5251
type = MimeType.JSON;
5352
AbstractElement request = AbstractNull.INSTANCE;
54-
switch (type){
53+
switch (type) {
5554
case JSON:
5655
request = AbstractElement.fromJson(body);
5756
break;
@@ -63,7 +62,7 @@ public <T> T body(Class<T> clazz){
6362
break;
6463
}
6564
ValidationResult result = Validator.getValidator(clazz).validate(new ValidationContext().attrib("exchange", this), request);
66-
if(!result.isValid())
65+
if (!result.isValid())
6766
throw new ValidationException(result);
6867
return server.getAbstractMapper().fromAbstract(request, clazz);
6968
}
@@ -80,41 +79,47 @@ public String getPath() {
8079
return path;
8180
}
8281

83-
public String getContentType(){
82+
public String getContentType() {
8483
return request.getContentType();
8584
}
8685

87-
public byte[] read(){
86+
public byte[] read() {
8887
ByteArrayOutputStream baos = new ByteArrayOutputStream();
8988
try {
9089
InputStream is = request.getInputStream();
91-
byte [] data = new byte[1024];
90+
byte[] data = new byte[1024];
9291
int r;
93-
while (is.available() > 0){
92+
while (is.available() > 0) {
9493
r = is.read(data);
9594
baos.write(data, 0, r);
9695
}
97-
}catch(IOException ex){
96+
} catch (IOException ex) {
9897
throw new RuntimeException(ex);
9998
}
10099
return baos.toByteArray();
101100
}
102-
public Exchange write(String data){
101+
102+
public Exchange write(String data) {
103103
write(data.getBytes(StandardCharsets.UTF_8));
104104
return this;
105105
}
106-
public Exchange write(byte[] bytes){
106+
107+
public Exchange write(byte[] bytes) {
107108
try {
108109
response.getOutputStream().write(bytes);
109-
}catch (IOException ignored){}
110+
} catch (IOException ignored) {
111+
}
110112
return this;
111113
}
112-
public Exchange write(byte[] bytes, int offset, int length){
114+
115+
public Exchange write(byte[] bytes, int offset, int length) {
113116
try {
114117
response.getOutputStream().write(bytes, offset, length);
115-
} catch (IOException ignored){}
118+
} catch (IOException ignored) {
119+
}
116120
return this;
117121
}
122+
118123
public Exchange write(InputStream stream) throws IOException {
119124
byte[] buffer = new byte[1024];
120125
int r;
@@ -123,55 +128,60 @@ public Exchange write(InputStream stream) throws IOException {
123128
stream.close();
124129
return this;
125130
}
131+
126132
public Exchange close() {
127133
try {
128134
response.getOutputStream().close();
129-
} catch (IOException ignored){}
135+
} catch (IOException ignored) {
136+
}
130137
return this;
131138
}
132-
public Exchange header(String header, String value){
133-
if(header.equalsIgnoreCase("content-type")){
139+
140+
public Exchange header(String header, String value) {
141+
if (header.equalsIgnoreCase("content-type")) {
134142
response.setContentType(value);
135143
return this;
136144
}
137145
response.setHeader(header, value);
138146
return this;
139147
}
140-
public Exchange status(int code){
148+
149+
public Exchange status(int code) {
141150
response.setStatus(code);
142151
return this;
143152
}
144-
public String header(String header){
153+
154+
public String header(String header) {
145155
return request.getHeader(header);
146156
}
147157

148-
public Exchange redirect(String url){
158+
public Exchange redirect(String url) {
149159
response.setStatus(302);
150160
try {
151161
response.sendRedirect(url);
152-
}catch (IOException ex){
162+
} catch (IOException ex) {
153163
throw new RuntimeException(ex);
154164
}
155165
return this;
156166
}
157167

158-
public List<Locale> locales(){
168+
public List<Locale> locales() {
159169
return Collections.list(request.getLocales());
160170
}
161171

162-
public Locale locale(Locale... possible){
163-
if(possible.length == 0)
172+
public Locale locale(Locale... possible) {
173+
if (possible.length == 0)
164174
return request.getLocale();
165175
List<Locale> possibleList = Arrays.asList(possible);
166-
for(Locale l : locales()){
167-
if(possibleList.contains(l))
176+
for (Locale l : locales()) {
177+
if (possibleList.contains(l))
168178
return l;
169179
}
170180
return possible[0];
171181
}
172182

173183
public Exchange contentType(MimeType type) {
174-
return contentType(type != null ? type.getMimeTypes().get(0): null);
184+
return contentType(type != null ? type.getMimeTypes().get(0) : null);
175185
}
176186

177187
public Exchange contentType(String contentType) {
@@ -180,56 +190,64 @@ public Exchange contentType(String contentType) {
180190
return header("Content-Type", contentType);
181191
}
182192

183-
public HttpServletRequest rawRequest(){
193+
public HttpServletRequest rawRequest() {
184194
return request;
185195
}
186-
public HttpServletResponse rawResponse(){
196+
197+
public HttpServletResponse rawResponse() {
187198
return response;
188199
}
189-
public <T> T attrib(String key){
190-
if(attributes.get(key) == null)
200+
201+
public <T> T attrib(String key) {
202+
if (attributes.get(key) == null)
191203
return null;
192204
return (T) attributes.get(key);
193205
}
194-
public Exchange attrib(String key, Object value){
206+
207+
public Exchange attrib(String key, Object value) {
195208
attributes.put(key, value);
196209
return this;
197210
}
198-
public String bearerAuth(){
211+
212+
public String bearerAuth() {
199213
String auth = header("Authorization");
200-
if(auth == null)
214+
if (auth == null)
201215
return null;
202-
if(!auth.startsWith("Bearer "))
216+
if (!auth.startsWith("Bearer "))
203217
return null;
204218
return auth.substring(7);
205219
}
206-
public <T> T getBodyPath(String path, Class<T> clazz){
220+
221+
public <T> T getBodyPath(String path, Class<T> clazz) {
207222
return server.getAbstractMapper().fromAbstract(getBodyPathElement(path), clazz);
208223
}
209-
public AbstractElement getBodyPathElement(String path){
224+
225+
public AbstractElement getBodyPathElement(String path) {
210226
return getPathElement(body(AbstractElement.class), path);
211227
}
212-
protected static AbstractElement getPathElement(AbstractElement source, String path){
213-
if(source == null || path == null || path.length() == 0)
228+
229+
protected static AbstractElement getPathElement(AbstractElement source, String path) {
230+
if (source == null || path == null || path.length() == 0)
214231
return source;
215-
if(!path.contains(".")){
216-
if(source.isObject()){
232+
if (!path.contains(".")) {
233+
if (source.isObject()) {
217234
return source.object().get(path);
218-
}else if(source.isArray()){
235+
} else if (source.isArray()) {
219236
return source.array().get(Integer.parseInt(path));
220-
}else{
237+
} else {
221238
return null;
222239
}
223240
}
224241
String[] spl = path.split("\\.");
225-
return getPathElement(getPathElement(source, spl[0]), path.substring(spl[0].length()+1));
242+
return getPathElement(getPathElement(source, spl[0]), path.substring(spl[0].length() + 1));
226243
}
227-
public Exchange enableMultipart(String location){
244+
245+
public Exchange enableMultipart(String location) {
228246
enableMultipart(location, -1L);
229247
return this;
230248
}
231249

232-
public Exchange enableMultipart(String location, long maxFileSize){
250+
public Exchange enableMultipart(String location, long maxFileSize) {
233251
request.setAttribute("org.eclipse.jetty.multipartConfig", new MultipartConfigElement(location, maxFileSize, -1L, 0));
234252
return this;
235253
}

0 commit comments

Comments
 (0)