Merge branch 'master' of github.com:jamesagnew/hapi-fhir

This commit is contained in:
jamesagnew 2019-07-04 09:01:35 -04:00
commit e762ac9d10
10 changed files with 104 additions and 24 deletions

View File

@ -36,6 +36,7 @@ public class ResourceGoneException extends BaseServerResponseException {
public static final int STATUS_CODE = Constants.STATUS_HTTP_410_GONE;
private static final long serialVersionUID = 1L;
private IIdType myResourceId;
/**
* Constructor which creates an error message based on a given resource ID
@ -44,6 +45,7 @@ public class ResourceGoneException extends BaseServerResponseException {
*/
public ResourceGoneException(IIdType theResourceId) {
super(STATUS_CODE, "Resource " + (theResourceId != null ? theResourceId.getValue() : "") + " is gone/deleted");
myResourceId = theResourceId;
}
/**
@ -53,6 +55,7 @@ public class ResourceGoneException extends BaseServerResponseException {
@Deprecated
public ResourceGoneException(Class<? extends IBaseResource> theClass, BaseIdentifierDt thePatientId) {
super(STATUS_CODE, "Resource of type " + theClass.getSimpleName() + " with ID " + thePatientId + " is gone/deleted");
myResourceId = null;
}
/**
@ -63,6 +66,7 @@ public class ResourceGoneException extends BaseServerResponseException {
*/
public ResourceGoneException(Class<? extends IBaseResource> theClass, IIdType theResourceId) {
super(STATUS_CODE, "Resource of type " + theClass.getSimpleName() + " with ID " + theResourceId + " is gone/deleted");
myResourceId = theResourceId;
}
/**
@ -84,4 +88,12 @@ public class ResourceGoneException extends BaseServerResponseException {
super(STATUS_CODE, theMessage);
}
public IIdType getResourceId() {
return myResourceId;
}
public void setResourceId(IIdType theResourceId) {
myResourceId = theResourceId;
}
}

View File

@ -54,6 +54,7 @@ import ca.uhn.fhir.util.*;
import org.apache.commons.lang3.Validate;
import org.hl7.fhir.instance.model.api.*;
import org.hl7.fhir.r4.model.InstantType;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
@ -863,7 +864,7 @@ public abstract class BaseHapiFhirResourceDao<T extends IBaseResource> extends B
throw new ResourceNotFoundException("No resource found with PID " + thePid);
}
if (entity.get().getDeleted() != null) {
throw new ResourceGoneException("Resource was deleted at " + new InstantType(entity.get().getDeleted()).getValueAsString());
throw newResourceGoneException(entity.get());
}
T retVal = toResource(myResourceType, entity.get(), null, false);
@ -872,6 +873,16 @@ public abstract class BaseHapiFhirResourceDao<T extends IBaseResource> extends B
return retVal;
}
@NotNull
private ResourceGoneException newResourceGoneException(BaseHasResource theResourceEntity) {
StringBuilder b = new StringBuilder();
b.append("Resource was deleted at ");
b.append(new InstantType(theResourceEntity.getDeleted()).getValueAsString());
ResourceGoneException retVal = new ResourceGoneException(b.toString());
retVal.setResourceId(theResourceEntity.getIdDt());
return retVal;
}
@Override
public T read(IIdType theId) {
@ -902,7 +913,7 @@ public abstract class BaseHapiFhirResourceDao<T extends IBaseResource> extends B
if (theDeletedOk == false) {
if (entity.getDeleted() != null) {
throw new ResourceGoneException("Resource was deleted at " + new InstantType(entity.getDeleted()).getValueAsString());
throw newResourceGoneException(entity);
}
}

View File

@ -15,7 +15,7 @@ import ca.uhn.fhir.jpa.term.VersionIndependentConcept;
import ca.uhn.fhir.jpa.util.CircularQueueCaptureQueriesListener;
import ca.uhn.fhir.jpa.util.ExpungeOptions;
import ca.uhn.fhir.jpa.util.JpaConstants;
import ca.uhn.fhir.jpa.util.LoggingRule;
import ca.uhn.fhir.test.utilities.LoggingRule;
import ca.uhn.fhir.model.dstu2.resource.Bundle;
import ca.uhn.fhir.model.dstu2.resource.Bundle.Entry;
import ca.uhn.fhir.rest.api.Constants;
@ -25,7 +25,6 @@ import ca.uhn.fhir.rest.server.servlet.ServletRequestDetails;
import ca.uhn.fhir.util.BundleUtil;
import ca.uhn.fhir.util.StopWatch;
import ca.uhn.fhir.util.TestUtil;
import com.google.common.base.Charsets;
import org.apache.commons.io.IOUtils;
import org.hibernate.HibernateException;
import org.hibernate.Session;
@ -56,7 +55,6 @@ import java.util.*;
import java.util.concurrent.Callable;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import static ca.uhn.fhir.util.TestUtil.randomizeLocale;

View File

@ -375,6 +375,34 @@ public class ResourceProviderR4Test extends BaseResourceProviderR4Test {
} catch (ResourceGoneException e) {
// good
}
}
@Test
public void testResourceGoneIncludesVersion() {
Patient p = new Patient();
p.addName().setFamily("FAM").addGiven("GIV");
IIdType id = myPatientDao.create(p).getId().toUnqualifiedVersionless();
ourClient
.delete()
.resourceById(id)
.execute();
CapturingInterceptor captureInterceptor = new CapturingInterceptor();
ourClient.registerInterceptor(captureInterceptor);
try {
ourClient.read().resource("Patient").withId(id.toUnqualifiedVersionless()).execute();
fail();
} catch (ResourceGoneException e) {
// good
}
List<String> locationHeader = captureInterceptor.getLastResponse().getHeaders(Constants.HEADER_LOCATION);
assertEquals(1, locationHeader.size());
assertThat(locationHeader.get(0), containsString(id.getValue() + "/_history/2"));
}
@Test

View File

@ -171,7 +171,7 @@ public class PointcutLatch implements IAnonymousInterceptor, IPointcutLatch {
super(getName() + ": " + message + " called with values: " + hookParamsToString(theArgs));
}
PointcutLatchException(String message) {
public PointcutLatchException(String message) {
super(getName() + ": " + message);
}
}

View File

@ -1046,6 +1046,20 @@ public class RestfulServer extends HttpServlet implements IRestfulServer<Servlet
exception = DEFAULT_EXCEPTION_HANDLER.preProcessOutgoingException(requestDetails, e, theRequest);
}
/*
* If it's a 410 Gone, we want to include a location header inthe response
* if we can, since that can include the resource version which is nice
* for the user.
*/
if (exception instanceof ResourceGoneException) {
IIdType resourceId = ((ResourceGoneException) exception).getResourceId();
if (resourceId != null && resourceId.hasResourceType() && resourceId.hasIdPart()) {
String baseUrl = myServerAddressStrategy.determineServerBase(theRequest.getServletContext(), theRequest);
resourceId = resourceId.withServerBase(baseUrl, resourceId.getResourceType());
requestDetails.getResponse().addHeader(Constants.HEADER_LOCATION, resourceId.getValue());
}
}
/*
* Next, interceptors get a shot at handling the exception
*/

View File

@ -42,6 +42,10 @@
<artifactId>spring-context</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
</dependencies>
<build>

View File

@ -14,7 +14,7 @@
* limitations under the License.
*/
package ca.uhn.fhir.jpa.util;
package ca.uhn.fhir.test.utilities;
import org.junit.rules.TestRule;
import org.junit.runner.Description;
@ -40,19 +40,22 @@ public class LoggingRule implements TestRule {
* @param description A description of the test implemented in <code>statement</code>.
* @return The modified statement.
*/
@Override
public Statement apply(final Statement statement, final Description description) {
return new Statement() {
@Override
public void evaluate() throws Throwable {
final Logger logger = LoggerFactory.getLogger(description.getTestClass());
logger.info(MessageFormat.format("Starting test case [{0}]", description.getDisplayName()));
boolean success = false;
try {
statement.evaluate();
success = true;
} catch (final Throwable e) {
logger.error(MessageFormat.format("Exception thrown in test case [{0}]: {1}", description.getDisplayName(), e.toString()), e);
throw e;
} finally {
logger.info(MessageFormat.format("Finished test case [{0}]", description.getDisplayName()));
logger.info(MessageFormat.format("Finished test case [{0}] (success={1})", description.getDisplayName(), success));
}
}
};

View File

@ -1492,6 +1492,11 @@
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.1</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jxr-plugin</artifactId>

View File

@ -220,6 +220,11 @@
request and add it to the response headers. Clients may supply the transaction
header via the <![CDATA[<code>X-Request-ID</code>]]> header.
</action>
<action type="add">
When attempting to read a resource that is deleted, a Location header is now
returned that includes the resource ID and the version ID for the deleted
resource.
</action>
</release>
<release version="3.8.0" date="2019-05-30" description="Hippo">
<action type="fix">