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