Skip to content

Commit

Permalink
OpenConceptLab/ocl_issues#556 fixing self url in bundle
Browse files Browse the repository at this point in the history
  • Loading branch information
Harsh Patel committed Jan 29, 2021
1 parent 8a39e3c commit d370927
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 22 deletions.
Expand Up @@ -47,8 +47,8 @@ public OclFhirRestfulServer(CodeSystemResourceProvider codeSystemResourceProvide
this.oclFhirLoggingInterceptor = oclFhirLoggingInterceptor;
}

@Value("${ocl.servlet.baseurl}")
private String baseUrl;
// @Value("${ocl.servlet.baseurl}")
// private String baseUrl;

@Override
protected void initialize() throws ServletException {
Expand Down
Expand Up @@ -81,7 +81,7 @@ public void deleteCodeSystem(@IdParam(optional = true) IdType idType,
public Bundle searchCodeSystems(@OptionalParam(name = PAGE) StringType page, RequestDetails details) {
List<Source> sources = filterSourceHead(getSources(publicAccess));
List<CodeSystem> codeSystems = codeSystemConverter.convertToCodeSystem(sources, false, getPage(page));
return OclFhirUtil.getBundle(codeSystems, details.getFhirServerBase(), details.getRequestPath());
return OclFhirUtil.getBundle(codeSystems, details.getCompleteUrl(), details.getRequestPath());
}

/**
Expand All @@ -100,7 +100,7 @@ public Bundle searchCodeSystemByUrl(@RequiredParam(name = CodeSystem.SP_URL) Str
boolean includeConcepts = !isValid(version) || !isVersionAll(version);
List<CodeSystem> codeSystems = codeSystemConverter.convertToCodeSystem(sources, includeConcepts,
getPage(page));
return OclFhirUtil.getBundle(codeSystems, details.getFhirServerBase(), details.getRequestPath());
return OclFhirUtil.getBundle(codeSystems, details.getCompleteUrl(), details.getRequestPath());
}

/**
Expand All @@ -115,7 +115,7 @@ public Bundle searchCodeSystemByOwner(@RequiredParam(name = OWNER) StringType ow
RequestDetails details) {
List<Source> sources = filterSourceHead(getSourceByOwner(owner, publicAccess));
List<CodeSystem> codeSystems = codeSystemConverter.convertToCodeSystem(sources, false, getPage(page));
return OclFhirUtil.getBundle(codeSystems, details.getFhirServerBase(), details.getRequestPath());
return OclFhirUtil.getBundle(codeSystems, details.getCompleteUrl(), details.getRequestPath());
}

/**
Expand All @@ -136,7 +136,7 @@ public Bundle searchCodeSystemByOwnerAndId(@RequiredParam(name = OWNER) StringTy
List<Source> sources = filterSourceHead(getSourceByOwnerAndIdAndVersion(id, owner, version, publicAccess));
boolean includeConcepts = !isVersionAll(version);
List<CodeSystem> codeSystems = codeSystemConverter.convertToCodeSystem(sources, includeConcepts, getPage(page));
return OclFhirUtil.getBundle(codeSystems, details.getFhirServerBase(), details.getRequestPath());
return OclFhirUtil.getBundle(codeSystems, details.getCompleteUrl(), details.getRequestPath());
}

/**
Expand Down
Expand Up @@ -50,7 +50,7 @@ public Class<? extends IBaseResource> getResourceType() {
public Bundle searchConceptMaps(@OptionalParam(name = PAGE) StringType page, RequestDetails details) {
List<Source> sources = filterSourceHead(getSources(publicAccess));
List<ConceptMap> conceptMaps = conceptMapConverter.convertToConceptMap(sources, false, getPage(page));
return OclFhirUtil.getBundle(conceptMaps, details.getFhirServerBase(), details.getRequestPath());
return OclFhirUtil.getBundle(conceptMaps, details.getCompleteUrl(), details.getRequestPath());
}

/**
Expand All @@ -69,7 +69,7 @@ public Bundle searchConceptMapByUrl(@RequiredParam(name = ConceptMap.SP_URL) Str
boolean includeMappings = !isValid(version) || !isVersionAll(version);
List<ConceptMap> conceptMaps = conceptMapConverter.convertToConceptMap(sources, includeMappings,
getPage(page));
return OclFhirUtil.getBundle(conceptMaps, details.getFhirServerBase(), details.getRequestPath());
return OclFhirUtil.getBundle(conceptMaps, details.getCompleteUrl(), details.getRequestPath());
}

/**
Expand All @@ -84,7 +84,7 @@ public Bundle searchConceptMapByOwner(@RequiredParam(name = OWNER) StringType ow
RequestDetails details) {
List<Source> sources = filterSourceHead(getSourceByOwner(owner, publicAccess));
List<ConceptMap> conceptMaps = conceptMapConverter.convertToConceptMap(sources, false, getPage(page));
return OclFhirUtil.getBundle(conceptMaps, details.getFhirServerBase(), details.getRequestPath());
return OclFhirUtil.getBundle(conceptMaps, details.getCompleteUrl(), details.getRequestPath());
}

/**
Expand All @@ -105,7 +105,7 @@ public Bundle searchConceptMapByOwnerAndId(@RequiredParam(name = OWNER) StringTy
List<Source> sources = filterSourceHead(getSourceByOwnerAndIdAndVersion(id, owner, version, publicAccess));
boolean includeMappings = !isVersionAll(version);
List<ConceptMap> conceptMaps = conceptMapConverter.convertToConceptMap(sources, includeMappings, getPage(page));
return OclFhirUtil.getBundle(conceptMaps, details.getFhirServerBase(), details.getRequestPath());
return OclFhirUtil.getBundle(conceptMaps, details.getCompleteUrl(), details.getRequestPath());
}

@Operation(name = TRANSLATE, idempotent = true)
Expand Down
Expand Up @@ -76,7 +76,7 @@ public MethodOutcome createValueSet(@ResourceParam ValueSet valueSet, RequestDet
public Bundle searchValueSets(@OptionalParam(name = PAGE) StringType page, RequestDetails details) {
List<Collection> collections = filterCollectionHead(getCollections(publicAccess));
List<ValueSet> valueSets = valueSetConverter.convertToValueSet(collections, false, getPage(page));
return OclFhirUtil.getBundle(valueSets, details.getFhirServerBase(), details.getRequestPath());
return OclFhirUtil.getBundle(valueSets, details.getCompleteUrl(), details.getRequestPath());
}

/**
Expand All @@ -93,7 +93,7 @@ public Bundle searchValueSetByUrl(@RequiredParam(name = ValueSet.SP_URL) StringT
RequestDetails details) {
List<Collection> collections = filterCollectionHead(getCollectionByUrl(url, version, publicAccess));
List<ValueSet> valueSets = valueSetConverter.convertToValueSet(collections, !isVersionAll(version), getPage(page));
return OclFhirUtil.getBundle(valueSets, details.getFhirServerBase(), details.getRequestPath());
return OclFhirUtil.getBundle(valueSets, details.getCompleteUrl(), details.getRequestPath());
}

/**
Expand All @@ -108,7 +108,7 @@ public Bundle searchValueSetByOwner(@RequiredParam(name = OWNER) StringType owne
RequestDetails details) {
List<Collection> collections = filterCollectionHead(getCollectionByOwner(owner, publicAccess));
List<ValueSet> valueSets = valueSetConverter.convertToValueSet(collections, false, getPage(page));
return OclFhirUtil.getBundle(valueSets, details.getFhirServerBase(), details.getRequestPath());
return OclFhirUtil.getBundle(valueSets, details.getCompleteUrl(), details.getRequestPath());
}

/**
Expand All @@ -128,7 +128,7 @@ public Bundle searchValueSetByOwnerAndId(@RequiredParam(name = OWNER) StringType
RequestDetails details) {
List<Collection> collections = filterCollectionHead(getCollectionByOwnerAndId(id, owner, version, publicAccess));
List<ValueSet> valueSets = valueSetConverter.convertToValueSet(collections, !isVersionAll(version), getPage(page));
return OclFhirUtil.getBundle(valueSets, details.getFhirServerBase(), details.getRequestPath());
return OclFhirUtil.getBundle(valueSets, details.getCompleteUrl(), details.getRequestPath());
}

@Operation(name = VALIDATE_CODE, idempotent = true)
Expand Down
Expand Up @@ -17,9 +17,13 @@
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.util.UriComponentsBuilder;

import javax.annotation.PostConstruct;

import java.net.URL;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.nio.file.Paths;
import java.util.*;
import java.util.stream.Collectors;
Expand All @@ -32,6 +36,14 @@ public class OclFhirUtil {

@Value("${server.port}")
private String port;

private static String BASE_URL;

@Value("${ocl.servlet.baseurl}")
public void setBaseUrl(String name) {
OclFhirUtil.BASE_URL = name;
}

private static final Log log = LogFactory.getLog(OclFhirUtil.class);
private static final FhirContext context;
private SourceRepository sourceRepository;
Expand All @@ -53,28 +65,32 @@ public OclFhirUtil(){
context = FhirContext.forR4();
}

private String serverBase = "";
public static IParser parser = context.newJsonParser();
public static JsonParser jsonParser = new JsonParser();
public static Gson gson = new Gson();

@PostConstruct
private void init() {
serverBase = String.format("http://localhost:%s/fhir",port);
}

public static FhirContext getFhirContext() {
return context;
}

public static <T extends Resource> Bundle getBundle(List<T> resource, String fhirBase, String requestPath) {
Bundle bundle = new Bundle();
bundle.setType(Bundle.BundleType.SEARCHSET);
// add self link
try {
Bundle.BundleLinkComponent com = new Bundle.BundleLinkComponent();
com.setRelation("self");
com.setUrl(UriComponentsBuilder.fromHttpUrl(URLDecoder.decode(fhirBase, StandardCharsets.UTF_8.toString()))
.host(new URL(BASE_URL).getHost())
.toUriString());
bundle.setLink(Collections.singletonList(com));
} catch (Exception e) {
log.error("Error parsing url " + fhirBase);
}
bundle.setTotal(resource.size());
resource.forEach(r -> {
Bundle.BundleEntryComponent component = new Bundle.BundleEntryComponent();
component.setResource(r);
//component.setFullUrl(getCompleteUrl(fhirBase, requestPath, r.getId()));
bundle.addEntry(component);
});
return bundle;
Expand Down Expand Up @@ -232,7 +248,7 @@ public static Optional<Identifier> getIdentifier(String value) {
return Optional.empty();
Identifier identifier = new Identifier();
identifier.setSystem(OCL_SYSTEM);
identifier.setValue(value.replace("sources", CODESYSTEM).replace("collections", VALUESET));
identifier.setValue(value.replace("sources", CODESYSTEM).replace("collections", VALUESET).trim());
identifier.getType().setText("Accession ID");
identifier.getType().getCodingFirstRep().setSystem(ACSN_SYSTEM).setCode(ACSN).setDisplay("Accession ID");
return Optional.of(identifier);
Expand Down
Expand Up @@ -208,6 +208,7 @@ public boolean isValid(String value) {

public ValueSetResourceProvider valueSetProvider() {
OclFhirUtil oclFhirUtil = new OclFhirUtil(sourceRepository, conceptRepository, conceptsSourceRepository);
oclFhirUtil.setBaseUrl("http://test.org");
ValueSetConverter converter = new TestValueSetConverter(sourceRepository, conceptRepository, oclFhirUtil, oclUser, conceptsSourceRepository, dataSource,
authtokenRepository, userProfilesOrganizationRepository, organizationRepository, userRepository, collectionRepository);
return spy(new ValueSetResourceProvider(null, null, collectionRepository, converter, null, oclFhirUtil));
Expand Down Expand Up @@ -262,6 +263,7 @@ protected Map<Long, String> getValidatedConceptIds(Long sourceId, List<String> c

public CodeSystemResourceProvider codeSystemProvider() {
OclFhirUtil oclFhirUtil = new OclFhirUtil(sourceRepository, conceptRepository, conceptsSourceRepository);
oclFhirUtil.setBaseUrl("http://test.org");
CodeSystemConverter converter = new TestCodeSystemConverter(sourceRepository, conceptRepository, oclFhirUtil,
oclUser, conceptsSourceRepository, dataSource, authtokenRepository, userProfilesOrganizationRepository,
organizationRepository, userRepository);
Expand Down
Expand Up @@ -43,6 +43,7 @@ public class TestCodeSystemResourceProvider extends OclFhirTest {
@Before
public void setUpBefore() {
MockitoAnnotations.initMocks(this);
when(requestDetails.getCompleteUrl()).thenReturn("http://test.org");
source1 = source(123L, V_1_0, concept1(), concept2(), concept3());
source2 = source(234L, "v2.0", concept1(), concept2(), concept3(), concept4());
source3 = source(345L, "v3.0", concept1(), concept2(), concept3(), concept4());
Expand Down
Expand Up @@ -52,6 +52,7 @@ public class TestValueSetResourceProvider extends OclFhirTest {
@Before
public void setUpBefore() {
MockitoAnnotations.initMocks(this);
when(requestDetails.getCompleteUrl()).thenReturn("http://test.org");
source1 = source(123L, "v1.0", concept1(), concept2(), concept3());
source2 = source(234L, "v2.0", concept1(), concept2(), concept3(), concept4());
source3 = source(345L, "v3.0", concept1(), concept2(), concept3(), concept4());
Expand Down

0 comments on commit d370927

Please sign in to comment.