Example usage for org.openqa.selenium.remote.http HttpResponse setStatus

List of usage examples for org.openqa.selenium.remote.http HttpResponse setStatus

Introduction

In this page you can find the example usage for org.openqa.selenium.remote.http HttpResponse setStatus.

Prototype

public HttpResponse setStatus(int status) 

Source Link

Usage

From source file:org.openqa.grid.web.servlet.DisplayHelpHandler.java

License:Apache License

@Override
public void execute(HttpRequest req, HttpResponse resp) throws IOException {
    String resource = req.getUri();
    if (resource.contains(HELPER_SERVLET_ASSET_PATH_PREFIX)
            && !resource.replace(HELPER_SERVLET_ASSET_PATH_PREFIX, "").contains("/")
            && !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);
        }/*from   www . j  av a  2  s.  c  o  m*/

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

        try (InputStream in = getResourceInputStream(resource)) {
            if (in == null) {
                resp.setStatus(HttpServletResponse.SC_NOT_FOUND);
                return;
            } else {
                resp.setStatus(HttpServletResponse.SC_OK);
                resp.setContent(ByteStreams.toByteArray(in));
                return;
            }
        }
    } else {
        // request is for an unknown entity. show the help page
        try (InputStream in = getResourceInputStream(HELPER_SERVLET_TEMPLATE)) {
            if (in == null) {
                resp.setStatus(HttpServletResponse.SC_NOT_FOUND);
            } else {
                StringBuilder jsonBuilder = new StringBuilder();
                try (JsonOutput out = json.newOutput(jsonBuilder)) {
                    out.setPrettyPrint(false).write(servletConfig);
                }

                final String json = jsonBuilder.toString();

                final String htmlTemplate;
                try (BufferedReader reader = new BufferedReader(new InputStreamReader(in, UTF_8))) {
                    htmlTemplate = reader.lines().collect(Collectors.joining("\n"));
                }
                final String updatedTemplate = htmlTemplate.replace(HELPER_SERVLET_TEMPLATE_CONFIG_JSON_VAR,
                        json);
                if (resource.equals("/")) {
                    resp.setStatus(HttpServletResponse.SC_OK);
                } else {
                    resp.setStatus(HttpServletResponse.SC_NOT_FOUND);
                }

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

From source file:org.openqa.grid.web.servlet.ProxyStatusServlet.java

License:Apache License

protected void process(HttpRequest request, HttpResponse response) {
    response.setHeader("Content-Type", MediaType.JSON_UTF_8.toString());
    response.setStatus(200);
    try {/*from   w  w w . java 2  s.  c  o m*/
        Object res = getResponse(request);
        response.setContent(json.toJson(res).getBytes(UTF_8));
    } catch (Throwable e) {
        throw new GridException(e.getMessage());
    }
}