Skip to content

Commit

Permalink
server side cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
ptrthomas committed Jan 26, 2024
1 parent e6c2e36 commit 8203dd2
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 35 deletions.
20 changes: 13 additions & 7 deletions karate-core/src/main/java/com/intuit/karate/http/RequestCycle.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Supplier;
import java.util.function.Function;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -66,17 +66,17 @@ protected static RequestCycle init(KarateTemplateEngine te, ServerContext contex
private final Response response;
private final ServerContext context;
private final ServerConfig config;
private final Supplier<Response> customHandler;
private final Function<ServerContext, Boolean> requestValidator;

private String switchTemplate;
private Map<String, Object> switchParams;

private RequestCycle(JsEngine engine, KarateTemplateEngine templateEngine, ServerContext context) {
this.engine = engine;
this.templateEngine = templateEngine;
this.requestValidator = context.getRequestValidator();
this.context = context;
config = context.getConfig();
customHandler = context.getCustomHandler();
Session session = context.getSession();
if (session != null && !session.isTemporary()) {
engine.put(SESSION, session.getData());
Expand Down Expand Up @@ -170,9 +170,15 @@ public void setSwitchParams(Map<String, Object> switchParams) {

protected Response handle() {
try {
if (customHandler != null) {
return customHandler.get();
} else if (context.isApi()) {
if (requestValidator != null) {
Boolean valid = requestValidator.apply(context);
if (valid == null || !valid) {
logger.error("unauthorized request: {}", request);
response.setStatus(401); // just for logging in finally block
return response().buildWithStatus(401);
}
}
if (context.isApi()) {
InputStream is = apiResource();
if (context.isLockNeeded()) {
synchronized (config) {
Expand All @@ -187,7 +193,7 @@ protected Response handle() {
}
} catch (Exception e) {
logger.error("handle failed: {}", e.getMessage());
response.setStatus(500); // just for logging below
response.setStatus(500); // just for logging in finally block
return response().buildWithStatus(500);
} finally {
close();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@

import com.intuit.karate.FileUtils;
import com.intuit.karate.JsonUtils;
import com.intuit.karate.LogAppender;
import com.intuit.karate.Match;
import com.intuit.karate.core.Variable;
import com.intuit.karate.graal.JsArray;
Expand Down Expand Up @@ -121,15 +120,13 @@ public class ServerContext implements ProxyObject {
private Session session; // can be pre-resolved, else will be set by RequestCycle.init()
private boolean switched;
private boolean closed;
private Supplier<Response> customHandler;
private int nextId;

private final Map<String, Object> variables;
private Object flash;
private String redirectPath;
private List<String> bodyAppends;
private LogAppender logAppender;
private RequestCycle mockRequestCycle;
private Function<ServerContext, Boolean> requestValidator;

public ServerContext(ServerConfig config, Request request) {
this(config, request, null);
Expand Down Expand Up @@ -348,21 +345,13 @@ public void setHttpGetAllowed(boolean httpGetAllowed) {
this.httpGetAllowed = httpGetAllowed;
}

public Supplier<Response> getCustomHandler() {
return customHandler;
public void setRequestValidator(Function<ServerContext, Boolean> requestValidator) {
this.requestValidator = requestValidator;
}

public void setCustomHandler(Supplier<Response> customHandler) {
this.customHandler = customHandler;
}

public void setMockRequestCycle(RequestCycle mockRequestCycle) {
this.mockRequestCycle = mockRequestCycle;
}

public RequestCycle getMockRequestCycle() {
return mockRequestCycle;
}
public Function<ServerContext, Boolean> getRequestValidator() {
return requestValidator;
}

public boolean isSwitched() {
return switched;
Expand All @@ -383,20 +372,9 @@ public void bodyAppend(String body) {
bodyAppends.add(body);
}

public LogAppender getLogAppender() {
return logAppender;
}

public void setLogAppender(LogAppender logAppender) {
this.logAppender = logAppender;
}

public void log(Object... args) {
String log = new LogWrapper(args).toString();
logger.info(log);
if (logAppender != null) {
logAppender.append(log);
}
}

private final Methods.FunVar GET_FUNCTION = args -> {
Expand Down

0 comments on commit 8203dd2

Please sign in to comment.