16
16
import javax .servlet .http .HttpServletResponse ;
17
17
import java .io .*;
18
18
import java .net .URLDecoder ;
19
+ import java .nio .charset .StandardCharsets ;
19
20
import java .util .Arrays ;
20
21
import java .util .Objects ;
21
22
import java .util .logging .Logger ;
22
23
23
- import static java .nio .charset .StandardCharsets .* ;
24
- import static javax .ws .rs .core .MediaType .* ;
24
+ import static java .nio .charset .StandardCharsets .UTF_8 ;
25
+ import static javax .ws .rs .core .MediaType .APPLICATION_JSON ;
25
26
26
27
public class BaseServlet extends HttpServlet {
27
28
protected static Logger log = Logger .getLogger (BaseServlet .class .getName ());
@@ -46,15 +47,11 @@ private String replacer(String value) {
46
47
}
47
48
48
49
protected String getRequestJson (@ NotNull HttpServletRequest req ) {
49
- try {
50
- req .setCharacterEncoding (UTF_8 .toString ());
51
- } catch (UnsupportedEncodingException e ) {
52
- e .printStackTrace ();
53
- }
54
50
StringBuilder sb = new StringBuilder ();
55
51
String s ;
56
52
try {
57
- while ((s = req .getReader ().readLine ()) != null ) {
53
+ BufferedReader reader = new BufferedReader (new InputStreamReader (req .getInputStream (), StandardCharsets .UTF_8 ));
54
+ while ((s = reader .readLine ()) != null ) {
58
55
sb .append (s );
59
56
}
60
57
} catch (IOException e ) {
@@ -115,12 +112,12 @@ protected void setGetResponseHeaders(@NotNull HttpServletResponse resp) {
115
112
protected void setOptionsResponseHeaders (@ NotNull HttpServletResponse resp ) {
116
113
resp .addHeader ("Access-Control-Allow-Credentials" , "true" );
117
114
resp .addHeader ("Access-Control-Allow-Methods" , "OPTIONS, DELETE, POST, GET, PUT" );
118
- resp .addHeader ("Access-Control-Allow-Origin" ,getOrigin ());
115
+ resp .addHeader ("Access-Control-Allow-Origin" , getOrigin ());
119
116
resp .addHeader ("Access-Control-Allow-Headers" , "Authorization, content-type" );
120
117
resp .setStatus (204 );
121
118
}
122
119
123
- private String getOrigin (){
120
+ private String getOrigin () {
124
121
return World .getInstance ().getBaseURL () != null ? World .getInstance ().getBaseURL () : "*" ;
125
122
}
126
123
@@ -140,12 +137,12 @@ private String getSessionId(@NotNull HttpServletRequest req) throws AqualityExce
140
137
validateAuthHeader (header );
141
138
String [] strings = header .split (" " );
142
139
return strings [1 ];
143
- } else if (cookies != null ){
140
+ } else if (cookies != null ) {
144
141
Cookie iio78 = Arrays .stream (cookies ).filter (x -> x .getName ().equals ("iio78" )).findFirst ().orElse (null );
145
- if (iio78 != null ) {
146
- try {
142
+ if (iio78 != null ) {
143
+ try {
147
144
return URLDecoder .decode (iio78 .getValue (), "utf-8" );
148
- } catch (UnsupportedEncodingException e ){
145
+ } catch (UnsupportedEncodingException e ) {
149
146
throw new AuthenticationException ("Your cookie is wrong!" );
150
147
}
151
148
}
0 commit comments