/*
HTTP stub server written in Java with embedded Jetty
Copyright (C) 2012 Alexander Zagniotov, Isa Goksu and Eric Mrak
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 3 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.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package by.stub.handlers;
import by.stub.database.DataStore;
import by.stub.server.JettyContext;
import by.stub.utils.ConsoleUtils;
import by.stub.utils.HandlerUtils;
import by.stub.utils.ReflectionUtils;
import by.stub.utils.StringUtils;
import by.stub.yaml.stubs.StubHttpLifecycle;
import by.stub.yaml.stubs.StubRequest;
import by.stub.yaml.stubs.StubResponse;
import org.eclipse.jetty.http.HttpHeaders;
import org.eclipse.jetty.http.HttpSchemes;
import org.eclipse.jetty.http.HttpStatus;
import org.eclipse.jetty.http.MimeTypes;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.handler.AbstractHandler;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
@SuppressWarnings("serial")
public final class PingHandler extends AbstractHandler {
private static final String CSS_CLASS_HIGHLIGHTABLE = "highlightable";
private static final String CSS_CLASS_NO_HIGHLIGHTABLE = "no-highlightable";
private static final String NAME = "admin";
private static final String HTML_TABLE_ROW_TEMPLATE = "<tr><td width='200px' valign='top' align='left'>%s</td><td class='%s' align='left'>%s</td></tr>";
private static final List<String> highlightableProperties = Collections.unmodifiableList(Arrays.asList("file", "body", "post"));
private final DataStore dataStore;
private final JettyContext jettyContext;
public PingHandler(final JettyContext newContext, final DataStore newDataStore) {
this.jettyContext = newContext;
this.dataStore = newDataStore;
}
@Override
public void handle(final String target, final Request baseRequest, final HttpServletRequest request, final HttpServletResponse response) throws IOException, ServletException {
ConsoleUtils.logIncomingRequest(request, NAME);
baseRequest.setHandled(true);
response.setContentType(MimeTypes.TEXT_HTML_UTF_8);
response.setStatus(HttpStatus.OK_200);
response.setHeader(HttpHeaders.SERVER, HandlerUtils.constructHeaderServerName());
try {
response.getWriter().println(getConfigDataPresentation());
} catch (final Exception ex) {
HandlerUtils.configureErrorResponse(response, HttpStatus.INTERNAL_SERVER_ERROR_500, ex.toString());
}
}
private String getConfigDataPresentation() throws Exception {
final List<StubHttpLifecycle> stubHttpLifecycles = dataStore.getStubHttpLifecycles();
final StringBuilder builder = new StringBuilder();
builder.append(buildSystemStatusHtmlTable());
builder.append("<br /><br />");
final String htmlTemplateContent = HandlerUtils.getHtmlResourceByName("snippet_request_response_tables");
for (final StubHttpLifecycle stubHttpLifecycle : stubHttpLifecycles) {
final StubRequest stubRequest = stubHttpLifecycle.getRequest();
builder.append(buildPageBodyHtml(htmlTemplateContent, "Request", ReflectionUtils.getProperties(stubRequest)));
final List<StubResponse> allResponses = stubHttpLifecycle.getAllResponses();
for (int idx = 0; idx < allResponses.size(); idx++) {
String responseTableTitle = (allResponses.size() == 1 ? "Response" : String.format("Response #%s", (1 + idx)));
final StubResponse stubResponse = allResponses.get(idx);
builder.append(buildPageBodyHtml(htmlTemplateContent, responseTableTitle, ReflectionUtils.getProperties(stubResponse)));
}
builder.append("<br /><br />");
}
return HandlerUtils.populateHtmlTemplate("ping", stubHttpLifecycles.size(), builder.toString());
}
private String buildSystemStatusHtmlTable() throws Exception {
final StringBuilder builder = new StringBuilder();
final String host = jettyContext.getHost();
final int clientPort = jettyContext.getStubsPort();
final int sslPort = jettyContext.getStubsSslPort();
final int adminPort = jettyContext.getAdminPort();
builder.append(populateTableRowTemplate("CLIENT PORT", CSS_CLASS_NO_HIGHLIGHTABLE, clientPort));
builder.append(populateTableRowTemplate("ADMIN PORT", CSS_CLASS_NO_HIGHLIGHTABLE, adminPort));
builder.append(populateTableRowTemplate("SSL PORT", CSS_CLASS_NO_HIGHLIGHTABLE, sslPort));
builder.append(populateTableRowTemplate("HOST", CSS_CLASS_NO_HIGHLIGHTABLE, host));
builder.append(populateTableRowTemplate("CONFIGURATION", CSS_CLASS_NO_HIGHLIGHTABLE, dataStore.getDataYaml().getAbsolutePath()));
final String endpointRegistration = HandlerUtils.linkifyRequestUrl(HttpSchemes.HTTP,
StubsRegistrationHandler.RESOURCE_STUBDATA_NEW, host, adminPort);
builder.append(populateTableRowTemplate("NEW STUB DATA POST URI", CSS_CLASS_NO_HIGHLIGHTABLE, endpointRegistration));
final String systemStatusTable = HandlerUtils.getHtmlResourceByName("snippet_system_status_table");
return String.format(systemStatusTable, builder.toString());
}
private String buildPageBodyHtml(final String htmlTemplateContent, final String tableName, final Map<String, String> stubObjectProperties) throws Exception {
final StringBuilder builder = new StringBuilder();
for (final Map.Entry<String, String> keyValue : stubObjectProperties.entrySet()) {
final String value = keyValue.getValue();
final String key = keyValue.getKey();
if (!StringUtils.isSet(value)) {
continue;
}
builder.append(constructHtmlTableRow(key, value));
}
return String.format(htmlTemplateContent, tableName, builder.toString());
}
private String constructHtmlTableRow(final String key, final String value) {
final String escapedValue = StringUtils.escapeHtmlEntities(value);
if (highlightableProperties.contains(key)) {
final String row = String.format("%s%s%s", "<pre><code>", escapedValue, "</code></pre>");
return populateTableRowTemplate(StringUtils.toUpper(key), CSS_CLASS_HIGHLIGHTABLE, row);
}
if (key.equals("url")) {
final String linkifiedUrl = HandlerUtils.linkifyRequestUrl(HttpSchemes.HTTP, escapedValue, jettyContext.getHost(), jettyContext.getStubsPort());
final String linkifiedSslUrl = HandlerUtils.linkifyRequestUrl(HttpSchemes.HTTPS, escapedValue, jettyContext.getHost(), jettyContext.getStubsSslPort());
final String tableRowWithUrl = populateTableRowTemplate(StringUtils.toUpper(key), CSS_CLASS_NO_HIGHLIGHTABLE, linkifiedUrl);
final String tableRowWithSslUrl = populateTableRowTemplate("SSL " + StringUtils.toUpper(key), CSS_CLASS_NO_HIGHLIGHTABLE, linkifiedSslUrl);
return String.format("%s%s", tableRowWithUrl, tableRowWithSslUrl);
}
return populateTableRowTemplate(StringUtils.toUpper(key), CSS_CLASS_NO_HIGHLIGHTABLE, escapedValue);
}
private String populateTableRowTemplate(final Object... tokens) {
return String.format(HTML_TABLE_ROW_TEMPLATE, tokens);
}
}