blob: 67e42c0b363dc92f5af5ff386b4fc2a3bd2a0546 [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;
Dave Borowitz9de65952012-08-13 16:09:45 -070021import javax.servlet.http.HttpServletRequest;
22
23/**
24 * Default implementation of {@link GitilesUrls}.
Dave Borowitz40255d52016-08-19 16:16:22 -040025 *
26 * <p>This implementation uses statically-configured defaults, and thus assumes that the servlet is
27 * running a single virtual host.
Dave Borowitz9de65952012-08-13 16:09:45 -070028 */
Dave Borowitz46116342013-03-26 23:53:33 -040029public class DefaultUrls implements GitilesUrls {
Dave Borowitz9de65952012-08-13 16:09:45 -070030 private final String canonicalHostName;
31 private final String baseGitUrl;
32 private final String baseGerritUrl;
33
Dave Borowitz46116342013-03-26 23:53:33 -040034 public DefaultUrls(String canonicalHostName, String baseGitUrl, String baseGerritUrl)
Dave Borowitz9de65952012-08-13 16:09:45 -070035 throws UnknownHostException {
36 if (canonicalHostName != null) {
37 this.canonicalHostName = canonicalHostName;
38 } else {
39 this.canonicalHostName = InetAddress.getLocalHost().getCanonicalHostName();
40 }
41 this.baseGitUrl = checkNotNull(baseGitUrl, "baseGitUrl");
Dave Borowitz03397692012-12-18 17:11:16 -080042 this.baseGerritUrl = baseGerritUrl;
Dave Borowitz9de65952012-08-13 16:09:45 -070043 }
44
45 @Override
46 public String getHostName(HttpServletRequest req) {
47 return canonicalHostName;
48 }
49
50 @Override
51 public String getBaseGitUrl(HttpServletRequest req) {
52 return baseGitUrl;
53 }
54
55 @Override
56 public String getBaseGerritUrl(HttpServletRequest req) {
57 return baseGerritUrl;
58 }
59}