Skip to content

Commit

Permalink
[TUBEMQ-504]Adjust the WebMethodMapper class interfaces (#388)
Browse files Browse the repository at this point in the history
Co-authored-by: gosonzhang <gosonzhang@tencent.com>
  • Loading branch information
gosonzhang and gosonzhang committed Jan 9, 2021
1 parent 67951ca commit 278ed84
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
import static org.apache.tubemq.server.common.webbase.WebMethodMapper.getWebApiRegInfo;
import static org.apache.tubemq.server.common.webbase.WebMethodMapper.registerWebMethod;
import java.io.IOException;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
Expand All @@ -44,7 +45,7 @@ protected void doGet(HttpServletRequest req,
doPost(req, resp);
}

public List<String> getSupportedMethod() {
public Set<String> getSupportedMethod() {
return getRegisteredWebMethod();
}

Expand All @@ -59,7 +60,7 @@ protected void doPost(HttpServletRequest req,
strBuffer.append("{\"result\":false,\"errCode\":400,\"errMsg\":\"")
.append("Please take with method parameter! \"}");
} else {
WebApiRegInfo webApiRegInfo = getWebApiRegInfo(true, method);
WebApiRegInfo webApiRegInfo = getWebApiRegInfo(method);
if (webApiRegInfo == null) {
strBuffer.append("{\"result\":false,\"errCode\":400,\"errMsg\":\"")
.append("Unsupported method ").append(method).append("\"}");
Expand All @@ -82,8 +83,10 @@ protected void doPost(HttpServletRequest req,
public abstract void registerWebApiMethod();

protected void innRegisterWebMethod(String webMethodName,
String clsMethodName) {
registerWebMethod(true, webMethodName, clsMethodName, this);
String clsMethodName,
boolean needAuthToken) {
registerWebMethod(webMethodName, clsMethodName,
false, needAuthToken, this);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -58,59 +58,59 @@ public BrokerAdminServlet(TubeBroker broker) {
public void registerWebApiMethod() {
// query consumer group's offset
innRegisterWebMethod("admin_query_group_offset",
"adminQueryCurrentGroupOffSet");
"adminQueryCurrentGroupOffSet", false);
// query snapshot message
innRegisterWebMethod("admin_snapshot_message",
"adminQuerySnapshotMessageSet");
"adminQuerySnapshotMessageSet", false);
// query broker's all consumer info
innRegisterWebMethod("admin_query_broker_all_consumer_info",
"adminQueryBrokerAllConsumerInfo");
"adminQueryBrokerAllConsumerInfo", false);
// get memory store status info
innRegisterWebMethod("admin_query_broker_memstore_info",
"adminGetMemStoreStatisInfo");
"adminGetMemStoreStatisInfo", false);
// query broker's all message store info
innRegisterWebMethod("admin_query_broker_all_store_info",
"adminQueryBrokerAllMessageStoreInfo");
"adminQueryBrokerAllMessageStoreInfo", false);
// query consumer register info
innRegisterWebMethod("admin_query_consumer_regmap",
"adminQueryConsumerRegisterInfo");
"adminQueryConsumerRegisterInfo", false);
// manual set offset
innRegisterWebMethod("admin_manual_set_current_offset",
"adminManualSetCurrentOffSet");
"adminManualSetCurrentOffSet", false);
// get all registered methods
innRegisterWebMethod("admin_get_methods",
"adminQueryAllMethods");
"adminQueryAllMethods", false);
// query topic's publish info
innRegisterWebMethod("admin_query_pubinfo",
"adminQueryPubInfo");
"adminQueryPubInfo", false);
// Query all consumer groups booked on the Broker.
innRegisterWebMethod("admin_query_group",
"adminQueryBookedGroup");
"adminQueryBookedGroup", false);
// query consumer group's offset
innRegisterWebMethod("admin_query_offset",
"adminQueryGroupOffSet");
"adminQueryGroupOffSet", false);
// clone consumer group's offset from source to target
innRegisterWebMethod("admin_clone_offset",
"adminCloneGroupOffSet");
"adminCloneGroupOffSet", false);
// set or update group's offset info
innRegisterWebMethod("admin_set_offset",
"adminSetGroupOffSet");
"adminSetGroupOffSet", false);
// remove group's offset info
innRegisterWebMethod("admin_rmv_offset",
"adminRemoveGroupOffSet");
"adminRemoveGroupOffSet", false);
}

public void adminQueryAllMethods(HttpServletRequest req,
StringBuilder sBuilder) {
int index = 0;
List<String> methods = getSupportedMethod();
Set<String> methods = getSupportedMethod();
sBuilder.append("{\"result\":true,\"errCode\":0,\"errMsg\":\"Success!\",\"dataSet\":[");
for (index = 0; index < methods.size(); index++) {
if (index > 0) {
for (String method : methods) {
if (index++ > 0) {
sBuilder.append(",");
}
sBuilder.append("{\"id\":").append(index + 1)
.append(",\"method\":\"").append(methods.get(index)).append("\"}");
.append(",\"method\":\"").append(method).append("\"}");
}
sBuilder.append("],\"totalCnt\":").append(index + 1).append("}");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ public void setFailResult(int errCode, final String errMsg) {
this.retData1 = null;
}

public void setFailResult(final String errMsg) {
this.success = false;
this.errCode = TErrCodeConstants.BAD_REQUEST;
this.errInfo = errMsg;
this.retData1 = null;
}

public void setSuccResult(Object retData) {
this.success = true;
this.errInfo = "";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,13 +242,17 @@ public static StringBuilder buildFailResult(StringBuilder strBuffer, String errM
.append(errMsg).append("\"}");
}

public static StringBuilder buildSuccessResult(StringBuilder strBuffer) {
return strBuffer.append("{\"result\":true,\"errCode\":0,\"errMsg\":\"OK\"}");
}

/**
* Parse the parameter value from an object value to a long value
*
* @param req Http Servlet Request
* @param fieldDef the parameter field definition
* @param required a boolean value represent whether the parameter is must required
* @param defValue a default value returned if failed to parse value from the given object
* @param defValue a default value returned if the field not exist
* @param result process result of parameter value
* @return process result
*/
Expand Down Expand Up @@ -329,7 +333,7 @@ public static boolean getIntParamValue(HttpServletRequest req,
* @param req Http Servlet Request
* @param fieldDef the parameter field definition
* @param required a boolean value represent whether the parameter is must required
* @param defValue a default value returned if failed to parse value from the given object
* @param defValue a default value returned if the field not exist
* @param minValue min value required
* @param result process result of parameter value
* @return process result
Expand Down Expand Up @@ -377,7 +381,7 @@ public static boolean getIntParamValue(HttpServletRequest req,
* @param req Http Servlet Request
* @param fieldDef the parameter field definition
* @param required a boolean value represent whether the parameter is must required
* @param defValue a default value returned if failed to parse value from the given object
* @param defValue a default value returned if the field not exist
* @param result process result
* @return valid result for the parameter value
*/
Expand All @@ -404,7 +408,7 @@ public static boolean getBooleanParamValue(HttpServletRequest req,
* @param req Http Servlet Request
* @param fieldDef the parameter field definition
* @param required a boolean value represent whether the parameter is must required
* @param defValue a default value returned if failed to parse value from the given object
* @param defValue a default value returned if the field not exist
* @param result process result
* @return valid result for the parameter value
*/
Expand Down Expand Up @@ -486,7 +490,7 @@ public static boolean getStringParamValue(HttpServletRequest req,
* @param req Http Servlet Request
* @param fieldDef the parameter field definition
* @param required a boolean value represent whether the parameter is must required
* @param defValue a default value returned if failed to parse value from the given object
* @param defValue a default value returned if the field not exist
* @param result process result
* @return valid result for the parameter value
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,9 @@
package org.apache.tubemq.server.common.webbase;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -30,36 +29,25 @@ public class WebMethodMapper {
// log printer
private static final Logger logger =
LoggerFactory.getLogger(WebMethodMapper.class);
// The query methods map
public static final Map<String, WebApiRegInfo> WEB_QRY_METHOD_MAP =
new HashMap<>();
// The modify methods map
public static final Map<String, WebApiRegInfo> WEB_MDY_METHOD_MAP =

public static final Map<String, WebApiRegInfo> WEB_METHOD_MAP =
new HashMap<>();


public static WebApiRegInfo getWebApiRegInfo(boolean isQryApi,
String webMethodName) {
if (isQryApi) {
return WEB_QRY_METHOD_MAP.get(webMethodName);
}
return WEB_MDY_METHOD_MAP.get(webMethodName);
public static WebApiRegInfo getWebApiRegInfo(String webMethodName) {
return WEB_METHOD_MAP.get(webMethodName);
}

public static void registerWebMethod(boolean isQryApi,
String webMethodName,
public static void registerWebMethod(String webMethodName,
String clsMethodName,
boolean onlyMasterOp,
boolean needAuthToken,
Object webHandler) {
Method[] methods = webHandler.getClass().getMethods();
for (Method item : methods) {
if (item.getName().equals(clsMethodName)) {
if (isQryApi) {
WEB_QRY_METHOD_MAP.put(webMethodName,
new WebApiRegInfo(item, webHandler));
} else {
WEB_MDY_METHOD_MAP.put(webMethodName,
new WebApiRegInfo(item, webHandler));
}
WEB_METHOD_MAP.put(webMethodName,
new WebApiRegInfo(item, webHandler, onlyMasterOp, needAuthToken));
return;
}
}
Expand All @@ -69,23 +57,27 @@ public static void registerWebMethod(boolean isQryApi,
.append(webHandler.getClass().getName()).toString());
}

public static List<String> getRegisteredWebMethod() {
List<String> methods = new ArrayList<>();
methods.addAll(WEB_QRY_METHOD_MAP.keySet());
methods.addAll(WEB_MDY_METHOD_MAP.keySet());
return methods;
public static Set<String> getRegisteredWebMethod() {
return WEB_METHOD_MAP.keySet();
}



public static class WebApiRegInfo {
public Method method;
public Object webHandler;
public boolean onlyMasterOp = false;
public boolean needAuthToken = false;


public WebApiRegInfo(Method method,
Object webHandler) {
Object webHandler,
boolean onlyMasterOp,
boolean needAuthToken) {
this.method = method;
this.webHandler = webHandler;
this.onlyMasterOp = onlyMasterOp;
this.needAuthToken = needAuthToken;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public void execute(RequestContext requestContext) {
"DesignatedPrimary happened...please check if the other member is down");
}
}
WebMethodMapper.WebApiRegInfo webApiRegInfo = getWebApiRegInfo(isQuery, method);
WebMethodMapper.WebApiRegInfo webApiRegInfo = getWebApiRegInfo(method);
if (webApiRegInfo == null) {
strBuffer.append("{\"result\":false,\"errCode\":400,\"errMsg\":\"Unsupported method: ")
.append(method).append("\"}");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,20 @@ public AbstractWebHandler(TMaster master) {

protected void registerQueryWebMethod(String webMethodName,
String clsMethodName) {
innRegisterWebMethod(true, webMethodName, clsMethodName);
innRegisterWebMethod(webMethodName, clsMethodName, false, false);
}

protected void registerModifyWebMethod(String webMethodName,
String clsMethodName) {
innRegisterWebMethod(false, webMethodName, clsMethodName);
innRegisterWebMethod(webMethodName, clsMethodName, true, true);
}

private void innRegisterWebMethod(boolean isQryApi,
String webMethodName,
String clsMethodName) {
registerWebMethod(isQryApi, webMethodName, clsMethodName, this);
private void innRegisterWebMethod(String webMethodName,
String clsMethodName,
boolean onlyMasterOp,
boolean needAuthToken) {
registerWebMethod(webMethodName, clsMethodName,
onlyMasterOp, needAuthToken, this);
}

}

0 comments on commit 278ed84

Please sign in to comment.