All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.helger.web.scope.IRequestWebScopeWithoutResponse Maven / Gradle / Ivy

There is a newer version: 10.1.9
Show newest version
/*
 * Copyright (C) 2014-2024 Philip Helger (www.helger.com)
 * philip[at]helger[dot]com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.helger.web.scope;

import java.nio.charset.Charset;
import java.security.Principal;

import javax.annotation.CheckForSigned;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;

import com.helger.commons.ValueEnforcer;
import com.helger.commons.annotation.Nonempty;
import com.helger.commons.annotation.ReturnsMutableObject;
import com.helger.commons.charset.CharsetHelper;
import com.helger.commons.http.EHttpMethod;
import com.helger.commons.http.HttpHeaderMap;
import com.helger.commons.string.StringHelper;
import com.helger.commons.url.ISimpleURL;
import com.helger.commons.url.SimpleURL;
import com.helger.http.EHttpVersion;
import com.helger.scope.IRequestScope;
import com.helger.scope.mgr.ScopeManager;
import com.helger.servlet.ServletHelper;
import com.helger.servlet.request.IRequestParamMap;
import com.helger.servlet.request.RequestHelper;
import com.helger.useragent.IUserAgent;
import com.helger.useragent.browser.BrowserInfo;

import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;

/**
 * Interface for a single web request scope object that does not offer access to
 * the HTTP response.
 *
 * @author Philip Helger
 */
public interface IRequestWebScopeWithoutResponse extends IRequestScope, IWebScope
{
  /**
   * @return A cached header map for this request. Never null.
   *         Alterations to this map are visible everywhere. Clone the object if
   *         you need to modify it.
   */
  @Nonnull
  @ReturnsMutableObject
  HttpHeaderMap headers ();

  /**
   * Get the user agent object of this HTTP request.
   *
   * @return A non-null user agent object.
   */
  @Nonnull
  @Deprecated (forRemoval = true, since = "10.1.6")
  default IUserAgent getUserAgent ()
  {
    return RequestHelper.getUserAgent (getRequest ());
  }

  /**
   * @return The information about the matching browser or null if
   *         no known browser was detected.
   */
  @Nullable
  @Deprecated (forRemoval = true, since = "10.1.6")
  default BrowserInfo getBrowserInfo ()
  {
    return getUserAgent ().getBrowserInfo ();
  }

  /**
   * @return The external URL parameters. Never null.
   */
  @Nonnull
  @ReturnsMutableObject
  IRequestParamContainer params ();

  /**
   * @return A cached request param map for this request. Never
   *         null.
   */
  @Nonnull
  IRequestParamMap getRequestParamMap ();

  @Nullable
  default String getSessionID (final boolean bCreateIfNotExisting)
  {
    final HttpSession aSession = getSession (bCreateIfNotExisting);
    return aSession == null ? null : aSession.getId ();
  }

  /**
   * Returns the name of the character encoding used in the body of this
   * request. This method returns null if the request does not
   * specify a character encoding
   *
   * @return a String containing the name of the character
   *         encoding, or null if the request does not specify a
   *         character encoding
   */
  @Nullable
  default String getCharacterEncoding ()
  {
    return getRequest ().getCharacterEncoding ();
  }

  /**
   * Returns the MIME type of the body of the request, or null if
   * the type is not known. For HTTP servlets, same as the value of the CGI
   * variable CONTENT_TYPE.
   *
   * @return a String containing the name of the MIME type of the
   *         request, or null if the type is not known
   */
  @Nullable
  default String getContentType ()
  {
    return getRequest ().getContentType ();
  }

  /**
   * Returns the length, in bytes, of the request body and made available by the
   * input stream, or -1 if the length is not known. For HTTP servlets, same as
   * the value of the CGI variable CONTENT_LENGTH.
   *
   * @return an integer containing the length of the request body or -1 if the
   *         length is not known
   */
  @CheckForSigned
  default long getContentLength ()
  {
    return RequestHelper.getContentLength (getRequest ());
  }

