Skip to content

Commit

Permalink
[ISSUE 4239] Optimal http header hard-code (apache#4295)
Browse files Browse the repository at this point in the history
* optimal http header converts

* fix test error

* fix
  • Loading branch information
Alonexc authored Jul 30, 2023
1 parent 5b8616b commit a7b24ea
Show file tree
Hide file tree
Showing 50 changed files with 682 additions and 2,194 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,12 @@
import org.apache.eventmesh.common.protocol.http.common.EventMeshRetCode;
import org.apache.eventmesh.common.protocol.http.common.ProtocolKey;
import org.apache.eventmesh.common.protocol.http.common.RequestURI;
import org.apache.eventmesh.common.utils.IPUtils;
import org.apache.eventmesh.common.utils.JsonUtils;
import org.apache.eventmesh.common.utils.ThreadUtils;

import org.apache.commons.lang3.StringUtils;

import java.net.URI;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
Expand Down Expand Up @@ -194,20 +193,19 @@ public DefaultFullHttpResponse httpResponse() throws Exception {

public void buildSysHeaderForClient() {
// sys attributes
sysHeaderMap.put(ProtocolKey.ClientInstanceKey.ENV, headerMap.getOrDefault(ProtocolKey.ClientInstanceKey.ENV, "env"));
sysHeaderMap.put(ProtocolKey.ClientInstanceKey.IDC, headerMap.getOrDefault(ProtocolKey.ClientInstanceKey.IDC, "idc"));
sysHeaderMap.put(ProtocolKey.ClientInstanceKey.IP, headerMap.getOrDefault(ProtocolKey.ClientInstanceKey.IP, IPUtils.getLocalAddress()));
sysHeaderMap.put(ProtocolKey.ClientInstanceKey.PID, headerMap.getOrDefault(ProtocolKey.ClientInstanceKey.PID, ThreadUtils.getPID()));
sysHeaderMap.put(ProtocolKey.ClientInstanceKey.SYS, headerMap.getOrDefault(ProtocolKey.ClientInstanceKey.SYS, "1234"));
sysHeaderMap.put(ProtocolKey.ClientInstanceKey.USERNAME, headerMap.getOrDefault(ProtocolKey.ClientInstanceKey.USERNAME, "eventmesh"));
sysHeaderMap.put(ProtocolKey.ClientInstanceKey.PASSWD, headerMap.getOrDefault(ProtocolKey.ClientInstanceKey.PASSWD, "pass"));
sysHeaderMap.put(ProtocolKey.ClientInstanceKey.PRODUCERGROUP,
headerMap.getOrDefault(ProtocolKey.ClientInstanceKey.PRODUCERGROUP, "em-http-producer"));
sysHeaderMap.put(ProtocolKey.ClientInstanceKey.CONSUMERGROUP,
headerMap.getOrDefault(ProtocolKey.ClientInstanceKey.CONSUMERGROUP, "em-http-consumer"));
sysHeaderMap.put(ProtocolKey.PROTOCOL_TYPE, "http");
sysHeaderMap.put(ProtocolKey.PROTOCOL_DESC, "http");
sysHeaderMap.put(ProtocolKey.ClientInstanceKey.TOKEN, headerMap.getOrDefault(ProtocolKey.ClientInstanceKey.TOKEN, "token"));
EnumSet<ProtocolKey.ClientInstanceKey> clientInstanceKeys = EnumSet.allOf(ProtocolKey.ClientInstanceKey.class);
for (ProtocolKey.ClientInstanceKey clientInstanceKey : clientInstanceKeys) {
switch (clientInstanceKey) {
case BIZSEQNO:
case UNIQUEID:
break;
default:
sysHeaderMap.put(clientInstanceKey.getKey(),
headerMap.getOrDefault(clientInstanceKey.getKey(), clientInstanceKey.getValue()));
}
}
}

public void buildSysHeaderForCE() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@

package org.apache.eventmesh.common.protocol.http.common;

import org.apache.eventmesh.common.utils.IPUtils;
import org.apache.eventmesh.common.utils.ThreadUtils;

public class ProtocolKey {

public static final String REQUEST_CODE = "code";
Expand All @@ -32,22 +35,39 @@ public class ProtocolKey {

public static final String CONTENT_TYPE = "contenttype";

public static class ClientInstanceKey {
public enum ClientInstanceKey {

////////////////////////////////////Protocol layer requester description///////////
public static final String ENV = "env";
public static final String IDC = "idc";
public static final String SYS = "sys";
public static final String PID = "pid";
public static final String IP = "ip";
public static final String USERNAME = "username";
public static final String PASSWD = "passwd";
public static final String BIZSEQNO = "bizseqno";
public static final String UNIQUEID = "uniqueid";
public static final String PRODUCERGROUP = "producergroup";
public static final String CONSUMERGROUP = "consumergroup";

public static final String TOKEN = "token";
ENV("env", "env"),
IDC("idc", "idc"),
SYS("sys", "1234"),
PID("pid", ThreadUtils.getPID()),
IP("ip", IPUtils.getLocalAddress()),
USERNAME("username", "eventmesh"),
PASSWD("passwd", "pass"),
BIZSEQNO("bizseqno", "bizseqno"),
UNIQUEID("uniqueid", "uniqueid"),
PRODUCERGROUP("producergroup", "em-http-producer"),
CONSUMERGROUP("consumergroup", "em-http-consumer"),

TOKEN("token", "token");

private final String key;

private final Object value;

public String getKey() {
return key;
}

public Object getValue() {
return value;
}

ClientInstanceKey(String key, Object value) {
this.key = key;
this.value = value;
}
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,20 @@

package org.apache.eventmesh.common.protocol.http.header.client;

import org.apache.eventmesh.common.Constants;
import org.apache.eventmesh.common.protocol.http.common.ProtocolKey;
import org.apache.eventmesh.common.protocol.http.common.ProtocolVersion;
import org.apache.eventmesh.common.protocol.http.header.Header;
import org.apache.eventmesh.common.utils.HttpConvertsUtils;

import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.Map;

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

@Setter
@Getter
@ToString
public class HeartbeatRequestHeader extends Header {

//request code
Expand Down Expand Up @@ -62,133 +65,16 @@ public class HeartbeatRequestHeader extends Header {
//PASSWD of the requester
private String passwd = "user@123";

public String getCode() {
return code;
}

public void setCode(String code) {
this.code = code;
}

public String getLanguage() {
return language;
}

public void setLanguage(String language) {
this.language = language;
}

public ProtocolVersion getVersion() {
return version;
}

public void setVersion(ProtocolVersion version) {
this.version = version;
}

public String getEnv() {
return env;
}

public void setEnv(String env) {
this.env = env;
}

public String getIdc() {
return idc;
}

public void setIdc(String idc) {
this.idc = idc;
}

public String getSys() {
return sys;
}

public void setSys(String sys) {
this.sys = sys;
}

public String getPid() {
return pid;
}

public void setPid(String pid) {
this.pid = pid;
}

public String getIp() {
return ip;
}

public void setIp(String ip) {
this.ip = ip;
}

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public String getPasswd() {
return passwd;
}

public void setPasswd(String passwd) {
this.passwd = passwd;
}

@Override
public Map<String, Object> toMap() {
Map<String, Object> map = new HashMap<String, Object>();
map.put(ProtocolKey.REQUEST_CODE, code);
map.put(ProtocolKey.LANGUAGE, language);
map.put(ProtocolKey.VERSION, version);
map.put(ProtocolKey.ClientInstanceKey.ENV, env);
map.put(ProtocolKey.ClientInstanceKey.IDC, idc);
map.put(ProtocolKey.ClientInstanceKey.SYS, sys);
map.put(ProtocolKey.ClientInstanceKey.PID, pid);
map.put(ProtocolKey.ClientInstanceKey.IP, ip);
map.put(ProtocolKey.ClientInstanceKey.USERNAME, username);
map.put(ProtocolKey.ClientInstanceKey.PASSWD, passwd);
return map;
}

@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("heartbeatRequestHeader={")
.append("code=").append(code).append(",")
.append("language=").append(language).append(",")
.append("version=").append(version).append(",")
.append("env=").append(env).append(",")
.append("idc=").append(idc).append(",")
.append("sys=").append(sys).append(",")
.append("pid=").append(pid).append(",")
.append("ip=").append(ip).append(",")
.append("username=").append(username).append(",")
.append("passwd=").append(passwd).append("}");
return sb.toString();
HttpConvertsUtils httpConvertsUtils = new HttpConvertsUtils();
ProtocolKey protocolKey = new ProtocolKey();
return httpConvertsUtils.httpMapConverts(this, protocolKey);
}

public static HeartbeatRequestHeader buildHeader(Map<String, Object> headerParam) {
HttpConvertsUtils httpConvertsUtils = new HttpConvertsUtils();
HeartbeatRequestHeader header = new HeartbeatRequestHeader();
header.setCode(MapUtils.getString(headerParam, ProtocolKey.REQUEST_CODE));
header.setVersion(ProtocolVersion.get(MapUtils.getString(headerParam, ProtocolKey.VERSION)));
String lan = StringUtils.isBlank(MapUtils.getString(headerParam, ProtocolKey.LANGUAGE))
? Constants.LANGUAGE_JAVA : MapUtils.getString(headerParam, ProtocolKey.LANGUAGE);
header.setLanguage(lan);
header.setEnv(MapUtils.getString(headerParam, ProtocolKey.ClientInstanceKey.ENV));
header.setIdc(MapUtils.getString(headerParam, ProtocolKey.ClientInstanceKey.IDC));
header.setSys(MapUtils.getString(headerParam, ProtocolKey.ClientInstanceKey.SYS));
header.setPid(MapUtils.getString(headerParam, ProtocolKey.ClientInstanceKey.PID));
header.setIp(MapUtils.getString(headerParam, ProtocolKey.ClientInstanceKey.IP));
header.setUsername(MapUtils.getString(headerParam, ProtocolKey.ClientInstanceKey.USERNAME));
header.setPasswd(MapUtils.getString(headerParam, ProtocolKey.ClientInstanceKey.PASSWD));
return header;
return (HeartbeatRequestHeader) httpConvertsUtils.httpHeaderConverts(header, headerParam);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,17 @@

import org.apache.eventmesh.common.protocol.http.common.ProtocolKey;
import org.apache.eventmesh.common.protocol.http.header.Header;
import org.apache.eventmesh.common.utils.HttpConvertsUtils;

import java.util.HashMap;
import java.util.Map;

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

@Setter
@Getter
@ToString
public class HeartbeatResponseHeader extends Header {

private int code;
Expand All @@ -36,46 +43,6 @@ public class HeartbeatResponseHeader extends Header {

private String eventMeshIdc;

public int getCode() {
return code;
}

public void setCode(int code) {
this.code = code;
}

public String getEventMeshCluster() {
return eventMeshCluster;
}

public void setEventMeshCluster(String eventMeshCluster) {
this.eventMeshCluster = eventMeshCluster;
}

public String getEventMeshIp() {
return eventMeshIp;
}

public void setEventMeshIp(String eventMeshIp) {
this.eventMeshIp = eventMeshIp;
}

public String getEventMeshEnv() {
return eventMeshEnv;
}

public void setEventMeshEnv(String eventMeshEnv) {
this.eventMeshEnv = eventMeshEnv;
}

public String getEventMeshIdc() {
return eventMeshIdc;
}

public void setEventMeshIdc(String eventMeshIdc) {
this.eventMeshIdc = eventMeshIdc;
}

public static HeartbeatResponseHeader buildHeader(Integer requestCode, String eventMeshCluster,
String eventMeshIp, String eventMeshEnv,
String eventMeshIDC) {
Expand All @@ -88,26 +55,11 @@ public static HeartbeatResponseHeader buildHeader(Integer requestCode, String ev
return heartbeatResponseHeader;
}

@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("heartbeatResponseHeader={")
.append("code=").append(code).append(",")
.append("eventMeshEnv=").append(eventMeshEnv).append(",")
.append("eventMeshIdc=").append(eventMeshIdc).append(",")
.append("eventMeshCluster=").append(eventMeshCluster).append(",")
.append("eventMeshIp=").append(eventMeshIp).append("}");
return sb.toString();
}

@Override
public Map<String, Object> toMap() {
Map<String, Object> map = new HashMap<String, Object>();
map.put(ProtocolKey.REQUEST_CODE, code);
map.put(ProtocolKey.EventMeshInstanceKey.EVENTMESHCLUSTER, eventMeshCluster);
map.put(ProtocolKey.EventMeshInstanceKey.EVENTMESHIP, eventMeshIp);
map.put(ProtocolKey.EventMeshInstanceKey.EVENTMESHENV, eventMeshEnv);
map.put(ProtocolKey.EventMeshInstanceKey.EVENTMESHIDC, eventMeshIdc);
return map;
HttpConvertsUtils httpConvertsUtils = new HttpConvertsUtils();
ProtocolKey protocolKey = new ProtocolKey();
ProtocolKey.EventMeshInstanceKey eventMeshInstanceKey = new ProtocolKey.EventMeshInstanceKey();
return httpConvertsUtils.httpMapConverts(this, protocolKey, eventMeshInstanceKey);
}
}
Loading

0 comments on commit a7b24ea

Please sign in to comment.