Skip to content

Instantly share code, notes, and snippets.

@jemshit
Last active July 29, 2021 11:28
Show Gist options
  • Save jemshit/82c0cd12e2381e10e7ee396fe64f7bb9 to your computer and use it in GitHub Desktop.
Save jemshit/82c0cd12e2381e10e7ee396fe64f7bb9 to your computer and use it in GitHub Desktop.
HTTP Status Codes, Helper Class in JAVA
public class HttpStatusCodeHelper {
/**
* Gets Http Status Code Information for given Code. No Reflection, No Enum
* @param code
* @return type + text
*/
public static String getHttpTypeAndTextByCode(final int code) {
//region 1xx
if (code==HttpStatusCodes.Continue.getCode()) {
return HttpStatusCodes.Continue.toString();
}else if (code==HttpStatusCodes.SwitchingProtocols.getCode()) {
return HttpStatusCodes.SwitchingProtocols.toString();
}else if (code==HttpStatusCodes.Processing.getCode()) {
return HttpStatusCodes.Processing.toString();
}
//endregion
//region 2xx
else if (code==HttpStatusCodes.Ok.getCode()) {
return HttpStatusCodes.Ok.toString();
}else if (code==HttpStatusCodes.Created.getCode()) {
return HttpStatusCodes.Created.toString();
}else if (code==HttpStatusCodes.Accepted.getCode()) {
return HttpStatusCodes.Accepted.toString();
}else if (code==HttpStatusCodes.NonAuthoritativeInformation.getCode()) {
return HttpStatusCodes.NonAuthoritativeInformation.toString();
}else if (code==HttpStatusCodes.NoContent.getCode()) {
return HttpStatusCodes.NoContent.toString();
}else if (code==HttpStatusCodes.ResetContent.getCode()) {
return HttpStatusCodes.ResetContent.toString();
}else if (code==HttpStatusCodes.PartialContent.getCode()) {
return HttpStatusCodes.PartialContent.toString();
}else if (code==HttpStatusCodes.MultiStatus.getCode()) {
return HttpStatusCodes.MultiStatus.toString();
}else if (code==HttpStatusCodes.AlreadyReported.getCode()) {
return HttpStatusCodes.AlreadyReported.toString();
}else if (code==HttpStatusCodes.ImUsed.getCode()) {
return HttpStatusCodes.ImUsed.toString();
}
//endregion
//region 3xx
else if (code==HttpStatusCodes.MultipleChoices.getCode()) {
return HttpStatusCodes.MultipleChoices.toString();
}else if (code==HttpStatusCodes.MovedPermanently.getCode()) {
return HttpStatusCodes.MovedPermanently.toString();
}else if (code==HttpStatusCodes.Found.getCode()) {
return HttpStatusCodes.Found.toString();
}else if (code==HttpStatusCodes.SeeOther.getCode()) {
return HttpStatusCodes.SeeOther.toString();
}else if (code==HttpStatusCodes.NotModified.getCode()) {
return HttpStatusCodes.NotModified.toString();
}else if (code==HttpStatusCodes.UseProxy.getCode()) {
return HttpStatusCodes.UseProxy.toString();
}else if (code==HttpStatusCodes.Reserved.getCode()) {
return HttpStatusCodes.Reserved.toString();
}else if (code==HttpStatusCodes.TemporaryRedirect.getCode()) {
return HttpStatusCodes.TemporaryRedirect.toString();
}else if (code==HttpStatusCodes.PermanentRedirect.getCode()) {
return HttpStatusCodes.PermanentRedirect.toString();
}
//endregion
//region 4xx
else if (code==HttpStatusCodes.BadRequest.getCode()) {
return HttpStatusCodes.BadRequest.toString();
}else if (code==HttpStatusCodes.Unauthorized.getCode()) {
return HttpStatusCodes.Unauthorized.toString();
}else if (code==HttpStatusCodes.PaymentRequired.getCode()) {
return HttpStatusCodes.PaymentRequired.toString();
}else if (code==HttpStatusCodes.Forbidden.getCode()) {
return HttpStatusCodes.Forbidden.toString();
}else if (code==HttpStatusCodes.NotFound.getCode()) {
return HttpStatusCodes.NotFound.toString();
}else if (code==HttpStatusCodes.MethodNotAllowed.getCode()) {
return HttpStatusCodes.MethodNotAllowed.toString();
}else if (code==HttpStatusCodes.NotAcceptable.getCode()) {
return HttpStatusCodes.NotAcceptable.toString();
}else if (code==HttpStatusCodes.ProxyAuthenticationRequired.getCode()) {
return HttpStatusCodes.ProxyAuthenticationRequired.toString();
}else if (code==HttpStatusCodes.RequestTimeout.getCode()) {
return HttpStatusCodes.RequestTimeout.toString();
}else if (code==HttpStatusCodes.Conflict.getCode()) {
return HttpStatusCodes.Conflict.toString();
}else if (code==HttpStatusCodes.Gone.getCode()) {
return HttpStatusCodes.Gone.toString();
}else if (code==HttpStatusCodes.LengthRequired.getCode()) {
return HttpStatusCodes.LengthRequired.toString();
}else if (code==HttpStatusCodes.PreconditionFailed.getCode()) {
return HttpStatusCodes.PreconditionFailed.toString();
}else if (code==HttpStatusCodes.RequestEntityTooLarge.getCode()) {
return HttpStatusCodes.RequestEntityTooLarge.toString();
}else if (code==HttpStatusCodes.RequestUriTooLong.getCode()) {
return HttpStatusCodes.RequestUriTooLong.toString();
}else if (code==HttpStatusCodes.UnsupportedMediaType.getCode()) {
return HttpStatusCodes.UnsupportedMediaType.toString();
}else if (code==HttpStatusCodes.RequestedRangeNotSatisfiable.getCode()) {
return HttpStatusCodes.RequestedRangeNotSatisfiable.toString();
}else if (code==HttpStatusCodes.ExpectationFailed.getCode()) {
return HttpStatusCodes.ExpectationFailed.toString();
}else if (code==HttpStatusCodes.ImATeapot.getCode()) {
return HttpStatusCodes.ImATeapot.toString();
}else if (code==HttpStatusCodes.EnhanceYourCalm.getCode()) {
return HttpStatusCodes.EnhanceYourCalm.toString();
}else if (code==HttpStatusCodes.UnprocessableEntity.getCode()) {
return HttpStatusCodes.UnprocessableEntity.toString();
}else if (code==HttpStatusCodes.Locked.getCode()) {
return HttpStatusCodes.Locked.toString();
}else if (code==HttpStatusCodes.FailedDependency.getCode()) {
return HttpStatusCodes.FailedDependency.toString();
}else if (code==HttpStatusCodes.UnorderedCollection.getCode()) {
return HttpStatusCodes.UnorderedCollection.toString();
}else if (code==HttpStatusCodes.UpgradeRequired.getCode()) {
return HttpStatusCodes.UpgradeRequired.toString();
}else if (code==HttpStatusCodes.PreconditionRequired.getCode()) {
return HttpStatusCodes.PreconditionRequired.toString();
}else if (code==HttpStatusCodes.TooManyRequests.getCode()) {
return HttpStatusCodes.TooManyRequests.toString();
}else if (code==HttpStatusCodes.RequestHeaderFieldsTooLarge.getCode()) {
return HttpStatusCodes.RequestHeaderFieldsTooLarge.toString();
}else if (code==HttpStatusCodes.NoResponse.getCode()) {
return HttpStatusCodes.NoResponse.toString();
}else if (code==HttpStatusCodes.RetryWith.getCode()) {
return HttpStatusCodes.RetryWith.toString();
}else if (code==HttpStatusCodes.BlockedByWindowsParentalControls.getCode()) {
return HttpStatusCodes.BlockedByWindowsParentalControls.toString();
}else if (code==HttpStatusCodes.UnavailableForLegalReasons.getCode()) {
return HttpStatusCodes.UnavailableForLegalReasons.toString();
}else if (code==HttpStatusCodes.ClientClosedRequest.getCode()) {
return HttpStatusCodes.ClientClosedRequest.toString();
}
//endregion
//region 5xx
else if (code==HttpStatusCodes.InternalServerError.getCode()) {
return HttpStatusCodes.InternalServerError.toString();
}else if (code==HttpStatusCodes.NotImplemented.getCode()) {
return HttpStatusCodes.NotImplemented.toString();
}else if (code==HttpStatusCodes.BadGateway.getCode()) {
return HttpStatusCodes.BadGateway.toString();
}else if (code==HttpStatusCodes.ServiceUnavailable.getCode()) {
return HttpStatusCodes.ServiceUnavailable.toString();
}else if (code==HttpStatusCodes.GatewayTimeout.getCode()) {
return HttpStatusCodes.GatewayTimeout.toString();
}else if (code==HttpStatusCodes.HttpVersionNotSupported.getCode()) {
return HttpStatusCodes.HttpVersionNotSupported.toString();
}else if (code==HttpStatusCodes.VariantAlsoNegotiates.getCode()) {
return HttpStatusCodes.VariantAlsoNegotiates.toString();
}else if (code==HttpStatusCodes.InsufficientStorage.getCode()) {
return HttpStatusCodes.InsufficientStorage.toString();
}else if (code==HttpStatusCodes.LoopDetected.getCode()) {
return HttpStatusCodes.LoopDetected.toString();
}else if (code==HttpStatusCodes.BandwidthLimitExceeded.getCode()) {
return HttpStatusCodes.BandwidthLimitExceeded.toString();
}else if (code==HttpStatusCodes.NotExtended.getCode()) {
return HttpStatusCodes.NotExtended.toString();
}else if (code==HttpStatusCodes.NetworkAuthenticationRequired.getCode()) {
return HttpStatusCodes.NetworkAuthenticationRequired.toString();
}else if (code==HttpStatusCodes.NetworkReadTimeoutError.getCode()) {
return HttpStatusCodes.NetworkReadTimeoutError.toString();
}else if (code==HttpStatusCodes.NetworkConnectTimeoutError.getCode()) {
return HttpStatusCodes.NetworkConnectTimeoutError.toString();
}
//endregion
else{
return "WTF Error: Given Http Code Does Not Have a Match";
}
}
}
public class HttpStatusCodes {
//region 1xx
public static final StatusCode Continue=new StatusCode(100,"Continue","Informational","");
public static final StatusCode SwitchingProtocols=new StatusCode(101,"Switching Protocols","Informational","");
public static final StatusCode Processing=new StatusCode(102,"Processing","Informational","");
//endregion
//region 2xx
public static final StatusCode Ok=new StatusCode(200,"OK","Successful","");
public static final StatusCode Created=new StatusCode(201,"Created","Successful","");
public static final StatusCode Accepted=new StatusCode(202,"Accepted","Successful","");
public static final StatusCode NonAuthoritativeInformation=new StatusCode(203,"Non-Authoritative Information","Successful","");
public static final StatusCode NoContent=new StatusCode(204,"No Content","Successful","");
public static final StatusCode ResetContent=new StatusCode(205,"Reset Content","Successful","");
public static final StatusCode PartialContent=new StatusCode(206,"Partial Content","Successful","");
public static final StatusCode MultiStatus=new StatusCode(207,"Multi-Status","Successful","");
public static final StatusCode AlreadyReported=new StatusCode(208,"Already Reported","Successful","");
public static final StatusCode ImUsed=new StatusCode(226,"IM Used","Successful","");
//endregion
//region 3xx
public static final StatusCode MultipleChoices=new StatusCode(300,"Multiple Choices","Redirection","");
public static final StatusCode MovedPermanently=new StatusCode(301,"Moved Permanently","Redirection","");
public static final StatusCode Found=new StatusCode(302,"Found","Redirection","");
public static final StatusCode SeeOther=new StatusCode(303,"See Other","Redirection","");
public static final StatusCode NotModified=new StatusCode(304,"Not Modified","Redirection","");
public static final StatusCode UseProxy=new StatusCode(305,"Use Proxy","Redirection","");
public static final StatusCode Reserved=new StatusCode(306,"Reserved","Redirection","");
public static final StatusCode TemporaryRedirect=new StatusCode(307,"Temporary Redirect","Redirection","");
public static final StatusCode PermanentRedirect=new StatusCode(308,"Permanent Redirect","Redirection","");
//endregion
//region 4xx
public static final StatusCode BadRequest=new StatusCode(400,"Bad Request","Client Error","");
public static final StatusCode Unauthorized=new StatusCode(401,"Unauthorized","Client Error","");
public static final StatusCode PaymentRequired=new StatusCode(402,"Payment Required","Client Error","");
public static final StatusCode Forbidden=new StatusCode(403,"Forbidden","Client Error","");
public static final StatusCode NotFound=new StatusCode(404,"Not Found","Client Error","");
public static final StatusCode MethodNotAllowed=new StatusCode(405,"Method Not Allowed","Client Error","");
public static final StatusCode NotAcceptable=new StatusCode(406,"Not Acceptable","Client Error","");
public static final StatusCode ProxyAuthenticationRequired=new StatusCode(407,"Proxy Authentication Required","Client Error","");
public static final StatusCode RequestTimeout=new StatusCode(408,"Request Timeout","Client Error","");
public static final StatusCode Conflict=new StatusCode(409,"Conflict","Client Error","");
public static final StatusCode Gone=new StatusCode(410,"Gone","Client Error","");
public static final StatusCode LengthRequired=new StatusCode(411,"Length Required","Client Error","");
public static final StatusCode PreconditionFailed=new StatusCode(412,"Precondition Failed","Client Error","");
public static final StatusCode RequestEntityTooLarge=new StatusCode(413,"Request Entity Too Large","Client Error","");
public static final StatusCode RequestUriTooLong=new StatusCode(414,"Request-URI Too Long","Client Error","");
public static final StatusCode UnsupportedMediaType=new StatusCode(415,"Unsupported Media Type","Client Error","");
public static final StatusCode RequestedRangeNotSatisfiable=new StatusCode(416,"Requested Range Not Satisfiable","Client Error","");
public static final StatusCode ExpectationFailed=new StatusCode(417,"Expectation Failed","Client Error","");
public static final StatusCode ImATeapot=new StatusCode(418,"I’m a teapot","Client Error","");
public static final StatusCode EnhanceYourCalm=new StatusCode(420,"Enhance Your Calm","Client Error","");
public static final StatusCode UnprocessableEntity=new StatusCode(422,"Unprocessable Entity","Client Error","");
public static final StatusCode Locked=new StatusCode(423,"Locked","Client Error","");
public static final StatusCode FailedDependency=new StatusCode(424,"Failed Dependency","Client Error","");
public static final StatusCode UnorderedCollection=new StatusCode(425,"Unordered Collection","Client Error","");
public static final StatusCode UpgradeRequired=new StatusCode(426,"Upgrade Required","Client Error","");
public static final StatusCode PreconditionRequired=new StatusCode(428,"Precondition Required","Client Error","");
public static final StatusCode TooManyRequests=new StatusCode(429,"Too Many Requests","Client Error","");
public static final StatusCode RequestHeaderFieldsTooLarge=new StatusCode(431,"Request Header Fields Too Large","Client Error","");
public static final StatusCode NoResponse=new StatusCode(444,"No Response","Client Error","");
public static final StatusCode RetryWith=new StatusCode(449,"Retry With","Client Error","");
public static final StatusCode BlockedByWindowsParentalControls=new StatusCode(450,"Blocked by Windows Parental Controls","Client Error","");
public static final StatusCode UnavailableForLegalReasons=new StatusCode(451,"Unavailable For Legal Reasons","Client Error","");
public static final StatusCode ClientClosedRequest=new StatusCode(499,"Client Closed Request","Client Error","");
//endregion
//region 5xx
public static final StatusCode InternalServerError=new StatusCode(500,"Internal Server Error","Server Error","");
public static final StatusCode NotImplemented=new StatusCode(501,"Not Implemented","Server Error","");
public static final StatusCode BadGateway=new StatusCode(502,"Bad Gateway","Server Error","");
public static final StatusCode ServiceUnavailable=new StatusCode(503,"Service Unavailable","Server Error","");
public static final StatusCode GatewayTimeout=new StatusCode(504,"Gateway Timeout","Server Error","");
public static final StatusCode HttpVersionNotSupported=new StatusCode(505,"HTTP Version Not Supported","Server Error","");
public static final StatusCode VariantAlsoNegotiates=new StatusCode(506,"Variant Also Negotiates","Server Error","");
public static final StatusCode InsufficientStorage=new StatusCode(507,"Insufficient Storage","Server Error","");
public static final StatusCode LoopDetected=new StatusCode(508,"Loop Detected","Server Error","");
public static final StatusCode BandwidthLimitExceeded=new StatusCode(509,"Bandwidth Limit Exceeded","Server Error","");
public static final StatusCode NotExtended=new StatusCode(510,"Not Extended","Server Error","");
public static final StatusCode NetworkAuthenticationRequired=new StatusCode(511,"Network Authentication Required","Server Error","");
public static final StatusCode NetworkReadTimeoutError=new StatusCode(598,"Network Read Timeout Error","Server Error","");
public static final StatusCode NetworkConnectTimeoutError=new StatusCode(599,"Network Connect Timeout Error","Server Error","");
//endregion
}
public class StatusCode {
private int code;
private String text;
private String type;
private String description;
public StatusCode(int code, String text, String type, String description) {
this.code = code;
this.text = text;
this.type = type;
this.description = description;
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String toTypeAndText(){
return type+": "+text;
}
@Override
public String toString() {
return "StatusCode{" +
"Code=" + code +
", Type='" + type + '\'' +
", Text='" + text + '\'' +
", Description='" + description + '\'' +
'}';
}
}
@A248
Copy link

A248 commented Nov 23, 2019

Use a Map<Integer, StatusCode> inside getHttpTypeAndTextByCode#

@jemshit
Copy link
Author

jemshit commented Nov 25, 2019

Use a Map<Integer, StatusCode> inside getHttpTypeAndTextByCode#

Yep, that would speed up things. I have never used this class anyway :D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment