View Javadoc

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