View Javadoc

1   /*
2    * Copyright 2004-2009 the Seasar Foundation and the Others.
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,
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  package org.seasar.cubby.internal.util;
17  
18  import javax.servlet.ServletRequest;
19  import javax.servlet.http.HttpServletRequest;
20  
21  /**
22   * サーブレット要求をに対するユーティリティクラスです。
23   * 
24   * @author baba
25   */
26  public class RequestUtils {
27  
28  	/**
29  	 * 要求の URI からコンテキストパスを除いたパスを返します。
30  	 * 
31  	 * @param request
32  	 *            要求
33  	 * @return コンテキストパスを除いたパス
34  	 */
35  	public static String getPath(final HttpServletRequest request) {
36  		final StringBuilder builder = new StringBuilder();
37  		builder.append(request.getServletPath());
38  		final String pathInfo = request.getPathInfo();
39  		if (pathInfo != null) {
40  			builder.append(pathInfo);
41  		}
42  		return builder.toString();
43  	}
44  
45  	/**
46  	 * 要求から属性を取得します。
47  	 * 
48  	 * @param <T>
49  	 *            取得する属性の型
50  	 * @param request
51  	 *            要求
52  	 * @param name
53  	 *            属性名
54  	 * @return 属性
55  	 */
56  	@SuppressWarnings("unchecked")
57  	public static <T> T getAttribute(final ServletRequest request,
58  			final String name) {
59  		return (T) request.getAttribute(name);
60  	}
61  
62  }