1 /** 2 * Copyright (c) 2011, University of Konstanz, Distributed Systems Group 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are met: 7 * * Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * * Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * * Neither the name of the University of Konstanz nor the 13 * names of its contributors may be used to endorse or promote products 14 * derived from this software without specific prior written permission. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY 20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 */ 27 package org.jaxrx.core; 28 29 import java.util.Map; 30 31 import javax.ws.rs.Path; 32 import javax.ws.rs.core.HttpHeaders; 33 34 /** 35 * This class contains information on the resource path and query parameters of 36 * a JAX-RX request. 37 * @author Sebastian Graf, Christian Gruen, Lukas Lewandowski, University of 38 * Konstanz 39 */ 40 @Path(JaxRxConstants.ROOTPATH) 41 public final class ResourcePath { 42 /** Query parameters. */ 43 private final Map<QueryParameter, String> params; 44 /** Resource path. */ 45 private final String[] resource; 46 /** HTTP header attributes. */ 47 private final HttpHeaders headers; 48 49 /** 50 * Constructs a new {@code ResourcePath}. 51 * @param resourcePath resource path string 52 */ 53 public ResourcePath(final String resourcePath) { 54 this(resourcePath, null, null); 55 } 56 57 /** 58 * Constructs a new {@code ResourcePath} with an additional 59 * {@link QueryParameter} map. 60 * @param resourcePath resource path string 61 * @param queryParameters query parameters 62 */ 63 public ResourcePath(final String resourcePath, 64 final Map<QueryParameter, String> queryParameters) { 65 66 this(resourcePath, queryParameters, null); 67 } 68 69 /** 70 * Constructs a new {@code ResourcePath} with an additional 71 * {@link QueryParameter} map. 72 * @param resourcePath resource path string 73 * @param httpHeaders HTTP header attributes. 74 */ 75 public ResourcePath(final String resourcePath, final HttpHeaders httpHeaders) { 76 this(resourcePath, null, httpHeaders); 77 } 78 79 /** 80 * Constructs a new {@code ResourcePath} with an additional 81 * {@link QueryParameter} map. 82 * @param resourcePath resource path string 83 * @param queryParameters query parameters 84 * @param httpHeaders HTTP header attributes. 85 */ 86 public ResourcePath(final String resourcePath, 87 final Map<QueryParameter, String> queryParameters, 88 final HttpHeaders httpHeaders) { 89 90 // chop trailing slashes, ignore empty steps 91 final String[] rp = resourcePath.replaceAll("/+$", "").split("/"); 92 resource = rp.length == 1 && rp[0].isEmpty() ? new String[] { } : rp; 93 params = queryParameters; 94 headers = httpHeaders; 95 96 } 97 98 /** 99 * Returns the complete resource path string. 100 * @return resource path string 101 */ 102 public String getResourcePath() { 103 final StringBuilder sb = new StringBuilder(); 104 for(final String r : resource) 105 sb.append(r + '/'); 106 return sb.substring(0, Math.max(0, sb.length() - 1)); 107 } 108 109 /** 110 * Returns the resource at the specified level. 111 * @param level resource level 112 * @return resource 113 */ 114 public String getResource(final int level) { 115 if(level < resource.length) { 116 return resource[level]; 117 } 118 // offset is out of bounds... 119 throw new IndexOutOfBoundsException("Index: " + level + ", Size: " 120 + resource.length); 121 } 122 123 /** 124 * Returns the depth of the resource path. 125 * @return resource depth 126 */ 127 public int getDepth() { 128 return resource.length; 129 } 130 131 /** 132 * Returns the value of the specified query parameter, or {@code null} if no 133 * value is mapped. 134 * @param key query key 135 * @return value 136 */ 137 public String getValue(final QueryParameter key) { 138 return params != null ? params.get(key) : null; 139 } 140 141 /** 142 * This map return all available query parameters. 143 * @return The parameter map. 144 */ 145 public Map<QueryParameter, String> getQueryParameter() { 146 return params; 147 } 148 149 /** 150 * Return HTTP headers from user request. 151 * @return HTTP headers. 152 */ 153 public HttpHeaders getHttpHeaders() { 154 return headers; 155 } 156 157 @Override 158 public String toString() { 159 return getResourcePath(); 160 } 161 }