public abstract class

RestUtils

extends Object
java.lang.Object
   ↳ com.atlassian.bitbucket.rest.util.RestUtils

Class Overview

Helper functions and constants for REST.

Summary

Constants
String APPLICATION_JSON_UTF8
String CHARSET_PREFIX
String CHARSET_UTF8
String CONTENT_TYPE
String DEFAULT_ENTITY_VERSION
String DEFAULT_LIMIT
int DEFAULT_RECENT_REPOS_LIMIT
String DEFAULT_START
String TEXT_HTML_UTF8
Public Constructors
RestUtils()
Public Methods
@Nullable static <F, T> T applyOrNull(F from, Function<F, T> function)
If from is non-null, return the result of apply(T).
@Nonnull static Response doConditionalGet(Request request, Date lastUpdate, Function<Response.ResponseBuilder, Response.ResponseBuilder> okFunction)
Conditionally handle gets where the request may include If-Last-Modified headers.
static Charset getCharset(ContainerRequest request, Charset defaultCharset)
Gets the charset of a request.
static boolean isImmutableBetween(String untilId, String sinceId)
static boolean isImmutableObjectId(String objectId)
@Nonnull static AvatarRequest makeAvatarRequest(RequestManager requestManager, Map<String, Object> options)
Extracts avatar-related query parameters from the provided options map (usually passed in to the to_json Soy call) describing them.
@Nullable static AvatarRequest makeAvatarRequest(HttpRequestContext request)
Extracts avatar-related query parameters from the provided HttpRequestContext, if present, and returns an AvatarRequest describing them.
@Nullable static AvatarRequest makeAvatarRequest(ContainerRequest request)
Extracts avatar-related query parameters from the provided ContainerRequest, if present, and returns an AvatarRequest describing them.
@Nonnull static PageRequest makePageRequest(HttpRequestContext request, int defaultLimit)
Extracts paging-related query parameters from the provided UriInfo and returns a PageRequest describing them.
@Nonnull static PageRequest makePageRequest(HttpRequestContext request)
Extracts paging-related query parameters from the provided UriInfo and returns a PageRequest describing them.
@Nonnull static RenderContext makeRenderContext(HttpRequestContext request)
Extracts markup rendering query parameters from the provided HttpRequestContext and returns a RenderContext describing them.
@Nonnull static <T> T notFoundIfNull(T t)
Throws a NotFoundException if the value is null, otherwise return the value.
@Nonnull static Response processAvatarResponse(ContainerRequest request, CacheControl cacheControl, CacheableAvatarSupplier supplier)
Correctly handles setting the content and response headers for an CacheableAvatarSupplier.
@Nullable static Object processEntities(Object entity, Function<Object, Object> processCallback)
Processes each entity, walking through common Iterable containers and invoking processCallback on the children.
[Expand]
Inherited Methods
From class java.lang.Object

Constants

public static final String APPLICATION_JSON_UTF8

Constant Value: "application/json;charset=UTF-8"

public static final String CHARSET_PREFIX

Constant Value: "charset="

public static final String CHARSET_UTF8

Constant Value: ";charset=UTF-8"

public static final String CONTENT_TYPE

Constant Value: "content-type"

public static final String DEFAULT_ENTITY_VERSION

Constant Value: "-1"

public static final String DEFAULT_LIMIT

Constant Value: "25"

public static final int DEFAULT_RECENT_REPOS_LIMIT

Constant Value: 15 (0x0000000f)

public static final String DEFAULT_START

Constant Value: "0"

public static final String TEXT_HTML_UTF8

Constant Value: "text/html;charset=UTF-8"

Public Constructors

public RestUtils ()

Public Methods

@Nullable public static T applyOrNull (F from, Function<F, T> function)

If from is non-null, return the result of apply(T). Otherwise return null.

@Nonnull public static Response doConditionalGet (Request request, Date lastUpdate, Function<Response.ResponseBuilder, Response.ResponseBuilder> okFunction)

Conditionally handle gets where the request may include If-Last-Modified headers.

