Remove dependency on servlet-api in client

This commit is contained in:
James Agnew 2015-06-12 10:02:52 -04:00
parent 6077114b32
commit 9b280e0ab7
4 changed files with 32 additions and 41 deletions

View File

@ -1,32 +0,0 @@
package ca.uhn.fhir.rest.client;
import static org.junit.Assert.fail;
import org.junit.Test;
import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.model.api.Bundle;
import ca.uhn.fhir.model.dstu.resource.Patient;
import ca.uhn.fhir.rest.client.exceptions.FhirClientConnectionException;
import ca.uhn.fhir.rest.gclient.ITransactionTyped;
public class ClientTest {
private static final FhirContext ctx = FhirContext.forDstu1();
@Test
public void testTransaction() {
Bundle bundle = new Bundle();
bundle.addEntry().setResource(new Patient().setId("Patient/unit_test_patient"));
IGenericClient client = ctx.newRestfulGenericClient("http://fhirtest.uhn.ca/baseDstu1");
ITransactionTyped<Bundle> transaction = client.transaction().withBundle(bundle);
try {
Bundle result = transaction.encodedJson().execute();
fail();
} catch (FhirClientConnectionException e) {
// good
}
}
}

View File

@ -267,18 +267,26 @@ public abstract class BaseMethodBinding<T> implements IClientResponseHandler<T>
} }
protected byte[] loadRequestContents(RequestDetails theRequest) throws IOException { protected byte[] loadRequestContents(RequestDetails theRequest) throws IOException {
/*
* This is weird, but this class is used both in clients and in servers, and
* we want to avoid needing to depend on servlet-api in clients since there is
* no point. So we dynamically load a class that does the servlet processing in
* servers. Down the road it may make sense to just split the method binding
* classes into server and client versions, but this isn't actually a huge deal
* I don't think.
*/
IRequestReader reader = ourRequestReader; IRequestReader reader = ourRequestReader;
if (reader == null) { if (reader == null) {
try { try {
Class.forName("javax.servlet.ServletInputStream"); Class.forName("javax.servlet.ServletInputStream");
String className = BaseMethodBinding.class.getName() + "." + "ActiveRequestReader"; String className = BaseMethodBinding.class.getName() + "$" + "ActiveRequestReader";
try { try {
reader = (IRequestReader) Class.forName(className).newInstance(); reader = (IRequestReader) Class.forName(className).newInstance();
} catch (Exception e1) { } catch (Exception e1) {
throw new ConfigurationException("Failed to instantiate class " + className, e1); throw new ConfigurationException("Failed to instantiate class " + className, e1);
} }
} catch (ClassNotFoundException e) { } catch (ClassNotFoundException e) {
String className = BaseMethodBinding.class.getName() + "." + "InactiveRequestReader"; String className = BaseMethodBinding.class.getName() + "$" + "InactiveRequestReader";
try { try {
reader = (IRequestReader) Class.forName(className).newInstance(); reader = (IRequestReader) Class.forName(className).newInstance();
} catch (Exception e1) { } catch (Exception e1) {
@ -596,7 +604,7 @@ public abstract class BaseMethodBinding<T> implements IClientResponseHandler<T>
* @see BaseMethodBinding#loadRequestContents(RequestDetails) * @see BaseMethodBinding#loadRequestContents(RequestDetails)
*/ */
@SuppressWarnings("unused") @SuppressWarnings("unused")
private static class ActiveRequestReader implements IRequestReader { static class ActiveRequestReader implements IRequestReader {
@Override @Override
public InputStream getInputStream(RequestDetails theRequestDetails) throws IOException { public InputStream getInputStream(RequestDetails theRequestDetails) throws IOException {
return theRequestDetails.getServletRequest().getInputStream(); return theRequestDetails.getServletRequest().getInputStream();
@ -607,7 +615,7 @@ public abstract class BaseMethodBinding<T> implements IClientResponseHandler<T>
* @see BaseMethodBinding#loadRequestContents(RequestDetails) * @see BaseMethodBinding#loadRequestContents(RequestDetails)
*/ */
@SuppressWarnings("unused") @SuppressWarnings("unused")
private static class InactiveRequestReader implements IRequestReader { static class InactiveRequestReader implements IRequestReader {
@Override @Override
public InputStream getInputStream(RequestDetails theRequestDetails) { public InputStream getInputStream(RequestDetails theRequestDetails) {
throw new IllegalStateException("The servlet-api JAR is not found on the classpath. Please check that this library is available."); throw new IllegalStateException("The servlet-api JAR is not found on the classpath. Please check that this library is available.");

View File

@ -382,12 +382,13 @@ public abstract class BaseFhirResourceDao<T extends IResource> extends BaseFhirD
Predicate joinPredicate = builder.not(builder.in(from.get("myId")).value(subQ)); Predicate joinPredicate = builder.not(builder.in(from.get("myId")).value(subQ));
Predicate typePredicate = builder.equal(from.get("myResourceType"), resourceType); Predicate typePredicate = builder.equal(from.get("myResourceType"), resourceType);
Predicate notDeletedPredicate = builder.isNull(from.get("myDeleted"));
if (thePids.size() > 0) { if (thePids.size() > 0) {
Predicate inPids = (from.get("myId").in(thePids)); Predicate inPids = (from.get("myId").in(thePids));
cq.where(builder.and(inPids, typePredicate, joinPredicate)); cq.where(builder.and(inPids, typePredicate, joinPredicate, notDeletedPredicate));
} else { } else {
cq.where(builder.and(typePredicate, joinPredicate)); cq.where(builder.and(typePredicate, joinPredicate, notDeletedPredicate));
} }
ourLog.info("Adding :missing qualifier for parameter '{}'", theParamName); ourLog.info("Adding :missing qualifier for parameter '{}'", theParamName);

View File

@ -81,6 +81,7 @@ import ca.uhn.fhir.rest.api.MethodOutcome;
import ca.uhn.fhir.rest.client.IGenericClient; import ca.uhn.fhir.rest.client.IGenericClient;
import ca.uhn.fhir.rest.client.ServerValidationModeEnum; import ca.uhn.fhir.rest.client.ServerValidationModeEnum;
import ca.uhn.fhir.rest.client.interceptor.LoggingInterceptor; import ca.uhn.fhir.rest.client.interceptor.LoggingInterceptor;
import ca.uhn.fhir.rest.gclient.IQuery;
import ca.uhn.fhir.rest.gclient.StringClientParam; import ca.uhn.fhir.rest.gclient.StringClientParam;
import ca.uhn.fhir.rest.gclient.TokenClientParam; import ca.uhn.fhir.rest.gclient.TokenClientParam;
import ca.uhn.fhir.rest.server.Constants; import ca.uhn.fhir.rest.server.Constants;
@ -109,7 +110,11 @@ public class ResourceProviderDstu2Test {
// private static JpaConformanceProvider ourConfProvider; // private static JpaConformanceProvider ourConfProvider;
private void delete(String theResourceType, String theParamName, String theParamValue) { private void delete(String theResourceType, String theParamName, String theParamValue) {
Bundle resources = ourClient.search().forResource(theResourceType).where(new StringClientParam(theParamName).matches().value(theParamValue)).execute(); IQuery<Bundle> forResource = ourClient.search().forResource(theResourceType);
if (theParamName != null) {
forResource = forResource.where(new StringClientParam(theParamName).matches().value(theParamValue));
}
Bundle resources = forResource.execute();
for (IResource next : resources.toListOfResources()) { for (IResource next : resources.toListOfResources()) {
ourLog.info("Deleting resource: {}", next.getId()); ourLog.info("Deleting resource: {}", next.getId());
ourClient.delete().resource(next).execute(); ourClient.delete().resource(next).execute();
@ -782,18 +787,26 @@ public class ResourceProviderDstu2Test {
@Test @Test
public void testSearchWithMissing() throws Exception { public void testSearchWithMissing() throws Exception {
ourLog.info("Starting testSearchWithMissing");
delete("Organization", null, null);
ourLog.info("Starting testSearchWithMissing"); ourLog.info("Starting testSearchWithMissing");
String methodName = "testSearchWithMissing"; String methodName = "testSearchWithMissing";
Organization org = new Organization();
IdDt deletedId = ourClient.create().resource(org).execute().getId().toUnqualifiedVersionless();
ourClient.delete().resourceById(deletedId).execute();
List<IResource> resources = new ArrayList<IResource>(); List<IResource> resources = new ArrayList<IResource>();
for (int i = 0; i < 20; i++) { for (int i = 0; i < 20; i++) {
Organization org = new Organization(); org = new Organization();
org.setName(methodName + "_0" + i); org.setName(methodName + "_0" + i);
resources.add(org); resources.add(org);
} }
ourClient.transaction().withResources(resources).prettyPrint().encodedXml().execute(); ourClient.transaction().withResources(resources).prettyPrint().encodedXml().execute();
Organization org = new Organization(); org = new Organization();
org.addIdentifier().setSystem("urn:system:rpdstu2").setValue(methodName + "01"); org.addIdentifier().setSystem("urn:system:rpdstu2").setValue(methodName + "01");
org.setName(methodName + "name"); org.setName(methodName + "name");
IdDt orgNotMissing = ourClient.create().resource(org).prettyPrint().encodedXml().execute().getId().toUnqualifiedVersionless(); IdDt orgNotMissing = ourClient.create().resource(org).prettyPrint().encodedXml().execute().getId().toUnqualifiedVersionless();
@ -832,6 +845,7 @@ public class ResourceProviderDstu2Test {
List<IdDt> list = toIdListUnqualifiedVersionless(found); List<IdDt> list = toIdListUnqualifiedVersionless(found);
ourLog.info(methodName + " found: " + list.toString() + " - Wanted " + orgMissing + " but not " + orgNotMissing); ourLog.info(methodName + " found: " + list.toString() + " - Wanted " + orgMissing + " but not " + orgNotMissing);
assertThat(list, not(containsInRelativeOrder(orgNotMissing))); assertThat(list, not(containsInRelativeOrder(orgNotMissing)));
assertThat(list, not(containsInRelativeOrder(deletedId)));
assertThat("Wanted " + orgMissing + " but found: " + list, list, containsInRelativeOrder(orgMissing)); assertThat("Wanted " + orgMissing + " but found: " + list, list, containsInRelativeOrder(orgMissing));
} }