Skip to content

Commit

Permalink
[debug-server] Fixing ability of Utf8Servlet to find files
Browse files Browse the repository at this point in the history
  • Loading branch information
barancev committed Nov 18, 2018
1 parent 8255f90 commit 82ba4d6
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,9 @@ public JettyAppServer(String hostName, int httpPort, int httpsPort) {

handlers = new ContextHandlerCollection();

Path webSrc = locate("common/src/web");
ServletContextHandler defaultContext = addResourceHandler(
DEFAULT_CONTEXT_PATH, locate("common/src/web"));
DEFAULT_CONTEXT_PATH, webSrc);
ServletContextHandler jsContext = addResourceHandler(
JS_SRC_CONTEXT_PATH, locate("javascript"));
addResourceHandler(CLOSURE_CONTEXT_PATH, locate("third_party/closure/goog"));
Expand All @@ -124,6 +125,7 @@ public JettyAppServer(String hostName, int httpPort, int httpsPort) {
defaultContext.setInitParameter("hostname", hostName);
defaultContext.setInitParameter("port", ""+port);
defaultContext.setInitParameter("path", TEMP_SRC_CONTEXT_PATH);
defaultContext.setInitParameter("webSrc", webSrc.toAbsolutePath().toString());

server.setHandler(handlers);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,34 +17,24 @@

package org.openqa.selenium.environment.webserver;

import com.google.common.io.ByteStreams;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Utf8Servlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

String fileName = this.getServletContext().getRealPath(request.getPathInfo());
String fileContent = "";
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {

try (InputStream is = new FileInputStream(fileName)) {
// Note: Must read the content as UTF8.
fileContent = new String(ByteStreams.toByteArray(is), Charset.forName("UTF-8"));
} catch (IOException e) {
throw new ServletException("Failed to file: " + fileName + " based on request path: " +
request.getPathInfo() + ", servlet path: " + request.getServletPath() +
" and context path: " + request.getContextPath());
}
Path target = Paths.get(this.getServletContext().getInitParameter("webSrc"),
request.getPathInfo());
String fileContent = new String(Files.readAllBytes(target), StandardCharsets.UTF_8);

response.setContentType("text/html; charset=UTF-8");
response.setStatus(HttpServletResponse.SC_OK);
Expand Down

0 comments on commit 82ba4d6

Please sign in to comment.