Deal with max lenghts in search params

This commit is contained in:
James 2014-07-07 12:15:20 -04:00
parent 7f9cc38bde
commit 46828030db
6 changed files with 155 additions and 49 deletions

View File

@ -796,12 +796,12 @@ public class RestfulServer extends HttpServlet {
if (theContext.getNarrativeGenerator() != null) { if (theContext.getNarrativeGenerator() != null) {
String title = theContext.getNarrativeGenerator().generateTitle(next); String title = theContext.getNarrativeGenerator().generateTitle(next);
ourLog.info("Narrative generator created title: {}", title); ourLog.trace("Narrative generator created title: {}", title);
if (StringUtils.isNotBlank(title)) { if (StringUtils.isNotBlank(title)) {
ResourceMetadataKeyEnum.TITLE.put(next, title); ResourceMetadataKeyEnum.TITLE.put(next, title);
} }
} else { } else {
ourLog.info("No narrative generator specified"); ourLog.trace("No narrative generator specified");
} }
bundle.addResource(next, theContext, theServerBase); bundle.addResource(next, theContext, theServerBase);

View File

@ -469,7 +469,12 @@ public abstract class BaseFhirDao {
if (nextObject instanceof IPrimitiveDatatype<?>) { if (nextObject instanceof IPrimitiveDatatype<?>) {
IPrimitiveDatatype<?> nextValue = (IPrimitiveDatatype<?>) nextObject; IPrimitiveDatatype<?> nextValue = (IPrimitiveDatatype<?>) nextObject;
ResourceIndexedSearchParamString nextEntity = new ResourceIndexedSearchParamString(resourceName, normalizeString(nextValue.getValueAsString()), nextValue.getValueAsString()); String searchTerm = nextValue.getValueAsString();
if (searchTerm.length() > ResourceIndexedSearchParamString.MAX_LENGTH) {
searchTerm=searchTerm.substring(0, ResourceIndexedSearchParamString.MAX_LENGTH);
}
ResourceIndexedSearchParamString nextEntity = new ResourceIndexedSearchParamString(resourceName, normalizeString(searchTerm), searchTerm);
nextEntity.setResource(theEntity); nextEntity.setResource(theEntity);
retVal.add(nextEntity); retVal.add(nextEntity);
} else { } else {
@ -544,31 +549,32 @@ public abstract class BaseFhirDao {
} }
List<Object> values = t.getValues(theResource, nextPath); List<Object> values = t.getValues(theResource, nextPath);
List<String> systems = new ArrayList<String>();
List<String> codes = new ArrayList<String>();
for (Object nextObject : values) { for (Object nextObject : values) {
ResourceIndexedSearchParamToken nextEntity;
if (nextObject instanceof IdentifierDt) { if (nextObject instanceof IdentifierDt) {
IdentifierDt nextValue = (IdentifierDt) nextObject; IdentifierDt nextValue = (IdentifierDt) nextObject;
if (nextValue.isEmpty()) { if (nextValue.isEmpty()) {
continue; continue;
} }
nextEntity = new ResourceIndexedSearchParamToken(nextSpDef.getName(), nextValue.getSystem().getValueAsString(), nextValue.getValue().getValue()); systems.add( nextValue.getSystem().getValueAsString());
codes.add( nextValue.getValue().getValue());
} else if (nextObject instanceof IPrimitiveDatatype<?>) { } else if (nextObject instanceof IPrimitiveDatatype<?>) {
IPrimitiveDatatype<?> nextValue = (IPrimitiveDatatype<?>) nextObject; IPrimitiveDatatype<?> nextValue = (IPrimitiveDatatype<?>) nextObject;
if (nextValue.isEmpty()) { if (nextValue.isEmpty()) {
continue; continue;
} }
nextEntity = new ResourceIndexedSearchParamToken(nextSpDef.getName(), null, nextValue.getValueAsString()); systems.add(null);
codes .add (nextValue.getValueAsString());
} else if (nextObject instanceof CodeableConceptDt) { } else if (nextObject instanceof CodeableConceptDt) {
CodeableConceptDt nextCC = (CodeableConceptDt) nextObject; CodeableConceptDt nextCC = (CodeableConceptDt) nextObject;
for (CodingDt nextCoding : nextCC.getCoding()) { for (CodingDt nextCoding : nextCC.getCoding()) {
if (nextCoding.isEmpty()) { if (nextCoding.isEmpty()) {
continue; continue;
} }
nextEntity = new ResourceIndexedSearchParamToken(nextSpDef.getName(), nextCoding.getSystem().getValueAsString(), nextCoding.getCode().getValue()); systems.add(nextCoding.getSystem().getValueAsString());
nextEntity.setResource(theEntity); codes.add( nextCoding.getCode().getValue());
retVal.add(nextEntity);
} }
nextEntity = null;
} else { } else {
if (!multiType) { if (!multiType) {
throw new ConfigurationException("Search param " + nextSpDef.getName() + " is of unexpected datatype: " + nextObject.getClass()); throw new ConfigurationException("Search param " + nextSpDef.getName() + " is of unexpected datatype: " + nextObject.getClass());
@ -576,11 +582,27 @@ public abstract class BaseFhirDao {
continue; continue;
} }
} }
if (nextEntity != null) { }
assert systems.size() == codes.size();
for (int i = 0; i < systems.size(); i++) {
String system = systems.get(i);
String code = codes.get(i);
if (system != null && system.length() > ResourceIndexedSearchParamToken.MAX_LENGTH) {
system = system.substring(0, ResourceIndexedSearchParamToken.MAX_LENGTH);
}
if (code != null && code.length() > ResourceIndexedSearchParamToken.MAX_LENGTH) {
code = code.substring(0, ResourceIndexedSearchParamToken.MAX_LENGTH);
}
ResourceIndexedSearchParamToken nextEntity;
nextEntity = new ResourceIndexedSearchParamToken(nextSpDef.getName(), system, code);
nextEntity.setResource(theEntity); nextEntity.setResource(theEntity);
retVal.add(nextEntity); retVal.add(nextEntity);
} }
}
} }
theEntity.setParamsTokenPopulated(retVal.size() > 0); theEntity.setParamsTokenPopulated(retVal.size() > 0);

View File

@ -366,20 +366,25 @@ public class FhirResourceDao<T extends IResource> extends BaseFhirDao implements
for (IQueryParameterType nextOr : theOrParams) { for (IQueryParameterType nextOr : theOrParams) {
IQueryParameterType params = nextOr; IQueryParameterType params = nextOr;
String string; String rawSearchTerm;
if (params instanceof IPrimitiveDatatype<?>) { if (params instanceof IPrimitiveDatatype<?>) {
IPrimitiveDatatype<?> id = (IPrimitiveDatatype<?>) params; IPrimitiveDatatype<?> id = (IPrimitiveDatatype<?>) params;
string = id.getValueAsString(); rawSearchTerm = id.getValueAsString();
} else { } else {
throw new IllegalArgumentException("Invalid token type: " + params.getClass()); throw new IllegalArgumentException("Invalid token type: " + params.getClass());
} }
String likeExpression = normalizeString(string); if (rawSearchTerm.length() > ResourceIndexedSearchParamString.MAX_LENGTH) {
throw new InvalidRequestException("Parameter[" + theParamName + "] has length (" + rawSearchTerm.length() + ") that is longer than maximum allowed ("
+ ResourceIndexedSearchParamString.MAX_LENGTH + "): " + rawSearchTerm);
}
String likeExpression = normalizeString(rawSearchTerm);
likeExpression = likeExpression.replace("%", "[%]") + "%"; likeExpression = likeExpression.replace("%", "[%]") + "%";
Predicate singleCode = builder.like(from.get("myValueNormalized").as(String.class), likeExpression); Predicate singleCode = builder.like(from.get("myValueNormalized").as(String.class), likeExpression);
if (params instanceof StringParam && ((StringParam) params).isExact()) { if (params instanceof StringParam && ((StringParam) params).isExact()) {
Predicate exactCode = builder.equal(from.get("myValueExact"), string); Predicate exactCode = builder.equal(from.get("myValueExact"), rawSearchTerm);
singleCode = builder.and(singleCode, exactCode); singleCode = builder.and(singleCode, exactCode);
} }
codePredicates.add(singleCode); codePredicates.add(singleCode);
@ -428,6 +433,15 @@ public class FhirResourceDao<T extends IResource> extends BaseFhirDao implements
throw new IllegalArgumentException("Invalid token type: " + params.getClass()); throw new IllegalArgumentException("Invalid token type: " + params.getClass());
} }
if (system != null && system.length() > ResourceIndexedSearchParamToken.MAX_LENGTH) {
throw new InvalidRequestException("Parameter[" + theParamName + "] has system (" + system.length() + ") that is longer than maximum allowed ("
+ ResourceIndexedSearchParamToken.MAX_LENGTH + "): " + system);
}
if (code != null && code.length() > ResourceIndexedSearchParamToken.MAX_LENGTH) {
throw new InvalidRequestException("Parameter[" + theParamName + "] has code (" + code.length() + ") that is longer than maximum allowed (" + ResourceIndexedSearchParamToken.MAX_LENGTH
+ "): " + code);
}
ArrayList<Predicate> singleCodePredicates = (new ArrayList<Predicate>()); ArrayList<Predicate> singleCodePredicates = (new ArrayList<Predicate>());
if (StringUtils.isNotBlank(system)) { if (StringUtils.isNotBlank(system)) {
singleCodePredicates.add(builder.equal(from.get("mySystem"), system)); singleCodePredicates.add(builder.equal(from.get("mySystem"), system));
@ -485,8 +499,6 @@ public class FhirResourceDao<T extends IResource> extends BaseFhirDao implements
return found; return found;
} }
@Override @Override
public void addTag(IdDt theId, String theScheme, String theTerm, String theLabel) { public void addTag(IdDt theId, String theScheme, String theTerm, String theLabel) {
BaseHasResource entity = readEntity(theId); BaseHasResource entity = readEntity(theId);
@ -554,7 +566,8 @@ public class FhirResourceDao<T extends IResource> extends BaseFhirDao implements
final T current = currentTmp; final T current = currentTmp;
String querySring = "SELECT count(h) FROM ResourceHistoryTable h " + "WHERE h.myResourceId = :PID AND h.myResourceType = :RESTYPE" + " AND h.myUpdated < :END" + (theSince != null ? " AND h.myUpdated >= :SINCE" : ""); String querySring = "SELECT count(h) FROM ResourceHistoryTable h " + "WHERE h.myResourceId = :PID AND h.myResourceType = :RESTYPE" + " AND h.myUpdated < :END"
+ (theSince != null ? " AND h.myUpdated >= :SINCE" : "");
TypedQuery<Long> countQuery = myEntityManager.createQuery(querySring, Long.class); TypedQuery<Long> countQuery = myEntityManager.createQuery(querySring, Long.class);
countQuery.setParameter("PID", theId.getIdPartAsLong()); countQuery.setParameter("PID", theId.getIdPartAsLong());
countQuery.setParameter("RESTYPE", resourceType); countQuery.setParameter("RESTYPE", resourceType);
@ -592,8 +605,9 @@ public class FhirResourceDao<T extends IResource> extends BaseFhirDao implements
retVal.add(current); retVal.add(current);
} }
TypedQuery<ResourceHistoryTable> q = myEntityManager.createQuery("SELECT h FROM ResourceHistoryTable h WHERE h.myResourceId = :PID AND h.myResourceType = :RESTYPE AND h.myUpdated < :END " + (theSince != null ? " AND h.myUpdated >= :SINCE" : "") TypedQuery<ResourceHistoryTable> q = myEntityManager.createQuery(
+ " ORDER BY h.myUpdated ASC", ResourceHistoryTable.class); "SELECT h FROM ResourceHistoryTable h WHERE h.myResourceId = :PID AND h.myResourceType = :RESTYPE AND h.myUpdated < :END "
+ (theSince != null ? " AND h.myUpdated >= :SINCE" : "") + " ORDER BY h.myUpdated ASC", ResourceHistoryTable.class);
q.setParameter("PID", theId.getIdPartAsLong()); q.setParameter("PID", theId.getIdPartAsLong());
q.setParameter("RESTYPE", resourceType); q.setParameter("RESTYPE", resourceType);
q.setParameter("END", end.getValue(), TemporalType.TIMESTAMP); q.setParameter("END", end.getValue(), TemporalType.TIMESTAMP);
@ -639,7 +653,8 @@ public class FhirResourceDao<T extends IResource> extends BaseFhirDao implements
throw new ConfigurationException("Unknown search param on resource[" + myResourceName + "] for secondary key[" + mySecondaryPrimaryKeyParamName + "]"); throw new ConfigurationException("Unknown search param on resource[" + myResourceName + "] for secondary key[" + mySecondaryPrimaryKeyParamName + "]");
} }
if (sp.getParamType() != SearchParamTypeEnum.TOKEN) { if (sp.getParamType() != SearchParamTypeEnum.TOKEN) {
throw new ConfigurationException("Search param on resource[" + myResourceName + "] for secondary key[" + mySecondaryPrimaryKeyParamName + "] is not a token type, only token is supported"); throw new ConfigurationException("Search param on resource[" + myResourceName + "] for secondary key[" + mySecondaryPrimaryKeyParamName
+ "] is not a token type, only token is supported");
} }
} }
@ -670,7 +685,8 @@ public class FhirResourceDao<T extends IResource> extends BaseFhirDao implements
if (entity == null) { if (entity == null) {
if (theId.hasVersionIdPart()) { if (theId.hasVersionIdPart()) {
TypedQuery<ResourceHistoryTable> q = myEntityManager.createQuery("SELECT t from ResourceHistoryTable t WHERE t.myResourceId = :RID AND t.myResourceType = :RTYP AND t.myResourceVersion = :RVER", ResourceHistoryTable.class); TypedQuery<ResourceHistoryTable> q = myEntityManager.createQuery(
"SELECT t from ResourceHistoryTable t WHERE t.myResourceId = :RID AND t.myResourceType = :RTYP AND t.myResourceVersion = :RVER", ResourceHistoryTable.class);
q.setParameter("RID", theId.getIdPartAsLong()); q.setParameter("RID", theId.getIdPartAsLong());
q.setParameter("RTYP", myResourceName); q.setParameter("RTYP", myResourceName);
q.setParameter("RVER", theId.getVersionIdPartAsLong()); q.setParameter("RVER", theId.getVersionIdPartAsLong());
@ -946,8 +962,7 @@ public class FhirResourceDao<T extends IResource> extends BaseFhirDao implements
} }
/** /**
* If set, the given param will be treated as a secondary primary key, and multiple resources will not be able to * If set, the given param will be treated as a secondary primary key, and multiple resources will not be able to share the same value.
* share the same value.
*/ */
public void setSecondaryPrimaryKeyParamName(String theSecondaryPrimaryKeyParamName) { public void setSecondaryPrimaryKeyParamName(String theSecondaryPrimaryKeyParamName) {
mySecondaryPrimaryKeyParamName = theSecondaryPrimaryKeyParamName; mySecondaryPrimaryKeyParamName = theSecondaryPrimaryKeyParamName;

View File

@ -4,15 +4,19 @@ import javax.persistence.Column;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.Table; import javax.persistence.Table;
import org.apache.commons.lang3.StringUtils;
@Entity @Entity
@Table(name = "HFJ_SPIDX_STRING"/*, indexes= {@Index(name="IDX_SP_STRING", columnList="SP_VALUE_NORMALIZED")}*/) @Table(name = "HFJ_SPIDX_STRING"/*, indexes= {@Index(name="IDX_SP_STRING", columnList="SP_VALUE_NORMALIZED")}*/)
@org.hibernate.annotations.Table(appliesTo="HFJ_SPIDX_STRING",indexes= { @org.hibernate.annotations.Table(appliesTo="HFJ_SPIDX_STRING",indexes= {
@org.hibernate.annotations.Index(name="IDX_SP_STRING", columnNames= {"RES_TYPE", "SP_NAME", "SP_VALUE_NORMALIZED"})}) @org.hibernate.annotations.Index(name="IDX_SP_STRING", columnNames= {"RES_TYPE", "SP_NAME", "SP_VALUE_NORMALIZED"})})
public class ResourceIndexedSearchParamString extends BaseResourceIndexedSearchParam { public class ResourceIndexedSearchParamString extends BaseResourceIndexedSearchParam {
public static final int MAX_LENGTH = 100;
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
@Column(name = "SP_VALUE_NORMALIZED", length = 100, nullable = true) @Column(name = "SP_VALUE_NORMALIZED", length = MAX_LENGTH, nullable = true)
public String myValueNormalized; public String myValueNormalized;
@Column(name="SP_VALUE_EXACT",length=100,nullable=true) @Column(name="SP_VALUE_EXACT",length=100,nullable=true)
@ -32,6 +36,9 @@ public class ResourceIndexedSearchParamString extends BaseResourceIndexedSearchP
} }
public void setValueNormalized(String theValueNormalized) { public void setValueNormalized(String theValueNormalized) {
if (StringUtils.defaultString(theValueNormalized).length() > MAX_LENGTH) {
throw new IllegalArgumentException("Value is too long: " + theValueNormalized.length());
}
myValueNormalized = theValueNormalized; myValueNormalized = theValueNormalized;
} }
@ -40,6 +47,9 @@ public class ResourceIndexedSearchParamString extends BaseResourceIndexedSearchP
} }
public void setValueExact(String theValueExact) { public void setValueExact(String theValueExact) {
if (StringUtils.defaultString(theValueExact).length() > MAX_LENGTH) {
throw new IllegalArgumentException("Value is too long: " + theValueExact.length());
}
myValueExact = theValueExact; myValueExact = theValueExact;
} }

View File

@ -14,9 +14,11 @@ import org.apache.commons.lang3.StringUtils;
}) })
public class ResourceIndexedSearchParamToken extends BaseResourceIndexedSearchParam { public class ResourceIndexedSearchParamToken extends BaseResourceIndexedSearchParam {
public static final int MAX_LENGTH = 100;
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
@Column(name = "SP_SYSTEM", nullable = true, length = 100) @Column(name = "SP_SYSTEM", nullable = true, length = MAX_LENGTH)
public String mySystem; public String mySystem;
@Column(name = "SP_VALUE", nullable = true, length = 100) @Column(name = "SP_VALUE", nullable = true, length = 100)

View File

@ -10,6 +10,7 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import org.apache.commons.lang3.RandomStringUtils;
import org.hamcrest.core.StringContains; import org.hamcrest.core.StringContains;
import org.junit.AfterClass; import org.junit.AfterClass;
import org.junit.BeforeClass; import org.junit.BeforeClass;
@ -17,6 +18,7 @@ import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;
import ca.uhn.fhir.context.FhirContext; import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.jpa.entity.ResourceIndexedSearchParamString;
import ca.uhn.fhir.model.api.IQueryParameterType; import ca.uhn.fhir.model.api.IQueryParameterType;
import ca.uhn.fhir.model.api.IResource; import ca.uhn.fhir.model.api.IResource;
import ca.uhn.fhir.model.api.ResourceMetadataKeyEnum; import ca.uhn.fhir.model.api.ResourceMetadataKeyEnum;
@ -40,6 +42,7 @@ import ca.uhn.fhir.model.primitive.StringDt;
import ca.uhn.fhir.rest.api.MethodOutcome; import ca.uhn.fhir.rest.api.MethodOutcome;
import ca.uhn.fhir.rest.client.exceptions.FhirClientConnectionException; import ca.uhn.fhir.rest.client.exceptions.FhirClientConnectionException;
import ca.uhn.fhir.rest.gclient.IQuery; import ca.uhn.fhir.rest.gclient.IQuery;
import ca.uhn.fhir.rest.gclient.TokenParam;
import ca.uhn.fhir.rest.param.DateRangeParam; import ca.uhn.fhir.rest.param.DateRangeParam;
import ca.uhn.fhir.rest.param.IdentifierListParam; import ca.uhn.fhir.rest.param.IdentifierListParam;
import ca.uhn.fhir.rest.param.QualifiedDateParam; import ca.uhn.fhir.rest.param.QualifiedDateParam;
@ -103,8 +106,69 @@ public class FhirResourceDaoTest {
} }
@Test
public void testStringParamWhichIsTooLong() {
Organization org = new Organization();
String str = "testStringParamLong__lvdaoy843s89tll8gvs89l4s3gelrukveilufyebrew8r87bv4b77feli7fsl4lv3vb7rexloxe7olb48vov4o78ls7bvo7vb48o48l4bb7vbvx";
str = str + str;
org.getName().setValue(str);
assertThat(str.length(), greaterThan(ResourceIndexedSearchParamString.MAX_LENGTH));
Set<Long> val = ourOrganizationDao.searchForIds("name", new StringParam("P"));
int initial = val.size();
ourOrganizationDao.create(org);
val = ourOrganizationDao.searchForIds("name", new StringParam("P"));
assertEquals(initial + 0, val.size());
val = ourOrganizationDao.searchForIds("name", new StringParam(str.substring(0, ResourceIndexedSearchParamString.MAX_LENGTH)));
assertEquals(initial + 1, val.size());
try {
ourOrganizationDao.searchForIds("name", new StringParam(str.substring(0, ResourceIndexedSearchParamString.MAX_LENGTH + 1)));
fail();
} catch (InvalidRequestException e) {
// ok
}
}
@Test
public void testTokenParamWhichIsTooLong() {
String longStr1 = RandomStringUtils.randomAlphanumeric(ResourceIndexedSearchParamString.MAX_LENGTH + 100);
String longStr2 = RandomStringUtils.randomAlphanumeric(ResourceIndexedSearchParamString.MAX_LENGTH + 100);
Organization org = new Organization();
org.getName().setValue("testTokenParamWhichIsTooLong");
org.getType().addCoding().setSystem(longStr1).setCode(longStr2);
String subStr1 = longStr1.substring(0, ResourceIndexedSearchParamString.MAX_LENGTH);
String subStr2 = longStr2.substring(0, ResourceIndexedSearchParamString.MAX_LENGTH);
Set<Long> val = ourOrganizationDao.searchForIds("type", new IdentifierDt(subStr1, subStr2));
int initial = val.size();
ourOrganizationDao.create(org);
val = ourOrganizationDao.searchForIds("type", new IdentifierDt(subStr1, subStr2));
assertEquals(initial + 1, val.size());
try {
ourOrganizationDao.searchForIds("type", new IdentifierDt(longStr1, subStr2));
fail();
} catch (InvalidRequestException e) {
// ok
}
try {
ourOrganizationDao.searchForIds("type", new IdentifierDt(subStr1, longStr2));
fail();
} catch (InvalidRequestException e) {
// ok
}
}
@Test @Test
public void testStoreUnversionedResources() { public void testStoreUnversionedResources() {
@ -312,7 +376,6 @@ public class FhirResourceDaoTest {
} }
@Test @Test
public void testPersistSearchParamQuantity() { public void testPersistSearchParamQuantity() {
Observation obs = new Observation(); Observation obs = new Observation();
@ -329,8 +392,6 @@ public class FhirResourceDaoTest {
} }
@Test @Test
public void testPersistSearchParams() { public void testPersistSearchParams() {
Patient patient = new Patient(); Patient patient = new Patient();
@ -448,8 +509,6 @@ public class FhirResourceDaoTest {
} }
@Test @Test
public void testSearchByIdParam() { public void testSearchByIdParam() {
IdDt id1; IdDt id1;
@ -465,7 +524,6 @@ public class FhirResourceDaoTest {
id2 = ourOrganizationDao.create(patient).getId(); id2 = ourOrganizationDao.create(patient).getId();
} }
Map<String, IQueryParameterType> params = new HashMap<String, IQueryParameterType>(); Map<String, IQueryParameterType> params = new HashMap<String, IQueryParameterType>();
params.put("_id", new StringDt(id1.getIdPart())); params.put("_id", new StringDt(id1.getIdPart()));
assertEquals(1, toList(ourPatientDao.search(params)).size()); assertEquals(1, toList(ourPatientDao.search(params)).size());
@ -478,7 +536,6 @@ public class FhirResourceDaoTest {
} }
@Test @Test
public void testSearchResourceLinkWithChain() { public void testSearchResourceLinkWithChain() {
Patient patient = new Patient(); Patient patient = new Patient();