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