Package org.apache.wiki.util

# Class HttpUtil

java.lang.Object
org.apache.wiki.util.HttpUtil

public final class HttpUtil extends Object
Contains useful utilities for some common HTTP tasks.

Since:
  2.1.61.

## Method Summary

Modifier and Type
Method
Description
`static void`
`addHeader(jakarta.servlet.ServletResponse response, String headerName, String headerValue)`
Add's a header to the response
`static boolean`
`checkFor304(jakarta.servlet.http.HttpServletRequest req, String pageName, Date lastModified)`
If returns true, then should return a 304 (HTTP_NOT_MODIFIED)
`static void`
`clearCookie(jakarta.servlet.http.HttpServletResponse response, String cookieName)`
`static String`
`createETag(String pageName, Date lastModified)`
Creates an ETag based on page information.
`static String`
`getAbsoluteUrl(jakarta.servlet.http.HttpServletRequest request)`
Generate an absolute URL based solely on the given HttpServletRequest.
`static String`
`getAbsoluteUrl(jakarta.servlet.http.HttpServletRequest request, String relativeUrl)`
Generates an absolute URL based on the given HttpServletRequest and a relative URL.
`static String`
`getRemoteAddress(jakarta.servlet.http.HttpServletRequest req)`
returns the remote address by looking into `x-forwarded-for` header or, if unavailable, into `ServletRequest.getRemoteAddr()`.
`static String`
`guessValidURI(String uri)`
Attempts to form a valid URI based on the string given.
`static boolean`
`ipIsInRange(jakarta.servlet.http.HttpServletRequest req, String ipOrRange)`
Returns whether or not the IP address of the request equals a given IP, or is in a given IP range
`static boolean`
`isIPV4Address(String name)`
Verifies whether a String represents an IPv4 address.
`static String`
`retrieveCookieValue(jakarta.servlet.http.HttpServletRequest request, String cookieName)`
Attempts to retrieve the given cookie value from the request.
`static String`
`safeGetQueryString(jakarta.servlet.http.HttpServletRequest request, Charset contentEncoding)`
Returns the query string (the portion after the question mark).

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

## Method Details

### getRemoteAddress

public static String getRemoteAddress(jakarta.servlet.http.HttpServletRequest req)
returns the remote address by looking into `x-forwarded-for` header or, if unavailable, into `ServletRequest.getRemoteAddr()`.

Parameters:
  `req` - http request
Returns:
  remote address associated to the request.

### ipIsInRange

public static boolean ipIsInRange(jakarta.servlet.http.HttpServletRequest req, String ipOrRange)
Returns whether or not the IP address of the request equals a given IP, or is in a given IP range

Parameters:
  `req` - http request
  `ipOrRange` - IP address or IP range to test against
Returns:
Since:
  3.0.0

### retrieveCookieValue

public static String retrieveCookieValue(jakarta.servlet.http.HttpServletRequest request, String cookieName)
Attempts to retrieve the given cookie value from the request. Returns the string value (which may or may not be decoded correctly, depending on browser!), or null if the cookie is not found. The algorithm will automatically trim leading and trailing double quotes, if found.

Parameters:
  `request` - The current request
  `cookieName` - The name of the cookie to fetch.
Returns:
  Value of the cookie, or null, if there is no such cookie.

### createETag

public static String createETag(String pageName, Date lastModified)
Creates an ETag based on page information. An ETag is unique to each page and version, so it can be used to check if the page has changed. Do not assume that the ETag is in any particular format.

Parameters:
  `pageName` - The page name for which the ETag should be created.
  `lastModified` - The page last modified date for which the ETag should be created.
Returns:
  A String depiction of an ETag.

### checkFor304

public static boolean checkFor304(jakarta.servlet.http.HttpServletRequest req, String pageName, Date lastModified)
If returns true, then should return a 304 (HTTP_NOT_MODIFIED)

Parameters:
  `req` - the HTTP request
  `pageName` - the wiki page name to check for
  `lastModified` - the last modified date of the wiki page to check for
Returns:
  the result of the check

### guessValidURI

public static String guessValidURI(String uri)
Attempts to form a valid URI based on the string given. Currently it can guess email addresses (mailto:). If nothing else is given, it assumes it to be an http:// url.

Parameters:
  `uri` - URI to take a poke at
Returns:
  Possibly a valid URI
Since:
  2.2.8

### safeGetQueryString

public static String safeGetQueryString(jakarta.servlet.http.HttpServletRequest request, Charset contentEncoding)
Returns the query string (the portion after the question mark).

Parameters:
  `request` - The HTTP request to parse.
Returns:
  The query string. If the query string is null, returns an empty string.
Since:
  2.1.3 (method moved from WikiEngine on 2.11.0.M6)

### isIPV4Address

public static boolean isIPV4Address(String name)
Verifies whether a String represents an IPv4 address. The algorithm is extremely efficient and does not allocate any objects.

Parameters:
  `name` - the address to test
Returns:
  the result

### clearCookie

public static void clearCookie(jakarta.servlet.http.HttpServletResponse response, String cookieName)

### getAbsoluteUrl

public static String getAbsoluteUrl(jakarta.servlet.http.HttpServletRequest request, String relativeUrl)
Generates an absolute URL based on the given HttpServletRequest and a relative URL. This method takes into account various headers like X-Forwarded-Host, X-Forwarded-Proto, and X-Forwarded-Server to construct the absolute URL.

Parameters:
  `request` - The HttpServletRequest object, used to obtain scheme, server name, and port.
  `relativeUrl` - The relative URL to be appended to the base URL. Can be null.
Returns:
  The absolute URL as a String.
Since:
  2.12.2

### getAbsoluteUrl

public static String getAbsoluteUrl(jakarta.servlet.http.HttpServletRequest request)
Generate an absolute URL based solely on the given HttpServletRequest. This is a convenience method that calls `getAbsoluteUrl(HttpServletRequest, String)` with a null relative URL.

Parameters:
  `request` - The HttpServletRequest object, used to obtain scheme, server name, and port.
Returns:
  The absolute URL as a String.
Since:
  2.12.2
See Also:
  - `getAbsoluteUrl(HttpServletRequest, String)`

### addHeader

public static void addHeader(jakarta.servlet.ServletResponse response, String headerName, String headerValue)
Add's a header to the response

Parameters:
  `response` - servlet response in which the header is added
  `headerName` - header's name
  `headerValue` - header's value