jigsawhttpservletrequest.java
来自「很棒的web服务器源代码」· Java 代码 · 共 1,199 行 · 第 1/3 页
JAVA
1,199 行
return new HeaderNames(request.enumerateHeaderDescriptions()); } /** * Gets, from the first line of the HTTP request, * the part of this request's URI that is to the left of any query string. */ public String getRequestURI() { String uri = null; if (request.hasState(JigsawRequestDispatcher.REQUEST_URI_P)) { uri = (String) request.getState(JigsawRequestDispatcher.REQUEST_URI_P); try { URL u = new URL(request.getURL(), uri); uri = u.getFile(); } catch (MalformedURLException muex) {} } else { //fixme test if (request.isProxy()) { uri = request.getURL().toExternalForm(); } else { uri = request.getURLPath(); } if (hasQueryString()) { String query = getQueryString(); int idx = uri.lastIndexOf(query); uri = uri.substring(0, idx-1); } } return uri; } /** * Gets, from the first line of the HTTP request, * the part of this request's URI that is to the left of any query string. */ public StringBuffer getRequestURL() { String uri = null; if (request.hasState(JigsawRequestDispatcher.REQUEST_URI_P)) { uri = (String) request.getState(JigsawRequestDispatcher.REQUEST_URI_P); try { URL u = new URL(request.getURL(), uri); uri = u.toExternalForm(); } catch (MalformedURLException muex) {} } else { uri = request.getURL().toExternalForm(); if (hasQueryString()) { String query = getQueryString(); int idx = uri.lastIndexOf(query); uri = uri.substring(0, idx-1); } } return new StringBuffer(uri); } /** * Returns a {@link RequestDispatcher} object that acts as a wrapper for * the resource located at the given path. * A <code>RequestDispatcher</code> object can be used to forward * a request to the resource or to include the resource in a response. * The resource can be dynamic or static. * * <p>The pathname specified may be relative, although it cannot extend * outside the current servlet context. If the path begins with * a "/" it is interpreted as relative to the current context root. * This method returns <code>null</code> if the servlet container * cannot return a <code>RequestDispatcher</code>. * * <p>The difference between this method and {@link * ServletContext#getRequestDispatcher} is that this method can take a * relative path. * * @param path a <code>String</code> specifying the pathname * to the resource * @return a <code>RequestDispatcher</code> object that acts as a * wrapper for the resource at the specified path * @see RequestDispatcher * @see ServletContext#getRequestDispatcher */ public RequestDispatcher getRequestDispatcher(String path) { if (path == null) { throw new IllegalArgumentException("null"); } String urlpath = null; ResourceReference rr = request.getTargetResource(); if (! path.startsWith("/")) { String uri = null; try { ResourceReference rrp = rr.lock().getParent(); try { Resource r = rrp.lock(); uri = r.getURLPath(); } catch (InvalidResourceException irex) { return null; } finally { rrp.unlock(); } } catch (InvalidResourceException ex) { return null; } finally { rr.unlock(); } urlpath = ( uri.endsWith("/") ? uri+path : uri+"/"+path ); } else { urlpath = path; } return JigsawRequestDispatcher.getRequestDispatcher(urlpath, getServer(), rr); } /** * Gets the part of this request's URI that refers to the servlet * being invoked. Analogous to the CGI variable SCRIPT_NAME. */ public String getServletPath() { if (request.hasState(JigsawRequestDispatcher.SERVLET_PATH_P)) { return (String) request.getState(JigsawRequestDispatcher.SERVLET_PATH_P); } else { ResourceReference rr = request.getTargetResource(); try { return rr.lock().getURLPath(); } catch (InvalidResourceException ex) { return null; } finally { rr.unlock(); } } } /** * @return the scheme of the URL used in this request, for example "http", * "https", or "ftp". Different schemes have different rules * for constructing URLs, as noted in RFC 1738. The URL used to create * a request may be reconstructed using this scheme, the server name * and port, and additional information such as URIs. */ public String getScheme() { return request.getURL().getProtocol(); } /** * Gets the array of cookies found in this request. * @return the array of cookies found in this request or * <strong>null</strong> if there is no cookie. */ public Cookie[] getCookies() { HttpCookieList cookielist = request.getCookie(); Cookie[] Scookies = null; if (cookielist != null) { HttpCookie[] cookies = cookielist.getCookies(); Scookies = new Cookie[cookies.length]; for (int i = 0 ; i < cookies.length ; i++ ) { Scookies[i] = convertCookie(cookies[i]); } } return Scookies; } protected Cookie convertCookie(HttpCookie httpCookie) { Cookie cookie = new Cookie(httpCookie.getName(), httpCookie.getValue()); String val = null; if ((val = httpCookie.getDomain()) != null) cookie.setDomain(val); if ((val = httpCookie.getPath()) != null) cookie.setPath(val); cookie.setVersion(httpCookie.getVersion()); return cookie; } protected String getRequestedSessionIdFromCookie() { HttpCookieList cookielist = request.getCookie(); if (cookielist != null) { HttpCookie httpCookie = request.getCookie().getCookie(getCookieName()); if (httpCookie != null) return httpCookie.getValue(); } return null; } protected String getRequestedSessionIdFromURL() { return getURLParameter(getCookieName()); } /** * Gets the session id specified with this request. This may differ * from the actual session id. For example, if the request specified an * id for an invalid session, then this will get a new session with a * new id. * @return the session id specified by this request, or null if the * request did not specify a session id. */ public String getRequestedSessionId() { if (requestedSessionID == null) { requestedSessionID = getRequestedSessionIdFromCookie(); if (requestedSessionID == null) requestedSessionID = getRequestedSessionIdFromURL(); } return requestedSessionID; } protected synchronized JigsawHttpSessionContext getSessionContext() { return sessionContext; } /** * Gets the current valid session associated with this request, if create * is false or, if necessary, creates a new session for the request, if * create is true. * @return the session associated with this request or null if create * was false and no valid session is associated with this request. */ public HttpSession getSession(boolean create) { if (httpSession == null) { httpSession = (JigsawHttpSession) getSession(getRequestedSessionId()); if (httpSession != null) // the client join the session httpSession.setNoMoreNew(); } if (httpSession == null & create) { httpSession = new JigsawHttpSession(getSessionContext(), servletContext, createCookie()); response.addCookie(httpSession.getCookie()); } else if (httpSession != null) { httpSession.setLastAccessedTime(); if (! httpSession.isValid()) { httpSession = new JigsawHttpSession(getSessionContext(), servletContext, createCookie()); response.addCookie(httpSession.getCookie()); } } return httpSession; } /** * Gets the current valid session associated with this request. * @return the session associated with this request. */ public HttpSession getSession() { return getSession(true); } protected String getCookieName() { ObservableProperties props = request.getClient().getServer().getProperties(); return props.getString(ServletProps.SERVLET_COOKIE_NAME, ServletProps.DEFAULT_COOKIE_NAME); } protected Cookie createCookie() { ObservableProperties props = request.getClient().getServer().getProperties(); String name = props.getString(ServletProps.SERVLET_COOKIE_NAME, ServletProps.DEFAULT_COOKIE_NAME); String path = props.getString(ServletProps.SERVLET_COOKIE_PATH, "/"); String domain = props.getString(ServletProps.SERVLET_COOKIE_DOMAIN, null); String comment = props.getString(ServletProps.SERVLET_COOKIE_COMMENT, null); int maxage = props.getInteger(ServletProps.SERVLET_COOKIE_MAXAGE, 86400); boolean secure = props.getBoolean(ServletProps.SERVLET_COOKIE_SECURE, false); Cookie cookie = new Cookie(name, null); cookie.setPath(path); cookie.setMaxAge(maxage); if ((comment != null) && (comment.length() > 0)) cookie.setComment(comment); if ((domain != null) && (domain.length() > 0)) cookie.setDomain(domain); cookie.setSecure(secure); return cookie; } protected HttpSession getSession(String sessionId) { if (sessionId != null) return getSessionContext().getSession(sessionId); return null; } /** * Checks whether this request is associated with a session that is valid * in the current session context. If it is not valid, the requested * session will never be returned from the getSession method. * @return true if this request is assocated with a session that is valid * in the current session context. */ public boolean isRequestedSessionIdValid() { JigsawHttpSession session = (JigsawHttpSession) getSession(getRequestedSessionId()); if (session == null) return false; return (session.isValid()); } /** * Checks whether the session id specified by this request came in as * a cookie. (The requested session may not be one returned by the * getSession method.) * @return true if the session id specified by this request came * in as a cookie; false otherwise */ public boolean isRequestedSessionIdFromCookie() { return (getRequestedSessionIdFromCookie() != null); } /** * Checks whether the session id specified by this request came in as * part of the URL. (The requested session may not be the one returned * by the getSession method.) * @return true if the session id specified by the request for this * session came in as part of the URL; false otherwise * @deprecated since jsdk2.1 */ public boolean isRequestedSessionIdFromUrl() { return (getRequestedSessionIdFromURL() != null); } /** * Checks whether the session id specified by this request came in as * part of the URL. (The requested session may not be the one returned * by the getSession method.) * @return true if the session id specified by the request for this * session came in as part of the URL; false otherwise */ public boolean isRequestedSessionIdFromURL() { return (getRequestedSessionIdFromURL() != null); } protected BufferedReader reader = null; /** * Returns a buffered reader for reading text in the request body. * This translates character set encodings as appropriate. * @exception UnsupportedEncodingException if the character set encoding * is unsupported, so the text can't be correctly decoded. * @exception IllegalStateException if getInputStream has been called on * this same request. * @exception IOException on other I/O related errors. * @see JigsawHttpServletRequest#getInputStream */ public BufferedReader getReader() throws IOException { if (stream_state == INPUT_STREAM_USED) throw new IllegalStateException("Input Stream used"); stream_state = STREAM_READER_USED; if (reader == null) { InputStream is = getJigsawInputStream(); String enc = getCharacterEncoding(); if (enc != null) { InputStreamReader isr = null; try { isr = new InputStreamReader(is, enc); } catch (UnsupportedEncodingException ex) { // not a valid encoding, skip it isr = null; } if (isr != null) reader = new BufferedReader(isr); else reader = new BufferedReader(new InputStreamReader(is)); } else { reader = new BufferedReader(new InputStreamReader(is)); } } return reader; } /** * Get the wrapped Jigsaw Request. * @return the request */ protected Request getRequest() { return request; } JigsawHttpServletRequest(Servlet servlet, JigsawServletContext servletContext, Request request, JigsawHttpServletResponse response, JigsawHttpSessionContext sessionContext) { this.servlet = servlet; this.servletContext = servletContext; this.request = request; this.response = response; this.sessionContext = sessionContext; }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?