blob: 27d4a4b619fb4c614a30c3df7fdd18e51a6d5745 [file] [log] [blame]
Dave Borowitz9de65952012-08-13 16:09:45 -07001// Copyright 2012 Google Inc. All Rights Reserved.
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
15package com.google.gitiles;
16
David Pletcherd7bdaf32014-08-27 14:50:32 -070017import static java.nio.charset.StandardCharsets.UTF_8;
18
Dave Borowitz9de65952012-08-13 16:09:45 -070019import java.io.UnsupportedEncodingException;
20import java.net.URLEncoder;
Dave Borowitz9de65952012-08-13 16:09:45 -070021import javax.servlet.http.HttpServletRequest;
22
23/** Interface for URLs displayed on source browsing pages. */
24public interface GitilesUrls {
25 /**
26 * Escapes repository or path names to be safely embedded into a URL.
Dave Borowitz40255d52016-08-19 16:16:22 -040027 *
28 * <p>This escape implementation escapes a repository or path name such as "foo/bar&lt;/child" to
29 * appear as "foo/bar%3C/child". Spaces are escaped as "%20". Its purpose is to escape a
30 * repository name to be safe for inclusion in the path component of the URL, where "/" is a valid
31 * character that should not be encoded, while almost any other non-alpha, non-numeric character
32 * will be encoded using URL style encoding.
Dave Borowitz9de65952012-08-13 16:09:45 -070033 */
David Pursehousefa845722016-10-04 16:26:17 +090034 static String escapeName(String name) {
35 try {
36 return URLEncoder.encode(name, UTF_8.name())
37 .replace("%2F", "/")
38 .replace("%2f", "/")
39 .replace("+", "%20")
40 .replace("%2B", "+")
41 .replace("%2b", "+");
42 } catch (UnsupportedEncodingException e) {
43 throw new IllegalStateException(e);
44 }
45 }
Dave Borowitz9de65952012-08-13 16:09:45 -070046
47 /**
48 * Return the name of the host from the request.
49 *
Dave Borowitz40255d52016-08-19 16:16:22 -040050 * <p>Used in various user-visible text, like "MyHost Git Repositories".
Dave Borowitz9de65952012-08-13 16:09:45 -070051 *
52 * @param req request.
53 * @return host name; may be null.
54 */
David Pursehousee3d3ec82016-06-15 22:10:48 +090055 String getHostName(HttpServletRequest req);
Dave Borowitz9de65952012-08-13 16:09:45 -070056
57 /**
58 * Return the base URL for git repositories on this host.
59 *
60 * @param req request.
61 * @return base URL for git repositories.
62 */
David Pursehousee3d3ec82016-06-15 22:10:48 +090063 String getBaseGitUrl(HttpServletRequest req);
Dave Borowitz9de65952012-08-13 16:09:45 -070064
65 /**
66 * Return the base URL for Gerrit projects on this host.
67 *
68 * @param req request.
Dave Borowitz40255d52016-08-19 16:16:22 -040069 * @return base URL for Gerrit Code Review, or null if Gerrit is not configured.
Dave Borowitz9de65952012-08-13 16:09:45 -070070 */
David Pursehousee3d3ec82016-06-15 22:10:48 +090071 String getBaseGerritUrl(HttpServletRequest req);
Dave Borowitz9de65952012-08-13 16:09:45 -070072}