blob: 7bbc2f3dfaccf6b4dae8b6e50e6142ec789511d5 [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 com.google.common.base.Function;
20
21import java.io.UnsupportedEncodingException;
22import java.net.URLEncoder;
23
24import javax.servlet.http.HttpServletRequest;
25
26/** Interface for URLs displayed on source browsing pages. */
27public interface GitilesUrls {
28 /**
29 * Escapes repository or path names to be safely embedded into a URL.
30 * <p>
31 * This escape implementation escapes a repository or path name such as
32 * "foo/bar</child" to appear as "foo/bar%3C/child". Spaces are escaped as
33 * "%20". Its purpose is to escape a repository name to be safe for inclusion
34 * in the path component of the URL, where "/" is a valid character that
35 * should not be encoded, while almost any other non-alpha, non-numeric
36 * character will be encoded using URL style encoding.
37 */
38 public static final Function<String, String> NAME_ESCAPER = new Function<String, String>() {
39 @Override
40 public String apply(String s) {
41 try {
David Pletcherd7bdaf32014-08-27 14:50:32 -070042 return URLEncoder.encode(s, UTF_8.name())
Dave Borowitz9de65952012-08-13 16:09:45 -070043 .replace("%2F", "/")
44 .replace("%2f", "/")
45 .replace("+", "%20")
46 .replace("%2B", "+")
47 .replace("%2b", "+");
48 } catch (UnsupportedEncodingException e) {
49 throw new IllegalStateException(e);
50 }
51 }
52 };
53
54 /**
55 * Return the name of the host from the request.
56 *
57 * Used in various user-visible text, like "MyHost Git Repositories".
58 *
59 * @param req request.
60 * @return host name; may be null.
61 */
62 public String getHostName(HttpServletRequest req);
63
64 /**
65 * Return the base URL for git repositories on this host.
66 *
67 * @param req request.
68 * @return base URL for git repositories.
69 */
70 public String getBaseGitUrl(HttpServletRequest req);
71
72 /**
73 * Return the base URL for Gerrit projects on this host.
74 *
75 * @param req request.
76 * @return base URL for Gerrit Code Review, or null if Gerrit is not
77 * configured.
78 */
79 public String getBaseGerritUrl(HttpServletRequest req);
80}