Parameters
request current request
lastUpdate time when this entity was last updated
okFunction will be called with response builder if this is a fresh request; should most likely be calling entity(Object).
Returns
  • response with the appropriate Last-Modified header

public static Charset getCharset (ContainerRequest request, Charset defaultCharset)

Gets the charset of a request.

Note: Nullability for the return value is intentionally not documented one way or another. It is expected that the caller will know the nullability based on the default they are passing and code accordingly.

Parameters
request request to introspect
defaultCharset default charset to use if none is found or if the charset is not supported
Returns
  • the request's charset or defaultCharset if none is found or if the charset is not supported

public static boolean isImmutableBetween (String untilId, String sinceId)

public static boolean isImmutableObjectId (String objectId)

@Nonnull public static AvatarRequest makeAvatarRequest (RequestManager requestManager, Map<String, Object> options)

Extracts avatar-related query parameters from the provided options map (usually passed in to the to_json Soy call) describing them. If avatar parameters are not found a default request is returned.

Parameters
requestManager the manager of the current request
options the options supplied to the to_json call
Returns
  • a description of the avatars requested by the caller or a default request if none were provided

@Nullable public static AvatarRequest makeAvatarRequest (HttpRequestContext request)

Extracts avatar-related query parameters from the provided HttpRequestContext, if present, and returns an AvatarRequest describing them. If "avatarSize" was not specified, null is returned to indicate avatars were not requested.

Parameters
request the request to parse query parameters for
Returns
  • a description of the avatars requested by the caller, or null if avatars were not requested
Throws
BadRequestException if "avatarSize" was set but could not be parsed as an int, or if the specified size was less than or equal to 0

@Nullable public static AvatarRequest makeAvatarRequest (ContainerRequest request)

Extracts avatar-related query parameters from the provided ContainerRequest, if present, and returns an AvatarRequest describing them. If "avatarSize" was not specified, null is returned to indicate avatars were not requested.

Parameters
request the request to parse query parameters for
Returns
  • a description of the avatars requested by the caller, or null if avatars were not requested
Throws
BadRequestException if "avatarSize" was set but could not be parsed as an int, or if the specified size was less than or equal to 0

@Nonnull public static PageRequest makePageRequest (HttpRequestContext request, int defaultLimit)

Extracts paging-related query parameters from the provided UriInfo and returns a PageRequest describing them. If paging parameters are not found, default parameters are assumed. The provided defaultLimit will be used if no explicit limit is found as a query parameter.

This method will never return null.

Parameters
request the request to parse query parameters for
defaultLimit the limit to apply if no explicit limit has been specified
Returns
  • a description of the page being requested

@Nonnull public static PageRequest makePageRequest (HttpRequestContext request)

Extracts paging-related query parameters from the provided UriInfo and returns a PageRequest describing them. If paging parameters are not found, default parameters are assumed.

This method will never return null.

Parameters
request the request to parse query parameters for
Returns
  • a description of the page being requested

@Nonnull public static RenderContext makeRenderContext (HttpRequestContext request)

Extracts markup rendering query parameters from the provided HttpRequestContext and returns a RenderContext describing them. For any rendering parameters which are not explicitly configured, the returned context will not have a value set and system-level defaults will be used.

This method will never return null.

Parameters
request the request to parse query parameters for
Returns
  • a context specifying how the response should be rendered
Throws
BadRequestException if "urlMode" was set but could not be parsed as a UrlMode

@Nonnull public static T notFoundIfNull (T t)

Throws a NotFoundException if the value is null, otherwise return the value.

Throws
NotFoundException

@Nonnull public static Response processAvatarResponse (ContainerRequest request, CacheControl cacheControl, CacheableAvatarSupplier supplier)

Correctly handles setting the content and response headers for an CacheableAvatarSupplier.

Parameters
request current request
cacheControl which may be different depending on the URL
supplier the avatar to send to the client
Returns
  • response with the appropriate headers and streaming avatar content

@Nullable public static Object processEntities (Object entity, Function<Object, Object> processCallback)

Processes each entity, walking through common Iterable containers and invoking processCallback on the children.