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