Class Utils


  • @Component
    public class Utils
    extends Object
    Collection of utility methods
    Author:
    Andrea Bollini (andrea.bollini at 4science.it)
    • Field Detail

      • DEFAULT_PAGE_SIZE

        public static final int DEFAULT_PAGE_SIZE
        The default page size, if unspecified in the request.
        See Also:
        Constant Field Values
    • Constructor Detail

      • Utils

        public Utils()
    • Method Detail

      • getPage

        public <T> org.springframework.data.domain.Page<T> getPage​(List<T> fullContents,
                                                                   @Nullable
                                                                   org.springframework.data.domain.Pageable optionalPageable)
      • getPageable

        public org.springframework.data.domain.Pageable getPageable​(@Nullable
                                                                    org.springframework.data.domain.Pageable optionalPageable)
        Convenience method to get a default pageable instance if needed.
        Parameters:
        optionalPageable - the existing pageable instance, may be null.
        Returns:
        the existing instance if it is not null, a default pageable instance otherwise.
      • linkToSingleResource

        public org.springframework.hateoas.Link linkToSingleResource​(RestAddressableModel data,
                                                                     String rel)
        Create a HAL Link to a single resource
        Parameters:
        data - the resource itself
        rel - name of the link relation to create
        Returns:
        created Link object
      • linkToSubResource

        public org.springframework.hateoas.Link linkToSubResource​(RestAddressableModel data,
                                                                  String rel)
        Create a HAL Link to a subresource of given resource. This method assumes the name & link to the subresource are both the same string value. See other linkToSubResource method if they are different.
        Parameters:
        data - main resource
        rel - name/subpath of the subresource (assumed to be the same)
        Returns:
        created Link object
      • linkToSubResource

        public org.springframework.hateoas.Link linkToSubResource​(RestAddressableModel data,
                                                                  String rel,
                                                                  String path)
        Create a HAL Link to a subresource of given resource using given path name and link name
        Parameters:
        data - main resource
        rel - name of the subresource link relation to create
        path - subpath for the subresource
        Returns:
        created Link object
      • getRepositories

        public String[] getRepositories()
        Find the names of all DSpaceRestRepository implementations.
        Returns:
        the names of all repository types.
      • makeSingular

        public static String makeSingular​(String modelPlural)
      • getLinkResourceRepository

        public LinkRestRepository getLinkResourceRepository​(String apiCategory,
                                                            String modelPlural,
                                                            String rel)
        Retrieve the LinkRestRepository associated with a specific link from the apiCategory and model specified in the parameters.
        Parameters:
        apiCategory - the apiCategory
        modelPlural - the model name in its plural form
        rel - the name of the relation
        Returns:
      • getClassLevelLinkRest

        public LinkRest getClassLevelLinkRest​(String rel,
                                              Class<? extends RestAddressableModel> restClass)
        Parameters:
        rel -
        restClass -
        Returns:
        the LinkRest annotation corresponding to the specified rel in the rest class, or null if not found.
      • getMetadataKey

        public String getMetadataKey​(String schema,
                                     String element,
                                     String qualifier)
        Build the canonical representation of a metadata key in DSpace. I.e. <schema>.<element>[.<qualifier>]
        Parameters:
        schema -
        element -
        qualifier -
        Returns:
      • getFile

        public static File getFile​(org.springframework.web.multipart.MultipartFile multipartFile,
                                   String prefixTempName,
                                   String suffixTempName)
                            throws IOException,
                                   FileNotFoundException
        Create a temporary file from a multipart file upload
        Parameters:
        multipartFile - the multipartFile representing the uploaded file. Please note that it is a complex object including additional information other than the binary like the original file name and the MIME type
        prefixTempName - the prefix to use to generate the filename of the temporary file
        suffixTempName - the suffix to use to generate the filename of the temporary file
        Returns:
        the temporary file on the server
        Throws:
        IOException
        FileNotFoundException
      • getFileName

        public static String getFileName​(org.springframework.web.multipart.MultipartFile multipartFile)
                                  throws IOException,
                                         FileNotFoundException
        Return the filename part from a multipartFile upload that could eventually contains the full path on the client
        Parameters:
        multipartFile - the file uploaded
        Returns:
        the filename part of the file on the client filesystem
        Throws:
        IOException
        FileNotFoundException
      • constructBitstreamFormatList

        public List<org.dspace.content.BitstreamFormat> constructBitstreamFormatList​(javax.servlet.http.HttpServletRequest request,
                                                                                     org.dspace.core.Context context)
        This method will construct a List of BitstreamFormats out of a request. It will call the getStringListFromRequest(HttpServletRequest) method to retrieve a list of links out of the request. The method will iterate over this list of links and parse the links to retrieve the integer ID from it. It will then retrieve the BitstreamFormat corresponding to this ID. If one is found, this BitstreamFormat is added to the List of BitstreamFormats that we will return.
        Parameters:
        request - The request out of which we'll create the List of BitstreamFormats
        context - The relevant DSpace context
        Returns:
        The resulting list of BitstreamFormats that we parsed out of the request
      • constructDSpaceObjectList

        public List<org.dspace.content.DSpaceObject> constructDSpaceObjectList​(org.dspace.core.Context context,
                                                                               List<String> list)
        This method will construct a List of DSpaceObjects by executing the method readFromRequest(HttpServletRequest) and fetching the List of Strings from the request. The method will iterate over this list of Strings and parse the String to retrieve the UUID from it. It will then look through all the DSpaceObjectServices to try and match this UUID to a DSpaceObject. If one is found, this DSpaceObject is added to the List of DSpaceObjects that we will return.
        Parameters:
        context - The relevant DSpace context
        list - The interesting UUIDs.
        Returns:
        The resulting list of DSpaceObjects that we parsed out of the request
      • getStringListFromRequest

        public List<String> getStringListFromRequest​(javax.servlet.http.HttpServletRequest request)
        This method will retrieve a list of DSpaceObjects from the Request by reading in the Request's InputStream with a Scanner and searching the InputStream for UUIDs which will then be resolved to a DSpaceObject. These will all be added to a list and returned by this method.
        Parameters:
        request - The request of which the InputStream will be used
        Returns:
        The list of DSpaceObjects that we could find in the InputStream
        Throws:
        IOException - If something goes wrong
      • getSortedUnion

        public String[] getSortedUnion​(String[]... arrays)
        Gets the alphanumerically sorted union of multiple string arrays.
        Parameters:
        arrays - the string arrays.
        Returns:
        the sorted union of them, with no duplicate values.
      • requireMethod

        public Method requireMethod​(Class clazz,
                                    String name)
        Gets the method with the given name in the given class.
        Parameters:
        clazz - the class.
        name - the method name.
        Returns:
        the first method found with the given name.
        Throws:
        IllegalArgumentException - if no such method is found.
      • obtainProjection

        public Projection obtainProjection()
        Gets the effective projection requested by the current servlet request, or DefaultProjection if none is specified.

        Any number of individual Projections that are spring-registered Components may be specified by name via the projection parameter. If multiple projections are specified, they will be wrapped in a CompositeProjection and applied in order as described there.

        In addition, any number of embeds may be specified by rel name via the embed parameter. When provided, these act as an "allow list" of embeds that may be included in the response, as described and implemented by EmbedRelsProjection.

        Returns:
        the requested or default projection, never null.
        Throws:
        IllegalArgumentException - if the request specifies an unknown projection name.
      • embedOrLinkClassLevelRels

        public void embedOrLinkClassLevelRels​(HALResource<RestAddressableModel> halResource,
                                              org.springframework.hateoas.Link... oldLinks)
        Adds embeds or links for all class-level LinkRel annotations for which embeds or links are allowed.
        Parameters:
        halResource - the resource.
        oldLinks - previously traversed links
      • embedMethodLevelRels

        public void embedMethodLevelRels​(HALResource<? extends RestAddressableModel> resource)
        Adds embeds (if the maximum embed level has not been exceeded yet) for all properties annotated with @LinkRel or whose return types are RestAddressableModel subclasses.
        Parameters:
        resource - the resource to be so augmented.
      • findLinkAnnotation

        @Nullable
        public LinkRest findLinkAnnotation​(Method readMethod)
        Gets the LinkRest annotation for the given method, if any.
        Parameters:
        readMethod - the method.
        Returns:
        the annotation, or null if not found.