  /**
   * @return The charset defined for this request. May be null if
   *         none is present;
   */
  @Nullable
  default Charset getCharset ()
  {
    final String sEncoding = getRequest ().getCharacterEncoding ();
    return StringHelper.hasNoText (sEncoding) ? null : CharsetHelper.getCharsetFromName (sEncoding);
  }

  /**
   * Returns the name and version of the protocol the request uses in the form
   * protocol/majorVersion.minorVersion, for example, HTTP/1.1. For HTTP
   * servlets, the value returned is the same as the value of the CGI variable
   * SERVER_PROTOCOL.
   *
   * @return a String containing the protocol name and version
   *         number
   */
  default String getProtocol ()
  {
    return getRequest ().getProtocol ();
  }

  /**
   * @return The {@link EHttpVersion} of the request.
   */
  @Nullable
  default EHttpVersion getHttpVersion ()
  {
    return RequestHelper.getHttpVersion (getRequest ());
  }

  /**
   * Returns the name of the scheme used to make this request, for example,
   * http, https, or ftp. Different
   * schemes have different rules for constructing URLs, as noted in RFC 1738.
   *
   * @return a String containing the name of the scheme used to
   *         make this request
   */
  default String getScheme ()
  {
    return getRequest ().getScheme ();
  }

  /**
   * Returns the host name of the server to which the request was sent. It is
   * the value of the part before ":" in the Host header value, if
   * any, or the resolved server name, or the server IP address.
   *
   * @return a String containing the name of the server
   */
  default String getServerName ()
  {
    return getRequest ().getServerName ();
  }

  /**
   * Returns the port number to which the request was sent. It is the value of
   * the part after ":" in the Host header value, if any, or the
   * server port where the client connection was accepted on.
   *
   * @return an integer specifying the port number
   */
  default int getServerPort ()
  {
    return getRequest ().getServerPort ();
  }

  /**
   * Returns the Internet Protocol (IP) address of the client or last proxy that
   * sent the request. For HTTP servlets, same as the value of the CGI variable
   * REMOTE_ADDR.
   *
   * @return a String containing the IP address of the client that
   *         sent the request
   */
  default String getRemoteAddr ()
  {
    return getRequest ().getRemoteAddr ();
  }

  /**
   * Returns the fully qualified name of the client or the last proxy that sent
   * the request. If the engine cannot or chooses not to resolve the hostname
   * (to improve performance), this method returns the dotted-string form of the
   * IP address. For HTTP servlets, same as the value of the CGI variable
   * REMOTE_HOST.
   *
   * @return a String containing the fully qualified name of the
   *         client
   */
  default String getRemoteHost ()
  {
    return getRequest ().getRemoteHost ();
  }

  /**
   * Returns the Internet Protocol (IP) source port of the client or last proxy
   * that sent the request.
   *
   * @return an integer specifying the port number
   */
  default int getRemotePort ()
  {
    return getRequest ().getRemotePort ();
  }

  /**
   * 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
   */
  default boolean isSecure ()
  {
    return getRequest ().isSecure ();
  }

  /**
   * Returns the host name of the Internet Protocol (IP) interface on which the
   * request was received.
   *
   * @return a String containing the host name of the IP on which
   *         the request was received.
   */
  default String getLocalName ()
  {
    return getRequest ().getLocalName ();
  }

  /**
   * Returns the Internet Protocol (IP) address of the interface on which the
   * request was received.
   *
   * @return a String containing the IP address on which the
   *         request was received.
   */
  default String getLocalAddr ()
  {
    return getRequest ().getLocalAddr ();
  }

  /**
   * Returns the Internet Protocol (IP) port number of the interface on which
   * the request was received.
   *
   * @return an integer specifying the port number
   */
  default int getLocalPort ()
  {
    return getRequest ().getLocalPort ();
  }

  // HttpServletRequest:

