Skip to content

Commit ee78f37

Browse files
committed
[feature] Serialize preserved XML Declaration by default from REST API
1 parent 97f1b81 commit ee78f37

File tree

1 file changed

+17
-10
lines changed

1 file changed

+17
-10
lines changed

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

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,6 @@ public class RESTServer {
120120

121121
static {
122122
defaultProperties.setProperty(OutputKeys.INDENT, "yes");
123-
defaultProperties.setProperty(OutputKeys.ENCODING, UTF_8.name());
124123
defaultProperties.setProperty(OutputKeys.MEDIA_TYPE, MimeType.XML_TYPE.getName());
125124
defaultProperties.setProperty(EXistOutputKeys.EXPAND_XINCLUDES, "yes");
126125
defaultProperties.setProperty(EXistOutputKeys.HIGHLIGHT_MATCHES, "elements");
@@ -129,8 +128,9 @@ public class RESTServer {
129128
public final static Properties defaultOutputKeysProperties = new Properties();
130129

131130
static {
131+
defaultOutputKeysProperties.setProperty(EXistOutputKeys.OMIT_ORIGINAL_XML_DECLARATION, "no");
132+
defaultOutputKeysProperties.setProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
132133
defaultOutputKeysProperties.setProperty(OutputKeys.INDENT, "yes");
133-
defaultOutputKeysProperties.setProperty(OutputKeys.ENCODING, UTF_8.name());
134134
defaultOutputKeysProperties.setProperty(OutputKeys.MEDIA_TYPE,
135135
MimeType.XML_TYPE.getName());
136136
}
@@ -141,6 +141,9 @@ public class RESTServer {
141141
+ "h1 { color: #C0C0C0; }" + ".path {" + " padding-bottom: 10px;"
142142
+ "}" + ".high { " + " color: #666699; " + " font-weight: bold;"
143143
+ "}" + "</style>" + "</head>" + "<body>" + "<h1>XQuery Error</h1>";
144+
145+
private static final String DEFAULT_ENCODING = UTF_8.name();
146+
144147
private final String formEncoding; // TODO: we may be able to remove this
145148
// eventually, in favour of
146149
// HttpServletRequestWrapper being setup in
@@ -315,7 +318,7 @@ public void doGet(final DBBroker broker, final Txn transaction, final HttpServle
315318
}
316319
} catch (final SAXException e) {
317320
final XPathException x = new XPathException(variables != null ? variables.getExpression() : null, e.toString());
318-
writeXPathException(response, HttpServletResponse.SC_BAD_REQUEST, UTF_8.name(), query, path, x);
321+
writeXPathException(response, HttpServletResponse.SC_BAD_REQUEST, DEFAULT_ENCODING, query, path, x);
319322
}
320323

321324
if ((option = getParameter(request, HowMany)) != null) {
@@ -381,7 +384,7 @@ public void doGet(final DBBroker broker, final Txn transaction, final HttpServle
381384
if ((encoding = getParameter(request, Encoding)) != null) {
382385
outputProperties.setProperty(OutputKeys.ENCODING, encoding);
383386
} else {
384-
encoding = UTF_8.name();
387+
encoding = DEFAULT_ENCODING;
385388
}
386389

387390
final String mimeType = outputProperties.getProperty(OutputKeys.MEDIA_TYPE);
@@ -567,7 +570,7 @@ public void doHead(final DBBroker broker, final Txn transaction, final HttpServl
567570
if ((encoding = getParameter(request, Encoding)) != null) {
568571
outputProperties.setProperty(OutputKeys.ENCODING, encoding);
569572
} else {
570-
encoding = UTF_8.name();
573+
encoding = DEFAULT_ENCODING;
571574
}
572575

573576
try(final LockedDocument lockedDocument = broker.getXMLResource(pathUri, LockMode.READ_LOCK)) {
@@ -638,7 +641,7 @@ public void doPost(final DBBroker broker, final Txn transaction, final HttpServl
638641
LockedDocument lockedDocument = null;
639642
DocumentImpl resource = null;
640643

641-
final String encoding = outputProperties.getProperty(OutputKeys.ENCODING);
644+
final String encoding = getEncoding(outputProperties);
642645
String mimeType = outputProperties.getProperty(OutputKeys.MEDIA_TYPE);
643646
try {
644647
// check if path leads to an XQuery resource.
@@ -1244,7 +1247,7 @@ private boolean checkForXQueryTarget(final DBBroker broker, final Txn transactio
12441247
executeXQuery(broker, transaction, resource, request, response,
12451248
outputProperties, servletPath.toString(), pathInfo);
12461249
} catch (final XPathException e) {
1247-
writeXPathExceptionHtml(response, HttpServletResponse.SC_BAD_REQUEST, UTF_8.name(), null, path.toString(), e);
1250+
writeXPathExceptionHtml(response, HttpServletResponse.SC_BAD_REQUEST, DEFAULT_ENCODING, null, path.toString(), e);
12481251
} finally {
12491252
lockedDocument.close();
12501253
}
@@ -1255,7 +1258,7 @@ private String getRequestContent(final HttpServletRequest request) throws IOExce
12551258

12561259
String encoding = request.getCharacterEncoding();
12571260
if (encoding == null) {
1258-
encoding = UTF_8.name();
1261+
encoding = DEFAULT_ENCODING;
12591262
}
12601263

12611264
final InputStream is = request.getInputStream();
@@ -2119,6 +2122,10 @@ protected void writeResults(final HttpServletResponse response, final DBBroker b
21192122

21202123
}
21212124

2125+
private static String getEncoding(final Properties outputProperties) {
2126+
return outputProperties.getProperty(OutputKeys.ENCODING, DEFAULT_ENCODING);
2127+
}
2128+
21222129
private void writeResultXML(final HttpServletResponse response,
21232130
final DBBroker broker, final Sequence results, final int howmany,
21242131
final int start, final boolean typed, final Properties outputProperties,
@@ -2129,7 +2136,7 @@ private void writeResultXML(final HttpServletResponse response,
21292136
try {
21302137

21312138
// set output headers
2132-
final String encoding = outputProperties.getProperty(OutputKeys.ENCODING);
2139+
final String encoding = getEncoding(outputProperties);
21332140
if (!response.containsHeader("Content-Type")) {
21342141
String mimeType = outputProperties.getProperty(OutputKeys.MEDIA_TYPE);
21352142
if (mimeType != null) {
@@ -2189,7 +2196,7 @@ private void writeResultJSON(final HttpServletResponse response,
21892196
outputProperties.setProperty(Serializer.GENERATE_DOC_EVENTS, "false");
21902197
try {
21912198
serializer.setProperties(outputProperties);
2192-
try (Writer writer = new OutputStreamWriter(response.getOutputStream(), outputProperties.getProperty(OutputKeys.ENCODING))) {
2199+
try (Writer writer = new OutputStreamWriter(response.getOutputStream(), getEncoding(outputProperties))) {
21932200
final JSONObject root = new JSONObject();
21942201
root.addObject(new JSONSimpleProperty("start", Integer.toString(start), true));
21952202
root.addObject(new JSONSimpleProperty("count", Integer.toString(howmany), true));

0 commit comments

Comments
 (0)