Fix some encoding issue for token params

This commit is contained in:
jamesagnew 2014-06-12 17:13:49 -04:00
parent 58bb97e6d0
commit ab3d656249
11 changed files with 248 additions and 20 deletions

View File

@ -49,6 +49,10 @@
Server methods with @Include parameter would sometimes fail when no _include was actually
specified in query strings.
</action>
<action type="fix">
Client requests for IdentifierDt types (such as Patient.identifier) did not create the correct
query string if the system is null.
</action>
</release>
</body>
</document>

View File

@ -415,7 +415,7 @@ public class IdentifierDt
if (org.apache.commons.lang3.StringUtils.isNotBlank(getSystem().getValueAsString())) {
return getSystem().getValueAsString() + '|' + getValue().getValueAsString();
} else {
return getValue().getValueAsString();
return '|' + getValue().getValueAsString();
}
}

View File

@ -32,7 +32,7 @@ class TokenCriterion implements ICriterion, ICriterionInternal {
if (StringUtils.isNotBlank(theSystem)) {
myValue = theSystem + "|" + StringUtils.defaultString(theCode);
} else {
myValue = StringUtils.defaultString(theCode);
myValue = "|" + StringUtils.defaultString(theCode);
}
}

View File

@ -20,6 +20,9 @@ package ca.uhn.fhir.rest.param;
* #L%
*/
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import ca.uhn.fhir.model.primitive.StringDt;
import ca.uhn.fhir.rest.server.Constants;
@ -62,6 +65,16 @@ public class StringParam extends StringDt {
return myExact;
}
@Override
public String toString() {
ToStringBuilder builder = new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE);
builder.append("value", getValue());
if (myExact) {
builder.append("exact", myExact);
}
return builder.toString();
}
public void setExact(boolean theExact) {
myExact = theExact;
}

View File

@ -0,0 +1,31 @@
package ca.uhn.fhir.model.primitive;
import static org.junit.Assert.*;
import org.junit.Test;
import ca.uhn.fhir.model.dstu.composite.IdentifierDt;
public class IdentifierDtTest {
@Test
public void testTokenWithPipeInValue() {
IdentifierDt dt = new IdentifierDt();
dt.setValueAsQueryToken(null, "a|b|c");
assertEquals("a", dt.getSystem().getValueAsString());
assertEquals("b|c", dt.getValue().getValue());
assertEquals("a|b|c", dt.getValueAsQueryToken());
}
@Test
public void testTokenWithPipeInValueAndNoSystem() {
IdentifierDt dt = new IdentifierDt();
dt.setValueAsQueryToken(null, "|b|c");
assertEquals("", dt.getSystem().getValueAsString());
assertEquals("b|c", dt.getValue().getValue());
assertEquals("|b|c", dt.getValueAsQueryToken());
}
}

View File

@ -0,0 +1,107 @@
package ca.uhn.fhir.rest.server;
import static org.junit.Assert.assertEquals;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.apache.commons.io.IOUtils;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.model.api.Bundle;
import ca.uhn.fhir.model.api.IResource;
import ca.uhn.fhir.model.dstu.resource.Patient;
import ca.uhn.fhir.model.primitive.IdDt;
import ca.uhn.fhir.rest.annotation.OptionalParam;
import ca.uhn.fhir.rest.annotation.RequiredParam;
import ca.uhn.fhir.rest.annotation.Search;
import ca.uhn.fhir.rest.param.StringParam;
import ca.uhn.fhir.testutil.RandomServerPortProvider;
/**
* Created by dsotnikov on 2/25/2014.
*/
public class SearchTest {
private static CloseableHttpClient ourClient;
private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(SearchTest.class);
private static int ourPort;
private static Server ourServer;
private static FhirContext ourCtx = new FhirContext();
@Test
public void testSearchById() throws Exception {
HttpGet httpGet = new HttpGet("http://localhost:" + ourPort + "/Patient?_id=aaa");
HttpResponse status = ourClient.execute(httpGet);
String responseContent = IOUtils.toString(status.getEntity().getContent());
assertEquals(200, status.getStatusLine().getStatusCode());
Bundle bundle = ourCtx.newXmlParser().parseBundle(responseContent);
assertEquals(1, bundle.getEntries().size());
Patient p = bundle.getResources(Patient.class).get(0);
assertEquals("idaaa", p.getNameFirstRep().getFamilyAsSingleString());
}
@AfterClass
public static void afterClass() throws Exception {
ourServer.stop();
}
@BeforeClass
public static void beforeClass() throws Exception {
ourPort = RandomServerPortProvider.findFreePort();
ourServer = new Server(ourPort);
DummyPatientResourceProvider patientProvider = new DummyPatientResourceProvider();
ServletHandler proxyHandler = new ServletHandler();
RestfulServer servlet = new RestfulServer();
servlet.setResourceProviders(patientProvider);
ServletHolder servletHolder = new ServletHolder(servlet);
proxyHandler.addServletWithMapping(servletHolder, "/*");
ourServer.setHandler(proxyHandler);
ourServer.start();
PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS);
HttpClientBuilder builder = HttpClientBuilder.create();
builder.setConnectionManager(connectionManager);
ourClient = builder.build();
}
/**
* Created by dsotnikov on 2/25/2014.
*/
public static class DummyPatientResourceProvider implements IResourceProvider {
@Search
public List<Patient> findPatient(@OptionalParam(name = "_id") StringParam theParam) {
ArrayList<Patient> retVal = new ArrayList<Patient>();
Patient patient = new Patient();
patient.setId("1");
patient.addName().addFamily("id"+theParam.getValue());
retVal.add(patient);
return retVal;
}
@Override
public Class<? extends IResource> getResourceType() {
return Patient.class;
}
}
}

