Skip to content

Commit db7874e

Browse files
When enableJSONOnly=true avoid loading Axiom
1 parent 4f720f7 commit db7874e

File tree

2 files changed

+69
-3
lines changed

2 files changed

+69
-3
lines changed

modules/transport/http/src/main/java/org/apache/axis2/transport/http/AxisServlet.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -614,6 +614,12 @@ protected void initParams() {
614614
disableREST = !JavaUtils.isFalseExplicitly(parameter.getValue());
615615
}
616616

617+
// Check if JSON-only mode is enabled
618+
parameter = axisConfiguration.getParameter(Constants.Configuration.ENABLE_JSON_ONLY);
619+
if (parameter != null) {
620+
enableJSONOnly = JavaUtils.isTrueExplicitly(parameter.getValue());
621+
}
622+
617623
// Should we close the reader(s)
618624
parameter = axisConfiguration.getParameter("axis2.close.reader");
619625
if (parameter != null) {

modules/transport/http/src/main/java/org/apache/axis2/transport/http/HTTPTransportUtils.java

Lines changed: 63 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,26 @@ public static InvocationResponse processHTTPPostRequest(MessageContext msgContex
169169
throws AxisFault {
170170
int soapVersion = VERSION_UNKNOWN;
171171
try {
172-
soapVersion = initializeMessageContext(msgContext, soapActionHeader, requestURI, contentType);
172+
// Early JSON-only mode check to avoid Axiom loading
173+
AxisConfiguration axisConfig = msgContext.getConfigurationContext().getAxisConfiguration();
174+
String enableJSONOnly = (String) axisConfig.getParameterValue(Constants.Configuration.ENABLE_JSON_ONLY);
175+
if (enableJSONOnly != null && enableJSONOnly.equalsIgnoreCase("true")) {
176+
if (contentType == null || contentType.isEmpty() || !isJSONRequest(contentType)) {
177+
// Handle JSON-only error without initializing MessageContext (avoids Axiom loading)
178+
throw new AxisFault("JSON-only mode enabled but content-type is not application/json: " + contentType);
179+
}
180+
// For JSON requests in JSON-only mode, use simplified initialization
181+
msgContext.setServerSide(true);
182+
msgContext.setTo(new EndpointReference(requestURI));
183+
msgContext.setDoingREST(true);
184+
// Skip BuilderUtil.getCharSetEncoding() call that triggers Axiom loading
185+
String charSetEnc = getCharSetEncodingSimple(contentType);
186+
msgContext.setProperty(Constants.Configuration.CHARACTER_SET_ENCODING, charSetEnc);
187+
soapVersion = VERSION_SOAP11; // Default for REST/JSON
188+
} else {
189+
// Normal path with full Axiom support
190+
soapVersion = initializeMessageContext(msgContext, soapActionHeader, requestURI, contentType);
191+
}
173192
msgContext.setProperty(MessageContext.TRANSPORT_OUT, out);
174193

175194
InputStream is = handleGZip(msgContext, in);
@@ -211,8 +230,27 @@ public static InvocationResponse processHTTPPostRequest(MessageContext msgContex
211230
throws AxisFault {
212231
int soapVersion = VERSION_UNKNOWN;
213232
try {
214-
soapVersion = initializeMessageContext(msgContext, soapActionHeader,
215-
requestURI, contentType);
233+
// Early JSON-only mode check to avoid Axiom loading
234+
AxisConfiguration axisConfig = msgContext.getConfigurationContext().getAxisConfiguration();
235+
String enableJSONOnly = (String) axisConfig.getParameterValue(Constants.Configuration.ENABLE_JSON_ONLY);
236+
if (enableJSONOnly != null && enableJSONOnly.equalsIgnoreCase("true")) {
237+
if (contentType == null || contentType.isEmpty() || !isJSONRequest(contentType)) {
238+
// Handle JSON-only error without initializing MessageContext (avoids Axiom loading)
239+
throw new AxisFault("JSON-only mode enabled but content-type is not application/json: " + contentType);
240+
}
241+
// For JSON requests in JSON-only mode, use simplified initialization
242+
msgContext.setServerSide(true);
243+
msgContext.setTo(new EndpointReference(requestURI));
244+
msgContext.setDoingREST(true);
245+
// Skip BuilderUtil.getCharSetEncoding() call that triggers Axiom loading
246+
String charSetEnc = getCharSetEncodingSimple(contentType);
247+
msgContext.setProperty(Constants.Configuration.CHARACTER_SET_ENCODING, charSetEnc);
248+
soapVersion = VERSION_SOAP11; // Default for REST/JSON
249+
} else {
250+
// Normal path with full Axiom support
251+
soapVersion = initializeMessageContext(msgContext, soapActionHeader,
252+
requestURI, contentType);
253+
}
216254
msgContext.setProperty(MessageContext.TRANSPORT_OUT, out);
217255

218256
msgContext.setEnvelope(
@@ -423,4 +461,26 @@ static InputStream getMetaInfResourceAsStream(AxisService service, String name)
423461
return null;
424462
}
425463
}
464+
465+
/**
466+
* Simple charset encoding extraction that avoids Axiom dependencies.
467+
* Used in JSON-only mode to prevent loading XML-oriented libraries.
468+
*
469+
* @param contentType HTTP content type header
470+
* @return charset encoding or default UTF-8
471+
*/
472+
private static String getCharSetEncodingSimple(String contentType) {
473+
if (contentType == null) {
474+
return MessageContext.DEFAULT_CHAR_SET_ENCODING;
475+
}
476+
int index = contentType.indexOf("charset=");
477+
if (index != -1) {
478+
String encoding = contentType.substring(index + 8);
479+
if (encoding.indexOf(';') != -1) {
480+
encoding = encoding.substring(0, encoding.indexOf(';'));
481+
}
482+
return encoding.trim();
483+
}
484+
return MessageContext.DEFAULT_CHAR_SET_ENCODING;
485+
}
426486
}

0 commit comments

Comments
 (0)