Exemplo n.º 1
0
  private FhirContext(
      FhirVersionEnum theVersion, Collection<Class<? extends IBaseResource>> theResourceTypes) {
    VersionUtil.getVersion();

    if (theVersion != null) {
      if (!theVersion.isPresentOnClasspath()) {
        throw new IllegalStateException(
            getLocalizer()
                .getMessage(
                    FhirContext.class, "noStructuresForSpecifiedVersion", theVersion.name()));
      }
      myVersion = theVersion.getVersionImplementation();
    } else if (FhirVersionEnum.DSTU1.isPresentOnClasspath()) {
      myVersion = FhirVersionEnum.DSTU1.getVersionImplementation();
    } else if (FhirVersionEnum.DSTU2.isPresentOnClasspath()) {
      myVersion = FhirVersionEnum.DSTU2.getVersionImplementation();
    } else if (FhirVersionEnum.DSTU2_HL7ORG.isPresentOnClasspath()) {
      myVersion = FhirVersionEnum.DSTU2_HL7ORG.getVersionImplementation();
    } else {
      throw new IllegalStateException(getLocalizer().getMessage(FhirContext.class, "noStructures"));
    }

    if (theVersion == null) {
      ourLog.info(
          "Creating new FhirContext with auto-detected version [{}]. It is recommended to explicitly select a version for future compatibility by invoking FhirContext.forDstuX()",
          myVersion.getVersion().name());
    } else {
      ourLog.info("Creating new FHIR context for FHIR version [{}]", myVersion.getVersion().name());
    }

    myResourceTypesToScan = theResourceTypes;
  }
Exemplo n.º 2
0
  public RuntimeResourceDefinition getResourceDefinition(
      FhirVersionEnum theVersion, String theResourceName) {
    Validate.notNull(theVersion, "theVersion can not be null");
    validateInitialized();

    if (theVersion.equals(myVersion.getVersion())) {
      return getResourceDefinition(theResourceName);
    }

    Map<String, Class<? extends IBaseResource>> nameToType =
        myVersionToNameToResourceType.get(theVersion);
    if (nameToType == null) {
      nameToType = new HashMap<String, Class<? extends IBaseResource>>();
      Map<Class<? extends IBase>, BaseRuntimeElementDefinition<?>> existing =
          Collections.emptyMap();
      ModelScanner.scanVersionPropertyFile(null, nameToType, theVersion, existing);

      Map<FhirVersionEnum, Map<String, Class<? extends IBaseResource>>>
          newVersionToNameToResourceType =
              new HashMap<FhirVersionEnum, Map<String, Class<? extends IBaseResource>>>();
      newVersionToNameToResourceType.putAll(myVersionToNameToResourceType);
      newVersionToNameToResourceType.put(theVersion, nameToType);
      myVersionToNameToResourceType = newVersionToNameToResourceType;
    }

    Class<? extends IBaseResource> resourceType = nameToType.get(theResourceName.toLowerCase());
    if (resourceType == null) {
      throw new DataFormatException(createUnknownResourceNameError(theResourceName, theVersion));
    }

    return getResourceDefinition(resourceType);
  }
Exemplo n.º 3
0
 public boolean isEquivalentTo(FhirVersionEnum theVersion) {
   if (this.equals(theVersion)) {
     return true;
   }
   if (myEquivalent != null) {
     return myEquivalent.equals(theVersion);
   }
   return false;
 }
  @SuppressWarnings("unchecked")
  void validateServerBase(String theServerBase, HttpClient theHttpClient, BaseClient theClient) {

    GenericClient client = new GenericClient(myContext, theHttpClient, theServerBase, this);
    for (IClientInterceptor interceptor : theClient.getInterceptors()) {
      client.registerInterceptor(interceptor);
    }
    client.setDontValidateConformance(true);

    IBaseResource conformance;
    try {
      @SuppressWarnings("rawtypes")
      Class implementingClass =
          myContext.getResourceDefinition("Conformance").getImplementingClass();
      conformance = (IBaseResource) client.fetchConformance().ofType(implementingClass).execute();
    } catch (FhirClientConnectionException e) {
      throw new FhirClientConnectionException(
          myContext
              .getLocalizer()
              .getMessage(
                  RestfulClientFactory.class,
                  "failedToRetrieveConformance",
                  theServerBase + Constants.URL_TOKEN_METADATA),
          e);
    }

    FhirTerser t = myContext.newTerser();
    String serverFhirVersionString = null;
    Object value = t.getSingleValueOrNull(conformance, "fhirVersion");
    if (value instanceof IPrimitiveType) {
      serverFhirVersionString = ((IPrimitiveType<?>) value).getValueAsString();
    }
    FhirVersionEnum serverFhirVersionEnum = null;
    if (StringUtils.isBlank(serverFhirVersionString)) {
      // we'll be lenient and accept this
    } else {
      if (serverFhirVersionString.startsWith("0.80")
          || serverFhirVersionString.startsWith("0.0.8")) {
        serverFhirVersionEnum = FhirVersionEnum.DSTU1;
      } else if (serverFhirVersionString.startsWith("0.4")) {
        serverFhirVersionEnum = FhirVersionEnum.DSTU2;
      } else if (serverFhirVersionString.startsWith("0.5")) {
        serverFhirVersionEnum = FhirVersionEnum.DSTU2;
      } else {
        // we'll be lenient and accept this
        ourLog.debug(
            "Server conformance statement indicates unknown FHIR version: {}",
            serverFhirVersionString);
      }
    }

    if (serverFhirVersionEnum != null) {
      FhirVersionEnum contextFhirVersion = myContext.getVersion().getVersion();
      if (!contextFhirVersion.isEquivalentTo(serverFhirVersionEnum)) {
        throw new FhirClientInappropriateForServerException(
            myContext
                .getLocalizer()
                .getMessage(
                    RestfulClientFactory.class,
                    "wrongVersionInConformance",
                    theServerBase + Constants.URL_TOKEN_METADATA,
                    serverFhirVersionString,
                    serverFhirVersionEnum,
                    contextFhirVersion));
      }
    }

    myValidatedServerBaseUrls.add(normalizeBaseUrlForMap(theServerBase));
  }
Exemplo n.º 5
0
 public boolean isNewerThan(FhirVersionEnum theVersion) {
   return ordinal() > theVersion.ordinal();
 }