| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 1 | // 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 | |
| 15 | package com.google.gitiles; |
| 16 | |
| 17 | import static com.google.common.base.Preconditions.checkNotNull; |
| 18 | |
| 19 | import java.net.InetAddress; |
| 20 | import java.net.UnknownHostException; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 21 | import javax.servlet.http.HttpServletRequest; |
| 22 | |
| 23 | /** |
| 24 | * Default implementation of {@link GitilesUrls}. |
| Dave Borowitz | 40255d5 | 2016-08-19 16:16:22 -0400 | [diff] [blame^] | 25 | * |
| 26 | * <p>This implementation uses statically-configured defaults, and thus assumes that the servlet is |
| 27 | * running a single virtual host. |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 28 | */ |
| Dave Borowitz | 4611634 | 2013-03-26 23:53:33 -0400 | [diff] [blame] | 29 | public class DefaultUrls implements GitilesUrls { |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 30 | private final String canonicalHostName; |
| 31 | private final String baseGitUrl; |
| 32 | private final String baseGerritUrl; |
| 33 | |
| Dave Borowitz | 4611634 | 2013-03-26 23:53:33 -0400 | [diff] [blame] | 34 | public DefaultUrls(String canonicalHostName, String baseGitUrl, String baseGerritUrl) |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 35 | 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 Borowitz | 0339769 | 2012-12-18 17:11:16 -0800 | [diff] [blame] | 42 | this.baseGerritUrl = baseGerritUrl; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 43 | } |
| 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 | } |