blob: 7218a4b70cc829b6eb7562fd1c4d67ed9e4a3795 [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 static com.google.common.base.Preconditions.checkNotNull;
18
19import java.net.InetAddress;
20import java.net.UnknownHostException;
21
22import javax.servlet.http.HttpServletRequest;
23
24/**
25 * Default implementation of {@link GitilesUrls}.
26 * <p>
27 * This implementation uses statically-configured defaults, and thus assumes
28 * that the servlet is running a single virtual host.
29 */
Dave Borowitz46116342013-03-26 23:53:33 -040030public class DefaultUrls implements GitilesUrls {
Dave Borowitz9de65952012-08-13 16:09:45 -070031 private final String canonicalHostName;
32 private final String baseGitUrl;
33 private final String baseGerritUrl;
34
Dave Borowitz46116342013-03-26 23:53:33 -040035 public DefaultUrls(String canonicalHostName, String baseGitUrl, String baseGerritUrl)
Dave Borowitz9de65952012-08-13 16:09:45 -070036 throws UnknownHostException {
37 if (canonicalHostName != null) {
38 this.canonicalHostName = canonicalHostName;
39 } else {
40 this.canonicalHostName = InetAddress.getLocalHost().getCanonicalHostName();
41 }
42 this.baseGitUrl = checkNotNull(baseGitUrl, "baseGitUrl");
Dave Borowitz03397692012-12-18 17:11:16 -080043 this.baseGerritUrl = baseGerritUrl;
Dave Borowitz9de65952012-08-13 16:09:45 -070044 }
45
46 @Override
47 public String getHostName(HttpServletRequest req) {
48 return canonicalHostName;
49 }
50
51 @Override
52 public String getBaseGitUrl(HttpServletRequest req) {
53 return baseGitUrl;
54 }
55
56 @Override
57 public String getBaseGerritUrl(HttpServletRequest req) {
58 return baseGerritUrl;
59 }
60}