LCOV - code coverage report
Current view: top level - httpd/raw - IndexServlet.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 28 31 90.3 %
Date: 2022-11-19 15:00:39 Functions: 4 4 100.0 %

          Line data    Source code
       1             : // Copyright (C) 2017 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.httpd.raw;
      16             : 
      17             : import static java.nio.charset.StandardCharsets.UTF_8;
      18             : import static javax.servlet.http.HttpServletResponse.SC_OK;
      19             : 
      20             : import com.google.common.collect.ImmutableMap;
      21             : import com.google.common.io.Resources;
      22             : import com.google.gerrit.common.Nullable;
      23             : import com.google.gerrit.extensions.api.GerritApi;
      24             : import com.google.gerrit.extensions.restapi.RestApiException;
      25             : import com.google.gerrit.server.experiments.ExperimentFeatures;
      26             : import com.google.template.soy.SoyFileSet;
      27             : import com.google.template.soy.data.SanitizedContent;
      28             : import com.google.template.soy.data.UnsafeSanitizedContentOrdainer;
      29             : import com.google.template.soy.jbcsrc.api.SoySauce;
      30             : import java.io.IOException;
      31             : import java.io.OutputStream;
      32             : import java.net.URISyntaxException;
      33             : import java.util.Map;
      34             : import java.util.function.Function;
      35             : import javax.servlet.http.HttpServlet;
      36             : import javax.servlet.http.HttpServletRequest;
      37             : import javax.servlet.http.HttpServletResponse;
      38             : 
      39             : public class IndexServlet extends HttpServlet {
      40             :   private static final long serialVersionUID = 1L;
      41             :   private static final String POLY_GERRIT_INDEX_HTML_SOY =
      42             :       "com/google/gerrit/httpd/raw/PolyGerritIndexHtml.soy";
      43             : 
      44             :   @Nullable private final String canonicalUrl;
      45             :   @Nullable private final String cdnPath;
      46             :   @Nullable private final String faviconPath;
      47             :   private final GerritApi gerritApi;
      48             :   private final ExperimentFeatures experimentFeatures;
      49             :   private final SoySauce soySauce;
      50             :   private final Function<String, SanitizedContent> urlOrdainer;
      51             : 
      52             :   IndexServlet(
      53             :       @Nullable String canonicalUrl,
      54             :       @Nullable String cdnPath,
      55             :       @Nullable String faviconPath,
      56             :       GerritApi gerritApi,
      57           1 :       ExperimentFeatures experimentFeatures) {
      58           1 :     this.canonicalUrl = canonicalUrl;
      59           1 :     this.cdnPath = cdnPath;
      60           1 :     this.faviconPath = faviconPath;
      61           1 :     this.gerritApi = gerritApi;
      62           1 :     this.experimentFeatures = experimentFeatures;
      63           1 :     this.soySauce =
      64           1 :         SoyFileSet.builder()
      65           1 :             .add(Resources.getResource(POLY_GERRIT_INDEX_HTML_SOY), POLY_GERRIT_INDEX_HTML_SOY)
      66           1 :             .build()
      67           1 :             .compileTemplates();
      68           1 :     this.urlOrdainer =
      69             :         (s) ->
      70           1 :             UnsafeSanitizedContentOrdainer.ordainAsSafe(
      71             :                 s, SanitizedContent.ContentKind.TRUSTED_RESOURCE_URI);
      72           1 :   }
      73             : 
      74             :   @Override
      75             :   protected void doGet(HttpServletRequest req, HttpServletResponse rsp) throws IOException {
      76             :     SoySauce.Renderer renderer;
      77             :     try {
      78           1 :       Map<String, String[]> parameterMap = req.getParameterMap();
      79             :       // TODO(hiesel): Remove URL ordainer as parameter once Soy is consistent
      80           1 :       ImmutableMap<String, Object> templateData =
      81           1 :           IndexHtmlUtil.templateData(
      82             :               gerritApi,
      83             :               experimentFeatures,
      84             :               canonicalUrl,
      85             :               cdnPath,
      86             :               faviconPath,
      87             :               parameterMap,
      88             :               urlOrdainer,
      89           1 :               getRequestUrl(req));
      90           1 :       renderer = soySauce.renderTemplate("com.google.gerrit.httpd.raw.Index").setData(templateData);
      91           0 :     } catch (URISyntaxException | RestApiException e) {
      92           0 :       throw new IOException(e);
      93           1 :     }
      94             : 
      95           1 :     rsp.setCharacterEncoding(UTF_8.name());
      96           1 :     rsp.setContentType("text/html");
      97           1 :     rsp.setStatus(SC_OK);
      98           1 :     try (OutputStream w = rsp.getOutputStream()) {
      99           1 :       w.write(renderer.renderHtml().get().toString().getBytes(UTF_8));
     100             :     }
     101           1 :   }
     102             : 
     103             :   @SuppressWarnings("JdkObsolete")
     104             :   @Nullable
     105             :   private static String getRequestUrl(HttpServletRequest req) {
     106           1 :     if (req.getRequestURL() == null) {
     107           1 :       return null;
     108             :     }
     109           0 :     return req.getRequestURL().toString();
     110             :   }
     111             : }

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