This is an automated email from the ASF dual-hosted git repository. robertlazarski pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/axis-axis2-java-core.git
The following commit(s) were added to refs/heads/master by this push: new eb7db458e2 AXIS2-6063 Add enableJSONOnly parameter to axis2.xml eb7db458e2 is described below commit eb7db458e231106d4497ccf483dfa83a425cc5aa Author: Robert Lazarski <robertlazar...@gmail.com> AuthorDate: Fri Dec 29 11:35:54 2023 -0500 AXIS2-6063 Add enableJSONOnly parameter to axis2.xml --- modules/kernel/conf/axis2.xml | 1 + modules/kernel/src/org/apache/axis2/Constants.java | 1 + .../org/apache/axis2/kernel/http/HTTPConstants.java | 1 + modules/samples/json/resources/axis2.xml | 1 + .../apache/axis2/transport/http/AxisServlet.java | 21 ++++++++++++++++++++- .../axis2/transport/http/HTTPTransportUtils.java | 17 +++++++++++++++++ modules/webapp/conf/axis2.xml | 1 + 7 files changed, 42 insertions(+), 1 deletion(-) diff --git a/modules/kernel/conf/axis2.xml b/modules/kernel/conf/axis2.xml index d57b3ad164..f31d6e69e6 100644 --- a/modules/kernel/conf/axis2.xml +++ b/modules/kernel/conf/axis2.xml @@ -25,6 +25,7 @@ <parameter name="hotupdate">false</parameter> <parameter name="enableMTOM">false</parameter> <parameter name="enableSwA">false</parameter> + <parameter name="enableJSONOnly">false</parameter> <!--Uncomment if you want to enable file caching for attachments --> <!--parameter name="cacheAttachments">true</parameter> diff --git a/modules/kernel/src/org/apache/axis2/Constants.java b/modules/kernel/src/org/apache/axis2/Constants.java index c02c50925d..011e30e2d2 100644 --- a/modules/kernel/src/org/apache/axis2/Constants.java +++ b/modules/kernel/src/org/apache/axis2/Constants.java @@ -333,6 +333,7 @@ public class Constants extends org.apache.axis2.namespace.Constants { public static interface Configuration { public static final String ENABLE_REST = "enableREST"; + public static final String ENABLE_JSON_ONLY = "enableJSONOnly"; public static final String ENABLE_HTTP_CONTENT_NEGOTIATION = "httpContentNegotiation"; public static final String ENABLE_REST_THROUGH_GET = "restThroughGet"; diff --git a/modules/kernel/src/org/apache/axis2/kernel/http/HTTPConstants.java b/modules/kernel/src/org/apache/axis2/kernel/http/HTTPConstants.java index b6cc840d31..9a11dc4677 100644 --- a/modules/kernel/src/org/apache/axis2/kernel/http/HTTPConstants.java +++ b/modules/kernel/src/org/apache/axis2/kernel/http/HTTPConstants.java @@ -37,6 +37,7 @@ public class HTTPConstants { public static final String MEDIA_TYPE_APPLICATION_XML = "application/xml"; public static final String MEDIA_TYPE_APPLICATION_SOAP_XML = "application/soap+xml"; public static final String MEDIA_TYPE_APPLICATION_ECHO_XML = "application/echo+xml"; + public static final String MEDIA_TYPE_APPLICATION_JSON = "application/json"; /** * Field REQUEST_URI diff --git a/modules/samples/json/resources/axis2.xml b/modules/samples/json/resources/axis2.xml index edf0707b06..e498db0c22 100644 --- a/modules/samples/json/resources/axis2.xml +++ b/modules/samples/json/resources/axis2.xml @@ -25,6 +25,7 @@ <parameter name="hotupdate">false</parameter> <parameter name="enableMTOM">false</parameter> <parameter name="enableSwA">false</parameter> + <parameter name="enableJSONOnly">false</parameter> <!--Uncomment if you want to enable file caching for attachments --> <!--parameter name="cacheAttachments">true</parameter> diff --git a/modules/transport/http/src/org/apache/axis2/transport/http/AxisServlet.java b/modules/transport/http/src/org/apache/axis2/transport/http/AxisServlet.java index 7780481eae..21e4fb9193 100644 --- a/modules/transport/http/src/org/apache/axis2/transport/http/AxisServlet.java +++ b/modules/transport/http/src/org/apache/axis2/transport/http/AxisServlet.java @@ -105,6 +105,7 @@ public class AxisServlet extends HttpServlet { protected transient String contextRoot = null; protected boolean disableREST = false; + protected boolean enableJSONOnly = false; private static final String LIST_SERVICES_SUFFIX = "/services/listServices"; private static final String LIST_FAULTY_SERVICES_SUFFIX = "/services/ListFaultyServices"; private boolean closeReader = true; @@ -153,7 +154,12 @@ public class AxisServlet extends HttpServlet { MessageContext msgContext; OutputStream out = response.getOutputStream(); String contentType = request.getContentType(); - if (!HTTPTransportUtils.isRESTRequest(contentType)) { + if (enableJSONOnly && (contentType == null || contentType.isEmpty() || !HTTPTransportUtils.isJSONRequest(contentType))) { + log.error("doPost() returning with no action taken, enableJSONOnly is true in the axis2.xml file and the content-type is not application/json: " + contentType); + response.setContentType("application/json"); + showJSONOnlyErrorMessage(response); + return; + } else if (!HTTPTransportUtils.isRESTRequest(contentType)) { msgContext = createMessageContext(request, response); msgContext.setProperty(Constants.Configuration.CONTENT_TYPE, contentType); try { @@ -354,6 +360,19 @@ public class AxisServlet extends HttpServlet { response.setStatus(HttpServletResponse.SC_ACCEPTED); } + /** + * Private method that deals with disabling of SOAP and REST support. + * + * @param response + * @throws IOException + */ + protected void showJSONOnlyErrorMessage(HttpServletResponse response) throws IOException { + response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); + PrintWriter writer = new PrintWriter(response.getOutputStream()); + writer.println("{ \"status\": \"error\",\"message\":\"content-type of application/json is mandatory\"}"); + writer.flush(); + } + /** * Close the builders. * diff --git a/modules/transport/http/src/org/apache/axis2/transport/http/HTTPTransportUtils.java b/modules/transport/http/src/org/apache/axis2/transport/http/HTTPTransportUtils.java index c6e8fca516..4ff74a10a4 100644 --- a/modules/transport/http/src/org/apache/axis2/transport/http/HTTPTransportUtils.java +++ b/modules/transport/http/src/org/apache/axis2/transport/http/HTTPTransportUtils.java @@ -325,6 +325,23 @@ public class HTTPTransportUtils { contentType.indexOf(HTTPConstants.MEDIA_TYPE_MULTIPART_FORM_DATA) > -1); } + /** + * This will match for content types that will be regarded as JSON + * This contains, + * 1. application/json + * <p/> + * If the request does not contain a content type, this will return false. + * + * @param contentType content type to check + * @return Boolean + */ + public static boolean isJSONRequest(String contentType) { + if (contentType == null || contentType.isEmpty()) { + return false; + } + return (contentType.toLowerCase().indexOf(HTTPConstants.MEDIA_TYPE_APPLICATION_JSON) != -1); + } + public static EndpointReference[] getEPRsForService(ConfigurationContext configurationContext, TransportInDescription trpInDesc, String serviceName, String ip, int port) throws AxisFault { diff --git a/modules/webapp/conf/axis2.xml b/modules/webapp/conf/axis2.xml index 1f07247b9f..8f156228f5 100644 --- a/modules/webapp/conf/axis2.xml +++ b/modules/webapp/conf/axis2.xml @@ -25,6 +25,7 @@ <parameter name="hotupdate">false</parameter> <parameter name="enableMTOM">false</parameter> <parameter name="enableSwA">false</parameter> + <parameter name="enableJSONOnly">false</parameter> <!--Uncomment if you want to enable file caching for attachments --> <!--parameter name="cacheAttachments">true</parameter>