-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathResourceUtility.java
More file actions
354 lines (328 loc) · 12.6 KB
/
Copy pathResourceUtility.java
File metadata and controls
354 lines (328 loc) · 12.6 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/*******************************************************************************
* Copyright 2015 Defense Health Agency (DHA)
*
* If your use of this software does not include any GPLv2 components:
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ----------------------------------------------------------------------------
* If your use of this software includes any GPLv2 components:
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*******************************************************************************/
package prerna.semoss.web.services.local;
import java.io.BufferedReader;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonSyntaxException;
import com.google.gson.ToNumberPolicy;
import com.google.gson.reflect.TypeToken;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import jakarta.ws.rs.core.Context;
import jakarta.ws.rs.core.Response;
import jakarta.ws.rs.core.StreamingOutput;
import prerna.auth.AuthProvider;
import prerna.auth.User;
import prerna.om.Insight;
import prerna.om.InsightStore;
import prerna.sablecc2.PixelRunner;
import prerna.sablecc2.PixelStreamUtility;
import prerna.sablecc2.comm.PixelJobManager;
import prerna.sablecc2.comm.PixelJobRunner;
import prerna.sablecc2.comm.PixelJobStatus;
import prerna.sablecc2.om.PixelOperationType;
import prerna.sablecc2.om.nounmeta.NounMetadata;
import prerna.util.Constants;
import prerna.util.Utility;
import prerna.util.insight.InsightUtility;
import prerna.web.services.util.WebUtility;
public class ResourceUtility {
private static final Logger classLogger = LogManager.getLogger(ResourceUtility.class);
protected static List<String> allowAccessWithoutUsers = new ArrayList<>();
static {
allowAccessWithoutUsers.add("config");
allowAccessWithoutUsers.add("config/fetchCsrf");
}
public static List<String> allowAccessWithoutLogin = new ArrayList<>();
static {
// allow these for successful dropping of
// sessions when browser is closed/refreshed
// these do their own session checks
allowAccessWithoutLogin.add("session/active");
allowAccessWithoutLogin.add("session/cleanSession");
allowAccessWithoutLogin.add("session/cancelCleanSession");
allowAccessWithoutLogin.add("session/invalidateSession");
allowAccessWithoutLogin.add("config");
allowAccessWithoutLogin.add("config/fetchCsrf");
allowAccessWithoutLogin.add("config/endpoints");
allowAccessWithoutLogin.add("auth/logins");
allowAccessWithoutLogin.add("auth/loginsAllowed");
allowAccessWithoutLogin.add("auth/login");
allowAccessWithoutLogin.add("auth/loginLDAP");
allowAccessWithoutLogin.add("auth/changeADPassword");
allowAccessWithoutLogin.add("auth/loginLinOTP");
allowAccessWithoutLogin.add("auth/createUser");
allowAccessWithoutLogin.add("auth/whoami");
allowAccessWithoutLogin.add("auth/user/setupResetPassword");
allowAccessWithoutLogin.add("auth/user/resetPassword");
for (AuthProvider v : AuthProvider.values()) {
allowAccessWithoutLogin.add("auth/userinfo/" + v.toString().toLowerCase());
allowAccessWithoutLogin.add("auth/login/" + v.toString().toLowerCase());
}
// legacy ms login
allowAccessWithoutLogin.add("auth/userinfo/ms");
allowAccessWithoutLogin.add("auth/login/ms");
}
/**
* Get the user
*
* @param request
* @return
* @throws IOException
*/
public static User getUser(@Context HttpServletRequest request) throws IllegalAccessException {
HttpSession session = request.getSession(false);
if (session == null) {
throw new IllegalAccessException("User session is invalid");
}
User user = (User) session.getAttribute(Constants.SESSION_USER);
if (user == null) {
throw new IllegalAccessException("User session is invalid");
}
return user;
}
/**
* Parse the body of an incoming request that is either application/json or
* application/x-www-form-urlencoded into a single map of parameters.
*
* For JSON, nested types (objects, arrays, numbers, booleans) are preserved.
* For form-urlencoded, all values are returned as String (or List<String> if
* multiple values for the same key).
*/
public static Map<String, Object> parseRequestBody(HttpServletRequest request) throws IOException {
Map<String, Object> params = new HashMap<>();
String contentType = request.getContentType();
if (contentType != null && contentType.toLowerCase().contains("application/json")) {
StringBuilder jsonBuffer = new StringBuilder();
try (BufferedReader reader = request.getReader()) {
String line;
while ((line = reader.readLine()) != null) {
jsonBuffer.append(line);
}
}
if (jsonBuffer.length() == 0) {
return params;
}
Gson gson = new GsonBuilder().disableHtmlEscaping().setObjectToNumberStrategy(ToNumberPolicy.LONG_OR_DOUBLE)
.create();
Type mapType = new TypeToken<Map<String, Object>>() {
}.getType();
try {
Map<String, Object> parsed = gson.fromJson(jsonBuffer.toString(), mapType);
if (parsed != null) {
params.putAll(parsed);
}
} catch (JsonSyntaxException e) {
throw new IOException("Invalid JSON syntax in request body", e);
}
} else {
Map<String, String[]> parameterMap = request.getParameterMap();
for (Map.Entry<String, String[]> e : parameterMap.entrySet()) {
String[] values = e.getValue();
if (values == null || values.length == 0) {
continue;
}
if (values.length == 1) {
params.put(e.getKey(), values[0]);
} else {
params.put(e.getKey(), Arrays.asList(values));
}
}
}
return params;
}
/**
* Build a fresh insight, register it in the InsightStore, and execute the given
* pixel expression for the user attached to this request.
*
* Used by engine resource endpoints that wrap pixel reactor calls. Unlike
* {@link NameServer#runPixelJob}, this returns HTTP 400 when the resulting
* pixel run carries an {@link PixelOperationType#ERROR} or
* {@link PixelOperationType#INVALID_SYNTAX} op type, instead of the standard
* always-200 SEMOSS pixel response.
*/
public static Response runPixel(HttpServletRequest request, String expression) {
User user;
try {
user = getUser(request);
} catch (IllegalAccessException e) {
Map<String, String> errorMap = new HashMap<>();
errorMap.put(Constants.ERROR_MESSAGE, "User session is invalid");
return WebUtility.getResponse(errorMap, 401);
}
HttpSession session = request.getSession(false);
String sessionId = session != null ? session.getId() : null;
Insight insight = new Insight();
InsightStore.getInstance().put(insight);
String insightId = insight.getInsightId();
if (sessionId != null) {
InsightStore.getInstance().addToSessionHash(sessionId, insightId);
}
insight.setUser(user);
user.setZoneId(Utility.getApplicationZoneIdObj());
if (!expression.endsWith(";")) {
expression = expression + ";";
}
final PixelJobManager manager = PixelJobManager.getManager();
final PixelJobRunner jobRunner = manager.makeJob(WebUtility.inputSanitizer(insightId), insight, sessionId,
null);
final String jobId = jobRunner.getJobId();
jobRunner.addPixel(expression);
jobRunner.run();
PixelRunner pixelRunner = jobRunner.getRunner();
int status = pixelRunHasError(pixelRunner) ? 400 : 200;
// Cleanup runs as the afterWrite hook in PixelStreamUtility, so the job
// and Insight stay alive until the response body has been fully written.
final Insight finalInsight = insight;
StreamingOutput stream = PixelStreamUtility.collectPixelData(pixelRunner, jobRunner,
() -> cleanupAfterPixel(finalInsight, manager, jobId, jobRunner));
if (stream == null) {
classLogger.error("PixelStreamUtility returned a null streaming output for insight '{}' running '{}'",
insightId, expression);
cleanupAfterPixel(insight, manager, jobId, jobRunner);
Map<String, Object> errorMap = new HashMap<>();
errorMap.put("errorType", "unknown");
errorMap.put(Constants.ERROR_MESSAGE, "Failed to build pixel response stream");
return Response.status(500).entity(errorMap)
.header("Cache-Control",
"no-store, no-cache, must-revalidate, max-age=0, post-check=0, pre-check=0")
.header("Pragma", "no-cache").build();
}
return Response.status(status).entity(stream)
.header("Cache-Control", "no-store, no-cache, must-revalidate, max-age=0, post-check=0, pre-check=0")
.header("Pragma", "no-cache").build();
}
/**
* Release per-request pixel resources: mark the job complete, remove it from
* the {@link PixelJobManager}, and drop the {@link Insight} (closes any
* Python/R environments and removes it from the {@link InsightStore}). Both
* cleanup branches are guarded so a failure in one does not skip the other.
* <p>
* Package-private so streaming endpoints in this package (e.g.
* {@link ModelEngineResource#llmStreaming}) can share the same post-stream
* cleanup contract.
*/
static void cleanupAfterPixel(Insight insight, PixelJobManager manager, String jobId, PixelJobRunner jobRunner) {
try {
if (jobRunner != null) {
jobRunner.setStatus(PixelJobStatus.COMPLETE);
}
if (manager != null && jobId != null) {
manager.clearJob(jobId);
manager.removeJob(jobId);
}
} catch (Exception e) {
classLogger.error("Failed to clear pixel job '{}' during post-stream cleanup", jobId, e);
}
try {
if (insight != null) {
InsightUtility.dropInsight(insight);
}
} catch (Exception e) {
classLogger.error("Failed to drop insight '{}' during post-stream cleanup",
insight == null ? null : insight.getInsightId(), e);
}
}
/**
* Returns true if any of the runner's pixel results carry an op type that
* indicates the pixel itself failed (a reactor threw, or the expression was not
* parseable). Used to map a SEMOSS pixel error onto an HTTP 400.
*/
private static boolean pixelRunHasError(PixelRunner pixelRunner) {
if (pixelRunner == null) {
return false;
}
List<NounMetadata> results = pixelRunner.getResults();
if (results == null || results.isEmpty()) {
return false;
}
for (NounMetadata result : results) {
if (result == null || result.getOpType() == null) {
continue;
}
if (result.getOpType().contains(PixelOperationType.ERROR)
|| result.getOpType().contains(PixelOperationType.INVALID_SYNTAX)) {
return true;
}
}
return false;
}
/**
* Need to ignore some URLs
*
* @param fullUrl
* @return
*/
public static boolean canAccessWithoutUsers(String fullUrl) {
for (String ignore : allowAccessWithoutUsers) {
if (fullUrl.endsWith(ignore)) {
return true;
}
}
return false;
}
/**
* Need to ignore some URLs
*
* @param fullUrl
* @return
*/
public static boolean canAccessWithoutLogin(String fullUrl) {
for (String ignore : allowAccessWithoutLogin) {
if (fullUrl.endsWith(ignore)) {
return true;
}
}
return false;
}
/**
* Need to ignore some URLs
*
* @param fullUrl
* @return
*/
public static boolean endsWithMatch(Collection<String> ignoreForFE, String fullUrl) {
for (String ignore : ignoreForFE) {
if (fullUrl.endsWith(ignore)) {
return true;
}
}
return false;
}
}