Skip to content

Instantly share code, notes, and snippets.

@avendasora
Last active November 5, 2015 16:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save avendasora/a5ed9acf6b1ee709a14a to your computer and use it in GitHub Desktop.
Save avendasora/a5ed9acf6b1ee709a14a to your computer and use it in GitHub Desktop.
Basic implementation of javax.ws.rs.core.Response.StatusType with additional HTTP Status codes. You should be able to use any of the constants defined in this enum just like you would use the constants (OK, CREATED, DELETED, etc.) defined in javax.ws.rs.core.Response.Status
package avendasora.http;
import javax.ws.rs.core.Response.StatusType;
import javax.ws.rs.core.Response.Status.Family;
public enum Status implements StatusType {
/**
* 100 Continue, see
* {@link <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.1.1">HTTP/1.1 documentation</a>}
* .
*/
CONTINUE(100, "Continue"),
/**
* 101 Switching Protocols, see
* {@link <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.1.2">HTTP/1.1 documentation</a>}
* .
*/
SWITCHING_PROTOCOLS(101, "Switching Protocols"), ;
private final int _code;
private final String _reason;
private final Family _family;
Status(final int statusCode,
final String reasonPhrase) {
_code = statusCode;
_reason = reasonPhrase;
_family = Family.familyOf(statusCode);
}
@Override
public Family getFamily() {
return _family;
}
@Override
public int getStatusCode() {
return _code;
}
@Override
public String getReasonPhrase() {
return toString();
}
@Override
public String toString() {
return _reason;
}
/**
* Converts a numerical status code into the corresponding Status. If
* this returns <code>null</code>, ask
* {@link javax.ws.rs.core.Response.Status#fromStatusCode(int) javax's
* implementation}, maybe it will know what Status the
* <code>statusCode</code> is for.
*
* @param statusCode
* the numerical status code.
* @return the matching Status or <code>null</code> is no matching
* Status is defined.
*/
public static Status fromStatusCode(final int statusCode) {
for (Status status : Status.values()) {
if (status.getStatusCode() == statusCode) {
return status;
}
}
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment