com.jccworld.personamockserver.responder.CatchAll Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of persona-mock-server Show documentation
Show all versions of persona-mock-server Show documentation
A tool to run a basic server that provides mock answers based on personas. I.e. logged out, logged in as Bob, logged in as Jim.
Allows full customisation over response, such as headers or logic based responses.
Provides consistent and reliable responses everytime.
package com.jccworld.personamockserver.responder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import spark.Request;
import spark.Response;
import spark.Route;
import java.util.ArrayList;
import java.util.List;
import static spark.Spark.externalStaticFileLocation;
/**
* @author johncrossley
* @see https://github.com/JohnCrossley/PersonaMockServer
*/
public class CatchAll extends Route {
public static final String PATH = "/*";
private static final Object CONTROL_TO_JETTY = null;
private final Logger logger = LoggerFactory.getLogger(CatchAll.class);
private final List ignoredPaths;
private final String baseUrl;
public CatchAll(final String baseUrl) {
super(PATH);
this.baseUrl = baseUrl;
this.ignoredPaths = new ArrayList<>(0);
}
public CatchAll(final String baseUrl, final List ignoredPaths) {
super(PATH);
this.baseUrl = baseUrl;
this.ignoredPaths = ignoredPaths;
}
@Override
public Object handle(final Request request, final Response response) {
String url = baseUrl + request.pathInfo();
for(final String mappedUrl : ignoredPaths) {
if (url.contains(mappedUrl)) {
logger.debug("Returning mapped content for: " + url);
return CONTROL_TO_JETTY;
}
}
logger.info("Failed to find explicit mapping for url: ``" + url + "''");
logger.info("Payload: " + request.body());
return "\n" +
" \n" +
" Persona Mock Server doesn't support this URL: " + url + " !
\n" +
" \n" +
"";
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy