Class Utils

java.lang.Object
org.dspace.app.rest.utils.Utils

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

    • DEFAULT_PAGE_SIZE

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

    • Utils

      public Utils()
  • Method Details

    • getPage

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

      public <T> List<T> getPageObjectList(List<T> fullList, @Nullable org.springframework.data.domain.Pageable optionalPageable)
      Returns list of objects for the current page.
      Type Parameters:
      T -
      Parameters:
      fullList - the complete list of objects
      optionalPageable -
      Returns:
      list of page objects
    • 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
    • getResourceRepository

      public DSpaceRestRepository getResourceRepository(String apiCategory, String modelPlural) throws RepositoryNotFoundException
      Retrieve the DSpaceRestRepository for the specified category and model in the plural form as used in the endpoints. If the model is available in its singular form use getResourceRepositoryByCategoryAndModel(String, String)
      Parameters:
      apiCategory -
      modelPlural -
      Returns:
      the requested repository.
      Throws:
      RepositoryNotFoundException - passed through.
    • getResourceRepositoryByCategoryAndModel

      public DSpaceRestRepository getResourceRepositoryByCategoryAndModel(String apiCategory, String modelPlural) throws RepositoryNotFoundException
      Retrieve the DSpaceRestRepository for the specified category and model. The model is in the singular form as returned by the RestModel.getType() method
      Parameters:
      apiCategory -
      modelPlural -
      Returns:
      the requested repository.
      Throws:
      RepositoryNotFoundException - if no such repository can be found.
    • 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(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(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<BitstreamFormat> constructBitstreamFormatList(jakarta.servlet.http.HttpServletRequest request, 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<DSpaceObject> constructDSpaceObjectList(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(jakarta.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
    • toResource

      public <T extends HALResource> T toResource(RestModel restObject)
    • 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.
    • castToPKClass

      public Serializable castToPKClass(ReloadableEntityObjectRepository repository, String pkStr)
      Convert the input string in the primary key class according to the repository interface
      Parameters:
      repository -
      pkStr -
      Returns:
    • getDSpaceAPIObjectFromRest

      public Object getDSpaceAPIObjectFromRest(Context context, BaseObjectRest restObj) throws IllegalArgumentException, SQLException
      Return the dspace api model object corresponding to the provided, not null, rest object. This only works when the rest object is supported by a DSpaceRestRepository that also implement the ReloadableEntityObjectRepository interface. If this is not the case the method will throw an IllegalArgumentException
      Parameters:
      context - the DSpace Context
      restObj - the not null rest object. If null the method will throws an IllegalArgumentException
      Returns:
      the dspace api model object corresponding to the provided, not null, rest object
      Throws:
      IllegalArgumentException - if the restObj is not supported by a DSpaceRestRepository that also implement the ReloadableEntityObjectRepository interface
      SQLException - if a database error occur
    • getBaseObjectRestFromUri

      public BaseObjectRest getBaseObjectRestFromUri(Context context, String uri) throws SQLException
      Get the rest object associated with the specified URI
      Parameters:
      context - the DSpace context
      uri - the uri of a BaseObjectRest
      Returns:
      the BaseObjectRest identified by the provided uri
      Throws:
      SQLException - if a database error occur
      IllegalArgumentException - if the uri is not valid
    • getBaseObjectRestFromTypeAndUUID

      public BaseObjectRest getBaseObjectRestFromTypeAndUUID(Context context, String type, String uuid)
      Gets the rest object of of specified type (i.e. "core.item", "core.collection", "workflow.workflowitem",...) having given uuid.
      Parameters:
      context - the DSpace context
      type - Object type
      uuid - Object uuid
      Returns:
      the BaseObjectRest identified by the provided uuid