@Override
 protected void doInit() throws ResourceException {
   super.doInit();
   // Add a Cache-Control header that tells clients this page never
   // expires.
   final List<CacheDirective> directives = new ArrayList<>();
   directives.add(CacheDirective.maxAge(Integer.MAX_VALUE));
   directives.add(CacheDirective.publicInfo());
   getResponseCacheDirectives().addAll(directives);
 }
  /**
   * Analyzes incoming variables and returns suitable XML representation, the method also sets
   * suitable cache directives.
   */
  public Representation getVersionAsXMLRepresentatioFromDatabase() throws ResourceException {
    Node dbResultObject = null;

    // Check that we actually got an ehrID else scream...
    if (ehrID == null || ehrID.length() == 0) {
      throw new ResourceException(
          Status.CLIENT_ERROR_BAD_REQUEST,
          "Error: No " + EEEConstants.EHR_ID + " supplied in request");
    }

    // Check that we actually got an objectID else scream...
    if (objectID == null || objectID.length() == 0) {
      throw new ResourceException(
          Status.CLIENT_ERROR_BAD_REQUEST,
          "Error: No " + EEEConstants.OBJECT_ID + " supplied in request");
    }

    if (command != null) {

      // Command examples
      //			 * ---- /ehr/{ehrId}/{versionedObject}/{command} ---
      //			 * http://localhost:8182/ehr/1234000/56780007/all_version_ids
      //			 * http://localhost:8182/ehr/1234000/56780007/all_versions
      //			 * http://localhost:8182/ehr/1234000/56780007/revision_history

      throw new ResourceException(
          Status.SERVER_ERROR_INTERNAL,
          "Broken URI routing. Commands should be routed by "
              + EHRRouter.class.getName()
              + " to "
              + VersionedObjectCommandResource.class.getCanonicalName()
              + " instead of "
              + this.getClass().getCanonicalName()
              + " (You tried to send a /command to the object "
              + objectID
              + " in the EHR "
              + ehrID
              + ")");

    } else if (lookupString != null) {

      // Enters here only if command was null and lookupString != null
      // @Command examples
      //			 * ---- /ehr/{ehrId}/{versionedObject}@{versionLookup}  ----
      //			 * http://localhost:8182/ehr/1234000/56780007@latest_trunk_version
      //			 * http://localhost:8182/ehr/1234000/56780007@latest_version
      //			 * http://localhost:8182/ehr/1234000/56780007@2005-08-02T04:30:00

      // TODO: Possibly insert a better string matcher for allowed @commands below

      try {
        if (lookupString.startsWith(LATEST_TRUNK_VERSION)) {
          dbResultObject = dbReader.getVersionedObject_latest_trunk_version(ehrID, objectID);
          getResponseCacheDirectives().add(CacheDirective.noCache());
        } else if (lookupString.startsWith(LATEST_VERSION)) {
          dbResultObject = dbReader.getVersionedObject_latest_version(ehrID, objectID);
          getResponseCacheDirectives().add(CacheDirective.noCache());
        }
        // TODO: Insert a better string matcher for timestamps below
        else if (lookupString.matches("(\\d{2,4})(?:\\-(\\d{2}))?(?:\\-(\\d{2}))?.*")) {
          // OK, matched a date go on do a date lookup
          dbResultObject = dbReader.getVersionedObject_at_time(ehrID, objectID, lookupString);
          getResponseCacheDirectives()
              .add(
                  CacheDirective
                      .noCache()); // FIXME: Consider if we actually could allow (private) caching
                                   // of timestamped lookup resources
        } else {
          // @parameter unknown...
          throw new ResourceException(
              Status.CLIENT_ERROR_BAD_REQUEST,
              "The parameter after @ was illegal. Try suffixing with a timestamp or '"
                  + LATEST_TRUNK_VERSION
                  + "' or '"
                  + LATEST_VERSION
                  + "' instead");
        }
      } catch (Exception e) {
        // - internal error, DB not reached etc
        throw new ResourceException(Status.SERVER_ERROR_INTERNAL, e);
      }

    } else if (treeID != null && systemID != null) {

      // The commonly used DB lookup when we have ehrID, objectID, systemID
      // and treeID is done here

      // These fully identified version never change and can thus be cached for long time
      getResponseCacheDirectives().add(CacheDirective.maxAge(identifiedVersionsMaxage.intValue()));
      getResponseCacheDirectives().add(CacheDirective.privateInfo());

      try {
        dbResultObject = dbReader.getVersionedObject(ehrID, objectID, systemID, treeID);
        // System.out.println("VersionedObjectResource ---> db OK");
      } catch (Exception e) {
        // - internal error, DB not reached etc
        throw new ResourceException(Status.SERVER_ERROR_INTERNAL, e);
      }

    } else if (systemID == null && treeID == null && command == null && lookupString == null) {
      // Now the resource was called with only ehrId and objectID, so return same as for
      // command=ALL_VERSION_IDS
      // TODO: Implement
      getResponseCacheDirectives().add(CacheDirective.noCache());
      throw new ResourceException(
          Status.SERVER_ERROR_NOT_IMPLEMENTED,
          "When implemented this would return all version IDs of the object "
              + objectID
              + " in the EHR "
              + ehrID);
    } else {
      throw new ResourceException(
          Status.CLIENT_ERROR_BAD_REQUEST,
          "This system did not recognize this combination of parameters "
              + "(Parameters found; ehrID:"
              + ehrID
              + ", objectID:"
              + objectID
              + ", systemID:"
              + systemID
              + ", treeID:"
              + treeID
              + ", command:"
              + command
              + ", lookupString:"
              + lookupString);
    }

    if (dbResultObject == null) {
      // System.out.println("VersionedObjectResource ---> dbResultObject==null");
      // Http error 404 if version not found
      throw new ResourceException(
          Status.CLIENT_ERROR_NOT_FOUND,
          "Correct query format, but there was no such versioned object stored here. (You asked for "
              + ehrID
              + "::"
              + objectID
              + ":"
              + systemID
              + ":"
              + treeID
              + " )");
    }

    // ***** Now, if we got all the way here, then return (a version) in XML *******
    return new DomRepresentation(MediaType.TEXT_XML, (Document) dbResultObject);
  }