0001: package servletunit;
0002:
0003: import javax.servlet.RequestDispatcher;
0004: import javax.servlet.ServletContext;
0005: import javax.servlet.ServletInputStream;
0006: import javax.servlet.ServletRequest;
0007: import javax.servlet.http.Cookie;
0008: import javax.servlet.http.HttpServletRequest;
0009: import javax.servlet.http.HttpSession;
0010: import java.io.BufferedReader;
0011: import java.io.IOException;
0012: import java.io.File;
0013: import java.security.Principal;
0014: import java.util.*;
0015: import java.text.SimpleDateFormat;
0016: import java.text.DateFormat;
0017: import java.text.ParseException;
0018:
0019: // StrutsTestCase - a JUnit extension for testing Struts actions
0020: // within the context of the ActionServlet.
0021: // Copyright (C) 2002 Deryl Seale
0022: //
0023: // This library is free software; you can redistribute it and/or
0024: // modify it under the terms of the Apache Software License as
0025: // published by the Apache Software Foundation; either version 1.1
0026: // of the License, or (at your option) any later version.
0027: //
0028: // This library is distributed in the hope that it will be useful,
0029: // but WITHOUT ANY WARRANTY; without even the implied warranty of
0030: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
0031: // Apache Software Foundation Licens for more details.
0032: //
0033: // You may view the full text here: http://www.apache.org/LICENSE.txt
0034:
0035: public class HttpServletRequestSimulator implements HttpServletRequest {
0036: private Hashtable attributes;
0037: private String scheme;
0038: private String protocol = "HTTP/1.1";
0039: private String requestURI;
0040: private String requestURL;
0041: private String contextPath = "";
0042: private String servletPath;
0043: private String pathInfo;
0044: private String queryString;
0045: private String method;
0046: private String contentType;
0047: private Locale locale;
0048: private Principal principal;
0049: String remoteAddr;
0050: String localAddr;
0051: String remoteHost;
0052: String localName;
0053: int remotePort;
0054: int localPort;
0055: private String remoteUser;
0056: private String userRole;
0057: private String reqSessionId;
0058: String authType;
0059: String charEncoding;
0060: private String serverName;
0061: private int port;
0062:
0063: private Hashtable parameters;
0064: private Hashtable headers;
0065: private Vector cookies;
0066:
0067: private HttpSession session;
0068: private ServletContext context;
0069:
0070: /**
0071: * Constant used by {@link #setMethod} to indicate that the GET method
0072: * made this request.
0073: */
0074:
0075: public final static int GET = 0;
0076:
0077: /**
0078: * Constant used by {@link #setMethod} to indicate that the POST method
0079: * made this request.
0080: */
0081: public final static int POST = 1;
0082:
0083: /**
0084: * Constant used by {@link #setMethod} to indicate that the PUT method
0085: * made this request.
0086: */
0087: public final static int PUT = 2;
0088:
0089: public HttpServletRequestSimulator(ServletContext context) {
0090: scheme = "http";
0091: attributes = new Hashtable();
0092: parameters = new Hashtable();
0093: headers = new Hashtable();
0094: cookies = new Vector();
0095: this .context = context;
0096: //if (getHeader("Accept")==null)
0097: //setHeader("Accept","dummy accept");
0098: }
0099:
0100: /**
0101: * Adds a parameter to this object's list of parameters
0102: *
0103: * @param key The name of the parameter
0104: * @param value The value of the parameter
0105: */
0106: public void addParameter(String key, String value) {
0107: if ((key != null) && (value != null))
0108: this .parameters.put(key, value);
0109: }
0110:
0111: /**
0112: * Adds a parameter as a String array to this object's list of parameters
0113: */
0114: public void addParameter(String name, String[] values) {
0115: if ((name != null) && (values != null))
0116: parameters.put(name, values);
0117: }
0118:
0119: /**
0120: * Returns a java.util.Map of the parameters of this request.
0121: * Request parameters
0122: * are extra information sent with the request. For HTTP servlets,
0123: * parameters are contained in the query string or posted form data.
0124: *
0125: * @return an immutable java.util.Map containing parameter names as
0126: * keys and parameter values as map values. The keys in the parameter
0127: * map are of type String. The values in the parameter map are of type
0128: * String array.
0129: *
0130: */
0131: public Map getParameterMap() {
0132: return this .parameters;
0133: }
0134:
0135: /**
0136: *
0137: * Returns the value of the named attribute as an <code>Object</code>,
0138: * or <code>null</code> if no attribute of the given name exists.
0139: *
0140: * <p> Attributes can be set two ways. The servlet container may set
0141: * attributes to make available custom information about a request.
0142: * For example, for requests made using HTTPS, the attribute
0143: * <code>javax.servlet.request.X509Certificate</code> can be used to
0144: * retrieve information on the certificate of the client. Attributes
0145: * can also be set programatically using
0146: * {@link ServletRequest#setAttribute}. This allows information to be
0147: * embedded into a request before a {@link RequestDispatcher} call.
0148: *
0149: * <p>Attribute names should follow the same conventions as package
0150: * names. This specification reserves names matching <code>java.*</code>,
0151: * <code>javax.*</code>, and <code>sun.*</code>.
0152: *
0153: * @param s a <code>String</code> specifying the name of
0154: * the attribute
0155: *
0156: * @return an <code>Object</code> containing the value
0157: * of the attribute, or <code>null</code> if
0158: * the attribute does not exist
0159: *
0160: */
0161: public Object getAttribute(String s) {
0162: return attributes.get(s);
0163: }
0164:
0165: /**
0166: * Returns an <code>Enumeration</code> containing the
0167: * names of the attributes available to this request.
0168: * This method returns an empty <code>Enumeration</code>
0169: * if the request has no attributes available to it.
0170: *
0171: *
0172: * @return an <code>Enumeration</code> of strings
0173: * containing the names
0174: * of the request's attributes
0175: *
0176: */
0177: public Enumeration getAttributeNames() {
0178: return attributes.keys();
0179: }
0180:
0181: /**
0182: * Returns the name of the authentication scheme used to protect
0183: * the servlet. All servlet containers support basic, form and client
0184: * certificate authentication, and may additionally support digest
0185: * authentication.
0186: * If the servlet is not authenticated <code>null</code> is returned.
0187: *
0188: * <p>Same as the value of the CGI variable AUTH_TYPE.
0189: *
0190: *
0191: * @return one of the static members BASIC_AUTH,
0192: * FORM_AUTH, CLIENT_CERT_AUTH, DIGEST_AUTH
0193: * (suitable for == comparison)
0194: * indicating the authentication scheme, or
0195: * <code>null</code> if the request was
0196: * not authenticated.
0197: *
0198: */
0199: public String getAuthType() {
0200: return authType;
0201: }
0202:
0203: /**
0204: * Returns the name of the character encoding used in the body of this
0205: * request. This method returns <code>null</code> if the request
0206: * does not specify a character encoding
0207: *
0208: *
0209: * @return a <code>String</code> containing the name of
0210: * the chararacter encoding, or <code>null</code>
0211: * if the request does not specify a character encoding
0212: */
0213: public String getCharacterEncoding() {
0214: return charEncoding;
0215: }
0216:
0217: /**
0218: * Returns the length, in bytes, of the request body
0219: * and made available by the input stream, or -1 if the
0220: * length is not known. For HTTP servlets, same as the value
0221: * of the CGI variable CONTENT_LENGTH.
0222: *
0223: * @return -1, since this is a mock container
0224: */
0225: public int getContentLength() {
0226: return -1;
0227: }
0228:
0229: /**
0230: * Returns the MIME type of the body of the request, or
0231: * <code>null</code> if the type is not known. For HTTP servlets,
0232: * same as the value of the CGI variable CONTENT_TYPE.
0233: *
0234: * @return a <code>String</code> containing the name
0235: * of the MIME type of
0236: * the request, or null if the type is not known
0237: *
0238: */
0239: public String getContentType() {
0240: return contentType;
0241: }
0242:
0243: /**
0244: *
0245: * Returns the portion of the request URI that indicates the context
0246: * of the request. The context path always comes first in a request
0247: * URI. The path starts with a "/" character but does not end with a "/"
0248: * character. For servlets in the default (root) context, this method
0249: * returns "". The container does not decode this string.
0250: *
0251: *
0252: * @return a <code>String</code> specifying the
0253: * portion of the request URI that indicates the context
0254: * of the request
0255: *
0256: *
0257: */
0258: public String getContextPath() {
0259: return contextPath;
0260: }
0261:
0262: /**
0263: * Adds a cookie that can be retrieved from this request via the
0264: * getCookies() method.
0265: *
0266: * @param cookie a Cookie object to be retrieved from this
0267: * request.
0268: *
0269: * @see #getCookies
0270: */
0271: public void addCookie(Cookie cookie) {
0272: cookies.addElement(cookie);
0273: }
0274:
0275: /**
0276: * Adds a set of cookies that can be retrieved from this request via the
0277: * getCookies() method.
0278: *
0279: * @param cookies an array of Cookie object to be retrieved from this
0280: * request.
0281: *
0282: * @see #getCookies
0283: */
0284: public void setCookies(Cookie[] cookies) {
0285: for (int i = 0; i < cookies.length; i++)
0286: this .cookies.addElement(cookies[i]);
0287: }
0288:
0289: /**
0290: *
0291: * Returns an array containing all of the <code>Cookie</code>
0292: * objects the client sent with this request.
0293: * This method returns <code>null</code> if no cookies were sent.
0294: *
0295: * @return an array of all the <code>Cookies</code>
0296: * included with this request, or <code>null</code>
0297: * if the request has no cookies
0298: *
0299: *
0300: */
0301: public Cookie[] getCookies() {
0302: if (cookies.isEmpty())
0303: return null;
0304: else {
0305: Cookie[] cookieArray = new Cookie[cookies.size()];
0306: return (Cookie[]) cookies.toArray(cookieArray);
0307: }
0308: }
0309:
0310: /**
0311: * Returns the value of the specified request header as a long value that represents a Date object. Use this
0312: * method with headers that contain dates, such as If-Modified-Since.
0313: * <br><br>
0314: * The date is returned as the number of milliseconds since January 1, 1970 GMT. The header name is case insensitive.
0315: * <br><br>
0316: * If the request did not have a header of the specified name, this method returns -1. If the header can't be converted to a date, the method throws an IllegalArgumentException.
0317: * @param name a String specifying the name of the header
0318: * @return a <code>long</code> value representing the date specified in the header expressed as the number of milliseconds since January 1, 1970 GMT, or -1 if the named header was not included with the reqest.
0319: */
0320: public long getDateHeader(String name) {
0321: String s1 = getHeader(name);
0322: if (s1 == null)
0323: return -1L;
0324: try {
0325: DateFormat dateFormat = new SimpleDateFormat(
0326: "EEE, d MMM yyyy HH:mm:ss z");
0327: return dateFormat.parse(s1).getTime();
0328: } catch (ParseException exception) {
0329: throw new IllegalArgumentException("Cannot parse date: "
0330: + s1);
0331: }
0332: }
0333:
0334: /**
0335: * Sets a header with the appropriate date string given the time in milliseconds.
0336: * @param name the name of the header
0337: * @param millis the time in milliseconds
0338: */
0339: public void setDateHeader(String name, long millis) {
0340: String dateString = new SimpleDateFormat(
0341: "EEE, d MMM yyyy HH:mm:ss z").format(new Date(millis));
0342: setHeader(name, dateString);
0343: }
0344:
0345: /**
0346: *
0347: * Returns the value of the specified request header
0348: * as a <code>String</code>. If the request did not include a header
0349: * of the specified name, this method returns <code>null</code>.
0350: * The header name is case insensitive. You can use
0351: * this method with any request header.
0352: *
0353: * @param s a <code>String</code> specifying the
0354: * header name
0355: *
0356: * @return a <code>String</code> containing the
0357: * value of the requested
0358: * header, or <code>null</code>
0359: * if the request does not
0360: * have a header of that name
0361: *
0362: */
0363: public String getHeader(String s) {
0364: return (String) headers.get(s);
0365: }
0366:
0367: /**
0368: *
0369: * Returns an enumeration of all the header names
0370: * this request contains. If the request has no
0371: * headers, this method returns an empty enumeration.
0372: *
0373: * <p>Some servlet containers do not allow do not allow
0374: * servlets to access headers using this method, in
0375: * which case this method returns <code>null</code>
0376: *
0377: * @return an enumeration of all the
0378: * header names sent with this
0379: * request; if the request has
0380: * no headers, an empty enumeration;
0381: * if the servlet container does not
0382: * allow servlets to use this method,
0383: * <code>null</code>
0384: *
0385: *
0386: */
0387: public Enumeration getHeaderNames() {
0388: return headers.keys();
0389: }
0390:
0391: /**
0392: * This operation is not supported.
0393: */
0394: public Enumeration getHeaders(String s) {
0395: throw new UnsupportedOperationException(
0396: "getHeaders operation is not supported!");
0397: }
0398:
0399: /**
0400: * This operation is not supported.
0401: */
0402: public ServletInputStream getInputStream() throws IOException {
0403: throw new UnsupportedOperationException(
0404: "getInputStream operation is not supported!");
0405: }
0406:
0407: /**
0408: *
0409: * Returns the value of the specified request header
0410: * as an <code>int</code>. If the request does not have a header
0411: * of the specified name, this method returns -1. If the
0412: * header cannot be converted to an integer, this method
0413: * throws a <code>NumberFormatException</code>.
0414: *
0415: * <p>The header name is case insensitive.
0416: *
0417: * @param s a <code>String</code> specifying the name
0418: * of a request header
0419: *
0420: * @return an integer expressing the value
0421: * of the request header or -1
0422: * if the request doesn't have a
0423: * header of this name
0424: *
0425: * @exception NumberFormatException If the header value
0426: * can't be converted
0427: * to an <code>int</code>
0428: */
0429: public int getIntHeader(String s) {
0430: Object header = headers.get(s);
0431: if (header != null) {
0432: try {
0433: Integer intHeader = (Integer) header;
0434: return intHeader.intValue();
0435: } catch (ClassCastException e) {
0436: throw new NumberFormatException("header '" + s
0437: + "' cannot be converted to number format.");
0438: }
0439: } else
0440: return -1;
0441: }
0442:
0443: /**
0444: *
0445: * Returns the preferred <code>Locale</code> that the client will
0446: * accept content in, based on the Accept-Language header.
0447: * If the client request doesn't provide an Accept-Language header,
0448: * this method returns the default locale for the server.
0449: *
0450: *
0451: * @return the preferred <code>Locale</code> for the client,
0452: * defaults to Locale.US if {@link #setLocale} has
0453: * not been called.
0454: *
0455: */
0456: public Locale getLocale() {
0457: if (this .locale == null)
0458: return Locale.US;
0459: else
0460: return this .locale;
0461: }
0462:
0463: /**
0464: * Returns an Enumeration of Locale objects indicating, in decreasing order starting with the preferred locale, the locales that are acceptable to the client based on the Accept-Language header. If the client request doesn't provide an Accept-Language header, this method returns an Enumeration containing one Locale, the default locale for the server.
0465: * @return an <code>Enumeration</code> of preferred Locale objects for the client
0466: */
0467:
0468: public Enumeration getLocales() {
0469: return java.util.Collections.enumeration(Collections
0470: .singleton(getLocale()));
0471: }
0472:
0473: /**
0474: *
0475: * Returns the name of the HTTP method with which this
0476: * request was made, for example, GET, POST, or PUT.
0477: * Same as the value of the CGI variable REQUEST_METHOD.
0478: *
0479: * @return a <code>String</code>
0480: * specifying the name
0481: * of the method with which
0482: * this request was made
0483: *
0484: */
0485: public String getMethod() {
0486: if (method == null)
0487: return "POST";
0488: else
0489: return method;
0490: }
0491:
0492: /**
0493: * Returns the value of a request parameter as a <code>String</code>,
0494: * or <code>null</code> if the parameter does not exist. Request parameters
0495: * are extra information sent with the request. For HTTP servlets,
0496: * parameters are contained in the query string or posted form data.
0497: *
0498: * <p>You should only use this method when you are sure the
0499: * parameter has only one value. If the parameter might have
0500: * more than one value, use {@link #getParameterValues}.
0501: *
0502: * <p>If you use this method with a multivalued
0503: * parameter, the value returned is equal to the first value
0504: * in the array returned by <code>getParameterValues</code>.
0505: *
0506: * <p>If the parameter data was sent in the request body, such as occurs
0507: * with an HTTP POST request, then reading the body directly via {@link
0508: * #getInputStream} or {@link #getReader} can interfere
0509: * with the execution of this method.
0510: *
0511: * @param s a <code>String</code> specifying the
0512: * name of the parameter
0513: *
0514: * @return a <code>String</code> representing the
0515: * single value of the parameter
0516: *
0517: * @see #getParameterValues
0518: *
0519: */
0520: public String getParameter(String s) {
0521: if (s == null)
0522: return null;
0523:
0524: Object param = parameters.get(s);
0525: if (null == param)
0526: return null;
0527: if (param.getClass().isArray())
0528: return ((String[]) param)[0];
0529: return (String) param;
0530: }
0531:
0532: /**
0533: *
0534: * Returns an <code>Enumeration</code> of <code>String</code>
0535: * objects containing the names of the parameters contained
0536: * in this request. If the request has
0537: * no parameters, the method returns an
0538: * empty <code>Enumeration</code>.
0539: *
0540: * @return an <code>Enumeration</code> of <code>String</code>
0541: * objects, each <code>String</code> containing
0542: * the name of a request parameter; or an
0543: * empty <code>Enumeration</code> if the
0544: * request has no parameters
0545: *
0546: */
0547: public Enumeration getParameterNames() {
0548: return parameters.keys();
0549: }
0550:
0551: /**
0552: * Returns an array of <code>String</code> objects containing
0553: * all of the values the given request parameter has, or
0554: * <code>null</code> if the parameter does not exist.
0555: *
0556: * <p>If the parameter has a single value, the array has a length
0557: * of 1.
0558: *
0559: * @param s a <code>String</code> containing the name of
0560: * the parameter whose value is requested
0561: *
0562: * @return an array of <code>String</code> objects
0563: * containing the parameter's values
0564: *
0565: * @see #getParameter
0566: *
0567: */
0568: public String[] getParameterValues(String s) {
0569: if (s == null)
0570: return null;
0571: Object param = parameters.get(s);
0572: if (null == param)
0573: return null;
0574: else {
0575: if (param.getClass().isArray()) {
0576: return (String[]) param;
0577: } else {
0578: return new String[] { (String) param };
0579: }
0580: }
0581: }
0582:
0583: /**
0584: *
0585: * Returns any extra path information associated with
0586: * the URL the client sent when it made this request.
0587: * The extra path information follows the servlet path
0588: * but precedes the query string.
0589: * This method returns <code>null</code> if there
0590: * was no extra path information.
0591: *
0592: * <p>Same as the value of the CGI variable PATH_INFO.
0593: *
0594: *
0595: * @return a <code>String</code>, decoded by the
0596: * web container, specifying
0597: * extra path information that comes
0598: * after the servlet path but before
0599: * the query string in the request URL;
0600: * or <code>null</code> if the URL does not have
0601: * any extra path information
0602: *
0603: */
0604: public String getPathInfo() {
0605: return pathInfo;
0606: }
0607:
0608: /**
0609: * This operation is not supported.
0610: */
0611: public String getPathTranslated() {
0612: throw new UnsupportedOperationException(
0613: "getPathTranslated operation is not supported!");
0614: }
0615:
0616: /**
0617: * Returns the name and version of the protocol the request uses
0618: * in the form <i>protocol/majorVersion.minorVersion</i>, for
0619: * example, HTTP/1.1. For HTTP servlets, the value
0620: * returned is the same as the value of the CGI variable
0621: * <code>SERVER_PROTOCOL</code>.
0622: *
0623: * @return a <code>String</code> containing the protocol
0624: * name and version number
0625: *
0626: */
0627: public String getProtocol() {
0628: return protocol;
0629: }
0630:
0631: /**
0632: *
0633: * Returns the query string that is contained in the request
0634: * URL after the path. This method returns <code>null</code>
0635: * if the URL does not have a query string. Same as the value
0636: * of the CGI variable QUERY_STRING.
0637: *
0638: * @return a <code>String</code> containing the query
0639: * string or <code>null</code> if the URL
0640: * contains no query string. The value is not
0641: * decoded by the container.
0642: *
0643: */
0644: public String getQueryString() {
0645: return queryString;
0646: }
0647:
0648: /**
0649: * This operation is not supported.
0650: */
0651: public BufferedReader getReader() throws IOException {
0652: throw new UnsupportedOperationException(
0653: "getReader operation is not supported!");
0654: }
0655:
0656: /**
0657: *
0658: * @deprecated As of Version 2.1 of the Java Servlet API,
0659: * use {@link ServletContext#getRealPath} instead.
0660: *
0661: */
0662: public String getRealPath(String path) {
0663: File contextDirectory = ((ServletContextSimulator) context)
0664: .getContextDirectory();
0665: if ((contextDirectory == null) || (path == null))
0666: return null;
0667: else
0668: return (new File(contextDirectory, path)).getAbsolutePath();
0669: }
0670:
0671: /**
0672: * Returns the Internet Protocol (IP) address of the client
0673: * that sent the request. For HTTP servlets, same as the value of the
0674: * CGI variable <code>REMOTE_ADDR</code>.
0675: *
0676: * @return a <code>String</code> containing the
0677: * IP address of the client that sent the request
0678: *
0679: */
0680: public String getRemoteAddr() {
0681: return remoteAddr;
0682: }
0683:
0684: /**
0685: * Returns the fully qualified name of the client that sent the
0686: * request. If the engine cannot or chooses not to resolve the hostname
0687: * (to improve performance), this method returns the dotted-string form of
0688: * the IP address. For HTTP servlets, same as the value of the CGI variable
0689: * <code>REMOTE_HOST</code>.
0690: *
0691: * @return a <code>String</code> containing the fully
0692: * qualified name of the client
0693: *
0694: */
0695: public String getRemoteHost() {
0696: return remoteHost;
0697: }
0698:
0699: /**
0700: * Returns the fully qualified name of the client that sent the
0701: * request. If the engine cannot or chooses not to resolve the hostname
0702: * (to improve performance), this method returns the dotted-string form of
0703: * the IP address. For HTTP servlets, same as the value of the CGI variable
0704: * <code>REMOTE_HOST</code>.
0705: *
0706: * @return a <code>String</code> containing the fully
0707: * qualified name of the client
0708: *
0709: */
0710: public String getRemoteUser() {
0711: return remoteUser;
0712: }
0713:
0714: /**
0715: *
0716: * Returns a {@link RequestDispatcher} object that acts as a wrapper for
0717: * the resource located at the given path.
0718: * A <code>RequestDispatcher</code> object can be used to forward
0719: * a request to the resource or to include the resource in a response.
0720: * The resource can be dynamic or static.
0721: *
0722: * <p>The pathname specified may be relative, although it cannot extend
0723: * outside the current servlet context. If the path begins with
0724: * a "/" it is interpreted as relative to the current context root.
0725: * This method returns <code>null</code> if the servlet container
0726: * cannot return a <code>RequestDispatcher</code>.
0727: *
0728: * <p>The difference between this method and {@link
0729: * ServletContext#getRequestDispatcher} is that this method can take a
0730: * relative path.
0731: *
0732: * @param url a <code>String</code> specifying the pathname
0733: * to the resource
0734: *
0735: * @return a <code>RequestDispatcher</code> object
0736: * that acts as a wrapper for the resource
0737: * at the specified path
0738: *
0739: * @see RequestDispatcherSimulator
0740: * @see ServletContextSimulator#getRequestDispatcher
0741: *
0742: */
0743: public RequestDispatcher getRequestDispatcher(String url) {
0744: return context.getRequestDispatcher(url);
0745: }
0746:
0747: /**
0748: *
0749: * Returns the session ID specified by the client. This may
0750: * not be the same as the ID of the actual session in use.
0751: * For example, if the request specified an old (expired)
0752: * session ID and the server has started a new session, this
0753: * method gets a new session with a new ID. If the request
0754: * did not specify a session ID, this method returns
0755: * <code>null</code>.
0756: *
0757: *
0758: * @return a <code>String</code> specifying the session
0759: * ID, or <code>null</code> if the request did
0760: * not specify a session ID
0761: *
0762: * @see #isRequestedSessionIdValid
0763: *
0764: */
0765: public String getRequestedSessionId() {
0766: return reqSessionId;
0767: }
0768:
0769: /**
0770: *
0771: * Returns the part of this request's URL from the protocol
0772: * name up to the query string in the first line of the HTTP request.
0773: * The web container does not decode this String.
0774: * For example:
0775: *
0776: *
0777: * <table>
0778: * <tr align=left><th>First line of HTTP request </th>
0779: * <th> Returned Value</th>
0780: * <tr><td>POST /some/path.html HTTP/1.1<td><td>/some/path.html
0781: * <tr><td>GET http://foo.bar/a.html HTTP/1.0
0782: * <td><td>/a.html
0783: * <tr><td>HEAD /xyz?a=b HTTP/1.1<td><td>/xyz
0784: * </table>
0785: *
0786: *
0787: * @return a <code>String</code> containing
0788: * the part of the URL from the
0789: * protocol name up to the query string
0790: *
0791: *
0792: */
0793: public String getRequestURI() {
0794: return requestURI;
0795: }
0796:
0797: /**
0798: * Reconstructs the URL the client used to make the request. The returned URL contains a protocol, server name, port number, and server path, but it does not include query string parameters.
0799: * <br><br>
0800: * Because this method returns a StringBuffer, not a string, you can modify the URL easily, for example, to append query parameters.
0801: * <br><br>
0802: * This method is useful for creating redirect messages and for reporting errors.
0803: * @return a <code>StringBuffer</code> object containing the reconstructed URL
0804: */
0805:
0806: public StringBuffer getRequestURL() {
0807: return new StringBuffer(requestURL);
0808: }
0809:
0810: /**
0811: * Returns the name of the scheme used to make this request,
0812: * for example,
0813: * <code>http</code>, <code>https</code>, or <code>ftp</code>.
0814: * Different schemes have different rules for constructing URLs,
0815: * as noted in RFC 1738.
0816: *
0817: * @return a <code>String</code> containing the name
0818: * of the scheme used to make this request
0819: *
0820: */
0821: public String getScheme() {
0822: return scheme;
0823: }
0824:
0825: /**
0826: * Returns the host name of the server that received
0827: * the request. For HTTP servlets, same as the value of
0828: * the CGI variable SERVER_NAME.
0829: * @return the name of the server to which the request was sent
0830: */
0831: public String getServerName() {
0832: return serverName;
0833: }
0834:
0835: /**
0836: * Returns the port number on which this request was received. For HTTP servlets, same as the value of the CGI variable SERVER_PORT.
0837: * @return an integer specifying the port number
0838: */
0839: public int getServerPort() {
0840: return this .port;
0841: }
0842:
0843: /**
0844: * Sets the server port to be used with {@link#getServerPort}.
0845: */
0846: public void setServerPort(int port) {
0847: this .port = port;
0848: }
0849:
0850: /**
0851: *
0852: * Returns the part of this request's URL that calls
0853: * the servlet. This includes either the servlet name or
0854: * a path to the servlet, but does not include any extra
0855: * path information or a query string. Same as the value
0856: * of the CGI variable SCRIPT_NAME.
0857: *
0858: *
0859: * @return a <code>String</code> containing
0860: * the name or path of the servlet being
0861: * called, as specified in the request URL,
0862: * decoded.
0863: *
0864: *
0865: */
0866: public String getServletPath() {
0867: return servletPath;
0868: }
0869:
0870: /**
0871: *
0872: * Returns the current session associated with this request,
0873: * or if the request does not have a session, creates one.
0874: *
0875: * @return the <code>HttpSession</code> associated
0876: * with this request
0877: *
0878: * @see #getSession(boolean)
0879: *
0880: */
0881: public HttpSession getSession() {
0882: return getSession(true);
0883: }
0884:
0885: /**
0886: *
0887: * Returns the current <code>HttpSession</code>
0888: * associated with this request or, if if there is no
0889: * current session and <code>create</code> is true, returns
0890: * a new session.
0891: *
0892: * <p>If <code>create</code> is <code>false</code>
0893: * and the request has no valid <code>HttpSession</code>,
0894: * this method returns <code>null</code>.
0895: *
0896: * <p>To make sure the session is properly maintained,
0897: * you must call this method before
0898: * the response is committed. If the container is using cookies
0899: * to maintain session integrity and is asked to create a new session
0900: * when the response is committed, an IllegalStateException is thrown.
0901: *
0902: *
0903: *
0904: *
0905: * @param b <code>true</code> to create
0906: * a new session for this request if necessary;
0907: * <code>false</code> to return <code>null</code>
0908: * if there's no current session
0909: *
0910: *
0911: * @return the <code>HttpSession</code> associated
0912: * with this request or <code>null</code> if
0913: * <code>create</code> is <code>false</code>
0914: * and the request has no valid session
0915: *
0916: * @see #getSession()
0917: *
0918: *
0919: */
0920: public HttpSession getSession(boolean b) {
0921: if ((session == null) && (b))
0922: this .session = new HttpSessionSimulator(context);
0923: else if ((session != null)
0924: && (!((HttpSessionSimulator) session).isValid()) && (b))
0925: this .session = new HttpSessionSimulator(context);
0926: if ((session != null)
0927: && (((HttpSessionSimulator) session).isValid()))
0928: return this .session;
0929: else
0930: return null;
0931: }
0932:
0933: /**
0934: *
0935: * Returns a <code>java.security.Principal</code> object containing
0936: * the name of the current authenticated user. If the user has not been
0937: * authenticated, the method returns <code>null</code>.
0938: *
0939: * @return a <code>java.security.Principal</code> containing
0940: * the name of the user making this request;
0941: * <code>null</code> if the user has not been
0942: * authenticated
0943: *
0944: */
0945: public Principal getUserPrincipal() {
0946: return this .principal;
0947: }
0948:
0949: /**
0950: *
0951: * Checks whether the requested session ID came in as a cookie.
0952: *
0953: * @return <code>true</code> in all cases
0954: *
0955: * @see #getSession
0956: *
0957: */
0958: public boolean isRequestedSessionIdFromCookie() {
0959: return true;
0960: }
0961:
0962: /**
0963: *
0964: * @deprecated As of Version 2.1 of the Java Servlet
0965: * API, use {@link #isRequestedSessionIdFromURL}
0966: * instead.
0967: *
0968: */
0969: public boolean isRequestedSessionIdFromUrl() {
0970: return isRequestedSessionIdFromURL();
0971: }
0972:
0973: /**
0974: *
0975: * Checks whether the requested session ID came in as part of the
0976: * request URL.
0977: *
0978: * @return <code>false</code> in all cases.
0979: *
0980: * @see #getSession
0981: *
0982: */
0983: public boolean isRequestedSessionIdFromURL() {
0984: return false;
0985: }
0986:
0987: /**
0988: *
0989: * Checks whether the requested session ID is still valid.
0990: *
0991: * @return <code>true</code> if this
0992: * request has an id for a valid session
0993: * in the current session context;
0994: * <code>false</code> otherwise
0995: *
0996: * @see #getRequestedSessionId
0997: * @see #getSession
0998: *
0999: */
1000: public boolean isRequestedSessionIdValid() {
1001: if (session != null) {
1002: try {
1003: session.getId();
1004: return true;
1005: } catch (IllegalStateException e) {
1006: return false;
1007: }
1008: } else
1009: return false;
1010: }
1011:
1012: /**
1013: *
1014: * Returns a boolean indicating whether this request was made using a
1015: * secure channel, such as HTTPS.
1016: *
1017: *
1018: * @return true if scheme has been set to HTTPS (ignoring case)
1019: *
1020: */
1021: public boolean isSecure() {
1022: if (scheme == null) {
1023: return false;
1024: } else {
1025: return scheme.equalsIgnoreCase("HTTPS");
1026: }
1027: }
1028:
1029: /**
1030: *
1031: * Returns a boolean indicating whether the authenticated user is included
1032: * in the specified logical "role". Roles and role membership can be
1033: * defined using deployment descriptors. If the user has not been
1034: * authenticated, the method returns <code>false</code>.
1035: *
1036: * @param s a <code>String</code> specifying the name
1037: * of the role
1038: *
1039: * @return <code>false</code> in all cases
1040: *
1041: */
1042: public boolean isUserInRole(String s) {
1043: return s.equals(userRole);
1044: }
1045:
1046: /**
1047: * Sets user role to be used in {@link #isUserInRole}
1048: */
1049: public void setUserRole(String role) {
1050: this .userRole = role;
1051: }
1052:
1053: /**
1054: *
1055: * Removes an attribute from this request. This method is not
1056: * generally needed as attributes only persist as long as the request
1057: * is being handled.
1058: *
1059: * <p>Attribute names should follow the same conventions as
1060: * package names. Names beginning with <code>java.*</code>,
1061: * <code>javax.*</code>, and <code>com.sun.*</code>, are
1062: * reserved for use by Sun Microsystems.
1063: *
1064: *
1065: * @param s a <code>String</code> specifying
1066: * the name of the attribute to remove
1067: *
1068: */
1069: public void removeAttribute(String s) {
1070: attributes.remove(s);
1071: }
1072:
1073: /**
1074: *
1075: * Stores an attribute in this request.
1076: * Attributes are reset between requests. This method is most
1077: * often used in conjunction with {@link RequestDispatcher}.
1078: *
1079: * <p>Attribute names should follow the same conventions as
1080: * package names. Names beginning with <code>java.*</code>,
1081: * <code>javax.*</code>, and <code>com.sun.*</code>, are
1082: * reserved for use by Sun Microsystems.
1083: *<br> If the value passed in is null, the effect is the same as
1084: * calling {@link #removeAttribute}.
1085: *
1086: *
1087: *
1088: * @param name a <code>String</code> specifying
1089: * the name of the attribute
1090: *
1091: * @param o the <code>Object</code> to be stored
1092: *
1093: */
1094: public void setAttribute(String name, Object o) {
1095: if (o == null)
1096: attributes.remove(name);
1097: else
1098: attributes.put(name, o);
1099: }
1100:
1101: /**
1102: * Sets authentication scheme to be used in {@link #getAuthType}.
1103: */
1104: public void setAuthType(String s) {
1105: authType = s;
1106: }
1107:
1108: /**
1109: * Sets character encoding to be used in {@link #getCharacterEncoding}.
1110: */
1111: public void setCharacterEncoding(String s) {
1112: charEncoding = s;
1113: }
1114:
1115: /**
1116: * Sets content type to be used in {@link #getContentType}.
1117: */
1118: public void setContentType(String s) {
1119: contentType = s;
1120: }
1121:
1122: /**
1123: * Sets a header to be used in {@link #getHeader}.
1124: */
1125: public void setHeader(String key, String value) {
1126: headers.put(key, value);
1127: }
1128:
1129: /**
1130: * Sets the name of the HTTP method with which this request
1131: * was made. This value will be returned in the getMethod
1132: * method.
1133: *
1134: *
1135: * @param methodType one of the following constant values
1136: * defined in this class: {@link #GET}, {@link #POST}, and {@link #PUT}
1137: *
1138: */
1139: public void setMethod(int methodType) {
1140: switch (methodType) {
1141: case GET:
1142: method = "GET";
1143: break;
1144: case PUT:
1145: method = "PUT";
1146: break;
1147: case POST:
1148: method = "POST";
1149: break;
1150: default:
1151: method = "POST";
1152: }
1153: }
1154:
1155: /**
1156: * Sets parameter value to be used by {@link #getParameter}.
1157: */
1158: public void setParameterValue(String key, String[] value) {
1159: parameters.put(key, value);
1160: }
1161:
1162: /**
1163: * Sets path information to be used by {@link #getPathInfo}.
1164: */
1165: public void setPathInfo(String s) {
1166: pathInfo = s;
1167: }
1168:
1169: /**
1170: * Sets query string to be used by {@link #getQueryString}.
1171: */
1172: public void setQueryString(String s) {
1173: this .queryString = s;
1174: }
1175:
1176: /**
1177: * Sets remote user to be used by {@link #getRemoteUser}.
1178: */
1179: public void setRemoteUser(String remoteUser) {
1180: this .remoteUser = remoteUser;
1181: }
1182:
1183: /**
1184: * Sets remote address to be used by {@link #getRemoteAddr}.
1185: */
1186: public void setRemoteAddr(String remoteAddr) {
1187: this .remoteAddr = remoteAddr;
1188: }
1189:
1190: /**
1191: * Sets remote host to be used by {@link #getRemoteHost}.
1192: */
1193: public void setRemoteHost(String remoteHost) {
1194: this .remoteHost = remoteHost;
1195: }
1196:
1197: /**
1198: * Sets requested session ID to be used by {@link #getRequestedSessionId}.
1199: */
1200: public void setRequestedSessionId(String s) {
1201: reqSessionId = s;
1202: }
1203:
1204: /**
1205: * Sets request URI to be used by {@link #getRequestURI}.
1206: */
1207: public void setRequestURI(String requestURI) {
1208: this .requestURI = requestURI;
1209: }
1210:
1211: /**
1212: * Sets the request URL to be used in this test. This method uses
1213: * the given request URL to also set the scheme, server name, server
1214: * port, request URI, and query string.
1215: */
1216: public void setRequestURL(String url) {
1217:
1218: // set request url
1219: int queryIndex = url.lastIndexOf('?');
1220: if (queryIndex < 0)
1221: queryIndex = url.length();
1222: this .requestURL = url.substring(0, queryIndex);
1223:
1224: // set query string
1225: if (queryIndex != url.length())
1226: setQueryString(url.substring(queryIndex + 1));
1227:
1228: // set scheme
1229: int schemeIndex = url.lastIndexOf("://");
1230: setScheme(url.substring(0, schemeIndex));
1231:
1232: // set uri
1233: setRequestURI(url.substring(url.indexOf('/', schemeIndex + 3),
1234: queryIndex));
1235:
1236: // set server name and port
1237: int portIndex = url.indexOf(':', schemeIndex + 2);
1238: if (portIndex > 0) {
1239: setServerName(url.substring(schemeIndex + 3, portIndex));
1240: setServerPort(Integer.parseInt(url.substring(portIndex + 1,
1241: url.indexOf('/', schemeIndex + 3))));
1242: } else {
1243: setServerName(url.substring(schemeIndex + 3, url.indexOf(
1244: '/', schemeIndex + 3)));
1245: if (isSecure())
1246: setServerPort(443);
1247: else
1248: setServerPort(80);
1249: }
1250: }
1251:
1252: /**
1253: * Sets scheme to be used by {@link #getScheme}.
1254: */
1255: public void setScheme(String s) {
1256: scheme = s;
1257: }
1258:
1259: /**
1260: * Sets servlet path to be used by {@link #getServletPath}.
1261: */
1262: public void setServletPath(String s) {
1263: servletPath = s;
1264: }
1265:
1266: /**
1267: * Sets server name to be used by {@link #getServerName}.
1268: */
1269: public void setServerName(String s) {
1270: serverName = s;
1271: }
1272:
1273: /**
1274: * Sets the context path to be used by {@link #getContextPath}.
1275: */
1276: public void setContextPath(String s) {
1277: contextPath = s;
1278: }
1279:
1280: /**
1281: * Sets the locale to be used by {@link #getLocale}.
1282: */
1283: public void setLocale(Locale locale) {
1284: this .locale = locale;
1285: }
1286:
1287: /**
1288: * Sets the Principal used by {@link #getUserPrincipal}.
1289: */
1290: public void setUserPrincipal(Principal principal) {
1291: this .principal = principal;
1292: }
1293:
1294: public int getRemotePort() {
1295: return remotePort;
1296: }
1297:
1298: public void setRemotePort(int remotePort) {
1299: this .remotePort = remotePort;
1300: }
1301:
1302: public String getLocalAddr() {
1303: return localAddr;
1304: }
1305:
1306: public void setLocalAddr(String localAddr) {
1307: this .localAddr = localAddr;
1308: }
1309:
1310: public String getLocalName() {
1311: return localName;
1312: }
1313:
1314: public void setLocalName(String localName) {
1315: this .localName = localName;
1316: }
1317:
1318: public int getLocalPort() {
1319: return localPort;
1320: }
1321:
1322: public void setLocalPort(int localPort) {
1323: this.localPort = localPort;
1324: }
1325:
1326: }
|