diff --git a/hippo4j-common/src/main/java/cn/hippo4j/common/constant/HttpResponseCode.java b/hippo4j-common/src/main/java/cn/hippo4j/common/constant/HttpResponseCode.java index e063e6a8..72e9df94 100644 --- a/hippo4j-common/src/main/java/cn/hippo4j/common/constant/HttpResponseCode.java +++ b/hippo4j-common/src/main/java/cn/hippo4j/common/constant/HttpResponseCode.java @@ -22,87 +22,202 @@ package cn.hippo4j.common.constant; * * @author Rongzhen Yan */ -public interface HttpResponseCode { - - int SC_CONTINUE = 100; - - int SC_SWITCHING_PROTOCOLS = 101; - - int SC_OK = 200; - - int SC_CREATED = 201; - - int SC_ACCEPTED = 202; - - int SC_NON_AUTHORITATIVE_INFORMATION = 203; - - int SC_NO_CONTENT = 204; - - int SC_RESET_CONTENT = 205; - - int SC_PARTIAL_CONTENT = 206; - - int SC_MULTIPLE_CHOICES = 300; - - int SC_MOVED_PERMANENTLY = 301; - - int SC_MOVED_TEMPORARILY = 302; - - int SC_FOUND = 302; - - int SC_SEE_OTHER = 303; - - int SC_NOT_MODIFIED = 304; - - int SC_USE_PROXY = 305; - - int SC_TEMPORARY_REDIRECT = 307; - - int SC_BAD_REQUEST = 400; - - int SC_UNAUTHORIZED = 401; - - int SC_PAYMENT_REQUIRED = 402; - - int SC_FORBIDDEN = 403; - - int SC_NOT_FOUND = 404; - - int SC_METHOD_NOT_ALLOWED = 405; - - int SC_NOT_ACCEPTABLE = 406; - - int SC_PROXY_AUTHENTICATION_REQUIRED = 407; - - int SC_REQUEST_TIMEOUT = 408; - - int SC_CONFLICT = 409; - - int SC_GONE = 410; - - int SC_LENGTH_REQUIRED = 411; - - int SC_PRECONDITION_FAILED = 412; - - int SC_REQUEST_ENTITY_TOO_LARGE = 413; - - int SC_REQUEST_URI_TOO_LONG = 414; - - int SC_UNSUPPORTED_MEDIA_TYPE = 415; - - int SC_REQUESTED_RANGE_NOT_SATISFIABLE = 416; - - int SC_EXPECTATION_FAILED = 417; - - int SC_INTERNAL_SERVER_ERROR = 500; - - int SC_NOT_IMPLEMENTED = 501; - - int SC_BAD_GATEWAY = 502; - - int SC_SERVICE_UNAVAILABLE = 503; - - int SC_GATEWAY_TIMEOUT = 504; - - int SC_HTTP_VERSION_NOT_SUPPORTED = 505; +public class HttpResponseCode { + + /* 2XX: generally "OK" */ + + /** + * HTTP Status-Code 200: OK. + */ + public static final int SC_OK = 200; + + /** + * HTTP Status-Code 201: Created. + */ + public static final int SC_HTTP_CREATED = 201; + + /** + * HTTP Status-Code 202: Accepted. + */ + public static final int SC_HTTP_ACCEPTED = 202; + + /** + * HTTP Status-Code 203: Non-Authoritative Information. + */ + public static final int SC_HTTP_NOT_AUTHORITATIVE = 203; + + /** + * HTTP Status-Code 204: No Content. + */ + public static final int SC_HTTP_NO_CONTENT = 204; + + /** + * HTTP Status-Code 205: Reset Content. + */ + public static final int SC_HTTP_RESET = 205; + + /** + * HTTP Status-Code 206: Partial Content. + */ + public static final int SC_HTTP_PARTIAL = 206; + + /* 3XX: relocation/redirect */ + + /** + * HTTP Status-Code 300: Multiple Choices. + */ + public static final int SC_HTTP_MULT_CHOICE = 300; + + /** + * HTTP Status-Code 301: Moved Permanently. + */ + public static final int SC_HTTP_MOVED_PERM = 301; + + /** + * HTTP Status-Code 302: Temporary Redirect. + */ + public static final int SC_HTTP_MOVED_TEMP = 302; + + /** + * HTTP Status-Code 303: See Other. + */ + public static final int SC_HTTP_SEE_OTHER = 303; + + /** + * HTTP Status-Code 304: Not Modified. + */ + public static final int SC_HTTP_NOT_MODIFIED = 304; + + /** + * HTTP Status-Code 305: Use Proxy. + */ + public static final int SC_HTTP_USE_PROXY = 305; + + /** + * HTTP 1.1 Status-Code 307: Temporary Redirect. + */ + public static final int SC_HTTP_TEMP_REDIRECT = 307; + + /** + * HTTP 1.1 Status-Code 308: Permanent Redirect + */ + public static final int SC_HTTP_PERMANENT_REDIRECT = 308; + + /* 4XX: client error */ + + /** + * HTTP Status-Code 400: Bad Request. + */ + public static final int SC_HTTP_BAD_REQUEST = 400; + + /** + * HTTP Status-Code 401: Unauthorized. + */ + public static final int SC_HTTP_UNAUTHORIZED = 401; + + /** + * HTTP Status-Code 402: Payment Required. + */ + public static final int SC_HTTP_PAYMENT_REQUIRED = 402; + + /** + * HTTP Status-Code 403: Forbidden. + */ + public static final int SC_HTTP_FORBIDDEN = 403; + + /** + * HTTP Status-Code 404: Not Found. + */ + public static final int SC_HTTP_NOT_FOUND = 404; + + /** + * HTTP Status-Code 405: Method Not Allowed. + */ + public static final int SC_HTTP_BAD_METHOD = 405; + + /** + * HTTP Status-Code 406: Not Acceptable. + */ + public static final int SC_HTTP_NOT_ACCEPTABLE = 406; + + /** + * HTTP Status-Code 407: Proxy Authentication Required. + */ + public static final int SC_HTTP_PROXY_AUTH = 407; + + /** + * HTTP Status-Code 408: Request Time-Out. + */ + public static final int SC_HTTP_CLIENT_TIMEOUT = 408; + + /** + * HTTP Status-Code 409: Conflict. + */ + public static final int SC_HTTP_CONFLICT = 409; + + /** + * HTTP Status-Code 410: Gone. + */ + public static final int SC_HTTP_GONE = 410; + + /** + * HTTP Status-Code 411: Length Required. + */ + public static final int SC_HTTP_LENGTH_REQUIRED = 411; + + /** + * HTTP Status-Code 412: Precondition Failed. + */ + public static final int SC_HTTP_PRECON_FAILED = 412; + + /** + * HTTP Status-Code 413: Request Entity Too Large. + */ + public static final int SC_HTTP_ENTITY_TOO_LARGE = 413; + + /** + * HTTP Status-Code 414: Request-URI Too Large. + */ + public static final int SC_HTTP_REQ_TOO_LONG = 414; + + /** + * HTTP Status-Code 415: Unsupported Media Type. + */ + public static final int SC_HTTP_UNSUPPORTED_TYPE = 415; + + /* 5XX: server error */ + + /** + * HTTP Status-Code 500: Internal Server Error. + */ + public static final int SC_HTTP_INTERNAL_ERROR = 500; + + /** + * HTTP Status-Code 501: Not Implemented. + */ + public static final int SC_HTTP_NOT_IMPLEMENTED = 501; + + /** + * HTTP Status-Code 502: Bad Gateway. + */ + public static final int SC_HTTP_BAD_GATEWAY = 502; + + /** + * HTTP Status-Code 503: Service Unavailable. + */ + public static final int SC_HTTP_UNAVAILABLE = 503; + + /** + * HTTP Status-Code 504: Gateway Timeout. + */ + public static final int SC_HTTP_GATEWAY_TIMEOUT = 504; + + /** + * HTTP Status-Code 505: HTTP Version Not Supported. + */ + public static final int SC_HTTP_VERSION = 505; + + public static boolean isOk(int code) { + return (code >= SC_OK && code < SC_HTTP_MULT_CHOICE) || code == SC_HTTP_NOT_MODIFIED; + } } diff --git a/hippo4j-common/src/main/java/cn/hippo4j/common/toolkit/http/HttpUtil.java b/hippo4j-common/src/main/java/cn/hippo4j/common/toolkit/http/HttpUtil.java index a992c521..1aaae2b9 100644 --- a/hippo4j-common/src/main/java/cn/hippo4j/common/toolkit/http/HttpUtil.java +++ b/hippo4j-common/src/main/java/cn/hippo4j/common/toolkit/http/HttpUtil.java @@ -319,7 +319,7 @@ public class HttpUtil { } connection.connect(); JdkHttpClientResponse response = new JdkHttpClientResponse(connection); - if (HttpResponseCode.SC_OK != response.getStatusCode()) { + if (!HttpResponseCode.isOk(response.getStatusCode())) { String msg = String.format("HttpPost response code error. [code] %s [url] %s [body] %s", response.getStatusCode(), connection.getURL(), response.getBodyString()); throw new ServiceException(msg); } diff --git a/hippo4j-server/hippo4j-config/src/main/java/cn/hippo4j/config/service/LongPollingService.java b/hippo4j-server/hippo4j-config/src/main/java/cn/hippo4j/config/service/LongPollingService.java index 426e47c3..8d20fc64 100644 --- a/hippo4j-server/hippo4j-config/src/main/java/cn/hippo4j/config/service/LongPollingService.java +++ b/hippo4j-server/hippo4j-config/src/main/java/cn/hippo4j/config/service/LongPollingService.java @@ -109,7 +109,7 @@ public class LongPollingService { @Override public void run() { try { - for (Iterator iter = allSubs.iterator(); iter.hasNext();) { + for (Iterator iter = allSubs.iterator(); iter.hasNext(); ) { ClientLongPolling clientSub = iter.next(); String identity = groupKey + GROUP_KEY_DELIMITER + identify; List parseMapForFilter = CollectionUtil.newArrayList(identity); @@ -150,7 +150,7 @@ public class LongPollingService { timeout = Math.max(10000, getFixedPollingInterval()); } else { List changedGroups = Md5ConfigUtil.compareMd5(req, clientMd5Map); - if (changedGroups.size() > 0) { + if (!changedGroups.isEmpty()) { generateResponse(rsp, changedGroups); return; } else if (noHangUpFlag != null && noHangUpFlag.equalsIgnoreCase(TRUE_STR)) { @@ -203,7 +203,7 @@ public class LongPollingService { allSubs.remove(ClientLongPolling.this); if (isFixedPolling()) { List changedGroups = Md5ConfigUtil.compareMd5((HttpServletRequest) asyncContext.getRequest(), clientMd5Map); - if (changedGroups.size() > 0) { + if (!changedGroups.isEmpty()) { sendResponse(changedGroups); } else { sendResponse(null); @@ -237,12 +237,13 @@ public class LongPollingService { * @param changedGroups Changed thread pool group key */ private void generateResponse(List changedGroups) { + HttpServletResponse response = (HttpServletResponse) asyncContext.getResponse(); if (null == changedGroups) { + response.setStatus(HttpServletResponse.SC_NOT_MODIFIED); // Tell web container to send http response. asyncContext.complete(); return; } - HttpServletResponse response = (HttpServletResponse) asyncContext.getResponse(); try { String respStr = buildRespStr(changedGroups); response.setHeader("Pragma", "no-cache");