View File

@ -626,7 +626,7 @@ public class FhirResourceDao<T extends IResource> extends BaseFhirDao implements
}
// Execute the query and make sure we return distinct results
List<T> retVal=new ArrayList<T>();
List<T> retVal = new ArrayList<T>();
loadResourcesByPid(loadPids, retVal);
// Load _include resources
@ -654,7 +654,7 @@ public class FhirResourceDao<T extends IResource> extends BaseFhirDao implements
}
}
}
if (!includePids.isEmpty()) {
ourLog.info("Loading {} included resources");
loadResourcesByPid(includePids, retVal);
@ -713,6 +713,36 @@ public class FhirResourceDao<T extends IResource> extends BaseFhirDao implements
for (Entry<String, List<List<IQueryParameterType>>> nextParamEntry : params.entrySet()) {
String nextParamName = nextParamEntry.getKey();
if (nextParamName.equals("_id")) {
if (nextParamEntry.getValue().isEmpty()) {
continue;
} else if (nextParamEntry.getValue().size() > 1) {
throw new InvalidRequestException("AND queries not supported for _id (Multiple instances of this param found)");
} else {
Set<Long> joinPids = new HashSet<Long>();
List<IQueryParameterType> nextValue = nextParamEntry.getValue().get(0);
if (nextValue == null || nextValue.size() == 0) {
continue;
} else {
for (IQueryParameterType next : nextValue) {
String value = next.getValueAsQueryToken();
long valueLong = Long.parseLong(value);
joinPids.add(valueLong);
}
if (joinPids.isEmpty()) {
continue;
}
}
if (pids.isEmpty()) {
pids.addAll(joinPids);
} else {
pids.retainAll(joinPids);
}
}
}
RuntimeSearchParam nextParamDef = resourceDef.getSearchParam(nextParamName);
if (nextParamDef != null) {
if (nextParamDef.getParamType() == SearchParamTypeEnum.TOKEN) {

View File

@ -6,6 +6,8 @@ import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.apache.commons.lang3.builder.ToStringBuilder;
import ca.uhn.fhir.model.api.IQueryParameterAnd;
import ca.uhn.fhir.model.api.IQueryParameterOr;
import ca.uhn.fhir.model.api.IQueryParameterType;
@ -16,9 +18,9 @@ public class SearchParameterMap extends HashMap<String, List<List<IQueryParamete
private static final long serialVersionUID = 1L;
private Set<Include> myIncludes;
public void add(String theName, IQueryParameterAnd theAnd) {
if (theAnd==null) {
if (theAnd == null) {
return;
}
if (!containsKey(theName)) {
@ -26,7 +28,7 @@ public class SearchParameterMap extends HashMap<String, List<List<IQueryParamete
}
for (IQueryParameterOr next : theAnd.getValuesAsQueryTokens()) {
if (next==null) {
if (next == null) {
continue;
}
get(theName).add(next.getValuesAsQueryTokens());
@ -46,8 +48,8 @@ public class SearchParameterMap extends HashMap<String, List<List<IQueryParamete
}
public Set<Include> getIncludes() {
if (myIncludes==null) {
myIncludes=new HashSet<Include>();
if (myIncludes == null) {
myIncludes = new HashSet<Include>();
}
return myIncludes;
}
@ -59,7 +61,17 @@ public class SearchParameterMap extends HashMap<String, List<List<IQueryParamete
public void addInclude(Include theInclude) {
getIncludes().add(theInclude);
}
@Override
public String toString() {
ToStringBuilder b = new ToStringBuilder(this);
if (isEmpty() == false) {
b.append("params", super.toString());
}
if (getIncludes().isEmpty() == false) {
b.append("includes", getIncludes());
}
return b.toString();
}
}

View File

@ -1,14 +1,12 @@
package ca.uhn.fhir.jpa.provider;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import org.springframework.beans.factory.annotation.Required;
import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.jpa.dao.IFhirResourceDao;
import ca.uhn.fhir.model.api.IQueryParameterType;
import ca.uhn.fhir.model.api.IResource;
import ca.uhn.fhir.model.api.TagList;
import ca.uhn.fhir.model.primitive.IdDt;
@ -19,7 +17,6 @@ import ca.uhn.fhir.rest.annotation.History;
import ca.uhn.fhir.rest.annotation.IdParam;
import ca.uhn.fhir.rest.annotation.Read;
import ca.uhn.fhir.rest.annotation.ResourceParam;
import ca.uhn.fhir.rest.annotation.Search;
import ca.uhn.fhir.rest.annotation.Since;
import ca.uhn.fhir.rest.annotation.Update;
import ca.uhn.fhir.rest.api.MethodOutcome;
@ -86,11 +83,6 @@ public class JpaResourceProvider<T extends IResource> implements IResourceProvid
return myDao.read(theId);
}
@Search
public List<T> search() {
return myDao.search(new HashMap<String, IQueryParameterType>());
}
@Required
public void setDao(IFhirResourceDao<T> theDao) {
myDao = theDao;

View File

@ -57,7 +57,7 @@ public class FhirResourceDaoTest {
private static IFhirResourceDao<Encounter> ourEncounterDao;
@Test
public void testPersistAndReadResource() {
public void testIdParam() {
Patient patient = new Patient();
patient.addIdentifier("urn:system", "001");
patient.addName().addFamily("Tester").addGiven("Joe");
@ -68,11 +68,48 @@ public class FhirResourceDaoTest {
Date now = new Date();
{
Patient retrieved = ourPatientDao.read(outcome.getId());
InstantDt published = (InstantDt) retrieved.getResourceMetadata().get(ResourceMetadataKeyEnum.PUBLISHED);
InstantDt updated = (InstantDt) retrieved.getResourceMetadata().get(ResourceMetadataKeyEnum.UPDATED);
assertTrue(published.before(now));
assertTrue(updated.before(now));
}
// Now search by _id
{
SearchParameterMap paramMap = new SearchParameterMap();
paramMap.add("_id", new StringParam(outcome.getId().getUnqualifiedId()));
List<Patient> ret = ourPatientDao.search(paramMap);
assertEquals(1,ret.size());
Patient p = ret.get(0);
assertEquals("Tester", p.getNameFirstRep().getFamilyAsSingleString());
}
{
SearchParameterMap paramMap = new SearchParameterMap();
paramMap.add("_id", new StringParam(outcome.getId().getUnqualifiedId()));
paramMap.add(Patient.SP_NAME, new StringParam("tester"));
List<Patient> ret = ourPatientDao.search(paramMap);
assertEquals(1,ret.size());
Patient p = ret.get(0);
assertEquals("Tester", p.getNameFirstRep().getFamilyAsSingleString());
}
{
SearchParameterMap paramMap = new SearchParameterMap();
paramMap.add(Patient.SP_NAME, new StringParam("tester"));
paramMap.add("_id", new StringParam(outcome.getId().getUnqualifiedId()));
List<Patient> ret = ourPatientDao.search(paramMap);
assertEquals(1,ret.size());
Patient p = ret.get(0);
assertEquals("Tester", p.getNameFirstRep().getFamilyAsSingleString());
}
{
SearchParameterMap paramMap = new SearchParameterMap();
paramMap.add(Patient.SP_NAME, new StringParam("tester"));
paramMap.add("_id", new StringParam("000"));
List<Patient> ret = ourPatientDao.search(paramMap);
assertEquals(0,ret.size());
}
}
@Test

View File

@ -65,6 +65,8 @@ public class ${className}ResourceProvider extends JpaResourceProvider<${classNam
#end
paramMap.setIncludes(theIncludes);
return getDao().search(paramMap);
}