LCOV - code coverage report
Current view: top level - extensions/restapi - Url.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 6 10 60.0 %
Date: 2022-11-19 15:00:39 Functions: 2 2 100.0 %

          Line data    Source code
       1             : // Copyright (C) 2012 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.extensions.restapi;
      16             : 
      17             : import static java.nio.charset.StandardCharsets.UTF_8;
      18             : 
      19             : import com.google.gerrit.common.Nullable;
      20             : import java.io.UnsupportedEncodingException;
      21             : import java.net.URLDecoder;
      22             : import java.net.URLEncoder;
      23             : 
      24             : /** URL related utility functions. */
      25             : public final class Url {
      26             :   /**
      27             :    * Encode a path segment, escaping characters not valid for a URL.
      28             :    *
      29             :    * <p>The following characters are not escaped:
      30             :    *
      31             :    * <ul>
      32             :    *   <li>{@code a..z, A..Z, 0..9}
      33             :    *   <li>{@code . - * _}
      34             :    * </ul>
      35             :    *
      36             :    * <p>' ' (space) is encoded as '+'.
      37             :    *
      38             :    * <p>All other characters (including '/') are converted to the triplet "%xy" where "xy" is the
      39             :    * hex representation of the character in UTF-8.
      40             :    *
      41             :    * @param component a string containing text to encode.
      42             :    * @return a string with all invalid URL characters escaped.
      43             :    */
      44             :   @Nullable
      45             :   public static String encode(String component) {
      46         151 :     if (component != null) {
      47             :       try {
      48         151 :         return URLEncoder.encode(component, UTF_8.name());
      49           0 :       } catch (UnsupportedEncodingException e) {
      50           0 :         throw new RuntimeException("JVM must support UTF-8", e);
      51             :       }
      52             :     }
      53          23 :     return null;
      54             :   }
      55             : 
      56             :   /** Decode a URL encoded string, e.g. from {@code "%2F"} to {@code "/"}. */
      57             :   @Nullable
      58             :   public static String decode(String str) {
      59         150 :     if (str != null) {
      60             :       try {
      61         150 :         return URLDecoder.decode(str, UTF_8.name());
      62           0 :       } catch (UnsupportedEncodingException e) {
      63           0 :         throw new RuntimeException("JVM must support UTF-8", e);
      64             :       }
      65             :     }
      66          26 :     return null;
      67             :   }
      68             : 
      69             :   private Url() {}
      70             : }

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