| 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 | |
| Dave Borowitz | c410f96 | 2014-09-23 10:49:26 -0700 | [diff] [blame] | 17 | import static com.google.common.base.MoreObjects.firstNonNull; |
| 18 | import static com.google.common.base.MoreObjects.toStringHelper; |
| Dave Borowitz | 4e8ffd8 | 2012-12-26 16:01:06 -0800 | [diff] [blame] | 19 | import static com.google.common.base.Preconditions.checkArgument; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 20 | import static com.google.common.base.Preconditions.checkNotNull; |
| Dave Borowitz | 6d9bc5a | 2013-06-19 09:12:52 -0700 | [diff] [blame] | 21 | import static com.google.common.base.Preconditions.checkState; |
| David Pursehouse | fa84572 | 2016-10-04 16:26:17 +0900 | [diff] [blame] | 22 | import static com.google.gitiles.GitilesUrls.escapeName; |
| David Pletcher | d7bdaf3 | 2014-08-27 14:50:32 -0700 | [diff] [blame] | 23 | import static java.nio.charset.StandardCharsets.UTF_8; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 24 | |
| Dave Borowitz | e8a5e36 | 2013-01-14 16:07:26 -0800 | [diff] [blame] | 25 | import com.google.common.annotations.VisibleForTesting; |
| Shawn Pearce | c709c4c | 2015-08-28 15:30:42 -0700 | [diff] [blame] | 26 | import com.google.common.base.Joiner; |
| Dave Borowitz | c410f96 | 2014-09-23 10:49:26 -0700 | [diff] [blame] | 27 | import com.google.common.base.MoreObjects.ToStringHelper; |
| Shawn Pearce | c709c4c | 2015-08-28 15:30:42 -0700 | [diff] [blame] | 28 | import com.google.common.base.Splitter; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 29 | import com.google.common.base.Strings; |
| 30 | import com.google.common.collect.ImmutableList; |
| 31 | import com.google.common.collect.ImmutableMap; |
| 32 | import com.google.common.collect.LinkedListMultimap; |
| 33 | import com.google.common.collect.ListMultimap; |
| 34 | import com.google.common.collect.Multimaps; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 35 | import java.io.UnsupportedEncodingException; |
| 36 | import java.net.URLEncoder; |
| Shawn Pearce | c709c4c | 2015-08-28 15:30:42 -0700 | [diff] [blame] | 37 | import java.util.ArrayList; |
| Dave Borowitz | 2705893 | 2014-12-03 15:44:46 -0800 | [diff] [blame] | 38 | import java.util.Arrays; |
| Dave Borowitz | 6d9bc5a | 2013-06-19 09:12:52 -0700 | [diff] [blame] | 39 | import java.util.EnumSet; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 40 | import java.util.List; |
| 41 | import java.util.Map; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 42 | import javax.servlet.http.HttpServletRequest; |
| Dave Borowitz | 3b744b1 | 2016-08-19 16:11:10 -0400 | [diff] [blame] | 43 | import org.eclipse.jgit.lib.Constants; |
| 44 | import org.eclipse.jgit.revwalk.RevObject; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 45 | |
| 46 | /** |
| 47 | * Information about a view in Gitiles. |
| Dave Borowitz | 40255d5 | 2016-08-19 16:16:22 -0400 | [diff] [blame] | 48 | * |
| 49 | * <p>Views are uniquely identified by a type, and dispatched to servlet types by {@link |
| 50 | * GitilesServlet}. This class contains the list of all types, as well as some methods containing |
| 51 | * basic information parsed from the URL. Construction happens in {@link ViewFilter}. |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 52 | */ |
| 53 | public class GitilesView { |
| Dave Borowitz | 6d9bc5a | 2013-06-19 09:12:52 -0700 | [diff] [blame] | 54 | private static final String DEFAULT_ARCHIVE_EXTENSION = ".tar.gz"; |
| 55 | |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 56 | /** All the possible view types supported in the application. */ |
| David Pursehouse | e3d3ec8 | 2016-06-15 22:10:48 +0900 | [diff] [blame] | 57 | public enum Type { |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 58 | HOST_INDEX, |
| 59 | REPOSITORY_INDEX, |
| Dave Borowitz | 209d0aa | 2012-12-28 14:28:53 -0800 | [diff] [blame] | 60 | REFS, |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 61 | REVISION, |
| 62 | PATH, |
| Shawn Pearce | 353ba2f | 2015-02-12 10:22:37 -0800 | [diff] [blame] | 63 | SHOW, |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 64 | DIFF, |
| Dave Borowitz | ba9c118 | 2013-03-13 14:16:43 -0700 | [diff] [blame] | 65 | LOG, |
| Dave Borowitz | 6d9bc5a | 2013-06-19 09:12:52 -0700 | [diff] [blame] | 66 | DESCRIBE, |
| Dave Borowitz | 68c7a9b | 2014-01-28 12:13:21 -0800 | [diff] [blame] | 67 | ARCHIVE, |
| Shawn Pearce | 374f184 | 2015-02-10 15:36:54 -0800 | [diff] [blame] | 68 | BLAME, |
| Shawn Pearce | 68311c7 | 2015-06-09 17:01:34 -0700 | [diff] [blame] | 69 | DOC, |
| 70 | ROOTED_DOC; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 71 | } |
| 72 | |
| Dave Borowitz | 6221d98 | 2013-01-10 10:39:20 -0800 | [diff] [blame] | 73 | /** Exception thrown when building a view that is invalid. */ |
| 74 | public static class InvalidViewException extends IllegalStateException { |
| 75 | private static final long serialVersionUID = 1L; |
| 76 | |
| 77 | public InvalidViewException(String msg) { |
| 78 | super(msg); |
| 79 | } |
| 80 | } |
| 81 | |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 82 | /** Builder for views. */ |
| 83 | public static class Builder { |
| Shawn Pearce | 68311c7 | 2015-06-09 17:01:34 -0700 | [diff] [blame] | 84 | private Type type; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 85 | private final ListMultimap<String, String> params = LinkedListMultimap.create(); |
| 86 | |
| 87 | private String hostName; |
| 88 | private String servletPath; |
| Shawn Pearce | c709c4c | 2015-08-28 15:30:42 -0700 | [diff] [blame] | 89 | private String repositoryPrefix; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 90 | private String repositoryName; |
| 91 | private Revision revision = Revision.NULL; |
| 92 | private Revision oldRevision = Revision.NULL; |
| 93 | private String path; |
| Dave Borowitz | 6d9bc5a | 2013-06-19 09:12:52 -0700 | [diff] [blame] | 94 | private String extension; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 95 | private String anchor; |
| 96 | |
| 97 | private Builder(Type type) { |
| 98 | this.type = type; |
| 99 | } |
| 100 | |
| 101 | public Builder copyFrom(GitilesView other) { |
| Shawn Pearce | 68311c7 | 2015-06-09 17:01:34 -0700 | [diff] [blame] | 102 | if (type == Type.DOC && other.type == Type.ROOTED_DOC) { |
| 103 | type = Type.ROOTED_DOC; |
| 104 | } |
| 105 | |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 106 | hostName = other.hostName; |
| 107 | servletPath = other.servletPath; |
| 108 | switch (type) { |
| Shawn Pearce | c709c4c | 2015-08-28 15:30:42 -0700 | [diff] [blame] | 109 | case HOST_INDEX: |
| 110 | repositoryPrefix = other.repositoryPrefix; |
| 111 | break; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 112 | case LOG: |
| 113 | case DIFF: |
| 114 | oldRevision = other.oldRevision; |
| David Pursehouse | 0138abf | 2018-07-25 10:00:17 +0100 | [diff] [blame] | 115 | // $FALL-THROUGH$ |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 116 | case PATH: |
| Shawn Pearce | 374f184 | 2015-02-10 15:36:54 -0800 | [diff] [blame] | 117 | case DOC: |
| Shawn Pearce | 68311c7 | 2015-06-09 17:01:34 -0700 | [diff] [blame] | 118 | case ROOTED_DOC: |
| Dave Borowitz | 5051e67 | 2013-11-11 11:09:40 -0800 | [diff] [blame] | 119 | case ARCHIVE: |
| Dave Borowitz | 68c7a9b | 2014-01-28 12:13:21 -0800 | [diff] [blame] | 120 | case BLAME: |
| Shawn Pearce | 353ba2f | 2015-02-12 10:22:37 -0800 | [diff] [blame] | 121 | case SHOW: |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 122 | path = other.path; |
| David Pursehouse | 0138abf | 2018-07-25 10:00:17 +0100 | [diff] [blame] | 123 | // $FALL-THROUGH$ |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 124 | case REVISION: |
| 125 | revision = other.revision; |
| David Pursehouse | 0138abf | 2018-07-25 10:00:17 +0100 | [diff] [blame] | 126 | // $FALL-THROUGH$ |
| Dave Borowitz | ba9c118 | 2013-03-13 14:16:43 -0700 | [diff] [blame] | 127 | case DESCRIBE: |
| Dave Borowitz | 209d0aa | 2012-12-28 14:28:53 -0800 | [diff] [blame] | 128 | case REFS: |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 129 | case REPOSITORY_INDEX: |
| 130 | repositoryName = other.repositoryName; |
| David Pursehouse | 0138abf | 2018-07-25 10:00:17 +0100 | [diff] [blame] | 131 | // $FALL-THROUGH$ |
| Chad Horohoe | ad23f14 | 2012-11-12 09:45:39 -0800 | [diff] [blame] | 132 | default: |
| 133 | break; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 134 | } |
| Dave Borowitz | 58a96f2 | 2014-05-06 14:29:24 -0700 | [diff] [blame] | 135 | if (other.type == type) { |
| 136 | // Only copy params for matching type. |
| 137 | params.putAll(other.params); |
| 138 | if (type == Type.ARCHIVE) { |
| 139 | extension = other.extension; |
| 140 | } |
| Dave Borowitz | 6d9bc5a | 2013-06-19 09:12:52 -0700 | [diff] [blame] | 141 | } |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 142 | return this; |
| 143 | } |
| 144 | |
| 145 | public Builder copyFrom(HttpServletRequest req) { |
| 146 | return copyFrom(ViewFilter.getView(req)); |
| 147 | } |
| 148 | |
| 149 | public Builder setHostName(String hostName) { |
| 150 | this.hostName = checkNotNull(hostName); |
| 151 | return this; |
| 152 | } |
| 153 | |
| 154 | public String getHostName() { |
| 155 | return hostName; |
| 156 | } |
| 157 | |
| 158 | public Builder setServletPath(String servletPath) { |
| 159 | this.servletPath = checkNotNull(servletPath); |
| 160 | return this; |
| 161 | } |
| 162 | |
| 163 | public String getServletPath() { |
| 164 | return servletPath; |
| 165 | } |
| 166 | |
| Shawn Pearce | c709c4c | 2015-08-28 15:30:42 -0700 | [diff] [blame] | 167 | public Builder setRepositoryPrefix(String prefix) { |
| David Pursehouse | cb91aaf | 2016-06-15 22:05:24 +0900 | [diff] [blame] | 168 | if (type == Type.HOST_INDEX) { |
| Dave Borowitz | 40255d5 | 2016-08-19 16:16:22 -0400 | [diff] [blame] | 169 | this.repositoryPrefix = |
| 170 | prefix != null ? Strings.emptyToNull(maybeTrimLeadingAndTrailingSlash(prefix)) : null; |
| David Pursehouse | cb91aaf | 2016-06-15 22:05:24 +0900 | [diff] [blame] | 171 | return this; |
| Shawn Pearce | c709c4c | 2015-08-28 15:30:42 -0700 | [diff] [blame] | 172 | } |
| David Pursehouse | cb91aaf | 2016-06-15 22:05:24 +0900 | [diff] [blame] | 173 | throw new IllegalStateException( |
| 174 | String.format("cannot set repository prefix on %s view", type)); |
| Shawn Pearce | c709c4c | 2015-08-28 15:30:42 -0700 | [diff] [blame] | 175 | } |
| 176 | |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 177 | public Builder setRepositoryName(String repositoryName) { |
| David Pursehouse | cb91aaf | 2016-06-15 22:05:24 +0900 | [diff] [blame] | 178 | if (type == Type.HOST_INDEX) { |
| 179 | throw new IllegalStateException( |
| 180 | String.format("cannot set repository name on %s view", type)); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 181 | } |
| David Pursehouse | cb91aaf | 2016-06-15 22:05:24 +0900 | [diff] [blame] | 182 | this.repositoryName = checkNotNull(repositoryName); |
| 183 | return this; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | public String getRepositoryName() { |
| 187 | return repositoryName; |
| 188 | } |
| 189 | |
| 190 | public Builder setRevision(Revision revision) { |
| 191 | switch (type) { |
| 192 | case HOST_INDEX: |
| 193 | case REPOSITORY_INDEX: |
| Dave Borowitz | 209d0aa | 2012-12-28 14:28:53 -0800 | [diff] [blame] | 194 | case REFS: |
| Dave Borowitz | ba9c118 | 2013-03-13 14:16:43 -0700 | [diff] [blame] | 195 | case DESCRIBE: |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 196 | throw new IllegalStateException(String.format("cannot set revision on %s view", type)); |
| David Pursehouse | cb91aaf | 2016-06-15 22:05:24 +0900 | [diff] [blame] | 197 | case ARCHIVE: |
| 198 | case BLAME: |
| 199 | case DIFF: |
| 200 | case DOC: |
| 201 | case LOG: |
| 202 | case PATH: |
| 203 | case REVISION: |
| 204 | case ROOTED_DOC: |
| 205 | case SHOW: |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 206 | default: |
| 207 | this.revision = checkNotNull(revision); |
| 208 | return this; |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | public Builder setRevision(String name) { |
| 213 | return setRevision(Revision.named(name)); |
| 214 | } |
| 215 | |
| 216 | public Builder setRevision(RevObject obj) { |
| 217 | return setRevision(Revision.peeled(obj.name(), obj)); |
| 218 | } |
| 219 | |
| 220 | public Builder setRevision(String name, RevObject obj) { |
| 221 | return setRevision(Revision.peeled(name, obj)); |
| 222 | } |
| 223 | |
| 224 | public Revision getRevision() { |
| 225 | return revision; |
| 226 | } |
| 227 | |
| 228 | public Builder setOldRevision(Revision revision) { |
| David Pursehouse | cb91aaf | 2016-06-15 22:05:24 +0900 | [diff] [blame] | 229 | if (type != Type.DIFF && type != Type.LOG) { |
| 230 | revision = firstNonNull(revision, Revision.NULL); |
| David Pursehouse | c53de2b | 2019-06-01 15:27:25 +0900 | [diff] [blame] | 231 | checkState(Revision.isNull(revision), "cannot set old revision on %s view", type); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 232 | } |
| Dave Borowitz | 1488fed | 2013-06-26 11:11:40 -0600 | [diff] [blame] | 233 | this.oldRevision = revision; |
| Dave Borowitz | c222cce | 2013-06-19 10:47:06 -0700 | [diff] [blame] | 234 | return this; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | public Builder setOldRevision(RevObject obj) { |
| 238 | return setOldRevision(Revision.peeled(obj.name(), obj)); |
| 239 | } |
| 240 | |
| 241 | public Builder setOldRevision(String name, RevObject obj) { |
| 242 | return setOldRevision(Revision.peeled(name, obj)); |
| 243 | } |
| 244 | |
| 245 | public Revision getOldRevision() { |
| Dave Borowitz | 5d5619d | 2014-04-18 17:01:45 -0700 | [diff] [blame] | 246 | return oldRevision; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 247 | } |
| 248 | |
| Dave Borowitz | dd3c3d9 | 2013-03-11 16:38:41 -0700 | [diff] [blame] | 249 | public Builder setPathPart(String path) { |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 250 | switch (type) { |
| 251 | case PATH: |
| 252 | case DIFF: |
| Shawn Pearce | 353ba2f | 2015-02-12 10:22:37 -0800 | [diff] [blame] | 253 | case SHOW: |
| Dave Borowitz | 1488fed | 2013-06-26 11:11:40 -0600 | [diff] [blame] | 254 | checkState(path != null, "cannot set null path on %s view", type); |
| Dave Borowitz | c222cce | 2013-06-19 10:47:06 -0700 | [diff] [blame] | 255 | break; |
| Dave Borowitz | 68c7a9b | 2014-01-28 12:13:21 -0800 | [diff] [blame] | 256 | case BLAME: |
| Dave Borowitz | 5051e67 | 2013-11-11 11:09:40 -0800 | [diff] [blame] | 257 | case ARCHIVE: |
| Dave Borowitz | ba9c118 | 2013-03-13 14:16:43 -0700 | [diff] [blame] | 258 | case DESCRIBE: |
| Dave Borowitz | d0b7e18 | 2013-01-11 15:55:09 -0800 | [diff] [blame] | 259 | case REFS: |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 260 | case LOG: |
| Shawn Pearce | 374f184 | 2015-02-10 15:36:54 -0800 | [diff] [blame] | 261 | case DOC: |
| Shawn Pearce | 68311c7 | 2015-06-09 17:01:34 -0700 | [diff] [blame] | 262 | case ROOTED_DOC: |
| Dave Borowitz | c222cce | 2013-06-19 10:47:06 -0700 | [diff] [blame] | 263 | break; |
| David Pursehouse | cb91aaf | 2016-06-15 22:05:24 +0900 | [diff] [blame] | 264 | case HOST_INDEX: |
| 265 | case REPOSITORY_INDEX: |
| 266 | case REVISION: |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 267 | default: |
| Dave Borowitz | c222cce | 2013-06-19 10:47:06 -0700 | [diff] [blame] | 268 | checkState(path == null, "cannot set path on %s view", type); |
| Dave Borowitz | c222cce | 2013-06-19 10:47:06 -0700 | [diff] [blame] | 269 | break; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 270 | } |
| Dave Borowitz | 1488fed | 2013-06-26 11:11:40 -0600 | [diff] [blame] | 271 | this.path = path != null ? maybeTrimLeadingAndTrailingSlash(path) : null; |
| Dave Borowitz | c222cce | 2013-06-19 10:47:06 -0700 | [diff] [blame] | 272 | return this; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 273 | } |
| 274 | |
| Dave Borowitz | dd3c3d9 | 2013-03-11 16:38:41 -0700 | [diff] [blame] | 275 | public String getPathPart() { |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 276 | return path; |
| 277 | } |
| 278 | |
| Dave Borowitz | 6d9bc5a | 2013-06-19 09:12:52 -0700 | [diff] [blame] | 279 | public Builder setExtension(String extension) { |
| David Pursehouse | cb91aaf | 2016-06-15 22:05:24 +0900 | [diff] [blame] | 280 | if (type != Type.ARCHIVE) { |
| 281 | checkState(extension == null, "cannot set extension on %s view", type); |
| Dave Borowitz | 6d9bc5a | 2013-06-19 09:12:52 -0700 | [diff] [blame] | 282 | } |
| David Pursehouse | cb91aaf | 2016-06-15 22:05:24 +0900 | [diff] [blame] | 283 | this.extension = extension; |
| Dave Borowitz | 6d9bc5a | 2013-06-19 09:12:52 -0700 | [diff] [blame] | 284 | return this; |
| 285 | } |
| 286 | |
| 287 | public String getExtension() { |
| 288 | return extension; |
| 289 | } |
| 290 | |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 291 | public Builder putParam(String key, String value) { |
| 292 | params.put(key, value); |
| 293 | return this; |
| 294 | } |
| 295 | |
| 296 | public Builder replaceParam(String key, String value) { |
| 297 | params.replaceValues(key, ImmutableList.of(value)); |
| 298 | return this; |
| 299 | } |
| 300 | |
| 301 | public Builder putAllParams(Map<String, String[]> params) { |
| 302 | for (Map.Entry<String, String[]> e : params.entrySet()) { |
| Dave Borowitz | 2705893 | 2014-12-03 15:44:46 -0800 | [diff] [blame] | 303 | this.params.putAll(e.getKey(), Arrays.asList(e.getValue())); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 304 | } |
| 305 | return this; |
| 306 | } |
| 307 | |
| 308 | public ListMultimap<String, String> getParams() { |
| 309 | return params; |
| 310 | } |
| 311 | |
| 312 | public Builder setAnchor(String anchor) { |
| 313 | this.anchor = anchor; |
| 314 | return this; |
| 315 | } |
| 316 | |
| 317 | public String getAnchor() { |
| 318 | return anchor; |
| 319 | } |
| 320 | |
| 321 | public GitilesView build() { |
| 322 | switch (type) { |
| 323 | case HOST_INDEX: |
| 324 | checkHostIndex(); |
| 325 | break; |
| 326 | case REPOSITORY_INDEX: |
| 327 | checkRepositoryIndex(); |
| 328 | break; |
| Dave Borowitz | 209d0aa | 2012-12-28 14:28:53 -0800 | [diff] [blame] | 329 | case REFS: |
| 330 | checkRefs(); |
| 331 | break; |
| Dave Borowitz | ba9c118 | 2013-03-13 14:16:43 -0700 | [diff] [blame] | 332 | case DESCRIBE: |
| 333 | checkDescribe(); |
| 334 | break; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 335 | case REVISION: |
| 336 | checkRevision(); |
| 337 | break; |
| 338 | case PATH: |
| Shawn Pearce | 353ba2f | 2015-02-12 10:22:37 -0800 | [diff] [blame] | 339 | case SHOW: |
| Shawn Pearce | b7e872d | 2015-07-10 15:21:47 -0700 | [diff] [blame] | 340 | case DOC: |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 341 | checkPath(); |
| 342 | break; |
| 343 | case DIFF: |
| 344 | checkDiff(); |
| 345 | break; |
| 346 | case LOG: |
| 347 | checkLog(); |
| 348 | break; |
| Dave Borowitz | 6d9bc5a | 2013-06-19 09:12:52 -0700 | [diff] [blame] | 349 | case ARCHIVE: |
| 350 | checkArchive(); |
| 351 | break; |
| Dave Borowitz | 68c7a9b | 2014-01-28 12:13:21 -0800 | [diff] [blame] | 352 | case BLAME: |
| 353 | checkBlame(); |
| 354 | break; |
| Shawn Pearce | 68311c7 | 2015-06-09 17:01:34 -0700 | [diff] [blame] | 355 | case ROOTED_DOC: |
| 356 | checkRootedDoc(); |
| Shawn Pearce | 374f184 | 2015-02-10 15:36:54 -0800 | [diff] [blame] | 357 | break; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 358 | } |
| Han-Wen Nienhuys | c0200f6 | 2016-05-02 17:34:51 +0200 | [diff] [blame] | 359 | return new GitilesView( |
| 360 | type, |
| 361 | hostName, |
| 362 | servletPath, |
| 363 | repositoryPrefix, |
| 364 | repositoryName, |
| 365 | revision, |
| 366 | oldRevision, |
| 367 | path, |
| 368 | extension, |
| 369 | params, |
| Shawn Pearce | c709c4c | 2015-08-28 15:30:42 -0700 | [diff] [blame] | 370 | anchor); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | public String toUrl() { |
| 374 | return build().toUrl(); |
| 375 | } |
| 376 | |
| Dave Borowitz | 6221d98 | 2013-01-10 10:39:20 -0800 | [diff] [blame] | 377 | private void checkView(boolean expr, String msg, Object... args) { |
| 378 | if (!expr) { |
| 379 | throw new InvalidViewException(String.format(msg, args)); |
| 380 | } |
| 381 | } |
| 382 | |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 383 | private void checkHostIndex() { |
| Dave Borowitz | 6221d98 | 2013-01-10 10:39:20 -0800 | [diff] [blame] | 384 | checkView(hostName != null, "missing hostName on %s view", type); |
| Shawn Pearce | 1b8322a | 2016-05-16 13:14:46 -0600 | [diff] [blame] | 385 | checkView(servletPath != null, "missing servletPath on %s view", type); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | private void checkRepositoryIndex() { |
| Dave Borowitz | 6221d98 | 2013-01-10 10:39:20 -0800 | [diff] [blame] | 389 | checkView(repositoryName != null, "missing repository name on %s view", type); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 390 | checkHostIndex(); |
| 391 | } |
| 392 | |
| Dave Borowitz | 209d0aa | 2012-12-28 14:28:53 -0800 | [diff] [blame] | 393 | private void checkRefs() { |
| 394 | checkRepositoryIndex(); |
| 395 | } |
| 396 | |
| Dave Borowitz | ba9c118 | 2013-03-13 14:16:43 -0700 | [diff] [blame] | 397 | private void checkDescribe() { |
| 398 | checkRepositoryIndex(); |
| 399 | } |
| 400 | |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 401 | private void checkRevision() { |
| David Pursehouse | c53de2b | 2019-06-01 15:27:25 +0900 | [diff] [blame] | 402 | checkView(!Revision.isNull(revision), "missing revision on %s view", type); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 403 | checkRepositoryIndex(); |
| 404 | } |
| 405 | |
| 406 | private void checkDiff() { |
| 407 | checkPath(); |
| 408 | } |
| 409 | |
| 410 | private void checkLog() { |
| Dave Borowitz | 80334b2 | 2013-01-11 14:19:11 -0800 | [diff] [blame] | 411 | checkRepositoryIndex(); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 412 | } |
| 413 | |
| 414 | private void checkPath() { |
| Dave Borowitz | 6221d98 | 2013-01-10 10:39:20 -0800 | [diff] [blame] | 415 | checkView(path != null, "missing path on %s view", type); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 416 | checkRevision(); |
| 417 | } |
| Dave Borowitz | 6d9bc5a | 2013-06-19 09:12:52 -0700 | [diff] [blame] | 418 | |
| 419 | private void checkArchive() { |
| 420 | checkRevision(); |
| 421 | } |
| Dave Borowitz | 68c7a9b | 2014-01-28 12:13:21 -0800 | [diff] [blame] | 422 | |
| 423 | private void checkBlame() { |
| 424 | checkPath(); |
| 425 | } |
| Shawn Pearce | 374f184 | 2015-02-10 15:36:54 -0800 | [diff] [blame] | 426 | |
| Shawn Pearce | 68311c7 | 2015-06-09 17:01:34 -0700 | [diff] [blame] | 427 | private void checkRootedDoc() { |
| 428 | checkView(hostName != null, "missing hostName on %s view", type); |
| 429 | checkView(servletPath != null, "missing hostName on %s view", type); |
| David Pursehouse | c53de2b | 2019-06-01 15:27:25 +0900 | [diff] [blame] | 430 | checkView(!Revision.isNull(revision), "missing revision on %s view", type); |
| Shawn Pearce | 68311c7 | 2015-06-09 17:01:34 -0700 | [diff] [blame] | 431 | checkView(path != null, "missing path on %s view", type); |
| 432 | } |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | public static Builder hostIndex() { |
| 436 | return new Builder(Type.HOST_INDEX); |
| 437 | } |
| 438 | |
| 439 | public static Builder repositoryIndex() { |
| 440 | return new Builder(Type.REPOSITORY_INDEX); |
| 441 | } |
| 442 | |
| Dave Borowitz | 209d0aa | 2012-12-28 14:28:53 -0800 | [diff] [blame] | 443 | public static Builder refs() { |
| 444 | return new Builder(Type.REFS); |
| 445 | } |
| 446 | |
| Dave Borowitz | ba9c118 | 2013-03-13 14:16:43 -0700 | [diff] [blame] | 447 | public static Builder describe() { |
| 448 | return new Builder(Type.DESCRIBE); |
| 449 | } |
| 450 | |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 451 | public static Builder revision() { |
| 452 | return new Builder(Type.REVISION); |
| 453 | } |
| 454 | |
| 455 | public static Builder path() { |
| 456 | return new Builder(Type.PATH); |
| 457 | } |
| 458 | |
| Shawn Pearce | 353ba2f | 2015-02-12 10:22:37 -0800 | [diff] [blame] | 459 | public static Builder show() { |
| 460 | return new Builder(Type.SHOW); |
| 461 | } |
| 462 | |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 463 | public static Builder diff() { |
| 464 | return new Builder(Type.DIFF); |
| 465 | } |
| 466 | |
| 467 | public static Builder log() { |
| 468 | return new Builder(Type.LOG); |
| 469 | } |
| 470 | |
| Dave Borowitz | 6d9bc5a | 2013-06-19 09:12:52 -0700 | [diff] [blame] | 471 | public static Builder archive() { |
| 472 | return new Builder(Type.ARCHIVE); |
| 473 | } |
| 474 | |
| Dave Borowitz | 68c7a9b | 2014-01-28 12:13:21 -0800 | [diff] [blame] | 475 | public static Builder blame() { |
| 476 | return new Builder(Type.BLAME); |
| 477 | } |
| 478 | |
| Shawn Pearce | 374f184 | 2015-02-10 15:36:54 -0800 | [diff] [blame] | 479 | public static Builder doc() { |
| 480 | return new Builder(Type.DOC); |
| 481 | } |
| 482 | |
| Shawn Pearce | 68311c7 | 2015-06-09 17:01:34 -0700 | [diff] [blame] | 483 | public static Builder rootedDoc() { |
| 484 | return new Builder(Type.ROOTED_DOC); |
| 485 | } |
| 486 | |
| Dave Borowitz | d0b7e18 | 2013-01-11 15:55:09 -0800 | [diff] [blame] | 487 | static String maybeTrimLeadingAndTrailingSlash(String str) { |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 488 | if (str.startsWith("/")) { |
| 489 | str = str.substring(1); |
| 490 | } |
| 491 | return !str.isEmpty() && str.endsWith("/") ? str.substring(0, str.length() - 1) : str; |
| 492 | } |
| 493 | |
| 494 | private final Type type; |
| 495 | private final String hostName; |
| 496 | private final String servletPath; |
| Shawn Pearce | c709c4c | 2015-08-28 15:30:42 -0700 | [diff] [blame] | 497 | private final String repositoryPrefix; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 498 | private final String repositoryName; |
| 499 | private final Revision revision; |
| 500 | private final Revision oldRevision; |
| 501 | private final String path; |
| Dave Borowitz | 6d9bc5a | 2013-06-19 09:12:52 -0700 | [diff] [blame] | 502 | private final String extension; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 503 | private final ListMultimap<String, String> params; |
| 504 | private final String anchor; |
| 505 | |
| Han-Wen Nienhuys | c0200f6 | 2016-05-02 17:34:51 +0200 | [diff] [blame] | 506 | private GitilesView( |
| 507 | Type type, |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 508 | String hostName, |
| 509 | String servletPath, |
| Shawn Pearce | c709c4c | 2015-08-28 15:30:42 -0700 | [diff] [blame] | 510 | String repositoryPrefix, |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 511 | String repositoryName, |
| 512 | Revision revision, |
| 513 | Revision oldRevision, |
| 514 | String path, |
| Dave Borowitz | 6d9bc5a | 2013-06-19 09:12:52 -0700 | [diff] [blame] | 515 | String extension, |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 516 | ListMultimap<String, String> params, |
| 517 | String anchor) { |
| 518 | this.type = type; |
| 519 | this.hostName = hostName; |
| 520 | this.servletPath = servletPath; |
| Shawn Pearce | c709c4c | 2015-08-28 15:30:42 -0700 | [diff] [blame] | 521 | this.repositoryPrefix = repositoryPrefix; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 522 | this.repositoryName = repositoryName; |
| Dave Borowitz | c410f96 | 2014-09-23 10:49:26 -0700 | [diff] [blame] | 523 | this.revision = firstNonNull(revision, Revision.NULL); |
| 524 | this.oldRevision = firstNonNull(oldRevision, Revision.NULL); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 525 | this.path = path; |
| Dave Borowitz | 6d9bc5a | 2013-06-19 09:12:52 -0700 | [diff] [blame] | 526 | this.extension = extension; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 527 | this.params = Multimaps.unmodifiableListMultimap(params); |
| 528 | this.anchor = anchor; |
| 529 | } |
| 530 | |
| Dave Borowitz | e02bd42 | 2014-05-01 11:44:39 -0700 | [diff] [blame] | 531 | public Builder copyFrom(GitilesView other) { |
| 532 | return new Builder(other.type).copyFrom(this); |
| 533 | } |
| 534 | |
| 535 | public Builder toBuilder() { |
| 536 | return copyFrom(this); |
| 537 | } |
| 538 | |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 539 | public String getHostName() { |
| 540 | return hostName; |
| 541 | } |
| 542 | |
| 543 | public String getServletPath() { |
| 544 | return servletPath; |
| 545 | } |
| 546 | |
| Shawn Pearce | c709c4c | 2015-08-28 15:30:42 -0700 | [diff] [blame] | 547 | public String getRepositoryPrefix() { |
| 548 | return repositoryPrefix; |
| 549 | } |
| 550 | |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 551 | public String getRepositoryName() { |
| 552 | return repositoryName; |
| 553 | } |
| 554 | |
| 555 | public Revision getRevision() { |
| 556 | return revision; |
| 557 | } |
| 558 | |
| 559 | public Revision getOldRevision() { |
| 560 | return oldRevision; |
| 561 | } |
| 562 | |
| 563 | public String getRevisionRange() { |
| David Pursehouse | c53de2b | 2019-06-01 15:27:25 +0900 | [diff] [blame] | 564 | if (Revision.isNull(oldRevision)) { |
| David Pursehouse | cb91aaf | 2016-06-15 22:05:24 +0900 | [diff] [blame] | 565 | if (type == Type.LOG || type == Type.DIFF) { |
| 566 | // For types that require two revisions, NULL indicates the empty |
| 567 | // tree/commit. |
| 568 | return revision.getName() + "^!"; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 569 | } |
| David Pursehouse | cb91aaf | 2016-06-15 22:05:24 +0900 | [diff] [blame] | 570 | // For everything else NULL indicates it is not a range, just a single |
| 571 | // revision. |
| 572 | return null; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 573 | } else if (type == Type.DIFF && isFirstParent(revision, oldRevision)) { |
| 574 | return revision.getName() + "^!"; |
| 575 | } else { |
| 576 | return oldRevision.getName() + ".." + revision.getName(); |
| 577 | } |
| 578 | } |
| 579 | |
| Dave Borowitz | dd3c3d9 | 2013-03-11 16:38:41 -0700 | [diff] [blame] | 580 | public String getPathPart() { |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 581 | return path; |
| 582 | } |
| 583 | |
| Dave Borowitz | 6d9bc5a | 2013-06-19 09:12:52 -0700 | [diff] [blame] | 584 | public String getExtension() { |
| 585 | return extension; |
| 586 | } |
| 587 | |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 588 | public ListMultimap<String, String> getParameters() { |
| 589 | return params; |
| 590 | } |
| 591 | |
| 592 | public String getAnchor() { |
| 593 | return anchor; |
| 594 | } |
| 595 | |
| 596 | public Type getType() { |
| 597 | return type; |
| 598 | } |
| 599 | |
| Dave Borowitz | 5530a16 | 2013-06-19 15:14:47 -0700 | [diff] [blame] | 600 | @Override |
| 601 | public String toString() { |
| Han-Wen Nienhuys | c0200f6 | 2016-05-02 17:34:51 +0200 | [diff] [blame] | 602 | ToStringHelper b = |
| 603 | toStringHelper(type.toString()) |
| 604 | .omitNullValues() |
| 605 | .add("host", hostName) |
| 606 | .add("servlet", servletPath) |
| 607 | .add("prefix", repositoryPrefix) |
| 608 | .add("repo", repositoryName) |
| 609 | .add("rev", revision) |
| 610 | .add("old", oldRevision) |
| 611 | .add("path", path) |
| 612 | .add("extension", extension); |
| Dave Borowitz | 5530a16 | 2013-06-19 15:14:47 -0700 | [diff] [blame] | 613 | if (!params.isEmpty()) { |
| 614 | b.add("params", params); |
| 615 | } |
| 616 | b.add("anchor", anchor); |
| 617 | return b.toString(); |
| 618 | } |
| 619 | |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 620 | /** @return an escaped, relative URL representing this view. */ |
| 621 | public String toUrl() { |
| 622 | StringBuilder url = new StringBuilder(servletPath).append('/'); |
| 623 | ListMultimap<String, String> params = this.params; |
| 624 | switch (type) { |
| 625 | case HOST_INDEX: |
| Shawn Pearce | c709c4c | 2015-08-28 15:30:42 -0700 | [diff] [blame] | 626 | if (repositoryPrefix != null) { |
| 627 | url.append(repositoryPrefix).append('/'); |
| 628 | } |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 629 | params = LinkedListMultimap.create(); |
| Shawn Pearce | c709c4c | 2015-08-28 15:30:42 -0700 | [diff] [blame] | 630 | if (repositoryPrefix == null && !this.params.containsKey("format")) { |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 631 | params.put("format", FormatType.HTML.toString()); |
| 632 | } |
| 633 | params.putAll(this.params); |
| 634 | break; |
| 635 | case REPOSITORY_INDEX: |
| 636 | url.append(repositoryName).append('/'); |
| 637 | break; |
| Dave Borowitz | 209d0aa | 2012-12-28 14:28:53 -0800 | [diff] [blame] | 638 | case REFS: |
| 639 | url.append(repositoryName).append("/+refs"); |
| 640 | break; |
| Dave Borowitz | ba9c118 | 2013-03-13 14:16:43 -0700 | [diff] [blame] | 641 | case DESCRIBE: |
| 642 | url.append(repositoryName).append("/+describe"); |
| 643 | break; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 644 | case REVISION: |
| Dave Borowitz | d3e6dd7 | 2012-12-20 15:48:24 -0800 | [diff] [blame] | 645 | url.append(repositoryName).append("/+/").append(revision.getName()); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 646 | break; |
| Dave Borowitz | 6d9bc5a | 2013-06-19 09:12:52 -0700 | [diff] [blame] | 647 | case ARCHIVE: |
| Dave Borowitz | 5051e67 | 2013-11-11 11:09:40 -0800 | [diff] [blame] | 648 | url.append(repositoryName).append("/+archive/").append(revision.getName()); |
| 649 | if (path != null) { |
| 650 | url.append('/').append(path); |
| 651 | } |
| Dave Borowitz | c410f96 | 2014-09-23 10:49:26 -0700 | [diff] [blame] | 652 | url.append(firstNonNull(extension, DEFAULT_ARCHIVE_EXTENSION)); |
| Dave Borowitz | 6d9bc5a | 2013-06-19 09:12:52 -0700 | [diff] [blame] | 653 | break; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 654 | case PATH: |
| Han-Wen Nienhuys | c0200f6 | 2016-05-02 17:34:51 +0200 | [diff] [blame] | 655 | url.append(repositoryName) |
| 656 | .append("/+/") |
| 657 | .append(revision.getName()) |
| 658 | .append('/') |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 659 | .append(path); |
| 660 | break; |
| Shawn Pearce | 353ba2f | 2015-02-12 10:22:37 -0800 | [diff] [blame] | 661 | case SHOW: |
| Han-Wen Nienhuys | c0200f6 | 2016-05-02 17:34:51 +0200 | [diff] [blame] | 662 | url.append(repositoryName) |
| 663 | .append("/+show/") |
| 664 | .append(revision.getName()) |
| 665 | .append('/') |
| 666 | .append(path); |
| Shawn Pearce | 353ba2f | 2015-02-12 10:22:37 -0800 | [diff] [blame] | 667 | break; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 668 | case DIFF: |
| 669 | url.append(repositoryName).append("/+/"); |
| 670 | if (isFirstParent(revision, oldRevision)) { |
| 671 | url.append(revision.getName()).append("^!"); |
| 672 | } else { |
| 673 | url.append(oldRevision.getName()).append("..").append(revision.getName()); |
| 674 | } |
| 675 | url.append('/').append(path); |
| 676 | break; |
| 677 | case LOG: |
| Dave Borowitz | 80334b2 | 2013-01-11 14:19:11 -0800 | [diff] [blame] | 678 | url.append(repositoryName).append("/+log"); |
| David Pursehouse | c53de2b | 2019-06-01 15:27:25 +0900 | [diff] [blame] | 679 | if (!Revision.isNull(revision)) { |
| Dave Borowitz | 80334b2 | 2013-01-11 14:19:11 -0800 | [diff] [blame] | 680 | url.append('/'); |
| David Pursehouse | c53de2b | 2019-06-01 15:27:25 +0900 | [diff] [blame] | 681 | if (!Revision.isNull(oldRevision)) { |
| Dave Borowitz | 80334b2 | 2013-01-11 14:19:11 -0800 | [diff] [blame] | 682 | url.append(oldRevision.getName()).append(".."); |
| 683 | } |
| 684 | url.append(revision.getName()); |
| 685 | if (path != null) { |
| 686 | url.append('/').append(path); |
| 687 | } |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 688 | } |
| 689 | break; |
| Dave Borowitz | 68c7a9b | 2014-01-28 12:13:21 -0800 | [diff] [blame] | 690 | case BLAME: |
| Han-Wen Nienhuys | c0200f6 | 2016-05-02 17:34:51 +0200 | [diff] [blame] | 691 | url.append(repositoryName) |
| 692 | .append("/+blame/") |
| 693 | .append(revision.getName()) |
| 694 | .append('/') |
| Dave Borowitz | 68c7a9b | 2014-01-28 12:13:21 -0800 | [diff] [blame] | 695 | .append(path); |
| 696 | break; |
| Shawn Pearce | 374f184 | 2015-02-10 15:36:54 -0800 | [diff] [blame] | 697 | case DOC: |
| Shawn Pearce | 353ba2f | 2015-02-12 10:22:37 -0800 | [diff] [blame] | 698 | url.append(repositoryName); |
| 699 | if (path != null && path.endsWith(".md")) { |
| 700 | url.append("/+/"); |
| 701 | } else { |
| 702 | url.append("/+doc/"); |
| 703 | } |
| 704 | url.append(revision.getName()); |
| Shawn Pearce | 374f184 | 2015-02-10 15:36:54 -0800 | [diff] [blame] | 705 | if (path != null) { |
| 706 | url.append('/').append(path); |
| 707 | } |
| 708 | break; |
| Shawn Pearce | 68311c7 | 2015-06-09 17:01:34 -0700 | [diff] [blame] | 709 | case ROOTED_DOC: |
| 710 | if (path != null) { |
| 711 | url.append(path); |
| 712 | } |
| 713 | break; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 714 | default: |
| 715 | throw new IllegalStateException("Unknown view type: " + type); |
| 716 | } |
| David Pursehouse | fa84572 | 2016-10-04 16:26:17 +0900 | [diff] [blame] | 717 | String baseUrl = escapeName(url.toString()); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 718 | url = new StringBuilder(); |
| 719 | if (!params.isEmpty()) { |
| 720 | url.append('?').append(paramsToString(params)); |
| 721 | } |
| 722 | if (!Strings.isNullOrEmpty(anchor)) { |
| David Pursehouse | fa84572 | 2016-10-04 16:26:17 +0900 | [diff] [blame] | 723 | url.append('#').append(escapeName(anchor)); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 724 | } |
| Dave Borowitz | 2705893 | 2014-12-03 15:44:46 -0800 | [diff] [blame] | 725 | return baseUrl + url; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 726 | } |
| 727 | |
| Dave Borowitz | 4e8ffd8 | 2012-12-26 16:01:06 -0800 | [diff] [blame] | 728 | /** |
| Dave Borowitz | 40255d5 | 2016-08-19 16:16:22 -0400 | [diff] [blame] | 729 | * @return a list of maps with "text" and "url" keys for all file paths leading up to the path |
| 730 | * represented by this view. All URLs allow auto-diving into one-entry subtrees; see also |
| Dave Borowitz | 33d4fda | 2013-10-22 16:40:20 -0700 | [diff] [blame] | 731 | * {@link #getBreadcrumbs(List)}. |
| Dave Borowitz | 4e8ffd8 | 2012-12-26 16:01:06 -0800 | [diff] [blame] | 732 | */ |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 733 | public List<Map<String, String>> getBreadcrumbs() { |
| Dave Borowitz | 4e8ffd8 | 2012-12-26 16:01:06 -0800 | [diff] [blame] | 734 | return getBreadcrumbs(null); |
| 735 | } |
| 736 | |
| Dave Borowitz | 6d9bc5a | 2013-06-19 09:12:52 -0700 | [diff] [blame] | 737 | private static final EnumSet<Type> NON_HTML_TYPES = EnumSet.of(Type.DESCRIBE, Type.ARCHIVE); |
| 738 | |
| Dave Borowitz | 4e8ffd8 | 2012-12-26 16:01:06 -0800 | [diff] [blame] | 739 | /** |
| Dave Borowitz | 40255d5 | 2016-08-19 16:16:22 -0400 | [diff] [blame] | 740 | * @param hasSingleTree list of booleans, one per path entry in this view's path excluding the |
| 741 | * leaf. True entries indicate the tree at that path only has a single entry that is another |
| 742 | * tree. |
| 743 | * @return a list of maps with "text" and "url" keys for all file paths leading up to the path |
| 744 | * represented by this view. URLs whose corresponding entry in {@code hasSingleTree} is true |
| 745 | * will disable auto-diving into one-entry subtrees. |
| Dave Borowitz | 4e8ffd8 | 2012-12-26 16:01:06 -0800 | [diff] [blame] | 746 | */ |
| 747 | public List<Map<String, String>> getBreadcrumbs(List<Boolean> hasSingleTree) { |
| Han-Wen Nienhuys | c0200f6 | 2016-05-02 17:34:51 +0200 | [diff] [blame] | 748 | checkArgument(!NON_HTML_TYPES.contains(type), "breadcrumbs for %s view not supported", type); |
| 749 | checkArgument( |
| 750 | type != Type.REFS || Strings.isNullOrEmpty(path), |
| Dave Borowitz | d0b7e18 | 2013-01-11 15:55:09 -0800 | [diff] [blame] | 751 | "breadcrumbs for REFS view with path not supported"); |
| Han-Wen Nienhuys | c0200f6 | 2016-05-02 17:34:51 +0200 | [diff] [blame] | 752 | checkArgument( |
| 753 | hasSingleTree == null || type == Type.PATH, "hasSingleTree must be null for %s view", type); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 754 | String path = this.path; |
| 755 | ImmutableList.Builder<Map<String, String>> breadcrumbs = ImmutableList.builder(); |
| Shawn Pearce | c709c4c | 2015-08-28 15:30:42 -0700 | [diff] [blame] | 756 | breadcrumbs.add(breadcrumb(hostName, hostIndex().copyFrom(this).setRepositoryPrefix(null))); |
| 757 | if (repositoryPrefix != null) { |
| 758 | breadcrumbs.addAll(hostIndexBreadcrumbs(repositoryPrefix)); |
| 759 | } else if (repositoryName != null) { |
| 760 | breadcrumbs.addAll(hostIndexBreadcrumbs(repositoryName)); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 761 | } |
| 762 | if (type == Type.DIFF) { |
| 763 | // TODO(dborowitz): Tweak the breadcrumbs template to allow us to render |
| 764 | // separate links in "old..new". |
| Dave Borowitz | dd3c3d9 | 2013-03-11 16:38:41 -0700 | [diff] [blame] | 765 | breadcrumbs.add(breadcrumb(getRevisionRange(), diff().copyFrom(this).setPathPart(""))); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 766 | } else if (type == Type.LOG) { |
| David Pursehouse | c53de2b | 2019-06-01 15:27:25 +0900 | [diff] [blame] | 767 | if (!Revision.isNull(revision)) { |
| Dave Borowitz | 80334b2 | 2013-01-11 14:19:11 -0800 | [diff] [blame] | 768 | // TODO(dborowitz): Add something in the navigation area (probably not |
| 769 | // a breadcrumb) to allow switching between /+log/ and /+/. |
| David Pursehouse | c53de2b | 2019-06-01 15:27:25 +0900 | [diff] [blame] | 770 | if (Revision.isNull(oldRevision)) { |
| Dave Borowitz | dd3c3d9 | 2013-03-11 16:38:41 -0700 | [diff] [blame] | 771 | breadcrumbs.add(breadcrumb(revision.getName(), log().copyFrom(this).setPathPart(null))); |
| Dave Borowitz | 80334b2 | 2013-01-11 14:19:11 -0800 | [diff] [blame] | 772 | } else { |
| Dave Borowitz | dd3c3d9 | 2013-03-11 16:38:41 -0700 | [diff] [blame] | 773 | breadcrumbs.add(breadcrumb(getRevisionRange(), log().copyFrom(this).setPathPart(null))); |
| Dave Borowitz | 80334b2 | 2013-01-11 14:19:11 -0800 | [diff] [blame] | 774 | } |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 775 | } else { |
| Dave Borowitz | 80334b2 | 2013-01-11 14:19:11 -0800 | [diff] [blame] | 776 | breadcrumbs.add(breadcrumb(Constants.HEAD, log().copyFrom(this))); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 777 | } |
| 778 | path = Strings.emptyToNull(path); |
| David Pursehouse | c53de2b | 2019-06-01 15:27:25 +0900 | [diff] [blame] | 779 | } else if (!Revision.isNull(revision)) { |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 780 | breadcrumbs.add(breadcrumb(revision.getName(), revision().copyFrom(this))); |
| 781 | } |
| 782 | if (path != null) { |
| Dave Borowitz | d0b7e18 | 2013-01-11 15:55:09 -0800 | [diff] [blame] | 783 | if (type != Type.LOG && type != Type.REFS) { |
| 784 | // The "." breadcrumb would be no different for LOG or REFS. |
| Dave Borowitz | 68c7a9b | 2014-01-28 12:13:21 -0800 | [diff] [blame] | 785 | breadcrumbs.add(breadcrumb(".", copyWithPath(false).setPathPart(""))); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 786 | } |
| 787 | StringBuilder cur = new StringBuilder(); |
| Dave Borowitz | cfc1c53 | 2015-02-18 13:41:19 -0800 | [diff] [blame] | 788 | List<String> parts = PathUtil.SPLITTER.omitEmptyStrings().splitToList(path); |
| Han-Wen Nienhuys | c0200f6 | 2016-05-02 17:34:51 +0200 | [diff] [blame] | 789 | checkArgument( |
| 790 | hasSingleTree == null |
| 791 | || (parts.isEmpty() && hasSingleTree.isEmpty()) |
| 792 | || hasSingleTree.size() == parts.size() - 1, |
| Dave Borowitz | 4e8ffd8 | 2012-12-26 16:01:06 -0800 | [diff] [blame] | 793 | "hasSingleTree has wrong number of entries"); |
| 794 | for (int i = 0; i < parts.size(); i++) { |
| 795 | String part = parts.get(i); |
| 796 | cur.append(part).append('/'); |
| 797 | String curPath = cur.toString(); |
| Dave Borowitz | 68c7a9b | 2014-01-28 12:13:21 -0800 | [diff] [blame] | 798 | boolean isLeaf = i == parts.size() - 1; |
| 799 | Builder builder = copyWithPath(isLeaf).setPathPart(curPath); |
| Dave Borowitz | 4e8ffd8 | 2012-12-26 16:01:06 -0800 | [diff] [blame] | 800 | if (hasSingleTree != null && i < parts.size() - 1 && hasSingleTree.get(i)) { |
| 801 | builder.replaceParam(PathServlet.AUTODIVE_PARAM, PathServlet.NO_AUTODIVE_VALUE); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 802 | } |
| Dave Borowitz | 4e8ffd8 | 2012-12-26 16:01:06 -0800 | [diff] [blame] | 803 | breadcrumbs.add(breadcrumb(part, builder)); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 804 | } |
| 805 | } |
| 806 | return breadcrumbs.build(); |
| 807 | } |
| 808 | |
| Shawn Pearce | c709c4c | 2015-08-28 15:30:42 -0700 | [diff] [blame] | 809 | private List<Map<String, String>> hostIndexBreadcrumbs(String name) { |
| 810 | List<String> parts = Splitter.on('/').splitToList(name); |
| 811 | List<Map<String, String>> r = new ArrayList<>(parts.size()); |
| 812 | for (int i = 0; i < parts.size(); i++) { |
| 813 | String prefix = Joiner.on('/').join(parts.subList(0, i + 1)); |
| Han-Wen Nienhuys | c0200f6 | 2016-05-02 17:34:51 +0200 | [diff] [blame] | 814 | r.add(breadcrumb(parts.get(i), hostIndex().copyFrom(this).setRepositoryPrefix(prefix))); |
| Shawn Pearce | c709c4c | 2015-08-28 15:30:42 -0700 | [diff] [blame] | 815 | } |
| 816 | return r; |
| 817 | } |
| 818 | |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 819 | private static Map<String, String> breadcrumb(String text, Builder url) { |
| 820 | return ImmutableMap.of("text", text, "url", url.toUrl()); |
| 821 | } |
| 822 | |
| Dave Borowitz | 68c7a9b | 2014-01-28 12:13:21 -0800 | [diff] [blame] | 823 | private Builder copyWithPath(boolean isLeaf) { |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 824 | Builder copy; |
| 825 | switch (type) { |
| 826 | case DIFF: |
| 827 | copy = diff(); |
| 828 | break; |
| 829 | case LOG: |
| 830 | copy = log(); |
| 831 | break; |
| Dave Borowitz | 68c7a9b | 2014-01-28 12:13:21 -0800 | [diff] [blame] | 832 | case BLAME: |
| 833 | copy = isLeaf ? blame() : path(); |
| 834 | break; |
| David Pursehouse | cb91aaf | 2016-06-15 22:05:24 +0900 | [diff] [blame] | 835 | case ARCHIVE: |
| 836 | case DESCRIBE: |
| 837 | case DOC: |
| 838 | case HOST_INDEX: |
| 839 | case PATH: |
| 840 | case REFS: |
| 841 | case REPOSITORY_INDEX: |
| 842 | case REVISION: |
| 843 | case ROOTED_DOC: |
| 844 | case SHOW: |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 845 | default: |
| 846 | copy = path(); |
| 847 | break; |
| 848 | } |
| 849 | return copy.copyFrom(this); |
| 850 | } |
| 851 | |
| 852 | private static boolean isFirstParent(Revision rev1, Revision rev2) { |
| David Pursehouse | c53de2b | 2019-06-01 15:27:25 +0900 | [diff] [blame] | 853 | return Revision.isNull(rev2) |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 854 | || rev2.getName().equals(rev1.getName() + "^") |
| 855 | || rev2.getName().equals(rev1.getName() + "~1"); |
| 856 | } |
| 857 | |
| Dave Borowitz | e8a5e36 | 2013-01-14 16:07:26 -0800 | [diff] [blame] | 858 | @VisibleForTesting |
| 859 | static String paramsToString(ListMultimap<String, String> params) { |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 860 | try { |
| Han-Wen Nienhuys | c0200f6 | 2016-05-02 17:34:51 +0200 | [diff] [blame] | 861 | StringBuilder sb = new StringBuilder(); |
| 862 | boolean first = true; |
| 863 | for (Map.Entry<String, String> e : params.entries()) { |
| 864 | if (!first) { |
| 865 | sb.append('&'); |
| 866 | } else { |
| 867 | first = false; |
| 868 | } |
| 869 | sb.append(URLEncoder.encode(e.getKey(), UTF_8.name())); |
| 870 | if (!"".equals(e.getValue())) { |
| 871 | sb.append('=').append(URLEncoder.encode(e.getValue(), UTF_8.name())); |
| 872 | } |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 873 | } |
| Han-Wen Nienhuys | c0200f6 | 2016-05-02 17:34:51 +0200 | [diff] [blame] | 874 | return sb.toString(); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 875 | } catch (UnsupportedEncodingException e) { |
| 876 | throw new IllegalStateException(e); |
| 877 | } |
| 878 | } |
| 879 | } |