Class VersionHandler


  • public class VersionHandler
    extends Object
    This class offers utility functions to encode and decode versioning information in various ways.

    Since Jenkins lacks the concept of a persistent user-connection across several HTTP interactions, the information about what versions a user has requested needs to be stored in and retrieved from a number of different communication paths.

    Different situations need different kinds of storage:

    • In the StaplerRequest, when dealing with Web GUI interactions
    • In the URL parameters, when dealing with Web GUI redirects
    • In the thread, once execution of a build starts
    Author:
    mhschroe
    • Field Detail

      • SINGLE_VERSION_KEY

        public static final String SINGLE_VERSION_KEY
        This field name is used as an URL parameter, to select the version of the job encoded in the lead-up to the URL.
        See Also:
        Constant Field Values
      • VERSIONING_KEY

        public static final String VERSIONING_KEY
        This field name is used as an URL parameter, so select versions of arbitrary subprojects.
        See Also:
        Constant Field Values
    • Constructor Detail

      • VersionHandler

        public VersionHandler()
    • Method Detail

      • getVersions

        public static Map<String,​Long> getVersions()
        Tries to fetch pre-initialised versioning information.

        If no values have been pre-initialised via initVersions(AbstractProject) or setVersions(Map), the map will be empty.

        Returns:
        may be empty, but never null. If empty, the caller should use the most recent stable versions, but this function will not actually return those.
      • initVersions

        public static Map<String,​Long> initVersions​(AbstractProject<?,​?> root)
        Will initialise the versioning information and distribute it across the various avenues of saving it.

        It will first attempt to read the values from the environment via getVersions(). It will then combine that map with the defaults from the given projects, with the former one overwriting values of the latter one.

        Parameters:
        root - the project to initialize the environment for
        Returns:
        all versions for the given project
      • initVersions

        public static Map<String,​Long> initVersions​(Map<String,​Long> map)
        Will initialise the versioning information and distribute it across the various avenues of saving it.

        It will take the information as given in the passed-in map.

        Parameters:
        map - the map to set in the current environment
        Returns:
        all versions for the given project
      • addVersions

        public static Map<String,​Long> addVersions​(Map<String,​Long> map)
        Will merge the currently configured versions (if any) with the given map.
        Parameters:
        map - the map to overwrite/add values with.
        Returns:
        the joined map.
      • clearVersions

        public static void clearVersions()
      • clearVersionsPartial

        public static void clearVersionsPartial()
        Same as clearVersions(), but only clears "permanent" storage areas, that might leak into subsequent runs.

        For example, it clears data saved inside the current Thread, but does not purge data from the StaplerRequest, which does not exist across invocations.

      • getFromProject

        public static Map<String,​Long> getFromProject​(AbstractProject<?,​?> root)
        This method loads the map of versions from the parameters passed in via the current StaperRequest's URL.
        Parameters:
        root - the project to retrieve versions for
        Returns:
        a map, may be empty, but never null
      • getFromUrlParameter

        public static Map<String,​Long> getFromUrlParameter()
        This method loads the map of versions from the parameters passed in via the current StaperRequest's URL.
        Returns:
        a map, may be empty, but never nulll
      • getFullUrlParameter

        public static String getFullUrlParameter​(String value)
      • getFromRequest

        public static Map<String,​Long> getFromRequest()
        Attempts to read the active versions from the current request, no matter if stored as inside the URL parameters or the form part of the request.

        This is safer than calling getFromFormRequest(StaplerRequest), since it will only do that, if the project/version parameter are set in the request.

        Returns:
        a map, may be empty, but never null
      • getFromFormRequest

        public static Map<String,​Long> getFromFormRequest​(org.kohsuke.stapler.StaplerRequest req)
        Decodes the versions stored in the form-part of the given request.

        Avoid calling this, if the request has no form, as it can lead to a HTTP error being send to the remote client.

        Parameters:
        req - the request to check
        Returns:
        a map, may be empty, but never null