LCOV - code coverage report
Current view: top level - pgm/init - Browser.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 3 54 5.6 %
Date: 2022-11-19 15:00:39 Functions: 1 6 16.7 %

          Line data    Source code
       1             : // Copyright (C) 2009 The Android Open Source Project
       2             : //
       3             : // Licensed under the Apache License, Version 2.0 (the "License");
       4             : // you may not use this file except in compliance with the License.
       5             : // You may obtain a copy of the License at
       6             : //
       7             : // http://www.apache.org/licenses/LICENSE-2.0
       8             : //
       9             : // Unless required by applicable law or agreed to in writing, software
      10             : // distributed under the License is distributed on an "AS IS" BASIS,
      11             : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      12             : // See the License for the specific language governing permissions and
      13             : // limitations under the License.
      14             : 
      15             : package com.google.gerrit.pgm.init;
      16             : 
      17             : import com.google.common.base.Strings;
      18             : import com.google.gerrit.pgm.init.api.InitUtil;
      19             : import com.google.gerrit.server.config.GerritServerConfig;
      20             : import com.google.inject.Inject;
      21             : import java.io.IOException;
      22             : import java.net.Socket;
      23             : import java.net.URI;
      24             : import java.net.URISyntaxException;
      25             : import org.eclipse.jgit.lib.Config;
      26             : 
      27             : /** Opens the user's web browser to the web UI. */
      28             : public class Browser {
      29             :   private final Config cfg;
      30             : 
      31             :   @Inject
      32          15 :   Browser(@GerritServerConfig Config cfg) {
      33          15 :     this.cfg = cfg;
      34          15 :   }
      35             : 
      36             :   public void open() throws Exception {
      37           0 :     open(null /* root page */);
      38           0 :   }
      39             : 
      40             :   public void open(String link) throws Exception {
      41           0 :     String url = cfg.getString("gerrit", null, "canonicalWebUrl");
      42           0 :     if (url == null) {
      43           0 :       url = cfg.getString("httpd", null, "listenUrl");
      44           0 :       if (url == null) {
      45           0 :         return;
      46             :       }
      47           0 :       if (url.startsWith("proxy-")) {
      48           0 :         url = url.substring("proxy-".length());
      49             :       }
      50             :     }
      51             : 
      52             :     final URI uri;
      53             :     try {
      54           0 :       uri = InitUtil.toURI(url);
      55           0 :     } catch (URISyntaxException e) {
      56           0 :       System.err.println("error: invalid httpd.listenUrl: " + url);
      57           0 :       return;
      58           0 :     }
      59           0 :     waitForServer(uri);
      60           0 :     openBrowser(uri, link);
      61           0 :   }
      62             : 
      63             :   private void waitForServer(URI uri) throws IOException {
      64           0 :     String host = uri.getHost();
      65           0 :     int port = InitUtil.portOf(uri);
      66           0 :     System.err.format("Waiting for server on %s:%d ... ", host, port);
      67           0 :     System.err.flush();
      68             :     for (; ; ) {
      69             :       Socket s;
      70             :       try {
      71           0 :         s = new Socket(host, port);
      72           0 :       } catch (IOException e) {
      73             :         try {
      74           0 :           Thread.sleep(100);
      75           0 :         } catch (InterruptedException ie) {
      76             :           // Ignored
      77           0 :         }
      78           0 :         continue;
      79           0 :       }
      80           0 :       s.close();
      81           0 :       break;
      82             :     }
      83           0 :     System.err.println("OK");
      84           0 :   }
      85             : 
      86             :   private String resolveUrl(URI uri, String link) {
      87           0 :     String url = cfg.getString("gerrit", null, "canonicalWebUrl");
      88           0 :     if (Strings.isNullOrEmpty(url)) {
      89           0 :       url = uri.toString();
      90             :     }
      91           0 :     if (!url.endsWith("/")) {
      92           0 :       url += "/";
      93             :     }
      94           0 :     if (!Strings.isNullOrEmpty(link)) {
      95           0 :       url += "#" + link;
      96             :     }
      97           0 :     return url;
      98             :   }
      99             : 
     100             :   private void openBrowser(URI uri, String link) {
     101           0 :     String url = resolveUrl(uri, link);
     102           0 :     System.err.format("Opening %s ...", url);
     103           0 :     System.err.flush();
     104             :     try {
     105           0 :       org.h2.tools.Server.openBrowser(url);
     106           0 :       System.err.println("OK");
     107           0 :     } catch (Exception e) {
     108           0 :       System.err.println("FAILED");
     109           0 :       System.err.println("Open Gerrit with a JavaScript capable browser:");
     110           0 :       System.err.println("  " + url);
     111           0 :     }
     112           0 :   }
     113             : }

Generated by: LCOV version 1.16+git.20220603.dfeb750