  /**
   * Returns the name of the authentication scheme used to protect the servlet.
   * All servlet containers support basic, form and client certificate
   * authentication, and may additionally support digest authentication. If the
   * servlet is not authenticated null is returned.
   * 

* Same as the value of the CGI variable AUTH_TYPE. * * @return one of the static members BASIC_AUTH, FORM_AUTH, CLIENT_CERT_AUTH, * DIGEST_AUTH (suitable for == comparison) or the container-specific * string indicating the authentication scheme, or null * if the request was not authenticated. */ default String getAuthType () { return getRequest ().getAuthType (); } /** * Returns an array containing all of the Cookie objects the * client sent with this request. This method returns null if no * cookies were sent. * * @return an array of all the Cookies included with this * request, or null if the request has no cookies */ @Nullable default Cookie [] getCookies () { return ServletHelper.getRequestCookies (getRequest ()); } /** * Returns the name of the HTTP method with which this request was made, for * example, GET, POST, or PUT. Same as the value of the CGI variable * REQUEST_METHOD. * * @return a String specifying the name of the method with which * this request was made */ default String getMethod () { return ServletHelper.getRequestMethod (getRequest ()); } /** * @return The {@link EHttpMethod} matching the {@link #getMethod()} */ @Nullable default EHttpMethod getHttpMethod () { return RequestHelper.getHttpMethod (getRequest ()); } /** * Returns any extra path information associated with the URL the client sent * when it made this request. The extra path information follows the servlet * path but precedes the query string and will start with a "/" character. *

* This method returns null if there was no extra path * information. *

* Same as the value of the CGI variable PATH_INFO. * * @return a String, decoded by the web container, specifying * extra path information that comes after the servlet path but before * the query string in the request URL; or null if the * URL does not have any extra path information */ default String getPathInfo () { return RequestHelper.getPathInfo (getRequest ()); } /** * Returns any extra path information after the servlet name but before the * query string, and translates it to a real path. Same as the value of the * CGI variable PATH_TRANSLATED. *

* If the URL does not have any extra path information, this method returns * null or the servlet container cannot translate the virtual * path to a real path for any reason (such as when the web application is * executed from an archive). The web container does not decode this string. * * @return a String specifying the real path, or * null if the URL does not have any extra path * information */ default String getPathTranslated () { return getRequest ().getPathTranslated (); } /** * @return 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 "". The container does not decode this string. E.g. * /context or an empty string for the root context. * Never with a trailing slash. * @see #getFullContextPath() */ @Nonnull String getContextPath (); /** * Returns the query string that is contained in the request URL after the * path. This method returns null if the URL does not have a * query string. Same as the value of the CGI variable QUERY_STRING. * * @return a String containing the query string or * null if the URL contains no query string. The value is * not decoded by the container. */ default String getQueryString () { return ServletHelper.getRequestQueryString (getRequest ()); } /** * Returns the login of the user making this request, if the user has been * authenticated, or null if the user has not been authenticated. * Whether the user name is sent with each subsequent request depends on the * browser and type of authentication. Same as the value of the CGI variable * REMOTE_USER. * * @return a String specifying the login of the user making this * request, or null if the user login is not known */ default String getRemoteUser () { return getRequest ().getRemoteUser (); } /** * 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 false. * * @param sRole * a String specifying the name of the role * @return a boolean indicating whether the user making this * request belongs to a given role; false if the user has * not been authenticated */ default boolean isUserInRole (final String sRole) { return getRequest ().isUserInRole (sRole); } /** * Returns a java.security.Principal object containing the name * of the current authenticated user. If the user has not been authenticated, * the method returns null. * * @return a java.security.Principal containing the name of the * user making this request; null if the user has not * been authenticated */ @Nullable default Principal getUserPrincipal () { return getRequest ().getUserPrincipal (); } /** * Get the request URI without an eventually appended session * (";jsessionid=...").
* This method considers the GlobalWebScope custom context path.
* This method returns the percent decoded parameters *

* * * * * * * * * * * * * * * * *
Examples of Returned Values
First line of HTTP requestReturned Value
POST /some/pa%3Ath.html;JSESSIONID=4711/some/pa:th.html
GET http://foo.bar/a.html;JSESSIONID=4711/a.html
HEAD /xyz;JSESSIONID=4711?a=b/xyz
* * @return The request URI without the optional session ID. Never * null. * @since 9.1.0 */ @Nonnull default String getRequestURIDecoded () { return RequestHelper.getRequestURIDecoded (getRequest ()); } /** * Get the request URI without an eventually appended session * (";jsessionid=...").
* This method considers the GlobalWebScope custom context path.
* This method returns the percent encoded parameters "as is" * * * * * * * * * * * * * * * * * *
Examples of Returned Values
First line of HTTP requestReturned Value
POST /some/pa%3Ath.html;JSESSIONID=4711/some/pa%3Ath.html
GET http://foo.bar/a.html;JSESSIONID=4711/a.html
HEAD /xyz;JSESSIONID=4711?a=b/xyz
* * @return The request URI without the optional session ID. Never * null. * @since 9.1.0 */ @Nonnull default String getRequestURIEncoded () { return RequestHelper.getRequestURIEncoded (getRequest ()); } /** * 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.
* This method returns the percent decoded parameters *

* If this request has been forwarded using * {@link jakarta.servlet.RequestDispatcher#forward}, the server path in the * reconstructed URL must reflect the path used to obtain the * RequestDispatcher, and not the server path specified by the client. *

* Because this method returns a StringBuilder, not a string, you * can modify the URL easily, for example, to append query parameters. *

* This method is useful for creating redirect messages and for reporting * errors. * * @return a StringBuilder object containing the reconstructed * URL * @since 9.1.10 */ @Nonnull @Nonempty default StringBuilder getRequestURLDecoded () { return RequestHelper.getRequestURLDecoded (getRequest ()); } /** * 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.
* This method returns the percent encoded parameters "as is" *

* If this request has been forwarded using * {@link jakarta.servlet.RequestDispatcher#forward}, the server path in the * reconstructed URL must reflect the path used to obtain the * RequestDispatcher, and not the server path specified by the client. *

* Because this method returns a StringBuilder, not a string, you * can modify the URL easily, for example, to append query parameters. *

* This method is useful for creating redirect messages and for reporting * errors. * * @return a StringBuilder object containing the reconstructed * URL * @since 9.1.10 */ @Nonnull @Nonempty default StringBuilder getRequestURLEncoded () { return RequestHelper.getRequestURLEncoded (getRequest ()); } /** * Returns the part of this request's URL that calls the servlet. This path * starts with a "/" character and includes either the servlet name or a path * to the servlet, but does not include any extra path information or a query * string. Same as the value of the CGI variable SCRIPT_NAME. *

* This method will return an empty string ("") if the servlet used to process * this request was matched using the "/*" pattern. * * @return a String containing the name or path of the servlet * being called, as specified in the request URL, decoded, or an empty * string if the servlet used to process the request is matched using * the "/*" pattern. */ @Nonnull default String getServletPath () { return ServletHelper.getRequestServletPath (getRequest ()); } /** * Returns the current HttpSession associated with this request * or, if there is no current session and create is true, returns * a new session. *

* If bCreateIfNotExisting is false and the request * has no valid HttpSession, this method returns * null. *

* To make sure the session is properly maintained, you must call this method * before the response is committed. If the container is using cookies to * maintain session integrity and is asked to create a new session when the * response is committed, an IllegalStateException is thrown. * * @param bCreateIfNotExisting * true to create a new session for this request if * necessary; false to return null if there's * no current session * @return the HttpSession associated with this request or * null if bCreateIfNotExisting is * false and the request has no valid session */ @Nullable default HttpSession getSession (final boolean bCreateIfNotExisting) { return ServletHelper.getRequestSession (getRequest (), bCreateIfNotExisting); } /** * Checks whether the requested session ID is still valid. *

* If the client did not specify any session ID, this method returns * false. * * @return true if this request has an id for a valid session in * the current session context; false otherwise * @see #getSession */ default boolean isRequestedSessionIdValid () { return getRequest ().isRequestedSessionIdValid (); } /** * Checks whether the requested session ID came in as a cookie. * * @return true if the session ID came in as a cookie; otherwise, * false * @see #getSession */ default boolean isRequestedSessionIdFromCookie () { return getRequest ().isRequestedSessionIdFromCookie (); } /** * Checks whether the requested session ID came in as part of the request URL. * * @return true if the session ID came in as part of a URL; * otherwise, false * @see #getSession */ default boolean isRequestedSessionIdFromURL () { return getRequest ().isRequestedSessionIdFromURL (); } // Extended API /** * @return Return the absolute server path. E.g. "http://localhost:8080" */ @Nonnull default String getFullServerPath () { return RequestHelper.getFullServerName (getRequest ()).toString (); } /** * @return Return the absolute context path. E.g. * http://localhost:8080/context. Never with a trailing * slash. * @see #getContextPath() */ @Nonnull default String getFullContextPath () { return RequestHelper.getFullServerName (getRequest ()).append (getContextPath ()).toString (); } /** * @return Return the absolute servlet path. E.g. * /context/config.jsp or /context/action/ */ @Nonnull String getContextAndServletPath (); /** * @return Return the absolute servlet path. E.g. * http://localhost:8080/context/config.jsp or * http://localhost:8080/context/action/ */ @Nonnull String getFullContextAndServletPath (); /** * Get the full URI (excl. protocol and host) and parameters of the current * request.
* /context/servlet/path/a/b?c=123&d=789 * * @return The full URI of the current request. * @since 9.1.10 */ @Nonnull @Nonempty default String getURIDecoded () { return (String) attrs ().computeIfAbsent (ScopeManager.SCOPE_ATTRIBUTE_PREFIX_INTERNAL + "req-uri-decoded", k -> RequestHelper.getURIDecoded (getRequest ())); } /** * Get the full URI (excl. protocol and host) and parameters of the current * request.
* /context/servlet/path/a/b?c=123&d=789 * * @return The full URI of the current request. * @since 9.1.10 */ @Nonnull @Nonempty default String getURIEncoded () { return (String) attrs ().computeIfAbsent (ScopeManager.SCOPE_ATTRIBUTE_PREFIX_INTERNAL + "req-uri-encoded", k -> RequestHelper.getURIEncoded (getRequest ())); } /** * Get the full URL (incl. protocol) and parameters of the current request. *
* http://hostname.com:81/context/servlet/path/a/b?c=123&d=789 * * @return The full URL of the current request. * @since 9.1.10 */ @Nonnull @Nonempty default String getURLDecoded () { return (String) attrs ().computeIfAbsent (ScopeManager.SCOPE_ATTRIBUTE_PREFIX_INTERNAL + "req-url-decoded", k -> RequestHelper.getURLDecoded (getRequest ())); } /** * Get the full URL (incl. protocol) and parameters of the current request. *
* http://hostname.com:81/context/servlet/path/a/b?c=123&d=789 * * @return The full URL of the current request. * @since 9.1.10 */ @Nonnull @Nonempty default String getURLEncoded () { return (String) attrs ().computeIfAbsent (ScopeManager.SCOPE_ATTRIBUTE_PREFIX_INTERNAL + "req-url-encoded", k -> RequestHelper.getURLEncoded (getRequest ())); } /** * Encodes the specified URL by including the session ID in it, or, if * encoding is not needed, returns the URL unchanged. The implementation of * this method includes the logic to determine whether the session ID needs to * be encoded in the URL. For example, if the browser supports cookies, or * session tracking is turned off, URL encoding is unnecessary. *

* For robust session tracking, all URLs emitted by a servlet should be run * through this method. Otherwise, URL rewriting cannot be used with browsers * which do not support cookies. * * @param sURL * the url to be encoded. May not be null. * @return the encoded URL if encoding is needed; the unchanged URL otherwise. */ @Nonnull String encodeURL (@Nonnull String sURL); /** * Encodes the specified URL by including the session ID in it, or, if * encoding is not needed, returns the URL unchanged. The implementation of * this method includes the logic to determine whether the session ID needs to * be encoded in the URL. For example, if the browser supports cookies, or * session tracking is turned off, URL encoding is unnecessary. *

* For robust session tracking, all URLs emitted by a servlet should be run * through this method. Otherwise, URL rewriting cannot be used with browsers * which do not support cookies. * * @param aURL * the url to be encoded. May not be null. * @return the encoded URL if encoding is needed. Never null. */ @Nonnull default ISimpleURL encodeURL (@Nonnull final ISimpleURL aURL) { ValueEnforcer.notNull (aURL, "URL"); // Encode only the path and copy params and anchor return new SimpleURL (encodeURL (aURL.getPath ()), aURL.params (), aURL.getAnchor ()); } /** * Encodes the specified URL for use in the sendRedirect method * or, if encoding is not needed, returns the URL unchanged. The * implementation of this method includes the logic to determine whether the * session ID needs to be encoded in the URL. Because the rules for making * this determination can differ from those used to decide whether to encode a * normal link, this method is separated from the encodeURL * method. *

* All URLs sent to the HttpServletResponse.sendRedirect method * should be run through this method. Otherwise, URL rewriting cannot be used * with browsers which do not support cookies. * * @param sURL * the url to be encoded. * @return the encoded URL if encoding is needed; the unchanged URL otherwise. * @see #encodeURL(String) */ @Nonnull String encodeRedirectURL (@Nonnull String sURL); /** * Encodes the specified URL for use in the sendRedirect method * or, if encoding is not needed, returns the URL unchanged. The * implementation of this method includes the logic to determine whether the * session ID needs to be encoded in the URL. Because the rules for making * this determination can differ from those used to decide whether to encode a * normal link, this method is separated from the encodeURL * method. *

* All URLs sent to the HttpServletResponse.sendRedirect method * should be run through this method. Otherwise, URL rewriting cannot be used * with browsers which do not support cookies. * * @param aURL * the url to be encoded. May not be null. * @return the encoded URL if encoding is needed. Never null. * @see #encodeURL(String) */ @Nonnull default ISimpleURL encodeRedirectURL (@Nonnull final ISimpleURL aURL) { ValueEnforcer.notNull (aURL, "URL"); // Encode only the path and copy params and anchor return new SimpleURL (encodeRedirectURL (aURL.getPath ()), aURL.params (), aURL.getAnchor ()); } /** * Check if this request uses a Cookie based session handling (meaning cookies * are enabled) or whether the session ID needs to be appended to a URL. * * @return true if the session ID is passed via cookies. */ default boolean areCookiesEnabled () { // Just check whether the session ID is appended to the URL or not return "a".equals (encodeURL ("a")); } /** * Return the URI of the request within the servlet context. * * @return the path within the web application and never null. By * default "/" is returned is an empty request URI is determined. */ @Nonnull default String getPathWithinServletContext () { return RequestHelper.getPathWithinServletContext (getRequest ()); } /** * Return the path within the servlet mapping for the given request, i.e. the * part of the request's URL beyond the part that called the servlet, or "" if * the whole URL has been used to identify the servlet.
* Detects include request URL if called within a RequestDispatcher include. *
* E.g.: servlet mapping = "/test/*"; request URI = "/test/a" -> "/a".
* E.g.: servlet mapping = "/test"; request URI = "/test" -> "".
* E.g.: servlet mapping = "/*.test"; request URI = "/a.test" -> "". * * @return the path within the servlet mapping, or "" */ @Nonnull default String getPathWithinServlet () { return RequestHelper.getPathWithinServlet (getRequest ()); } /** * @return The underlying HTTP servlet request object. Never null * . */ @Nonnull HttpServletRequest getRequest (); }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy