Skip to content

Commit

Permalink
Merge branch 'feature-2.0.0-beta-osx-new' of https://github.com/Feder…
Browse files Browse the repository at this point in the history
…atedAI/FATE into feature-2.0.0-beta-osx-new
  • Loading branch information
forgivedengkai committed Jul 20, 2023
2 parents b6dd00b + 85a8d30 commit 5b3d238
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
* limitations under the License.
*/
package com.osx.broker.grpc;

import com.osx.broker.ServiceContainer;
import com.osx.broker.util.DebugUtil;
import com.osx.core.exceptions.SysException;
import com.osx.core.provider.TechProvider;
import io.grpc.stub.StreamObserver;
Expand Down Expand Up @@ -46,7 +48,7 @@ public io.grpc.stub.StreamObserver<Osx.Inbound> transport(
public void invoke(Osx.Inbound request,
io.grpc.stub.StreamObserver<Osx.Outbound> responseObserver) {


DebugUtil.printGrpcParams(request);
Map<String, String> metaDataMap = request.getMetadataMap();
String techProviderCode = metaDataMap.get(Osx.Header.TechProviderCode.name());
TechProvider techProvider = ServiceContainer.techProviderRegister.select(techProviderCode);
Expand Down Expand Up @@ -76,6 +78,7 @@ private void init(Osx.Inbound inbound) {
String techProviderCode = metaDataMap.get(Osx.Header.TechProviderCode.name());
techProvider = ServiceContainer.techProviderRegister.select(techProviderCode);
if (techProvider != null) {
DebugUtil.printGrpcParams(inbound);
requestObserver = techProvider.processGrpcTransport(inbound, responseObserver);
} else {
//抛出异常
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package com.osx.broker.http;

import com.osx.broker.ServiceContainer;
import com.osx.broker.util.DebugUtil;
import com.osx.core.constant.PtpHttpHeader;
import com.osx.core.provider.TechProvider;
import org.apache.commons.lang3.StringUtils;
Expand All @@ -34,6 +35,7 @@ public class DispatchServlet extends HttpServlet {

protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//处理get请求
DebugUtil.printHttpParams(req);
String protocol = req.getProtocol();
if (!protocol.endsWith("1.1")) {
resp.sendError(405, "http.method_get_not_supported");
Expand All @@ -56,6 +58,7 @@ protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws Se

protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//处理post请求
DebugUtil.printHttpParams(req);
String requestUri = req.getRequestURI();
//logger.info("receive request uri {}",requestUri);
String protocol = req.getProtocol();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.osx.broker.util;

import com.osx.api.constants.Protocol;
import com.osx.core.config.MetaInfo;
import com.osx.core.utils.JsonUtil;
import org.ppc.ptp.Osx;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

public class DebugUtil {

static Logger logger = LoggerFactory.getLogger(DebugUtil.class);

public static void printGrpcParams(Osx.Inbound request) {
try {
if (MetaInfo.PROTOCOL_PARAMS_PRINT) {
logger.info("【{}】====> {}", Protocol.grpc.name(), JsonUtil.object2Json(request.getMetadataMap()));
}
}catch (Exception e){
logger.error("DebugUtil.printGrpcParams error : ",e);
}
}

public static void printHttpParams(HttpServletRequest request) {
try {
if (MetaInfo.PROTOCOL_PARAMS_PRINT) {
StringBuilder info = new StringBuilder("【" + Protocol.http.name() + "】====> " + "(uri) = " + request.getRequestURI() + "\n(head) = ");
Enumeration<String> headerNames = request.getHeaderNames();
Map<String, Object> headMap = new HashMap<>();
if (headerNames.hasMoreElements()) {
String headerName = headerNames.nextElement();
headMap.put(headerName, request.getHeader(headerName));
}
info.append("\n").append(JsonUtil.object2Json(headMap)).append("\n(body) = ");
try (BufferedReader reader = request.getReader()) {
String line;
while ((line = reader.readLine()) != null) {
info.append(line);
}
} catch (IOException e) {
e.printStackTrace();
}
logger.info(info.toString());
}
} catch (Exception e) {
logger.error("DebugUtil.printGrpcParams error : ",e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,9 @@ public class MetaInfo {
public static String PROPERTY_TOKEN_GENERATOR_CONFIG_PATH;
public static String PROPERTY_CONFIG_DIR;

@Config(confKey = "protocol.params.print", pattern = Dict.BOOLEAN_PATTERN)
public static Boolean PROTOCOL_PARAMS_PRINT = false;


public static boolean isCluster() {
return PROPERTY_DEPLOY_MODE.equals(DeployMode.cluster.name());
Expand Down

0 comments on commit 5b3d238

Please sign in to comment.