001// ***************************************************************************************************************************
002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.  See the NOTICE file *
003// * distributed with this work for additional information regarding copyright ownership.  The ASF licenses this file        *
004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance            *
005// * with the License.  You may obtain a copy of the License at                                                              *
006// *                                                                                                                         *
007// *  http://www.apache.org/licenses/LICENSE-2.0                                                                             *
008// *                                                                                                                         *
009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an  *
010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the        *
011// * specific language governing permissions and limitations under the License.                                              *
012// ***************************************************************************************************************************
013package org.apache.juneau.rest.util;
014
015import static org.apache.juneau.internal.StringUtils.*;
016
017import org.apache.juneau.*;
018import org.apache.juneau.marshall.*;
019
020/**
021 * Represents a parsed URL path-info string.
022 */
023public class UrlPathInfo {
024
025   final String[] parts;
026   final String path;
027
028   /**
029    * Constructor.
030    *
031    * @param path The path.
032    */
033   public UrlPathInfo(String path) {
034      if (path != null && ! path.startsWith("/"))
035         throw new RuntimeException("Invalid path specified.  Must be null or start with '/' per HttpServletRequest.getPathInfo().");
036      this.path = path;
037      parts = path == null ? new String[0] : split(path.substring(1), '/');
038      for (int i = 0; i < parts.length; i++)
039         parts[i] = urlDecode(parts[i]);
040   }
041
042   /**
043    * Returns the path parts.
044    *
045    * @return The path parts.
046    */
047   public String[] getParts() {
048      return parts;
049   }
050
051   /**
052    * Returns the raw path passed into this object.
053    *
054    * @return The raw path passed into this object.
055    */
056   public String getPath() {
057      return path;
058   }
059
060   /**
061    * Returns <jk>true</jk> if this path ends with a slash.
062    *
063    * @return <jk>true</jk> if this path ends with a slash.
064    */
065   public boolean isTrailingSlash() {
066      return path.endsWith("/");
067   }
068
069   /**
070    * Converts this object to a map.
071    *
072    * @return This object converted to a map.
073    */
074   public ObjectMap toMap() {
075      return new DefaultFilteringObjectMap().append("raw", path).append("parts", parts);
076   }
077
078   @Override /* Object */
079   public String toString() {
080      return SimpleJson.DEFAULT.toString(toMap());
081   }
082}