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)
-
-
Constructor Summary
Constructors Constructor Description Utils()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description List<BitstreamFormat>constructBitstreamFormatList(javax.servlet.http.HttpServletRequest request, Context context)This method will construct a List of BitstreamFormats out of a request.List<DSpaceObject>constructDSpaceObjectList(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)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.LinkRestgetClassLevelLinkRest(String rel, Class<? extends RestAddressableModel> restClass)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 fullpath 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()DSpaceRestRepositorygetResourceRepository(String apiCategory, String modelPlural)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(DSpaceResource r, String rel)org.springframework.hateoas.LinklinkToSingleResource(RestAddressableModel data, String rel)org.springframework.hateoas.LinklinkToSubResource(RestAddressableModel data, String rel)org.springframework.hateoas.LinklinkToSubResource(RestAddressableModel data, String rel, String path)static StringmakeSingular(String modelPlural)ProjectionobtainProjection()Gets the 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)
-
-
-
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(DSpaceResource r, String rel)
-
linkToSingleResource
public org.springframework.hateoas.Link linkToSingleResource(RestAddressableModel data, String rel)
-
linkToSubResource
public org.springframework.hateoas.Link linkToSubResource(RestAddressableModel data, String rel)
-
linkToSubResource
public org.springframework.hateoas.Link linkToSubResource(RestAddressableModel data, String rel, String path)
-
getResourceRepository
public DSpaceRestRepository getResourceRepository(String apiCategory, String modelPlural)
-
getRepositories
public String[] getRepositories()
-
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. ie. [. ] - Parameters:
schema-element-- 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 orginal file name and the mimetypeprefixTempName- the prefix to use to generate the filename of the temporary filesuffixTempName- the suffic 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 fullpath on the client- Parameters:
multipartFile- the file uploaded- Returns:
- the filename part of the file on the client filesystem
- Throws:
IOExceptionFileNotFoundException
-
constructBitstreamFormatList
public List<BitstreamFormat> constructBitstreamFormatList(javax.servlet.http.HttpServletRequest request, 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<DSpaceObject> constructDSpaceObjectList(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 context- 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 projection requested by the current servlet request, orDefaultProjectionif none is specified.- 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)
Adds embeds or links for all class-level LinkRel annotations for which embeds or links are allowed.- Parameters:
halResource- the resource.
-
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.
-
-