LCOV - code coverage report
Current view: top level - httpd - NumericChangeIdRedirectServlet.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 19 21 90.5 %
Date: 2022-11-19 15:00:39 Functions: 2 2 100.0 %

          Line data    Source code
       1             : // Copyright (C) 2018 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;
      16             : 
      17             : import com.google.gerrit.common.PageLinks;
      18             : import com.google.gerrit.entities.Change;
      19             : import com.google.gerrit.extensions.restapi.ResourceConflictException;
      20             : import com.google.gerrit.extensions.restapi.ResourceNotFoundException;
      21             : import com.google.gerrit.server.change.ChangeResource;
      22             : import com.google.gerrit.server.permissions.PermissionBackendException;
      23             : import com.google.gerrit.server.restapi.change.ChangesCollection;
      24             : import com.google.inject.Inject;
      25             : import com.google.inject.Singleton;
      26             : import java.io.IOException;
      27             : import java.util.Optional;
      28             : import javax.servlet.http.HttpServlet;
      29             : import javax.servlet.http.HttpServletRequest;
      30             : import javax.servlet.http.HttpServletResponse;
      31             : 
      32             : /** Redirects {@code domain.tld/123} to {@code domain.tld/c/project/+/123}. */
      33             : @Singleton
      34             : public class NumericChangeIdRedirectServlet extends HttpServlet {
      35             :   private static final long serialVersionUID = 1L;
      36             : 
      37             :   private final ChangesCollection changesCollection;
      38             : 
      39             :   @Inject
      40          99 :   NumericChangeIdRedirectServlet(ChangesCollection changesCollection) {
      41          99 :     this.changesCollection = changesCollection;
      42          99 :   }
      43             : 
      44             :   @Override
      45             :   protected void doGet(HttpServletRequest req, HttpServletResponse rsp) throws IOException {
      46           1 :     String idString = req.getPathInfo();
      47           1 :     if (idString.endsWith("/")) {
      48           1 :       idString = idString.substring(0, idString.length() - 1);
      49             :     }
      50           1 :     Optional<Change.Id> id = Change.Id.tryParse(idString);
      51           1 :     if (!id.isPresent()) {
      52           1 :       rsp.sendError(HttpServletResponse.SC_NOT_FOUND);
      53           1 :       return;
      54             :     }
      55             : 
      56             :     ChangeResource changeResource;
      57             :     try {
      58           1 :       changeResource = changesCollection.parse(id.get());
      59           1 :     } catch (ResourceConflictException | ResourceNotFoundException e) {
      60           1 :       rsp.sendError(HttpServletResponse.SC_NOT_FOUND);
      61           1 :       return;
      62           0 :     } catch (PermissionBackendException | RuntimeException e) {
      63           0 :       throw new IOException("Unable to lookup change " + id.get(), e);
      64           1 :     }
      65           1 :     String path =
      66           1 :         PageLinks.toChange(changeResource.getProject(), changeResource.getChange().getId());
      67           1 :     UrlModule.toGerrit(path, req, rsp);
      68           1 :   }
      69             : }

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