Package de.unirostock.sems.cbarchive.web
Class Tools
- java.lang.Object
-
- de.unirostock.sems.cbarchive.web.Tools
-
public class Tools extends Object
The Class Tools.- Author:
- Martin Scharm
-
-
Field Summary
Fields Modifier and Type Field Description static SimpleDateFormatDATE_FORMATTERThe Constant DATE_FORMATTER.
-
Constructor Summary
Constructors Constructor Description Tools()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static voidaddOmexMetaData(de.unirostock.sems.cbarchive.meta.MetaDataHolder entity, de.unirostock.sems.cbarchive.meta.omex.VCard creator, boolean create)Adds current date as modification and adds the creator, if not done yet, to every Omex description Also creates new Omex description, if create is set to true and only if necessary.static voidaddOmexMetaData(de.unirostock.sems.cbarchive.meta.MetaDataHolder entity, de.unirostock.sems.cbarchive.meta.omex.VCard creator, String additionalDescription, boolean create)Adds current date as modification and an additional description, as well as the creator, if not done yet, to every Omex description.static booleanareVCardEqual(de.unirostock.sems.cbarchive.meta.omex.VCard vcard1, de.unirostock.sems.cbarchive.meta.omex.VCard vcard2)Compares 2 VCards and returns true if both are identical in means of String.equal() or if both are nullstatic booleancheckQuota(long currentValue, long quota)Returns false, if a quota is exceeded.static voidcheckQuotasOrFail(long fileSize, Archive archive, UserManager user)Checks for all quotas required to add/update an file within a CombineArchive.static StringcleanUpFileName(String fileName)removes all non alpha-numeric symbols from a file name.static booleancontainsVCard(Collection<de.unirostock.sems.cbarchive.meta.omex.VCard> collection, de.unirostock.sems.cbarchive.meta.omex.VCard vcard)Checks if the given VCard exists already in the Collection.static longcopyStream(InputStream input, OutputStream output, long maxLength)Copies an InputStream into an OutputStream and closes all streams afterwards.static UserManagerdoLogin(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)Tries to obtain user instance (workspace), if fails it crates a new onestatic UserManagerdoLogin(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, boolean createNew)Tries to obtain user instance (workspace)
if createNew is true, it also tries to create a new user instance.static StringextractFileName(javax.servlet.http.Part part)Extract file name.static URIgenerateArchiveRedirectUri(javax.servlet.http.HttpServletRequest requestContext, String archiveId)Generates a redirect URI to an archive.static StringgenerateHashId(String input)Generate hash id.static URIgenerateWorkspaceRedirectUri(javax.servlet.http.HttpServletRequest requestContext, String workspaceId)Generates Share URI to a workspace.static UserManagergetUser(CookieManager cookies)Gets the user.static booleanisFilenameBlacklisted(String filename)checks whether a filename is blacklisted or notstatic voidstoreUserCookies(CookieManager cookies, UserManager user)Store user cookies.static StringsuggestFileNameFromHttpResponse(String remoteUrl, org.apache.http.HttpResponse response)Suggests a filename for the queried file, base on the Content-Disposition Header field or the URL.static StringsuggestFileNameFromHttpResponse(org.apache.http.HttpRequest request, org.apache.http.HttpResponse response)Suggests a filename for the queried file, base on the Content-Disposition Header field or the URL.static StringsuggestFileNameFromHttpResponse(org.apache.http.HttpResponse response)Suggests a filename for the queried file, base on the Content-Disposition Header field.static PathwriteStreamToTempFile(String tempFileName, InputStream input)writes a input stream entirely into a newly created temp file.
-
-
-
Field Detail
-
DATE_FORMATTER
public static final SimpleDateFormat DATE_FORMATTER
The Constant DATE_FORMATTER.
-
-
Method Detail
-
doLogin
public static UserManager doLogin(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws CombineArchiveWebException, CombineArchiveWebCriticalException
Tries to obtain user instance (workspace), if fails it crates a new one- Parameters:
request- the requestresponse- the response- Returns:
- the user manager
- Throws:
CombineArchiveWebException- signals default problemsCombineArchiveWebCriticalException- signals critical problems
-
doLogin
public static UserManager doLogin(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, boolean createNew) throws CombineArchiveWebException, CombineArchiveWebCriticalException
Tries to obtain user instance (workspace)
if createNew is true, it also tries to create a new user instance.- Parameters:
request- the requestresponse- the responsecreateNew- try to create new user instance?- Returns:
- the user manager
- Throws:
CombineArchiveWebException- signals default problemsCombineArchiveWebCriticalException- signals critical problems
-
getUser
public static UserManager getUser(CookieManager cookies) throws IOException
Gets the user.- Parameters:
cookies- the cookies- Returns:
- the user
- Throws:
IOException- the IO exception
-
storeUserCookies
public static void storeUserCookies(CookieManager cookies, UserManager user)
Store user cookies.- Parameters:
cookies- the cookiesuser- the user
-
extractFileName
public static final String extractFileName(javax.servlet.http.Part part)
Extract file name.- Parameters:
part- the part- Returns:
- the string
-
generateHashId
public static String generateHashId(String input)
Generate hash id.- Parameters:
input- the input- Returns:
- the hash id
-
checkQuota
public static boolean checkQuota(long currentValue, long quota)Returns false, if a quota is exceeded. Otherwise true- Parameters:
currentValue- the current sizequota- the quota size- Returns:
- true, if quota is not exceeded
-
checkQuotasOrFail
public static void checkQuotasOrFail(long fileSize, Archive archive, UserManager user) throws QuotaExceptionChecks for all quotas required to add/update an file within a CombineArchive. If a quota is exceeded it fails by throwing an QuotaException.- Parameters:
fileSize- of the uploaded filearchive- the archiveuser- the user- Throws:
QuotaException- an error if the quotas on the server are exhausted
-
generateArchiveRedirectUri
public static URI generateArchiveRedirectUri(javax.servlet.http.HttpServletRequest requestContext, String archiveId)
Generates a redirect URI to an archive.- Parameters:
requestContext- the request contextarchiveId- the archive id- Returns:
- the uri
-
generateWorkspaceRedirectUri
public static URI generateWorkspaceRedirectUri(javax.servlet.http.HttpServletRequest requestContext, String workspaceId)
Generates Share URI to a workspace.- Parameters:
requestContext- the request contextworkspaceId- the workspace id- Returns:
- the uri
-
isFilenameBlacklisted
public static boolean isFilenameBlacklisted(String filename)
checks whether a filename is blacklisted or not- Parameters:
filename- the file name- Returns:
- true if filename is blacklisted
-
addOmexMetaData
public static void addOmexMetaData(de.unirostock.sems.cbarchive.meta.MetaDataHolder entity, de.unirostock.sems.cbarchive.meta.omex.VCard creator, boolean create)Adds current date as modification and adds the creator, if not done yet, to every Omex description Also creates new Omex description, if create is set to true and only if necessary.- Parameters:
entity- the entitycreator- the creatorcreate- should omex description be created?
-
addOmexMetaData
public static void addOmexMetaData(de.unirostock.sems.cbarchive.meta.MetaDataHolder entity, de.unirostock.sems.cbarchive.meta.omex.VCard creator, String additionalDescription, boolean create)Adds current date as modification and an additional description, as well as the creator, if not done yet, to every Omex description. Also creates new Omex description, if create is set to true and only if necessary- Parameters:
entity- the entitycreator- the creatoradditionalDescription- the additional descriptioncreate- should omex description be created?
-
containsVCard
public static boolean containsVCard(Collection<de.unirostock.sems.cbarchive.meta.omex.VCard> collection, de.unirostock.sems.cbarchive.meta.omex.VCard vcard)
Checks if the given VCard exists already in the Collection.- Parameters:
collection- the vcard collectionvcard- the vcard in question- Returns:
- true, if collection contains vcard
-
areVCardEqual
public static boolean areVCardEqual(de.unirostock.sems.cbarchive.meta.omex.VCard vcard1, de.unirostock.sems.cbarchive.meta.omex.VCard vcard2)Compares 2 VCards and returns true if both are identical in means of String.equal() or if both are null- Parameters:
vcard1- the vcard1vcard2- the vcard2- Returns:
- true, if vcards are equal
-
copyStream
public static long copyStream(InputStream input, OutputStream output, long maxLength) throws IOException
Copies an InputStream into an OutputStream and closes all streams afterwards. Stops at max length.- Parameters:
input- the inputoutput- the outputmaxLength- the max length- Returns:
- the size of the stream copied
- Throws:
IOException- the IO exception
-
writeStreamToTempFile
public static Path writeStreamToTempFile(String tempFileName, InputStream input) throws IOException
writes a input stream entirely into a newly created temp file. Closes all streams afterwards- Parameters:
tempFileName- the temp file nameinput- the input- Returns:
- Path to temp file
- Throws:
IOException- the IO exception
-
suggestFileNameFromHttpResponse
public static String suggestFileNameFromHttpResponse(org.apache.http.HttpRequest request, org.apache.http.HttpResponse response)
Suggests a filename for the queried file, base on the Content-Disposition Header field or the URL.- Parameters:
request- the requestresponse- the response- Returns:
- A name suggestion or null
-
suggestFileNameFromHttpResponse
public static String suggestFileNameFromHttpResponse(String remoteUrl, org.apache.http.HttpResponse response)
Suggests a filename for the queried file, base on the Content-Disposition Header field or the URL.- Parameters:
remoteUrl- the remote urlresponse- the response- Returns:
- A name suggestion or null
-
suggestFileNameFromHttpResponse
public static String suggestFileNameFromHttpResponse(org.apache.http.HttpResponse response)
Suggests a filename for the queried file, base on the Content-Disposition Header field.- Parameters:
response- the response- Returns:
- A name suggestion or null
-
-