Skip to content

Commit 8ffd3f9

Browse files
committed
[feature] Add the Query String parmeter '_omit-xml-declaration' and '_omit-original-xml-declaration' to control the output:omit-xml-declaration and exist:omit-original-xml-declaration serialization option via HTTP GET to the REST Server
1 parent ee78f37 commit 8ffd3f9

File tree

7 files changed

+160
-3
lines changed

7 files changed

+160
-3
lines changed

exist-core/src/main/java/org/exist/http/RESTServer.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,22 @@ public void doGet(final DBBroker broker, final Txn transaction, final HttpServle
360360
final String outputDocType = broker.getConfiguration().getProperty(Serializer.PROPERTY_OUTPUT_DOCTYPE, "yes");
361361
outputProperties.setProperty(EXistOutputKeys.OUTPUT_DOCTYPE, outputDocType);
362362
}
363+
if ((option = getParameter(request, Omit_Xml_Declaration)) != null) {
364+
// take user query-string specified omit-xml-declaration setting
365+
outputProperties.setProperty(OutputKeys.OMIT_XML_DECLARATION, option);
366+
} else {
367+
// set omit-xml-declaration by configuration
368+
final String omitXmlDeclaration = broker.getConfiguration().getProperty(Serializer.PROPERTY_OMIT_XML_DECLARATION, "yes");
369+
outputProperties.setProperty(OutputKeys.OMIT_XML_DECLARATION, omitXmlDeclaration);
370+
}
371+
if ((option = getParameter(request, Omit_Original_Xml_Declaration)) != null) {
372+
// take user query-string specified omit-original-xml-declaration setting
373+
outputProperties.setProperty(EXistOutputKeys.OMIT_ORIGINAL_XML_DECLARATION, option);
374+
} else {
375+
// set omit-original-xml-declaration by configuration
376+
final String omitOriginalXmlDeclaration = broker.getConfiguration().getProperty(Serializer.PROPERTY_OMIT_ORIGINAL_XML_DECLARATION, "yes");
377+
outputProperties.setProperty(EXistOutputKeys.OMIT_ORIGINAL_XML_DECLARATION, omitOriginalXmlDeclaration);
378+
}
363379
if ((option = getParameter(request, Source)) != null && !safeMode) {
364380
source = "yes".equals(option);
365381
}

