abstracthttprequest.java
来自「RESIN 3.2 最新源码」· Java 代码 · 共 2,711 行 · 第 1/4 页
JAVA
2,711 行
SessionImpl session = (SessionImpl) getSession(false); return session != null && session.isValid() && session.getId().equals(id); } /** * Returns true if the current sessionId came from a cookie. */ public boolean isRequestedSessionIdFromCookie() { return findSessionIdFromCookie() != null; } /** * Returns true if the current sessionId came from the url. */ public boolean isRequestedSessionIdFromURL() { return findSessionIdFromUrl() != null; } /** * @deprecated */ public boolean isRequestedSessionIdFromUrl() { return isRequestedSessionIdFromURL(); } /** * Returns the session id in the HTTP request. The cookie has * priority over the URL. Because the webApp might be using * the cookie to change the page contents, the caching sets * vary: JSESSIONID. */ public String getRequestedSessionIdNoVary() { boolean varyCookies = _varyCookies; String varyCookie = _varyCookie; boolean hasCookie = _hasCookie; boolean privateCache = _response.getPrivateCache(); String id = getRequestedSessionId(); _varyCookies = varyCookies; _varyCookie = varyCookie; _hasCookie = hasCookie; _response.setPrivateOrResinCache(privateCache); return id; } /** * Returns the session id in the HTTP request. The cookie has * priority over the URL. Because the webApp might be using * the cookie to change the page contents, the caching sets * vary: JSESSIONID. */ public String getRequestedSessionId() { SessionManager manager = getSessionManager(); String cookieName = null; if (manager != null && manager.enableSessionCookies()) { setVaryCookie(getSessionCookie(manager)); String id = findSessionIdFromCookie(); if (id != null) { _isSessionIdFromCookie = true; setHasCookie(); return id; } } String id = findSessionIdFromUrl(); if (id != null) { return id; } if (manager != null && manager.enableSessionCookies()) return null; else return findSessionIdFromConnection(); } /** * For SSL connections, use the SSL identifier. */ public String findSessionIdFromConnection() { return null; } /** * Returns the session id in the HTTP request cookies. * Because the webApp might use the cookie to change * the page contents, the caching sets vary: JSESSIONID. */ private String findSessionIdFromCookie() { SessionManager manager = getSessionManager(); if (manager == null || ! manager.enableSessionCookies()) return null; Cookie cookie = findCookie(getSessionCookie(manager)); if (cookie != null) { _isSessionIdFromCookie = true; return cookie.getValue(); } else return null; } /** * Returns the session id in the HTTP request from the url. */ private String findSessionIdFromUrl() { // server/1319 // setVaryCookie(getSessionCookie(manager)); String id = _invocation != null ? _invocation.getSessionId() : null; if (id != null) setHasCookie(); return id; } public int getSessionGroup() { return _sessionGroup; } /** * Returns the current session. * * XXX: duplicated in RequestAdapter * * @param create true if a new session should be created * * @return the current session */ private SessionImpl createSession(boolean create, boolean hasOldSession) { SessionManager manager = getSessionManager(); if (manager == null) return null; String id = getRequestedSessionId(); long now = Alarm.getCurrentTime(); SessionImpl session; if (id != null && id.length() > 6) { // server/01t0 session = manager.getSession(id, now, false, _isSessionIdFromCookie); if (session == null) { } else if (session.isValid()) { if (session != null) { setVaryCookie(getSessionCookie(manager)); setHasCookie(); } if (! session.getId().equals(id) && manager.enableSessionCookies()) getResponse().setSessionId(session.getId()); return session; } } else id = null; if (! create) return null; // Must accept old ids because different webApps in the same // server must share the same cookie // // But, if the session group doesn't match, then create a new // session. session = manager.createSession(id, now, this, _isSessionIdFromCookie); if (session != null) setHasCookie(); if (session.getId().equals(id)) return session; if (manager.enableSessionCookies()) getResponse().setSessionId(session.getId()); return session; } /** * Returns the session manager. */ protected final SessionManager getSessionManager() { WebApp webApp = getWebApp(); if (webApp != null) return webApp.getSessionManager(); else return null; } /** * Returns the session cookie. */ protected final String getSessionCookie(SessionManager manager) { if (isSecure()) return manager.getSSLCookieName(); else return manager.getCookieName(); } /** * Gets the authorization type */ public String getAuthType() { Object login = getAttribute(com.caucho.server.security.AbstractAuthenticator.LOGIN_NAME); if (login instanceof X509Certificate) return CLIENT_CERT_AUTH; WebApp app = getWebApp(); if (app != null && app.getLogin() != null && getUserPrincipal() != null) return app.getLogin().getAuthType(); else return null; } /** * Internal logging return to get the remote user. If the request already * knows the user, get it, otherwise just return null. */ public String getRemoteUser(boolean create) { if (_session == null) return null; Principal user = _session.getUser(); if (user == null) { if (! create) return null; user = getUserPrincipal(); } if (user != null) return user.getName(); else return null; } /** * Authenticate the user. */ public boolean authenticate() throws ServletException, IOException { Principal user = null; if (_session == null) getSession(false); // If the user object is already an attribute, return it. if (_session != null) { user = _session.getUser(); if (user != null) return true; } WebApp app = getWebApp(); if (app == null) { if (log.isLoggable(Level.FINE)) log.finer("authentication failed, no web-app found"); _response.sendError(HttpServletResponse.SC_FORBIDDEN); return false; } // If the authenticator can find the user, return it. AbstractLogin login = app.getLogin(); if (login != null) { user = login.authenticate(this, getResponse(), app); if (user == null) return false; if (_session == null) getSession(true); _session.setUser(user); return true; } else { if (log.isLoggable(Level.FINE)) log.finer("authentication failed, no login module found for " + app); _response.sendError(HttpServletResponse.SC_FORBIDDEN); return false; } } /** * Gets the remote user from the authorization type */ public String getRemoteUser() { Principal principal = getUserPrincipal(); if (principal != null) return principal.getName(); else return null; } /** * Returns the Principal representing the logged in user. */ public Principal getUserPrincipal() { try { Principal user; user = (Principal) getAttribute(AbstractAuthenticator.LOGIN_NAME); if (user != null) return user; if (_session == null) getSession(false); // If the user object is already an attribute, return it. if (_session != null) { user = _session.getUser(); if (user != null) return user; } WebApp app = getWebApp(); if (app == null) return null; // If the authenticator can find the user, return it. AbstractLogin login = app.getLogin(); if (login != null) { user = login.getUserPrincipal(this, getResponse(), app); if (user != null) { getSession(true); _session.setUser(user); _response.setPrivateCache(true); } else { // server/123h, server/1920 // distinguishes between setPrivateCache and setPrivateOrResinCache // _response.setPrivateOrResinCache(true); } } return user; } catch (ServletException e) { log.log(Level.WARNING, e.toString(), e); return null; } } /** * Logs out the principal. */ public void logout() { if (_session != null) _session.logout(); } /** * Clear the principal from the request object. */ public void logoutUserPrincipal() { if (_session != null) _session.logout(); } /** * Sets the overriding role. */ public String runAs(String role) { String oldRunAs = _runAs; _runAs = role; return oldRunAs; } /** * Returns true if the user represented by the current request * plays the named role. * * @param role the named role to test. * @return true if the user plays the role. */ public boolean isUserInRole(String role) { HashMap<String,String> roleMap = _invocation.getSecurityRoleMap(); if (roleMap != null) { String linkRole = roleMap.get(role); if (linkRole != null) role = linkRole; } if (_runAs != null) return _runAs.equals(role); WebApp app = getWebApp(); AbstractLogin login = app == null ? null : app.getLogin(); if (login == null) return false; boolean inRole = false; Principal user = getUserPrincipal(); try { inRole = login.isUserInRole(this, getResponse(), app, user, role); } catch (ServletException e) { if (app != null) app.log(String.valueOf(e), e); log.log(Level.FINE, e.toString(), e); } if (log.isLoggable(Level.FINE)) { if (user == null) log.fine("no user for isUserInRole"); else if (inRole) log.fine(user + " is in role: " + role); else log.fine("failed " + user + " in role: " + role); } return inRole; } /** * Returns true if the transport is secure. */ public boolean isTransportSecure() { return _conn.isSecure(); } /** * Returns the requests underlying read stream, e.g. the post stream. */ public ReadStream getStream() throws IOException { return getStream(true); } /** * Returns the requests underlying read stream, e.g. the post stream. */ public ReadStream getStream(boolean isReader) throws IOException { if (! _hasReadStream) { _hasReadStream = true; initStream(_readStream, _rawRead); if (isReader) { // Encoding is based on getCharacterEncoding. // getReader needs the encoding. String charEncoding = getCharacterEncoding(); String javaEncoding = Encoding.getJavaName(charEncoding); _readStream.setEncoding(javaEncoding); } if (_expect100Continue) { _expect100Continue = false; _response.writeContinue(); } } return _readStream; } /** * Returns the raw read buffer. */ public byte []getRawReadBuffer() { return _rawRead.getBuffer(); } protected void skip() throws IOException { if (! _hasReadStream) { if (! initStream(_readStream, _rawRead)) return; _hasReadStream = true; } while ((_readStream.skip(8192) > 0)) { } } /** * Initialize the read stream from the raw stream. */ abstract protected boolean initStream(ReadStream readStream, ReadStream rawStream) throws IOException; /** * Returns the raw input stream. */ public ReadStream getRawInput() { throw new UnsupportedOperationException(L.l("raw mode is not supported in this configuration")); } /** * Returns a stream for reading POST data. */ public ServletInputStream getInputStream() throws IOException { if (_hasReader) throw new IllegalStateException(L.l("getInputStream() can't be called after getReader()")); _hasInputStream = true; ReadStream stream = getStream(false); _is.init(stream); return _is; } /** * Returns a Reader for the POST contents */ public BufferedReader getReader() throws IOException { if (_hasInputStream) throw new IllegalStateException(L.l("getReader() can't be called after getInputStream()")); _hasReader = true; try { // bufferedReader is just an adapter to get the signature right. _bufferedReader.init(getStream(true)); return _bufferedReader; } catch (java.nio.charset.UnsupportedCharsetException e) { throw new UnsupportedEncodingException(e.getMessage()); } } /** * Returns an enumeration of the form names. */ public Enumeration<String> getParameterNames() { if (_filledForm == null) _filledForm = parseQuery(); return Collections.enumeration(_filledForm.keySet()); } /** * Returns a map of the form. */ public Map<String,String[]> getParameterMap() { if (_filledForm == null) _filledForm = parseQuery(); return Collections.unmodifiableMap(_filledForm); } /** * Returns the form's values for the given name. * * @param name key in the form * @return value matching the key */ public String []getParameterValues(String name) { if (_filledForm == null) _filledForm = parseQuery(); return (String []) _filledForm.get(name); } /** * Returns the form primary value for the given name. */ public String getParameter(String name) { String []values = getParameterValues(name); if (values != null && values.length > 0) return values[0]; else return null; } /** * Parses the query, either from the GET or the post. * * <p/>The character encoding is somewhat tricky. If it's a post, then * assume the encoded form uses the same encoding as * getCharacterEncoding(). * * <p/>If the request doesn't provide the encoding, use the * character-encoding parameter from the webApp. * * <p/>Otherwise use the default system encoding. */ private HashMapImpl<String,String[]> parseQuery() { try { _form.clear(); String query = getQueryString(); CharSegment contentType = getContentTypeBuffer(); if (query == null && contentType == null) return _form; String charEncoding = getCharacterEncoding(); if (charEncoding == null) charEncoding = (String) getAttribute(CAUCHO_CHAR_ENCODING); if (charEncoding == null) charEncoding = (String) getAttribute(CHAR_ENCODING); if (charEncoding == null) { Locale locale = (Locale) getAttribute(FORM_LOCALE); if (locale != null) charEncoding = Encoding.getMimeName(locale); } if (query != null) { String queryEncoding = charEncoding;
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?