Skip to content

Commit 6dd0b6b

Browse files
committed
fix #581 - improper serialization of RawStructuredQueryDefinition with a FileHandle contents
2 parents eb83289 + 1622494 commit 6dd0b6b

File tree

5 files changed

+61
-16
lines changed

5 files changed

+61
-16
lines changed

src/main/java/com/marklogic/client/impl/JerseyServices.java

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2115,7 +2115,7 @@ void init() {
21152115
addEncodedParam(params, "q", text);
21162116
}
21172117
}
2118-
if (queryDef instanceof StructuredQueryDefinition) {
2118+
if (queryDef instanceof StructuredQueryDefinition && ! (queryDef instanceof RawQueryDefinition)) {
21192119
structure = ((StructuredQueryDefinition) queryDef).serialize();
21202120

21212121
if (logger.isDebugEnabled()) {
@@ -2124,19 +2124,8 @@ void init() {
21242124
logger.debug("Searching for structure {}{}", structure, qtextMessage);
21252125
}
21262126

2127-
String payloadMimetype = "application/xml";
2128-
if (queryDef instanceof RawQueryDefinition) {
2129-
StructureWriteHandle handle = ((RawQueryDefinition) queryDef).getHandle();
2130-
baseHandle = HandleAccessor.checkHandle(handle, "search");
2131-
2132-
Format payloadFormat = getStructuredQueryFormat(baseHandle);
2133-
payloadMimetype = getMimetypeWithDefaultXML(payloadFormat, baseHandle);
2134-
}
2135-
21362127
webResource = getConnection().path("search").queryParams(params);
2137-
builder = (payloadMimetype != null) ?
2138-
webResource.type(payloadMimetype).accept(mimetype) :
2139-
webResource.accept(mimetype);
2128+
builder = webResource.type("application/xml").accept(mimetype);
21402129
} else if (queryDef instanceof RawQueryDefinition) {
21412130
if (logger.isDebugEnabled())
21422131
logger.debug("Raw search");
@@ -2220,7 +2209,7 @@ ClientResponse getResponse() {
22202209

22212210
if (queryDef instanceof KeyValueQueryDefinition) {
22222211
response = doGet(builder);
2223-
} else if (queryDef instanceof StructuredQueryDefinition) {
2212+
} else if (queryDef instanceof StructuredQueryDefinition && ! (queryDef instanceof RawQueryDefinition)) {
22242213
response = doPost(reqlog, builder, structure, true);
22252214
} else if (queryDef instanceof CombinedQueryDefinition) {
22262215
response = doPost(reqlog, builder, structure, true);
@@ -2431,7 +2420,7 @@ public <T> T values(Class<T> as, ValuesDefinition valDef, String mimetype,
24312420
addEncodedParam(docParams, "q", text);
24322421
}
24332422
}
2434-
if (queryDef instanceof StructuredQueryDefinition) {
2423+
if (queryDef instanceof StructuredQueryDefinition && ! (queryDef instanceof RawQueryDefinition)) {
24352424
String structure = ((StructuredQueryDefinition) queryDef)
24362425
.serialize();
24372426
if (structure != null) {

src/main/java/com/marklogic/client/impl/RawQueryDefinitionImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public RawStructuredQueryDefinition withHandle(StructureWriteHandle handle) {
6363
@Override
6464
public String serialize() {
6565
if (getHandle() == null) return "";
66-
return getHandle().toString();
66+
return HandleAccessor.contentAsString(getHandle());
6767
}
6868

6969
@Override

src/test/java/com/marklogic/client/test/RawQueryDefinitionTest.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
import com.marklogic.client.admin.QueryOptionsManager;
5050
import com.marklogic.client.document.JSONDocumentManager;
5151
import com.marklogic.client.document.XMLDocumentManager;
52+
import com.marklogic.client.io.DOMHandle;
5253
import com.marklogic.client.io.FileHandle;
5354
import com.marklogic.client.io.Format;
5455
import com.marklogic.client.io.JacksonHandle;
@@ -65,6 +66,7 @@
6566
import com.marklogic.client.query.QueryManager;
6667
import com.marklogic.client.query.RawCombinedQueryDefinition;
6768
import com.marklogic.client.query.RawQueryByExampleDefinition;
69+
import com.marklogic.client.query.RawStructuredQueryDefinition;
6870
import com.marklogic.client.query.StructuredQueryBuilder;
6971
import com.marklogic.client.query.StructuredQueryDefinition;
7072
import com.marklogic.client.query.Tuple;
@@ -547,6 +549,33 @@ public void testExtractDocumentData() throws Exception {
547549
}
548550
}
549551

552+
@Test
553+
public void test_issue581_RawStructuredQueryFromFileHandle() throws Exception {
554+
Common.client.newDocumentManager().write("test_issue581_RawStructuredQueryFromFileHandle.xml",
555+
new FileHandle(new File("src/test/resources/constraint5.xml")));
556+
557+
// get the combined query
558+
File file = new File("src/test/resources/combinedQueryOption.xml");
559+
560+
// create a handle for the search criteria
561+
FileHandle rawHandle = new FileHandle(file);
562+
563+
QueryManager queryMgr = Common.client.newQueryManager();
564+
565+
// create a search definition based on the handle
566+
RawStructuredQueryDefinition querydef = queryMgr.newRawStructuredQueryDefinition(rawHandle);
567+
568+
// create result handle
569+
DOMHandle resultsHandle = new DOMHandle();
570+
queryMgr.search(querydef, resultsHandle);
571+
572+
// get the result
573+
Document resultDoc = resultsHandle.get();
574+
575+
assertXpathEvaluatesTo("1", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc);
576+
assertXpathEvaluatesTo("0026", "string(//*[local-name()='result'][1]//*[local-name()='id'])", resultDoc);
577+
}
578+
550579
private static Document parseXml(String xml) throws Exception {
551580
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
552581
DocumentBuilder builder = factory.newDocumentBuilder();
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<search xmlns="http://marklogic.com/appservices/search">
2+
<query>
3+
<value-constraint-query>
4+
<constraint-name>id</constraint-name>
5+
<text>0026</text>
6+
</value-constraint-query>
7+
</query>
8+
<options>
9+
<return-metrics>false</return-metrics>
10+
<return-qtext>false</return-qtext>
11+
<debug>true</debug>
12+
<transform-results apply="raw"/>
13+
<constraint name="id">
14+
<value>
15+
<element ns="" name="id"/>
16+
</value>
17+
</constraint>
18+
</options>
19+
</search>

src/test/resources/constraint5.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<root>
2+
<title>The memex</title>
3+
<popularity>5</popularity>
4+
<id>0026</id>
5+
<date xmlns="http://purl.org/dc/elements/1.1/">2009-05-05</date>
6+
<price xmlns="http://cloudbank.com" amt="123.45"/>
7+
<p>The Memex, unfortunately, had no automated search feature.</p>
8+
</root>

0 commit comments

Comments
 (0)