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.collections.*;
019import org.apache.juneau.marshall.*;
020
021/**
022 * Represents a parsed URL path-info string.
023 */
024public class UrlPathInfo {
025
026   final String[] parts;
027   final String path;
028
029   /**
030    * Constructor.
031    *
032    * @param path The path.
033    */
034   public UrlPathInfo(String path) {
035      if (path != null && ! path.startsWith("/"))
036         throw new RuntimeException("Invalid path specified.  Must be null or start with '/' per HttpServletRequest.getPathInfo().");
037      this.path = path;
038      parts = path == null ? new String[0] : split(path.substring(1), '/');
039      for (int i = 0; i < parts.length; i++)
040         parts[i] = urlDecode(parts[i]);
041   }
042
043   /**
044    * Returns the path parts.
045    *
046    * @return The path parts.
047    */
048   public String[] getParts() {
049      return parts;
050   }
051
052   /**
053    * Returns the raw path passed into this object.
054    *
055    * @return The raw path passed into this object.
056    */
057   public String getPath() {
058      return path;
059   }
060
061   /**
062    * Returns <jk>true</jk> if this path ends with a slash.
063    *
064    * @return <jk>true</jk> if this path ends with a slash.
065    */
066   public boolean isTrailingSlash() {
067      return path.endsWith("/");
068   }
069
070   /**
071    * Converts this object to a map.
072    *
073    * @return This object converted to a map.
074    */
075   public OMap toMap() {
076      return new DefaultFilteringOMap().append("raw", path).append("parts", parts);
077   }
078
079   @Override /* Object */
080   public String toString() {
081      return SimpleJson.DEFAULT.toString(toMap());
082   }
083}