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.server.config; 16 : 17 : import com.google.common.base.Strings; 18 : import com.google.gerrit.common.Nullable; 19 : import com.google.gerrit.entities.Change; 20 : import com.google.gerrit.entities.Project; 21 : import java.util.Optional; 22 : 23 : /** 24 : * Formats URLs to different parts of the Gerrit API and UI. 25 : * 26 : * <p>By default, these gerrit URLs are formed by adding suffixes to the web URL. The interface 27 : * centralizes these conventions, and also allows introducing different, custom URL schemes. 28 : * 29 : * <p>Unfortunately, Gerrit operates in modes for which there is no canonical URL. This can be in 30 : * standalone utilities that have no HTTP server (eg. index upgrade commands), in servers that run 31 : * SSH only, or in a HTTP/SSH server that is accessed over SSH without canonical web URL configured. 32 : */ 33 : public interface UrlFormatter { 34 : 35 : /** 36 : * The canonical base URL where this Gerrit installation can be reached. 37 : * 38 : * <p>For the default implementations below to work, it must end in "/". 39 : */ 40 : Optional<String> getWebUrl(); 41 : 42 : /** Returns the URL for viewing a change. */ 43 : default Optional<String> getChangeViewUrl(Project.NameKey project, Change.Id id) { 44 : 45 : // In the PolyGerrit URL (contrary to REST URLs) there is no need to URL-escape strings, since 46 : // the /+/ separator unambiguously defines how to parse the path. 47 103 : return getWebUrl().map(url -> url + "c/" + project.get() + "/+/" + id.get()); 48 : } 49 : 50 : /** Returns the URL for viewing the comment tab view of a change. */ 51 : default Optional<String> getCommentsTabView(Change change) { 52 4 : return getChangeViewUrl(change.getProject(), change.getId()).map(url -> url + "?tab=comments"); 53 : } 54 : 55 : /** Returns the URL for viewing the findings tab view of a change. */ 56 : default Optional<String> getFindingsTabView(Change change) { 57 1 : return getChangeViewUrl(change.getProject(), change.getId()).map(url -> url + "?tab=findings"); 58 : } 59 : 60 : /** Returns the URL for viewing a comment in a file for a change. */ 61 : default Optional<String> getInlineCommentView(Change change, String uuid) { 62 22 : return getChangeViewUrl(change.getProject(), change.getId()) 63 22 : .map(url -> url + "/comment/" + uuid); 64 : } 65 : 66 : /** Returns a URL pointing to the settings page. */ 67 : default Optional<String> getSettingsUrl() { 68 107 : return getWebUrl().map(url -> url + "settings"); 69 : } 70 : 71 : /** 72 : * Returns a URL pointing to a section of the settings page, or the settings page if {@code 73 : * section} is null. 74 : */ 75 : default Optional<String> getSettingsUrl(@Nullable String section) { 76 3 : return Strings.isNullOrEmpty(section) 77 1 : ? getSettingsUrl() 78 3 : : getSettingsUrl().map(url -> url + "#" + section); 79 : } 80 : 81 : /** Returns a URL pointing to a documentation page, at a given named anchor. */ 82 : default Optional<String> getDocUrl(String page, String anchor) { 83 1 : return getWebUrl().map(url -> url + "Documentation/" + page + "#" + anchor); 84 : } 85 : 86 : /** Returns a URL pointing to a plugin documentation page, at a given named anchor. */ 87 : default Optional<String> getPluginDocUrl(String pluginName, String page, String anchor) { 88 0 : return getWebUrl() 89 0 : .map(url -> url + "plugins/" + pluginName + "/Documentation/" + page + "#" + anchor); 90 : } 91 : 92 : /** Returns a REST API URL for a given suffix (eg. "accounts/self/details") */ 93 : default Optional<String> getRestUrl(String suffix) { 94 0 : return getWebUrl().map(url -> url + suffix); 95 : } 96 : }