View Javadoc

1   /*--------------------------------------------------------------------------
2    *  Copyright 2008 utgenome.org
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   *--------------------------------------------------------------------------*/
16  //--------------------------------------
17  // utgb-core Project
18  //
19  // HandlerName.java
20  // Since: Jul 8, 2008
21  //
22  // $URL$ 
23  // $Author$
24  //--------------------------------------
25  package org.utgenome.gwt.utgb.server;
26  
27  import java.util.ArrayList;
28  import java.util.List;
29  
30  /**
31   * RequestURI information
32   * 
33   * @author leo
34   * 
35   */
36  public class RequestURI {
37  
38  	private String uri;
39  	private String suffix = "";
40  	private String fullName = null;
41  
42  	public RequestURI(String uri) {
43  		this.uri = uri;
44  		int dotIndex = uri.lastIndexOf(".");
45  		if (dotIndex > 0) {
46  			suffix = uri.substring(dotIndex + 1);
47  			fullName = uri.substring(0, dotIndex);
48  		}
49  		else
50  			fullName = uri;
51  
52  		assert (fullName != null);
53  	}
54  
55  	/**
56  	 * Has suffix in the action request? e.g., .xml, .json, etc.
57  	 * 
58  	 * @return
59  	 */
60  	public boolean hasSuffix() {
61  		return suffix != null;
62  	}
63  
64  	/**
65  	 * Get the suffix of the action request
66  	 * 
67  	 * @return
68  	 */
69  	public String getSuffix() {
70  		return suffix;
71  	}
72  
73  	/**
74  	 * Return the path component. For example, if the request is hello/world.json, its component list is ["hello",
75  	 * "world"]
76  	 * 
77  	 * @return the path component list
78  	 */
79  	public List<String> getPathComponentList() {
80  		ArrayList<String> result = new ArrayList<String>();
81  		String[] component = fullName.split("\\/");
82  
83  		if (component == null) {
84  			result.add(fullName);
85  		}
86  		else {
87  			for (String c : component)
88  				result.add(c);
89  		}
90  
91  		return result;
92  
93  	}
94  
95  	/**
96  	 * Get the leaf name of the action request
97  	 * 
98  	 * @return
99  	 */
100 	public String getName() {
101 		int leafIndex = fullName.lastIndexOf("/");
102 		if (leafIndex > 0)
103 			return fullName.substring(leafIndex + 1).replaceAll("/", ".");
104 		else
105 			return fullName.replaceAll("/", ".");
106 	}
107 
108 	public String getHandlerName() {
109 		return fullName.replaceAll("/", ".");
110 	}
111 
112 	/**
113 	 * Get the full path of the action request
114 	 * 
115 	 * @return
116 	 */
117 	public String getFullName() {
118 		return fullName;
119 	}
120 
121 	/**
122 	 * Get the original URI
123 	 * 
124 	 * @return
125 	 */
126 	public String getURI() {
127 		return uri;
128 	}
129 
130 	/**
131 	 * Get the prefix before the handler name
132 	 * 
133 	 * @return
134 	 */
135 	public String getPrefix() {
136 		int leafIndex = fullName.lastIndexOf("/");
137 		if (leafIndex > 0) {
138 			return "/" + fullName.substring(0, leafIndex);
139 		}
140 		else
141 			return "/";
142 	}
143 
144 	@Override
145 	public String toString() {
146 		return String.format("%s (prefix=%s, suffix=%s)", getName(), getPrefix(), suffix);
147 	}
148 
149 }