1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * See LICENSE.txt included in this distribution for the specific 9 * language governing permissions and limitations under the License. 10 * 11 * When distributing Covered Code, include this CDDL HEADER in each 12 * file and include the License file at LICENSE.txt. 13 * If applicable, add the following below this CDDL HEADER, with the 14 * fields enclosed by brackets "[]" replaced with your own identifying 15 * information: Portions Copyright [yyyy] [name of copyright owner] 16 * 17 * CDDL HEADER END 18 */ 19 20 /* 21 * Copyright (c) 2011, Jens Elkner. 22 * Copyright (c) 2011, 2019, Oracle and/or its affiliates. All rights reserved. 23 * Portions Copyright (c) 2020, Chris Fraire <cfraire@me.com>. 24 */ 25 package org.opengrok.indexer.web; 26 27 import java.util.Map; 28 import java.util.TreeMap; 29 30 /** 31 * URL Prefixes usually tied to a certain servlet. 32 * 33 * @author Jens Elkner 34 * @version $Revision$ 35 */ 36 public enum Prefix { 37 /** Unknown prefix. */ 38 UNKNOWN(""), 39 /** A cross reference. */ 40 XREF_P("/xref"), 41 /** A show cross reference, i.e. add Line and Navigation toggle button in the menu bar. */ 42 XREF_S("/xr"), 43 /** show more lines. If a search result set for a file matches more lines 44 * than a given limit (default: 10), only the first <i>limit</i> lines gets 45 * shown as an "[all ...]" link, which can be used to show all matching 46 * lines. The servlet path of this link starts with this prefix. */ 47 MORE_P("/more"), 48 /** Reserved (not used). */ 49 MORE_S("/mo"), 50 /** Diff to previous version (link prefix). */ 51 DIFF_P("/diff"), 52 /** Reserved (not used). */ 53 DIFF_S("/di"), 54 /** Reserved (not used). */ 55 HIST_P("/hist"), 56 /** Reserved (not used). */ 57 HIST_S("/hi"), 58 /** Show the history for a file (link prefix). */ 59 HIST_L("/history"), 60 /** RSS XML Feed of latest changes (link prefix). */ 61 RSS_P("/rss"), 62 /** Download file (link prefix). */ 63 DOWNLOAD_P("/download"), 64 /** Raw file display (link prefix). */ 65 RAW_P("/raw"), 66 /** Full-blown search from main page or top bar (link prefix). */ 67 SEARCH_P("/search"), 68 /** Search from cross reference, can lead to direct match (which opens 69 * directly) or to a matches Summary page. */ 70 SEARCH_R("/s"), 71 /** opensearch description page. */ 72 SEARCH_O("/opensearch"), 73 /** Related source file or directory not found/unavailable/ignored. */ 74 NOT_FOUND("/enoent"), 75 /** Misc error occurred. */ 76 ERROR("/error"), 77 /** RESTful API. */ 78 REST_API("/api"), 79 /** Monitoring. */ 80 METRICS("/metrics"), 81 /** CSS and images. */ 82 STATIC("/default"), 83 /** JavaScript. */ 84 JS("/js"), 85 /** JavaScript in Web JARs. */ 86 WEBJARS("/webjars"); 87 88 private final String prefix; Prefix(String prefix)89 Prefix(String prefix) { 90 this.prefix = prefix; 91 } 92 93 /** 94 * Get the string used as prefix. 95 * @return the prefix 96 */ 97 @Override toString()98 public String toString() { 99 return prefix; 100 } 101 102 // should be sufficient for now 103 private static final Map<String, Prefix> lookupTable; 104 static { 105 lookupTable = new TreeMap<>(); 106 for (Prefix p : Prefix.values()) { p.toString()107 lookupTable.put(p.toString(), p); 108 } 109 } 110 111 /** 112 * Get the prefix of the given path. 113 * @param servletPath path to check 114 * @return {@link Prefix#UNKNOWN} if <var>path</var> is {@code null} or has 115 * no or unknown prefix, the corresponding prefix otherwise. 116 * @see #toString() 117 */ get(String servletPath)118 public static Prefix get(String servletPath) { 119 if (servletPath == null || servletPath.length() < 2 || servletPath.charAt(0) != '/') { 120 return UNKNOWN; 121 } 122 int idx = servletPath.indexOf('/', 1); 123 String pathPrefix = (idx == -1) ? 124 servletPath : servletPath.substring(0, idx); 125 Prefix p = lookupTable.get(pathPrefix); 126 return p == null ? UNKNOWN : p; 127 } 128 } 129