jigsawhttpservletrequest.java
来自「很棒的web服务器源代码」· Java 代码 · 共 1,199 行 · 第 1/3 页
JAVA
1,199 行
* Returns an input stream for reading binary data in the request body. * @exception IllegalStateException if getReader has been called on * this same request. * @exception IOException on other I/O related errors. * @see JigsawHttpServletRequest#getReader */ public ServletInputStream getInputStream() throws IOException { if (stream_state == STREAM_READER_USED) throw new IllegalStateException("Reader used"); stream_state = INPUT_STREAM_USED; return getJigsawInputStream(); } /** * @exception IOException if an IO error occurs */ protected ServletInputStream getJigsawInputStream() throws IOException { // If alredy computed return: if ( is != null ) return is; // Built it: InputStream stream = null; if ((stream = request.getInputStream()) == null) { stream = new ContentLengthInputStream(null, 0); } return is = new JigsawServletInputStream(stream); } /** * ServletRequest implementation - Get a parameter value. * @return The String encoded value for the parameter. */ public String getParameter(String name) { prepareQueryParameters(); if ( queryParameters != null ) { Object value = queryParameters.get(name); if (value != null) { return ((String[])value)[0]; } } return null; } /** * ServletRequest implementation - Get a parameter value. (v2.3) * @return a Map of the parameters in this request */ public Map getParameterMap() { prepareQueryParameters(); return queryParameters; } /** * ServletRequest implementation - Get the parameters value. * @return The String array encoded value for the parameter. */ public String[] getParameterValues(String parameter) { Vector V = new Vector(23); prepareQueryParameters(); if (queryParameters == null) { return null; } Object value = queryParameters.get(parameter); if (value == null) { return null; } return (String[])value; } /** * ServletRequest implementation - List available parameters. * @return An enumeration of parameter names. */ public Enumeration getParameterNames() { prepareQueryParameters(); return ((queryParameters == null) ? new EmptyEnumeration() : queryParameters.keys()); } /** * ServletRequest implementation - Get an attribute of the request. * This closely match Jigsaw's notion of request state. * @param name The name of the attribute. * @return An object that gives the value of the attribute. */ public Object getAttribute(String name) { return request.getState(name); } public void setAttribute(String name, Object object) { request.setState(name, object); } /** * Removes an attribute from this request. This method is not * generally needed as attributes only persist as long as the request * is being handled. * * <p>Attribute names should follow the same conventions as * package names. Names beginning with <code>java.*</code>, * <code>javax.*</code>, and <code>com.sun.*</code>, are * reserved for use by Sun Microsystems. * * @param name a <code>String</code> specifying * the name of the attribute to remove */ public void removeAttribute(String name) { request.delState(name); } public Enumeration getAttributeNames() { return request.getStateNames(); } /** * Returns the preferred <code>Locale</code> that the client will * accept content in, based on the Accept-Language header. * If the client request doesn't provide an Accept-Language header, * this method returns the default locale for the server. * * @return the preferred <code>Locale</code> for the client */ public Locale getLocale() { return (Locale)getLocales().nextElement(); } /** * Returns an <code>Enumeration</code> of <code>Locale</code> 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 <code>Enumeration</code> containing one * <code>Locale</code>, the default locale for the server. * * @return an <code>Enumeration</code> of preferred * <code>Locale</code> objects for the client */ public Enumeration getLocales() { HttpAcceptLanguage languages[] = request.getAcceptLanguage(); if (languages == null) { Vector def = new Vector(); def.addElement(Locale.getDefault()); return def.elements(); } //LinkedList is better, but we must be JDK1.1 compliant Vector locales = new Vector(); for (int i = 0 ; i < languages.length ; i++) { HttpAcceptLanguage language = languages[i]; double quality = language.getQuality(); String lang = language.getLanguage(); String country = ""; int idx = lang.indexOf('-'); if (idx > -1) { country = lang.substring(idx + 1).trim(); lang = lang.substring(0, idx).trim(); } // insert the Locale in ordered list int qidx = 0; int size = locales.size(); if (size > 0) { QLocale ql = (QLocale) locales.firstElement(); while ((qidx < size) && (ql.getLanguageQuality() >= quality)) { try { ql = (QLocale) locales.elementAt(++qidx); } catch (ArrayIndexOutOfBoundsException ex) { //end of vector, so append } } locales.insertElementAt(new QLocale(lang, country, quality), qidx); } else { locales.addElement(new QLocale(lang, country, quality)); } } // because Locale is final :( int size = locales.size(); Vector vlocale = new Vector(size); for (int i = 0 ; i < size ; i ++) { vlocale.addElement(((QLocale)locales.elementAt(i)).getLocale()); } return vlocale.elements(); } /** * Returns a boolean indicating whether this request was made using a * secure channel, such as HTTPS. * * @return a boolean indicating if the request was made using a * secure channel */ public boolean isSecure() { // only https secure? return (request.getURL().getProtocol().equalsIgnoreCase("https")); } /** * HttpServletRequest implementation - Get the request's method. * @return A String instance. */ public String getMethod() { return request.getMethod(); } /** * HttpServletRequest implementation - Get the request's path info. * @return A String instance or <strong>null</strong>. */ public String getPathInfo() { if (request.hasState(JigsawRequestDispatcher.PATH_INFO_P)) { String pathinfo = (String) request.getState(JigsawRequestDispatcher.PATH_INFO_P); return (pathinfo.equals("/")) ? null : pathinfo; } return null; } /** * HttpServletRequest implementation - Get the request's path translated. * @return A String instance or <strong>null</strong>. */ public String getPathTranslated() { String pathinfo = getPathInfo(); if ( pathinfo != null ) return getRealPath(pathinfo); return null; } /** * Returns the portion of the request URI that indicates the context * of the request. The context path always comes first in a request * URI. The path starts with a "/" character but does not end with a "/" * character. For servlets in the default (root) context, this method * returns "". * @return a <code>String</code> specifying the portion of the request * URI that indicates the context of the request */ public String getContextPath() { return ""; } public boolean hasQueryString() { if (request.hasQueryString()) { return true; } else { return request.hasState(JigsawRequestDispatcher.QUERY_STRING_P); } } /** * HttpServletRequest implementation - Get the request's query string. * @return A String instance or <strong>null</strong>. */ public String getQueryString() { if (request.hasQueryString()) { return request.getQueryString(); } else if (request.hasState(JigsawRequestDispatcher.QUERY_STRING_P)) { return (String) request.getState(JigsawRequestDispatcher.QUERY_STRING_P); } return null; } /** * HttpServletRequest implementation - Get the request's user (if any). * @return A String instance or <strong>null</strong>. */ public String getRemoteUser() { return (String) request.getState(AuthFilter.STATE_AUTHUSER); } /** * Returns a boolean indicating whether the authenticated user is included * in the specified logical "role". Roles and role membership can be * defined using deployment descriptors. If the user has not been * authenticated, the method returns <code>false</code>. * * @param role a <code>String</code> specifying the name of the role * @return a <code>boolean</code> indicating whether the user making this * request belongs to a given role; <code>false</code> if the user has not * been authenticated */ public boolean isUserInRole(String role) { throw new RuntimeException("Not Yet Implemented"); } /** * Returns a <code>java.security.Principal</code> object containing * the name of the current authenticated user. If the user has not been * authenticated, the method returns <code>null</code>. * * @return a <code>java.security.Principal</code> containing * the name of the user making this request; <code>null</code> if the * user has not been authenticated */ public Principal getUserPrincipal() { return new PrincipalImpl(getRemoteUser()); } /** * HttpServletRequest implementation - Get the request's auth method. * @return A String instance or <strong>null</strong>. */ public String getAuthType() { return (String) request.getState(AuthFilter.STATE_AUTHTYPE); } /** * HttpServletRequest implementation - Get a request header as a String. * @return A String instance or <strong>null</strong>. */ public String getHeader(String name) { return request.getValue(name); } /** * Returns all the values of the specified request header * as an <code>Enumeration</code> of <code>String</code> objects. * * <p>Some headers, such as <code>Accept-Language</code> can be sent * by clients as several headers each with a different value rather than * sending the header as a comma separated list. * * <p>WARNING, this can't happen with Jigsaw, all multiple values are * grouped in one, and only one, header. So, this method always return * ONE header value. * * <p>If the request did not include any headers * of the specified name, this method returns an empty * <code>Enumeration</code>. * The header name is case insensitive. You can use * this method with any request header. * * @param name a <code>String</code> specifying the header name * @return a <code>Enumeration</code> containing the values of the * requested header, or <code>null</code> if the request does not * have any headers of that name */ public Enumeration getHeaders(String name) { String value = getHeader(name); String array[] = { value }; return new ArrayEnumeration(array); } /** * HttpServletRequest implementation - Get a request header as an int. * @return An int, or <strong>-1</strong>. */ public int getIntHeader(String name) { HeaderValue v = request.getHeaderValue(name); if ( v != null ) { Object o = v.getValue(); if ((o != null) && (o instanceof Integer)) return ((Integer) o).intValue(); } return -1; } /** * HttpServletRequest implementation - Get a request header as an date. * @return An long (as a number of milliseconds), or <strong>-1</strong>. */ public long getDateHeader(String name) { HeaderValue v = request.getHeaderValue(name, null); if ( v != null ) { Object o = v.getValue(); if ((o != null) && (o instanceof Long)) return ((Long) o).longValue(); } return (long) -1; } /** * HttpServletRequest implementation - Get a all header names. * @return An enumeration. */ public Enumeration getHeaderNames() {
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?