AXIS2-6063 Add enableJSONOnly parameter to axis2.xml
diff --git a/modules/kernel/conf/axis2.xml b/modules/kernel/conf/axis2.xml
index d57b3ad..f31d6e6 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 c02c509..011e30e 100644
--- a/modules/kernel/src/org/apache/axis2/Constants.java
+++ b/modules/kernel/src/org/apache/axis2/Constants.java
@@ -333,6 +333,7 @@
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 b6cc840..9a11dc4 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 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 edf0707..e498db0 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 7780481..21e4fb9 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 @@
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 @@
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 {
@@ -355,6 +361,19 @@
}
/**
+ * 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.
*
* @param messageContext
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 c6e8fca..4ff74a1 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 @@
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 1f07247..8f15622 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>