org.hudsonci.rest.common
Class RestPreconditions

java.lang.Object
  extended by org.hudsonci.rest.common.RestPreconditions

public abstract class RestPreconditions
extends Object

Preconditions to check common arguments to REST calls

Since:
2.1.0
Author:
plynch

Field Summary
protected static org.slf4j.Logger log
           
 
Constructor Summary
protected RestPreconditions()
           
 
Method Summary
static int checkBuilderIndex(int index)
          Check a project builder index for shallow validity
static int checkBuildNumber(int buildNumber)
          Check a project buildNumber for being greater than zero.
static int checkBuildStateIndex(int index)
          Check a project build state index for shallow validity.
static String checkDocumentId(String id)
          Check a DocumentDTO ID for shallow validity
static
<T> T
checkNotNull(T reference, Class<T> clazz)
          IF the passed reference is null, throws a WebApplicationException with status 400 and generates a response body containing suitable client message.
static
<T> T
checkNotNull(T reference, String msgKey)
          Check an argument for null.
static String checkProjectName(String projectName)
          Check the projectName for shallow validity
protected static void checkUUID(String uuid)
          Check a uuid string for validity.
protected static boolean isNegative(int index)
          DSL style negative checking
static void throwBadRequest(String message)
          Throws a WebApplicationException with status 400 and generates a response body containing the specified message.
static void throwConflict(String message)
          Throws a WebApplicationException with status 409 and generates a response body containing the specified message.
static void throwWebApplicationException(javax.ws.rs.core.Response.Status status, String message)
          Throws a WebApplicationException with specified status and generates a response body containing the specified message.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

log

protected static final org.slf4j.Logger log
Constructor Detail

RestPreconditions

protected RestPreconditions()
Method Detail

checkBuildNumber

public static int checkBuildNumber(int buildNumber)
Check a project buildNumber for being greater than zero.

Parameters:
buildNumber - buildNumber to test for validity
Returns:
the passed in argument if valid
Throws:
javax.ws.rs.WebApplicationException - with Response.Status#BAD_REQUEST if buildNumber is less than one

checkBuildStateIndex

public static int checkBuildStateIndex(int index)
Check a project build state index for shallow validity.

Parameters:
index - a value that should be non-negative
Returns:
the passed in argument if valid
Throws:
javax.ws.rs.WebApplicationException - with Response.Status#BAD_REQUEST if index is negative

checkBuilderIndex

public static int checkBuilderIndex(int index)
Check a project builder index for shallow validity

Parameters:
index -
Returns:
the passed in argument if valid
Throws:
javax.ws.rs.WebApplicationException - with Response.Status#BAD_REQUEST if index is negative

checkProjectName

public static String checkProjectName(String projectName)
Check the projectName for shallow validity

Parameters:
projectName - the project name to check
Returns:
the unmodified projectName
Throws:
javax.ws.rs.WebApplicationException - status 400 if projectName is not a valid format or null

checkDocumentId

public static String checkDocumentId(String id)
Check a DocumentDTO ID for shallow validity

Parameters:
projectName - the project name to check
Returns:
the unmodified projectName
Throws:
javax.ws.rs.WebApplicationException - status 400 if document id is not a valid format or null

checkUUID

protected static void checkUUID(String uuid)
Check a uuid string for validity.

Parameters:
uuid -
Throws:
javax.ws.rs.WebApplicationException - status 400 if UUID cannot be parsed from uuid string

checkNotNull

public static <T> T checkNotNull(T reference,
                                 String msgKey)
Check an argument for null. If it is null, then calls throwBadRequest(String).

Parameters:
reference -
msgKey - the key to lookup the value name

checkNotNull

public static <T> T checkNotNull(T reference,
                                 Class<T> clazz)
IF the passed reference is null, throws a WebApplicationException with status 400 and generates a response body containing suitable client message. The message will include the name of the class if clazz is null.

While not itself a precondition, this is a common operation if a precondition fails and can be used when custom validation was performed for a rest argument.

Type Parameters:
T - reference to a value to check for null
Parameters:
reference - reference to a value to check for null
clazz - the type of reference, may be null
Returns:
reference unchanged

throwBadRequest

public static void throwBadRequest(String message)
Throws a WebApplicationException with status 400 and generates a response body containing the specified message.

While not itself a precondition, this is a common operation if a precondition fails and can be used when custom validation was performed for a rest argument.

Parameters:
message - the message to include in the response to the client
Throws:
javax.ws.rs.WebApplicationException - status 400 with message included as response entity

throwConflict

public static void throwConflict(String message)
Throws a WebApplicationException with status 409 and generates a response body containing the specified message.

While not itself a precondition, this is a common operation if a precondition fails and can be used when custom validation was performed for a rest argument.

Parameters:
message - the message to include in the response to the client
Throws:
javax.ws.rs.WebApplicationException - status 400 with message included as response entity

throwWebApplicationException

public static void throwWebApplicationException(javax.ws.rs.core.Response.Status status,
                                                String message)
Throws a WebApplicationException with specified status and generates a response body containing the specified message.

While not itself a precondition, this is a common operation if a precondition fails and can be used when custom validation was performed for a rest argument.

Parameters:
message - the message to include in the response to the client
status - the status code of the response in the exception
Throws:
javax.ws.rs.WebApplicationException - status 400 with message included as response entity

isNegative

protected static boolean isNegative(int index)
DSL style negative checking

Parameters:
index -
Returns:
true if index is negative


Copyright © 2004-2011 Hudson. All Rights Reserved.