exist-core/src/main/java/org/exist/http/RESTServerParameter.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,29 @@ enum RESTServerParameter {
357357
*
358358
* The value of the parameter should be either "yes" or "no".
359359
*/
360-
Output_Doctype;
360+
Output_Doctype,
361+
362+
/**
363+
* Can be used in the Query String of a GET request
364+
* to indicate that the XML Declaration of an XML document should not
365+
* be serialized if present.
366+
*
367+
* Contexts: GET
368+
*
369+
* The value of the parameter should be either "yes" or "no".
370+
*/
371+
Omit_Xml_Declaration,
372+
373+
/**
374+
* Can be used in the Query String of a GET request
375+
* to indicate that the original persisted XML Declaration of an XML document should not
376+
* be serialized if present.
377+
*
378+
* Contexts: GET
379+
*
380+
* The value of the parameter should be either "yes" or "no".
381+
*/
382+
Omit_Original_Xml_Declaration;
361383

362384
/**
363385
* Get the parameter key that is

exist-core/src/main/java/org/exist/storage/serializers/Serializer.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,10 @@ public abstract class Serializer implements XMLReader {
107107
protected static final Logger LOG = LogManager.getLogger(Serializer.class);
108108

109109
public static final String CONFIGURATION_ELEMENT_NAME = "serializer";
110+
public static final String OMIT_XML_DECLARATION_ATTRIBUTE = "omit-xml-declaration";
111+
public static final String PROPERTY_OMIT_XML_DECLARATION = "serialization.omit-xml-declaration";
112+
public static final String OMIT_ORIGINAL_XML_DECLARATION_ATTRIBUTE = "omit-original-xml-declaration";
113+
public static final String PROPERTY_OMIT_ORIGINAL_XML_DECLARATION = "serialization.omit-original-xml-declaration";
110114
public static final String OUTPUT_DOCTYPE_ATTRIBUTE = "output-doctype";
111115
public static final String PROPERTY_OUTPUT_DOCTYPE = "serialization.output-doctype";
112116
public static final String ENABLE_XINCLUDE_ATTRIBUTE = "enable-xinclude";

exist-core/src/main/java/org/exist/util/Configuration.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -652,6 +652,18 @@ private void configureHtmlToXmlParser(final Element parser) {
652652
*/
653653
private void configureSerializer( Element serializer )
654654
{
655+
final String omitXmlDeclaration = getConfigAttributeValue( serializer, Serializer.OMIT_XML_DECLARATION_ATTRIBUTE );
656+
if (omitXmlDeclaration != null) {
657+
config.put(Serializer.PROPERTY_OMIT_XML_DECLARATION, omitXmlDeclaration);
658+
LOG.debug(Serializer.PROPERTY_OMIT_XML_DECLARATION + ": {}", config.get(Serializer.PROPERTY_OMIT_XML_DECLARATION));
659+
}
660+
661+
final String omitOriginalXmlDeclaration = getConfigAttributeValue( serializer, Serializer.OMIT_ORIGINAL_XML_DECLARATION_ATTRIBUTE );
662+
if (omitOriginalXmlDeclaration != null) {
663+
config.put(Serializer.PROPERTY_OMIT_ORIGINAL_XML_DECLARATION, omitOriginalXmlDeclaration);
664+
LOG.debug(Serializer.PROPERTY_OMIT_ORIGINAL_XML_DECLARATION + ": {}", config.get(Serializer.PROPERTY_OMIT_ORIGINAL_XML_DECLARATION));
665+
}
666+
655667
final String outputDocType = getConfigAttributeValue( serializer, Serializer.OUTPUT_DOCTYPE_ATTRIBUTE );
656668
if (outputDocType != null) {
657669
config.put(Serializer.PROPERTY_OUTPUT_DOCTYPE, outputDocType);

exist-core/src/test/java/org/exist/http/RESTServiceTest.java

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,12 @@ public class RESTServiceTest {
203203

204204
private static final XmldbURI TEST_XML_DOC_WITH_XSLPI_URI = XmldbURI.create("test-with-xslpi.xml");
205205

206+
private static final String XML_WITH_XMLDECL =
207+
"<?xml version=\"1.1\" encoding=\"ISO-8859-1\" standalone=\"yes\"?>\n" +
208+
"<bookmap id=\"bookmap-2\"/>";
209+
210+
private static final XmldbURI TEST_XMLDECL_COLLECTION_URI = XmldbURI.ROOT_COLLECTION_URI.append("rest-test-xmldecl");
211+
private static final XmldbURI TEST_XML_DOC_WITH_XMLDECL_URI = XmldbURI.create("test-with-xmldecl.xml");
206212

207213
private static String credentials;
208214
private static String badCredentials;
@@ -231,6 +237,10 @@ private static String getResourceWithDocTypeUri() {
231237
return getServerUri() + TEST_DOCTYPE_COLLECTION_URI.append(TEST_XML_DOC_WITH_DOCTYPE_URI);
232238
}
233239

240+
private static String getResourceWithXmlDeclUri() {
241+
return getServerUri() + TEST_XMLDECL_COLLECTION_URI.append(TEST_XML_DOC_WITH_XMLDECL_URI);
242+
}
243+
234244
/* About path components of URIs:
235245
236246
** reserved characters # http://tools.ietf.org/html/rfc3986#section-2.2
@@ -319,6 +329,11 @@ public static void setup() throws PermissionDeniedException, IOException, Trigge
319329
broker.saveCollection(transaction, col);
320330
}
321331

332+
try (final Collection col = broker.getOrCreateCollection(transaction, TEST_XMLDECL_COLLECTION_URI)) {
333+
broker.storeDocument(transaction, TEST_XML_DOC_WITH_XMLDECL_URI, new StringInputSource(XML_WITH_XMLDECL), MimeType.XML_TYPE, col);
334+
broker.saveCollection(transaction, col);
335+
}
336+
322337
transaction.commit();
323338
} catch (EXistException | SAXException | LockException e) {
324339
throw new RuntimeException(e);
@@ -1386,6 +1401,82 @@ public void getDocWithXslPi_twice() throws IOException {
13861401
getDocWithXslPi();
13871402
}
13881403

1404+
@Test
1405+
public void getXmlDeclDefault() throws IOException {
1406+
final HttpURLConnection connect = getConnection(getResourceWithXmlDeclUri());
1407+
try {
1408+
connect.setRequestMethod("GET");
1409+
connect.connect();
1410+
1411+
final int r = connect.getResponseCode();
1412+
assertEquals("Server returned response code " + r, HttpStatus.OK_200, r);
1413+
String contentType = connect.getContentType();
1414+
final int semicolon = contentType.indexOf(';');
1415+
if (semicolon > 0) {
1416+
contentType = contentType.substring(0, semicolon).trim();
1417+
}
1418+
assertEquals("Server returned content type " + contentType, "application/xml", contentType);
1419+
1420+
final String response = readResponse(connect.getInputStream());
1421+
1422+
assertEquals("<bookmap id=\"bookmap-2\"/>\r\n", response);
1423+
1424+
} finally {
1425+
connect.disconnect();
1426+
}
1427+
}
1428+
1429+
@Test
1430+
public void getXmlDeclNo() throws IOException {
1431+
final HttpURLConnection connect = getConnection(getResourceWithXmlDeclUri() + "?_omit-xml-declaration=no&_omit-original-xml-declaration=no");
1432+
try {
1433+
connect.setRequestMethod("GET");
1434+
connect.connect();
1435+
1436+
final int r = connect.getResponseCode();
1437+
assertEquals("Server returned response code " + r, HttpStatus.OK_200, r);
1438+
String contentType = connect.getContentType();
1439+
final int semicolon = contentType.indexOf(';');
1440+
if (semicolon > 0) {
1441+
contentType = contentType.substring(0, semicolon).trim();
1442+
}
1443+
assertEquals("Server returned content type " + contentType, "application/xml", contentType);
1444+
1445+
final String response = readResponse(connect.getInputStream());
1446+
1447+
assertEquals("<?xml version=\"1.1\" encoding=\"ISO-8859-1\" standalone=\"yes\"?>\r\n" +
1448+
"<bookmap id=\"bookmap-2\"/>\r\n", response);
1449+
1450+
} finally {
1451+
connect.disconnect();
1452+
}
1453+
}
1454+
1455+
@Test
1456+
public void getXmlDeclYes() throws IOException {
1457+
final HttpURLConnection connect = getConnection(getResourceWithXmlDeclUri() + "?_omit-xml-declaration=yes&_omit-original-xml-declaration=yes");
1458+
try {
1459+
connect.setRequestMethod("GET");
1460+
connect.connect();
1461+
1462+
final int r = connect.getResponseCode();
1463+
assertEquals("Server returned response code " + r, HttpStatus.OK_200, r);
1464+
String contentType = connect.getContentType();
1465+
final int semicolon = contentType.indexOf(';');
1466+
if (semicolon > 0) {
1467+
contentType = contentType.substring(0, semicolon).trim();
1468+
}
1469+
assertEquals("Server returned content type " + contentType, "application/xml", contentType);
1470+
1471+
final String response = readResponse(connect.getInputStream());
1472+
1473+
assertEquals("<bookmap id=\"bookmap-2\"/>\r\n", response);
1474+
1475+
} finally {
1476+
connect.disconnect();
1477+
}
1478+
}
1479+
13891480
private void chmod(final String resourcePath, final String mode) throws IOException {
13901481
final String uri = getCollectionUri() +"?_query=" + URLEncoder.encode(
13911482
"sm:chmod(xs:anyURI('" + resourcePath + "'), '" + mode + "')",

exist-distribution/src/main/config/conf.xml

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -787,6 +787,14 @@
787787
service.setProperty("compress-output", "yes");
788788
to uncompress the retrieved result in the client too.
789789
790+
- omit-xml-declaration:
791+
should the XML Declaration for a document be serialized
792+
if it is present?
793+
794+
- omit-original-xml-declaration:
795+
should the original persisted XML Declaration for a document be serialized
796+
if it is present? Requires omit-xml-declaration to also be set tp 'no'.
797+
790798
- output-doctype:
791799
should the Doctype Declaration for a document be serialized
792800
if it is present?
@@ -815,8 +823,10 @@
815823
Set the parameter to "yes" to enable this feature.
816824
817825
-->
818-
<serializer add-exist-id="none" compress-output="no" output-doctype="yes" enable-xinclude="yes"
819-
enable-xsl="no" indent="yes" match-tagging-attributes="no"
826+
<serializer add-exist-id="none" compress-output="no"
827+
omit-xml-declaration="yes" omit-original-xml-declaration="yes"
828+
output-doctype="yes" enable-xinclude="yes"
829+
enable-xsl="no" indent="yes" match-tagging-attributes="no"
820830
match-tagging-elements="no">
821831
<!--
822832
You may add as many custom-filters as you want, they will be executed

schema/conf.xsd

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,8 @@
302302
</xs:simpleType>
303303
</xs:attribute>
304304
<xs:attribute name="compress-output" type="yes_no" default="no"/>
305+
<xs:attribute name="omit-xml-declaration" type="yes_no" default="yes"/>
306+
<xs:attribute name="omit-original-xml-declaration" type="yes_no" default="yes"/>
305307
<xs:attribute name="output-doctype" type="yes_no" default="yes"/>
306308
<xs:attribute name="enable-xinclude" type="yes_no" default="yes"/>
307309
<xs:attribute name="enable-xsl" type="yes_no" default="no"/>

0 commit comments

Comments
 (0)