Package org.dspace.app.rest.utils
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 Summary
Fields Modifier and Type Field Description static intDEFAULT_PAGE_SIZEThe default page size, if unspecified in the request.
-
Constructor Summary
Constructors Constructor Description Utils()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description SerializablecastToPKClass(ReloadableEntityObjectRepository repository, String pkStr)Convert the input string in the primary key class according to the repository interfaceList<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.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 methodreadFromRequest(HttpServletRequest)and fetching the List of Strings from the request.voidembedMethodLevelRels(HALResource<? extends RestAddressableModel> resource)Adds embeds (if the maximum embed level has not been exceeded yet) for all properties annotated with@LinkRelor whose return types areRestAddressableModelsubclasses.voidembedOrLinkClassLevelRels(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.LinkRestfindLinkAnnotation(Method readMethod)Gets the LinkRest annotation for the given method, if any.BaseObjectRestgetBaseObjectRestFromUri(org.dspace.core.Context context, String uri)Get the rest object associated with the specified URILinkRestgetClassLevelLinkRest(String rel, Class<? extends RestAddressableModel> restClass)ObjectgetDSpaceAPIObjectFromRest(org.dspace.core.Context context, BaseObjectRest restObj)Return the dspace api model object corresponding to the provided, not null, rest object.static FilegetFile(org.springframework.web.multipart.MultipartFile multipartFile, String prefixTempName, String suffixTempName)Create a temporary file from a multipart file uploadstatic StringgetFileName(org.springframework.web.multipart.MultipartFile multipartFile)Return the filename part from a multipartFile upload that could eventually contains the full path on the clientLinkRestRepositorygetLinkResourceRepository(String apiCategory, String modelPlural, String rel)Retrieve the LinkRestRepository associated with a specific link from the apiCategory and model specified in the parameters.StringgetMetadataKey(String schema, String element, String qualifier)Build the canonical representation of a metadata key in DSpace.<T> org.springframework.data.domain.Page<T>getPage(List<T> fullContents, org.springframework.data.domain.Pageable optionalPageable)org.springframework.data.domain.PageablegetPageable(org.springframework.data.domain.Pageable optionalPageable)Convenience method to get a default pageable instance if needed.String[]getRepositories()Find the names of allDSpaceRestRepositoryimplementations.DSpaceRestRepositorygetResourceRepository(String apiCategory, String modelPlural)Retrieve theDSpaceRestRepositoryfor the specified category and model in the plural form as used in the endpoints.DSpaceRestRepositorygetResourceRepositoryByCategoryAndModel(String apiCategory, String modelSingular)Retrieve theDSpaceRestRepositoryfor the specified category and model.String[]getSortedUnion(String[]... arrays)Gets the alphanumerically sorted union of multiple string arrays.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.org.springframework.hateoas.LinklinkToSingleResource(RestAddressableModel data, String rel)Create a HAL Link to a single resourceorg.springframework.hateoas.LinklinkToSubResource(RestAddressableModel data, String rel)Create a HAL Link to a subresource of given resource.org.springframework.hateoas.LinklinkToSubResource(RestAddressableModel data, String rel, String path)Create a HAL Link to a subresource of given resource using given path name and link namestatic StringmakeSingular(String modelPlural)ProjectionobtainProjection()Gets the effective projection requested by the current servlet request, orDefaultProjectionif none is specified.MethodrequireMethod(Class clazz, String name)Gets the method with the given name in the given class.<T extends HALResource>
TtoResource(RestModel restObject)
-
-
-
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
-
-
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 itselfrel- 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 resourcerel- 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 resourcerel- name of the subresource link relation to createpath- subpath for the subresource- Returns:
- created Link object
-
getResourceRepository
public DSpaceRestRepository getResourceRepository(String apiCategory, String modelPlural) throws RepositoryNotFoundException
Retrieve theDSpaceRestRepositoryfor the specified category and model in the plural form as used in the endpoints. If the model is available in its singular form usegetResourceRepositoryByCategoryAndModel(String, String)- Parameters:
apiCategory-modelPlural-- Returns:
- the requested repository.
- Throws:
RepositoryNotFoundException- passed through.
-
getResourceRepositoryByCategoryAndModel
public DSpaceRestRepository getResourceRepositoryByCategoryAndModel(String apiCategory, String modelSingular) throws RepositoryNotFoundException
Retrieve theDSpaceRestRepositoryfor the specified category and model. The model is in the singular form as returned by theRestModel.getType()method- Parameters:
apiCategory-modelSingular-- Returns:
- the requested repository.
- Throws:
RepositoryNotFoundException- if no such repository can be found.
-
getRepositories
public String[] getRepositories()
Find the names of allDSpaceRestRepositoryimplementations.- Returns:
- the names of all repository types.
-
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 apiCategorymodelPlural- the model name in its plural formrel- 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 typeprefixTempName- the prefix to use to generate the filename of the temporary filesuffixTempName- the suffix to use to generate the filename of the temporary file- Returns:
- the temporary file on the server
- Throws:
IOExceptionFileNotFoundException
-
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:
IOExceptionFileNotFoundException
-
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 thegetStringListFromRequest(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 BitstreamFormatscontext- 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 methodreadFromRequest(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 contextlist- 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
-
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, orDefaultProjectionif none is specified.Any number of individual
Projectionsthat are spring-registeredComponents may be specified by name via theprojectionparameter. If multiple projections are specified, they will be wrapped in aCompositeProjectionand applied in order as described there.In addition, any number of embeds may be specified by rel name via the
embedparameter. When provided, these act as an "allow list" of embeds that may be included in the response, as described and implemented byEmbedRelsProjection.- 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@LinkRelor whose return types areRestAddressableModelsubclasses.- 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
nullif 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(org.dspace.core.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 aDSpaceRestRepositorythat also implement theReloadableEntityObjectRepositoryinterface. If this is not the case the method will throw an IllegalArgumentException- Parameters:
context- the DSpace ContextrestObj- the not null rest object. If null the method will throws anIllegalArgumentException- Returns:
- the dspace api model object corresponding to the provided, not null, rest object
- Throws:
IllegalArgumentException- if the restObj is not supported by aDSpaceRestRepositorythat also implement theReloadableEntityObjectRepositoryinterfaceSQLException- if a database error occur
-
getBaseObjectRestFromUri
public BaseObjectRest getBaseObjectRestFromUri(org.dspace.core.Context context, String uri) throws SQLException
Get the rest object associated with the specified URI- Parameters:
context- the DSpace contexturi- the uri of aBaseObjectRest- Returns:
- the
BaseObjectRestidentified by the provided uri - Throws:
SQLException- if a database error occurIllegalArgumentException- if the uri is not valid
-
-