001 /*******************************************************************************
002 * Copyright (c) PicoContainer Organization. All rights reserved.
003 * ---------------------------------------------------------------------------
004 * The software in this package is published under the terms of the BSD style
005 * license a copy of which has been included with this distribution in the
006 * LICENSE.txt file.
007 ******************************************************************************/
008 package org.picocontainer.web;
009
010 import javax.servlet.http.HttpServletRequest;
011
012 import org.picocontainer.injectors.ProviderAdapter;
013
014 import java.io.Serializable;
015
016 /**
017 * Use this to make a request level component that pulls information from HTTP
018 * request header. If a header of the supplied name is not available for the current
019 * HttpServletRequest, then a NotFound exception will be thrown.
020 * <h4>Headers with dashes:</h4>
021 * <p>Many standard request headers have hyphens in them,
022 * (see <a href="http://en.wikipedia.org/wiki/List_of_HTTP_headers">Wikipedia List of Headers</a>
023 * </p>
024 * <p>To handle that, this class translates all hyphens to underscores ('_'). The
025 * end result is that you can construct a class that takes the
026 * User-Agent as a constructor argument like so:</p>
027 * <pre>
028 * public static class Integration {
029 * public Integration(String User_Agent) {
030 * //Does nothing.
031 * }
032 * }
033 * </pre>
034 */
035 public class StringFromHeader extends ProviderAdapter implements Serializable {
036
037 /**
038 * The component key that we use to integrate with
039 * Pico
040 */
041 private final String headerKey;
042
043
044 /**
045 * The header name we're searching for.
046 */
047 private final String headerName;
048
049 /**
050 * Constructs a new String From Header
051 */
052 public StringFromHeader(String headername) {
053 super();
054 this.headerName = headername;
055 if (headername == null) {
056 throw new NullPointerException("headername");
057 }
058
059 headerKey = headername.replaceAll("\\-", "_");
060 }
061
062 @Override
063 public Class getComponentImplementation() {
064 return String.class;
065 }
066
067 @Override
068 public Object getComponentKey() {
069 return headerKey;
070 }
071
072 /**
073 * {@inheritDoc}
074 * <p>Provides the header as specified by the header name.</p>
075 **/
076 public String provide(final HttpServletRequest request) {
077
078 String result = request.getHeader(headerName);
079 if (result == null) {
080 throw new HeaderNotFound(headerName);
081 }
082
083 return result;
084 }
085
086
087 @Override
088 public String toString() {
089 return "String from header. Component Key " + headerKey + " Servlet Request Header Name: " + headerName;
090 }
091
092 @SuppressWarnings("serial")
093 public static class HeaderNotFound extends PicoContainerWebException {
094 private HeaderNotFound(String name) {
095 super("'" + name + "' not found in header");
096 }
097 }
098
099 }