From 4cb21257e681bb32a6e8c5a2153b3d2c2f9e5dae Mon Sep 17 00:00:00 2001 From: Chris Rankin Date: Fri, 21 Apr 2017 14:46:29 +0100 Subject: [PATCH] Add the Corda node's legalName to the Web error page. (#569) --- .../net/corda/webserver/WebServerConfig.kt | 1 + .../corda/webserver/internal/NodeWebServer.kt | 30 +++++++++++++++---- 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/webserver/src/main/kotlin/net/corda/webserver/WebServerConfig.kt b/webserver/src/main/kotlin/net/corda/webserver/WebServerConfig.kt index 1afef7a671..ad17a38ed1 100644 --- a/webserver/src/main/kotlin/net/corda/webserver/WebServerConfig.kt +++ b/webserver/src/main/kotlin/net/corda/webserver/WebServerConfig.kt @@ -16,6 +16,7 @@ class WebServerConfig(val baseDirectory: Path, val config: Config) : SSLConfigur override val trustStorePassword: String by config val exportJMXto: String get() = "http" val useHTTPS: Boolean by config + val myLegalName: String by config val p2pAddress: HostAndPort by config // TODO: Use RPC port instead of P2P port (RPC requires authentication, P2P does not) val webAddress: HostAndPort by config } \ No newline at end of file diff --git a/webserver/src/main/kotlin/net/corda/webserver/internal/NodeWebServer.kt b/webserver/src/main/kotlin/net/corda/webserver/internal/NodeWebServer.kt index 02b140a7a3..64b7c85f60 100644 --- a/webserver/src/main/kotlin/net/corda/webserver/internal/NodeWebServer.kt +++ b/webserver/src/main/kotlin/net/corda/webserver/internal/NodeWebServer.kt @@ -12,6 +12,7 @@ import net.corda.webserver.servlets.ObjectMapperConfig import net.corda.webserver.servlets.ResponseFilter import org.apache.activemq.artemis.api.core.ActiveMQNotConnectedException import org.eclipse.jetty.server.* +import org.eclipse.jetty.server.handler.ErrorHandler import org.eclipse.jetty.server.handler.HandlerCollection import org.eclipse.jetty.servlet.DefaultServlet import org.eclipse.jetty.servlet.ServletContextHandler @@ -22,13 +23,17 @@ import org.glassfish.jersey.server.ResourceConfig import org.glassfish.jersey.server.ServerProperties import org.glassfish.jersey.servlet.ServletContainer import org.slf4j.LoggerFactory +import java.io.IOException +import java.io.Writer import java.lang.reflect.InvocationTargetException -import java.util.* +import java.util.ServiceLoader +import javax.servlet.http.HttpServletRequest +import javax.ws.rs.core.MediaType class NodeWebServer(val config: WebServerConfig) { private companion object { val log = loggerFor() - val retryDelay = 1000L // Milliseconds + const val retryDelay = 1000L // Milliseconds } val address = config.webAddress @@ -58,7 +63,7 @@ class NodeWebServer(val config: WebServerConfig) { handlerCollection.addHandler(WebAppContext().apply { // Find the jolokia WAR file on the classpath. contextPath = "/monitoring/json" - setInitParameter("mimeType", "application/json") + setInitParameter("mimeType", MediaType.APPLICATION_JSON) war = warpath }) } else { @@ -106,14 +111,27 @@ class NodeWebServer(val config: WebServerConfig) { private fun buildServletContextHandler(localRpc: CordaRPCOps): ServletContextHandler { return ServletContextHandler().apply { contextPath = "/" + errorHandler = object : ErrorHandler() { + @Throws(IOException::class) + override fun writeErrorPageHead(request: HttpServletRequest, writer: Writer, code: Int, message: String) { + writer.write("\n") + writer.write("Corda ${config.myLegalName} : Error $code\n") + } + + @Throws(IOException::class) + override fun writeErrorPageMessage(request: HttpServletRequest, writer: Writer, code: Int, message: String , uri: String) { + writer.write("

Corda ${config.myLegalName}

\n") + super.writeErrorPageMessage(request, writer, code, message, uri) + } + } setAttribute("rpc", localRpc) addServlet(DataUploadServlet::class.java, "/upload/*") addServlet(AttachmentDownloadServlet::class.java, "/attachments/*") val resourceConfig = ResourceConfig() - resourceConfig.register(ObjectMapperConfig(localRpc)) - resourceConfig.register(ResponseFilter()) - resourceConfig.register(APIServerImpl(localRpc)) + .register(ObjectMapperConfig(localRpc)) + .register(ResponseFilter()) + .register(APIServerImpl(localRpc)) val webAPIsOnClasspath = pluginRegistries.flatMap { x -> x.webApis } for (webapi in webAPIsOnClasspath) {