-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathWrapperUtils.java
More file actions
71 lines (59 loc) · 2.48 KB
/
WrapperUtils.java
File metadata and controls
71 lines (59 loc) · 2.48 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
package com.genexus;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.stream.Collectors;
import com.genexus.opentelemetry.OpenTelemetryHelper;
import com.genexus.servlet.http.IHttpServletRequest;
import org.json.JSONException;
import com.genexus.json.JSONObjectWrapper;
import org.apache.commons.io.IOUtils;
import org.apache.logging.log4j.Logger;
public class WrapperUtils {
private static Logger log = org.apache.logging.log4j.LogManager.getLogger(WrapperUtils.class);
public static String getJsonFromRestException(int[] statusCode, String reasonPhrase, boolean applicationException, Throwable ex) {
OpenTelemetryHelper.recordException(ex);
int localStatusCode = statusCode[0];
if (!applicationException) {
localStatusCode = 500;
reasonPhrase = "Internal Server Error";
if (ex instanceof com.fasterxml.jackson.core.JsonProcessingException)
{
localStatusCode = 400;
reasonPhrase = "Bad Request";
}
}
log.error("Error executing REST service", ex);
JSONObjectWrapper errorJson = new JSONObjectWrapper();
try
{
JSONObjectWrapper obj = new JSONObjectWrapper();
obj.put("code", localStatusCode);
obj.put("message", reasonPhrase);
errorJson.put("error", obj);
}
catch(JSONException e)
{
log.error("Invalid JSON", e);
}
statusCode[0] = localStatusCode;
return errorJson.toString();
}
public static ThreadLocal<String> requestBodyThreadLocal = new ThreadLocal<String>();
public static InputStream storeRestRequestBody(InputStream is) throws IOException {
String body = new BufferedReader(new InputStreamReader(is, "UTF-8")).lines().collect(Collectors.joining("\n"));
requestBodyThreadLocal.set(body);
return IOUtils.toInputStream(body, "UTF-8");
}
public static boolean isSecureConnection(IHttpServletRequest req) {
return req.isSecure() ||
"https".equalsIgnoreCase(req.getHeader("X-Forwarded-Proto")) ||
"on".equalsIgnoreCase(req.getHeader("X-Forwarded-Ssl")) ||
"1".equals(req.getHeader("X-Forwarded-Ssl")) ||
"true".equalsIgnoreCase(req.getHeader("X-Forwarded-Ssl")) ||
"https".equalsIgnoreCase(req.getHeader("X-Forwarded-Scheme")) ||
"on".equalsIgnoreCase(req.getHeader("Front-End-Https")) ||
"https".equalsIgnoreCase(req.getHeader("X-Url-Scheme"));
}
}