Skip to content

Commit

Permalink
Fix mime-types of displayed content in help servlet
Browse files Browse the repository at this point in the history
  • Loading branch information
shs96c committed Nov 10, 2018
1 parent 24dbcdd commit 72bc0f0
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 103 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,16 @@

package org.openqa.grid.web.servlet;

import static com.google.common.net.MediaType.CSS_UTF_8;
import static com.google.common.net.MediaType.HTML_UTF_8;
import static com.google.common.net.MediaType.ICO;
import static com.google.common.net.MediaType.JAVASCRIPT_UTF_8;
import static com.google.common.net.MediaType.JPEG;
import static com.google.common.net.MediaType.PNG;
import static java.nio.charset.StandardCharsets.UTF_8;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.ImmutableMap;
import com.google.common.io.ByteStreams;
import com.google.common.net.MediaType;

Expand All @@ -34,18 +42,23 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

import javax.servlet.http.HttpServletResponse;

/**
* Displays a somewhat useful help signpost page. Expects {@link #HELPER_TYPE_PARAMETER} to be
* set as a servlet context init parameter with a value of "hub", "node", or "standalone"
* Displays a somewhat useful help signpost page.
*/
public class DisplayHelpHandler implements CommandHandler {

public static final String HELPER_TYPE_PARAMETER = "webdriver.server.displayhelpservlet.type";
private static Map<String, MediaType> TYPES = ImmutableMap.of(
".js", JAVASCRIPT_UTF_8,
".css", CSS_UTF_8,
".png", PNG,
".jpg", JPEG,
".ico", ICO);

private static final String HELPER_SERVLET_TEMPLATE = "displayhelpservlet.html";
private static final String HELPER_SERVLET_ASSET_PATH_PREFIX = "/assets/";
Expand All @@ -54,13 +67,11 @@ public class DisplayHelpHandler implements CommandHandler {

private final Json json;
private final GridRole role;
private final String consolePath;
private final DisplayHelpServletConfig servletConfig;

public DisplayHelpHandler(Json json, GridRole role, String consolePath) {
this.json = Objects.requireNonNull(json);
this.role = Objects.requireNonNull(role);
this.consolePath = Objects.requireNonNull(consolePath);

this.servletConfig = new DisplayHelpServletConfig(
new BuildInfo().getReleaseLabel(),
Expand All @@ -76,6 +87,15 @@ public void execute(HttpRequest req, HttpResponse resp) throws IOException {
!resource.replace(HELPER_SERVLET_ASSET_PATH_PREFIX, "").equals("")) {
// request is for an asset of the help page
resource = resource.replace(HELPER_SERVLET_ASSET_PATH_PREFIX, "");
int index = resource.lastIndexOf('.');
MediaType type = HTML_UTF_8;
if (index != -1) {
String extension = resource.substring(index);
type = TYPES.getOrDefault(extension, HTML_UTF_8);
}

resp.setHeader("Content-Type", type.toString());

try (InputStream in = getResourceInputStream(resource)) {
if (in == null) {
resp.setStatus(HttpServletResponse.SC_NOT_FOUND);
Expand Down Expand Up @@ -111,14 +131,15 @@ public void execute(HttpRequest req, HttpResponse resp) throws IOException {
resp.setStatus(HttpServletResponse.SC_NOT_FOUND);
}

resp.setHeader("Content-Type", MediaType.HTML_UTF_8.toString());
resp.setHeader("Content-Type", HTML_UTF_8.toString());
resp.setContent(updatedTemplate.getBytes(UTF_8));
}
}
}
}

private String getHelperType() {
@VisibleForTesting
String getHelperType() {
switch (role) {
case HUB: {
return "Grid Hub";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,6 @@ public class DisplayHelpServlet extends HttpServlet {
private static final long serialVersionUID = 8484071790930378855L;
public static final String HELPER_TYPE_PARAMETER = "webdriver.server.displayhelpservlet.type";

private static final String HELPER_SERVLET_TEMPLATE = "displayhelpservlet.html";
private static final String HELPER_SERVLET_ASSET_PATH_PREFIX = "/assets/";
private static final String HELPER_SERVLET_RESOURCE_PATH = "org/openqa/grid/images/";
private static final String HELPER_SERVLET_TEMPLATE_CONFIG_JSON_VAR = "${servletConfigJson}";
private CommandHandler handler;

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,11 @@ public CommandHandlerServlet(Routes routes) {

@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws IOException {

HttpRequest request = new ServletRequestWrappingHttpRequest(req);
HttpResponse response = new ServletResponseWrappingHttpResponse(resp);

System.out.println(String.format("(%s) %s", request.getMethod(), request.getUri()));

Optional<CommandHandler> possibleMatch = routes.match(injector, request);
if (possibleMatch.isPresent()) {
possibleMatch.get().execute(request, response);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The SFC licenses this file
// to you 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.

package org.openqa.grid.web.servlet;

import static java.net.HttpURLConnection.HTTP_NOT_FOUND;
import static java.net.HttpURLConnection.HTTP_OK;
import static org.assertj.core.api.Assertions.assertThat;
import static org.openqa.selenium.remote.http.HttpMethod.GET;

import org.junit.Before;
import org.junit.Test;
import org.openqa.grid.common.GridRole;
import org.openqa.selenium.json.Json;
import org.openqa.selenium.remote.http.HttpRequest;
import org.openqa.selenium.remote.http.HttpResponse;

import java.io.IOException;

import javax.servlet.ServletException;

public class DisplayHelpHandlerTest {

private DisplayHelpHandler handler;

@Before
public void setUp() {
handler = new DisplayHelpHandler(new Json(), GridRole.NOT_GRID, "/wd/hub");
}

@Test
public void testGetHelpPageForStandalone() throws IOException {
assertThat(handler.getHelperType())
.isEqualTo("Standalone");

HttpRequest request = new HttpRequest(GET, "/");
HttpResponse response = new HttpResponse();
handler.execute(request, response);
assertThat(response.getStatus()).isEqualTo(HTTP_OK);

String body = response.getContentString();
assertThat(body).isNotNull().contains(
"Whoops! The URL specified routes to this help page.",
"\"type\": \"Standalone\"",
"\"consoleLink\": \"\\u002fwd\\u002fhub\"");
}

@Test
public void testGetHelpPageAsset() throws IOException {
HttpResponse response = new HttpResponse();

handler.execute(new HttpRequest(GET, "/assets/displayhelpservlet.css"), response);

assertThat(response.getStatus()).isEqualTo(HTTP_OK);
assertThat(response.getContentString()).isNotNull().contains("#help-heading #logo");
}

@Test
public void testNoSuchAsset() throws IOException {
HttpResponse response = new HttpResponse();

handler.execute(new HttpRequest(GET, "/assets/foo.bar"), response);

assertThat(response.getStatus()).isEqualTo(HTTP_NOT_FOUND);
}

@Test
public void testAccessRoot() throws IOException {
HttpResponse response = new HttpResponse();

handler.execute(new HttpRequest(GET, "/"), response);

assertThat(response.getStatus()).isEqualTo(HTTP_OK);
}

}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@

@RunWith(Suite.class)
@Suite.SuiteClasses({
DisplayHelpServletTest.class,
ResourceServletTest.class,
ConsoleServletTest.class,
RegistrationServletTest.class,
HubStatusServletTest.class
DisplayHelpHandlerTest.class,
ResourceServletTest.class,
ConsoleServletTest.class,
RegistrationServletTest.class,
HubStatusServletTest.class
})
public class GridServletTests {

}

0 comments on commit 72bc0f0

Please sign in to comment.