// 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 com.google.common.io.ByteStreams;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.logging.Logger;

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

public class DisplayHelpServlet extends HttpServlet {
  private static final long serialVersionUID = 8484071790930378855L;
  private static final Logger log = Logger.getLogger(DisplayHelpServlet.class.getName());
  private static String coreVersion;
  private static String coreRevision;

  public DisplayHelpServlet() {
    getVersion();
  }


  @Override
  protected void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    process(request, response);
  }

  @Override
  protected void doPost(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    process(request, response);
  }

  protected void process(HttpServletRequest request, HttpServletResponse response)
      throws IOException {
    response.setContentType("text/html");
    response.setCharacterEncoding("UTF-8");
    response.setStatus(200);

    StringBuilder builder = new StringBuilder();

    builder.append("<html>");
    builder.append("<head>");

    builder.append("<title>Selenium Grid2.0 help</title>");


    builder.append("</head>");

    builder.append("<body>");
    builder.append("You are using grid ").append(coreVersion).append(coreRevision);
    builder
        .append("<br>Find help on the official selenium wiki : <a href='https://github.com/SeleniumHQ/selenium/wiki/Grid2' >more help here</a>");
    builder.append("<br>default monitoring page : <a href='/grid/console' >console</a>");

    builder.append("</body>");
    builder.append("</html>");

    InputStream in = new ByteArrayInputStream(builder.toString().getBytes("UTF-8"));
    try {
      ByteStreams.copy(in, response.getOutputStream());
    } finally {
      in.close();
      response.flushBuffer();
    }
  }

  private void getVersion() {
    final Properties p = new Properties();

    InputStream stream =
        Thread.currentThread().getContextClassLoader().getResourceAsStream("VERSION.txt");
    if (stream == null) {
      log.severe("Couldn't determine version number");
      return;
    }
    try {
      p.load(stream);
    } catch (IOException e) {
      log.severe("Cannot load version from VERSION.txt" + e.getMessage());
    }
    coreVersion = p.getProperty("selenium.core.version");
    coreRevision = p.getProperty("selenium.core.revision");
    if (coreVersion == null) {
      log.severe("Cannot load selenium.core.version from VERSION.txt");
    }
  }
}

@@ -986,23 +986,46 @@
 ava.
-util.Properti
+net.URL;%0Aimport java.util.jar.Manif
 es
+t
 ;%0Aim
@@ -1481,46 +1481,8 @@
 ion;
-%0A  private static String coreRevision;
 %0A%0A  
@@ -2263,19 +2263,48 @@
 ium Grid
-2.0
+ %22).append(coreVersion).append(%22
  help%3C/t
@@ -2441,29 +2441,8 @@
 ion)
-.append(coreRevision)
 ;%0A  
@@ -2573,17 +2573,16 @@
 i/Grid2'
- 
 %3Emore he
@@ -2669,17 +2669,16 @@
 console'
- 
 %3Econsole
@@ -3013,408 +3013,529 @@
     
-final Properties p = new Properties();%0A%0A    InputStream stream =%0A        Thread.currentThread().getContextClassLoader().getResourceAsStream(%22VERSION.txt%22);%0A    if (stream == null) %7B%0A      log.severe(%22Couldn't determine version number%22);%0A      return;%0A    %7D%0A    try %7B%0A      p.load(stream);%0A    %7D catch (IOException e) %7B%0A      log.severe(%22Cannot load version from VERSION.txt%22 + e.getMessage()
+InputStream stream = null;%0A    try %7B%0A      String classPath = this.getClass().getResource(this.getClass().getSimpleName() + %22.class%22).toString();%0A      String manifest = classPath.substring(0, classPath.lastIndexOf(%22!%22) + 1) + %22/META-INF/MANIFEST.MF%22;%0A      stream = new URL(manifest).openStream();%0A    %7D catch (Exception e) %7B%0A      e.printStackTrace();%0A    %7D%0A    if (stream == null) %7B%0A      log.severe(%22Couldn't determine version number%22);%0A      return;%0A    %7D%0A    try %7B%0A      Manifest manifest = new Manifest(stream
 );%0A  
-  %7D%0A
     
@@ -3552,135 +3552,111 @@
 n = 
-p.getProperty(%22selenium.core.version%22);%0A    coreRevision = p.getProperty(%22s
+manifest.getEntries().get(%22Build-Info%22).getValue(%22S
 elenium
-.core.revision%22);%0A    if (coreVersion == null
+-Version%22).trim();%0A    %7D catch (IOException e
 ) %7B%0A
@@ -3689,22 +3689,8 @@
 oad 
-selenium.core.
 vers
@@ -3710,16 +3710,33 @@
 ION.txt%22
+ + e.getMessage()
 );%0A    %7D

Fix DisplayHelpServlet so it does not display "nullnull" (#2688)