39
39
import com .alibaba .fastjson2 .JSON ;
40
40
import com .alibaba .fastjson2 .filter .Filter ;
41
41
import com .alibaba .fastjson2 .filter .NameFilter ;
42
+ import com .alibaba .fastjson2 .filter .PropertyFilter ;
42
43
import com .alibaba .fastjson2 .filter .ValueFilter ;
43
44
44
45
import lombok .extern .slf4j .Slf4j ;
56
57
@ EventMeshHttpHandler (path = "/v2/configuration" )
57
58
public class ConfigurationHandler extends AbstractHttpHandler {
58
59
60
+ private final CommonConfiguration commonConfiguration ;
59
61
private final EventMeshTCPConfiguration eventMeshTCPConfiguration ;
60
62
private final EventMeshHTTPConfiguration eventMeshHTTPConfiguration ;
61
63
private final EventMeshGrpcConfiguration eventMeshGrpcConfiguration ;
@@ -68,10 +70,12 @@ public class ConfigurationHandler extends AbstractHttpHandler {
68
70
* @param eventMeshGrpcConfiguration the gRPC configuration for EventMesh
69
71
*/
70
72
public ConfigurationHandler (
73
+ CommonConfiguration commonConfiguration ,
71
74
EventMeshTCPConfiguration eventMeshTCPConfiguration ,
72
75
EventMeshHTTPConfiguration eventMeshHTTPConfiguration ,
73
76
EventMeshGrpcConfiguration eventMeshGrpcConfiguration ) {
74
77
super ();
78
+ this .commonConfiguration = commonConfiguration ;
75
79
this .eventMeshTCPConfiguration = eventMeshTCPConfiguration ;
76
80
this .eventMeshHTTPConfiguration = eventMeshHTTPConfiguration ;
77
81
this .eventMeshGrpcConfiguration = eventMeshGrpcConfiguration ;
@@ -85,34 +89,51 @@ public ConfigurationHandler(
85
89
* <p>When {@code properties}, the field names are returned in Properties format;
86
90
* <p>When {@code bean}, the field names themselves are used as json keys.
87
91
* </li>
92
+ * <li>
93
+ * {@code configs}: String; Optional, DefaultValue: {@code exclusive}, SelectableValue: {@code all}.
94
+ * <p>When {@code exclusive}, protocol-specific configurations will only contain protocol-exclusive fields
95
+ * and won't contain any {@link CommonConfiguration} fields;
96
+ * <p>When {@code all}, protocol-specific configurations will contain all fields, including those in {@link CommonConfiguration}.
97
+ * </li>
88
98
* </ul>
89
99
*/
90
100
@ Override
91
101
protected void get (HttpRequest httpRequest , ChannelHandlerContext ctx ) {
92
102
String format = HttpRequestUtil .getQueryParam (httpRequest , "format" , "properties" );
93
-
94
- Filter [] filters ;
95
- if (format .equals ("properties" )) {
96
- filters = new Filter [] {new ConfigFieldFilter (), new IPAddressToStringFilter ()};
97
- } else if (format .equals ("bean" )) {
98
- filters = new Filter [] {new IPAddressToStringFilter ()};
99
- } else {
100
- log .warn ("Invalid format param: {}" , format );
101
- writeBadRequest (ctx , "Invalid format param: " + format );
102
- return ;
103
+ String configs = HttpRequestUtil .getQueryParam (httpRequest , "configs" , "exclusive" );
104
+
105
+ List <Filter > filters = new ArrayList <>();
106
+ switch (configs ) {
107
+ case "exclusive" :
108
+ filters .add (new SuperClassFieldFilter ());
109
+ break ;
110
+ case "all" : break ;
111
+ default :
112
+ throw new IllegalArgumentException ("Invalid param 'configs': " + configs );
113
+ }
114
+ switch (format ) {
115
+ case "properties" :
116
+ filters .add (new ConfigFieldFilter ());
117
+ break ;
118
+ case "bean" : break ;
119
+ default :
120
+ throw new IllegalArgumentException ("Invalid param 'format': " + format );
103
121
}
122
+ filters .add (new IPAddressToStringFilter ());
104
123
105
124
GetConfigurationResponse getConfigurationResponse = new GetConfigurationResponse (
125
+ commonConfiguration ,
106
126
eventMeshTCPConfiguration ,
107
127
eventMeshHTTPConfiguration ,
108
- eventMeshGrpcConfiguration
128
+ eventMeshGrpcConfiguration ,
129
+ "v1.10.0-release" // TODO get version number after merging https://github.com/apache/eventmesh/pull/4055
109
130
);
110
- String json = JSON .toJSONString (Result .success (getConfigurationResponse ), filters );
131
+ String json = JSON .toJSONString (Result .success (getConfigurationResponse ), filters . toArray ( new Filter [ 0 ]) );
111
132
writeJson (ctx , json );
112
133
}
113
134
114
135
/**
115
- * For each member of {@link EventMeshTCPConfiguration}, {@link EventMeshHTTPConfiguration}, and {@link EventMeshGrpcConfiguration} ,
136
+ * For each member of configuration classes ,
116
137
* the value of the {@link ConfigField} annotation for each field is obtained through reflection,
117
138
* and then concatenated with the configuration prefix in the {@link Config} annotation to serve as the JSON key for this field.
118
139
* <p>
@@ -155,6 +176,39 @@ private Field findFieldInClassHierarchy(Class<?> clazz, String fieldName) throws
155
176
}
156
177
}
157
178
179
+ /**
180
+ * For each member of {@link EventMeshTCPConfiguration}, {@link EventMeshHTTPConfiguration}, and {@link EventMeshGrpcConfiguration},
181
+ * if the {@code name} is a member that exists in {@link CommonConfiguration} class, it will be skipped.
182
+ */
183
+ static class SuperClassFieldFilter implements PropertyFilter {
184
+ @ Override
185
+ public boolean apply (Object object , String name , Object value ) {
186
+ try {
187
+ Field field = findFieldInClassNonHierarchy (object .getClass (), name );
188
+ return field != null ;
189
+ } catch (NoSuchFieldException e ) {
190
+ log .error ("Failed to get field {} from object {}" , name , object , e );
191
+ }
192
+ return true ;
193
+ }
194
+
195
+ /**
196
+ * If a field of a subclass exists in the superclass, return null, causing FastJSON to skip this field.
197
+ */
198
+ private Field findFieldInClassNonHierarchy (Class <?> clazz , String fieldName ) throws NoSuchFieldException {
199
+ try {
200
+ return clazz .getDeclaredField (fieldName );
201
+ } catch (NoSuchFieldException e ) {
202
+ Class <?> superclass = clazz .getSuperclass ();
203
+ if (superclass == null ) {
204
+ throw e ;
205
+ } else {
206
+ return null ;
207
+ }
208
+ }
209
+ }
210
+ }
211
+
158
212
/**
159
213
* {@link IPAddress} can't be serialized directly by FastJSON,
160
214
* so this filter converts {@link IPAddress} objects to their string representation.
0 commit comments