Package org.cip4.jdflib.util
Class UrlUtil
java.lang.Object
org.cip4.jdflib.util.UrlUtil
- Direct Known Subclasses:
MimeUtil
collection of helper routines to convert urls
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classDeprecated.use the real classstatic enumlist of all http methodsstatic enumrough classification of protocol typestatic classDeprecated.use the real class -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final Stringstatic final Stringzip, maybe?static final Stringstatic final Stringpdf, duh...static final Stringps, duh...static final Stringthe preferred value for XML!static final Stringzip, maybe?static final Stringstatic final Stringstatic final Stringstatic final Stringstatic final Stringstatic final Stringstatic final Stringstatic final Stringstatic final Stringmore commonly used stringsstatic final Stringstatic final Stringmore commonly used stringsstatic final Stringstatic final Stringstatic final Stringstatic final Stringhttp get methodstatic final Stringhttp head methodstatic final Stringstatic final Stringstatic final Stringstatic final Stringstrings that must be escaped in uncsstatic final Stringstrings that must be escaped in urlsstatic final intstatic final Stringstatic final Stringstatic final Stringhttp post methodstatic final Stringhttp put methodstatic final Stringstatic final Stringstatic final Stringstatic final Stringstatic final Stringstatic final Stringstatic final Stringstatic final Stringstatic final Stringstatic final Stringstatic final Stringstatic final Stringstatic final Stringstatic final Stringstatic final Stringstatic final Stringstatic final String -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic StringaddParameter(String baseUrl, String key, String val) adds a parameter to a given url using either ? or &static Stringadds a path to a given url , keeping the parametersstatic Stringsecurely add a subdirectorystatic Stringremove any internal "../" "./" and "//" from a urlstatic StringcleanHttpURL(String url) create a "real" url from a user input url add http://static StringcreateHttpUrl(boolean bSecure, String host, int port, String path) get a prinect url for this host and pathstatic Stringstandard url escapingstatic Stringstandard url escapingstatic Stringget the filename extension of pathName excluding the '.' if no '.' is found, returns null if trailing .static StringConvert a File to a valid file URL or IRL
note that some internal functions use network protocol and therefor performance may be non-optimalstatic byte[]getBytesFromIP(String ip) get the ip address as a set of bytes with the left byte at pos 0static InputStreamgetCidURLStream(String url, javax.mail.Multipart multipart) get a readable inputstream from the CID urlstatic intReturns connection timeout in milliseconds as integer.static FilegetCreateDirectory(String newDir) Deprecated.use FileUtil.getCreateDirectory(newDir);static StringgetExtensionFromMimeType(String contentType) generates the correct extension for a given mime content typestatic StringgetFileName(String url, javax.mail.Multipart mp) get the file name for a url.static StringgetIPFromBytes(byte[] ip) get the ip address as a string with the left byte at pos 0static StringgetLocalURL(String directory, String url) get the local url without directory schemes in the base url are case insensitive, all others are case sensitivestatic StringgetMimeTypeFromURL(String url) generates the correct MIMEType for a given URL and sets itstatic StringgetParameter(String baseUrl, String key) static StringgetParentDirectory(String url) get the parent directory of a file or urlstatic UrlUtil.URLProtocolgetProtocol(String url) returns the general protocol type of a urlstatic StringgetRelativePath(File f, File fCWD) returns the relative URL of a file relative to the current working directory
this includes escaping of %20 etc.static StringgetRelativeURL(File f, File baseDir, boolean bEscape128) returns the relative URL of a file relative to the current working directorystatic StringgetSecurePath(String url, boolean allowAbsolute) secure check of a file pathstatic InputStreamgetURLInputStream(String urlString) static InputStreamgetURLInputStream(String urlString, javax.mail.BodyPart bodyPart) get the opened input stream for a given url stringstatic InputStreamgetURLInputStream(String urlString, javax.mail.BodyPart bodyPart, ZipReader zr) get the opened input stream for a given url stringstatic UrlPart[]getURLParts(HttpURLConnection connection) get an array of urlparts, regardless of whether this was mime or not if the stream is mime/multipart get also extract thatstatic StringgetURLWithDirectory(String directory, String url) concatenate directory and url to a single path IF and only IF url is a relative url
relative urls MUST NOT have a scheme (e.g.static booleantest whether a given url is a cid (cid:)static booleantest whether a given url is escaped as utf-8static booleanis this a valid file urlstatic booleanchecks whether there is a remote chance that the file is useful for readingstatic booleantest whether a given url is an http url (excluding https - @see isHttps)static booleantest whether a given url is an http url (excluding https - @see isHttps)static booleantest whether a given url is an https urlstatic booleanstatic booleanisJSONType(String contentType) check whether the mime type is a known xml dialectstatic booleancheck whether a file is a mime file only check extensions TODO sniff file rather than check extensionsstatic booleanisMIMEExtenstension(String lower) check whether a file is a mime filestatic booleantest whether a given url is any network style url, currently either http, https or ftpstatic booleantest whether a given url is NOT a cid this may be a local identifierstatic booleanisRedirect(int responseCode) return true if the response code should redirectstatic booleanisRelativeURL(String url) if true this url is relativestatic booleanisReturnCodeOK(int responseCode) return true if the response code should redirectstatic booleanreturn true if the response code should redirectstatic booleantest whether a given url is a unc pathstatic booleanstatic booleanisWindowsLocalPath(String pathName) static booleancheck whether the mime type is a known xml dialectstatic booleancheck whether the url type is a known zipstatic booleancheck whether the mime type is a known zip dialectstatic FilemoveToDir(IURLSetter parent, File dir, boolean overWrite) Deprecated.use moveToDir(parent, dir, null, overWrite);static FilemoveToDir(IURLSetter urlSetter, File dir, String cwd, boolean overWrite) physically store the file at the location specified in dir and also modify parent to reflect the new locationstatic FilemoveToDir(IURLSetter urlSetter, File dir, String cwd, boolean overWrite, boolean deleteFile) physically store the file at the location specified in dir and also modify parent to reflect the new locationstatic StringnewExtension(String strWork, String newExt) replace the .extension of a file namestatic Stringnormalize a url string by grinding it through urlstatic Stringinverse of extensionstatic StringremoveExtension(String pathName) Deprecated.use prefix(pathName)static StringremoveProtocol(String url) remove the protocol part of a url, if it is specifiedstatic voidsetConnectionTimeout(int timeout) Returns connection timeout in milliseconds as integer.static StringsetParameter(String baseUrl, String key, String val) adds a parameter to a given url using either ? or &static URLstringToURL(String urlString) Create a URL for any url string using heuristics and escapingstatic Stringstatic Stringstandard url unescapingstatic FileRetrieve a file for a relative or absolute file urlstatic StringurlToFileName(String urlString) static StringurlToString(URL url) null safe url to string converterstatic StringRetrieve a UNC path for a relative or absolute file url, any of '/'or "\\" is assumed a path separatorstatic UrlPartwriterToURL(String strUrl, IStreamWriter streamWriter, String method, String contentType, HTTPDetails det) write the contents of an IStreamWriter to an output URL File: and http: are currently supported Use HttpURLConnection.getInputStream() to retrieve the http responsestatic UrlPartwriteToURL(String strUrl, InputStream stream, String method, String contentType, HTTPDetails det) write a Stream to an output URL File: and http: are currently supported Use HttpURLConnection.getInputStream() to retrieve the http response
-
Field Details
-
ZIP
- See Also:
-
FILE
- See Also:
-
FTP
- See Also:
-
HTTPS
- See Also:
-
HTTP
- See Also:
-
MAX_STREAM
public static final int MAX_STREAM- See Also:
-
POST
http post method -
PUT
http put method -
GET
http get method -
HEAD
http head method -
DELETE
-
CONNECT
-
OPTIONS
-
TRACE
-
PATCH
-
KEEPALIVE
- See Also:
-
ACCEPT
- See Also:
-
CLOSE
- See Also:
-
CONTENT_TRANSFER_ENCODING
- See Also:
-
m_URIEscape
strings that must be escaped in urls- See Also:
-
m_UNCEscape
strings that must be escaped in uncs- See Also:
-
TEXT_HTML
- See Also:
-
TEXT_PLAIN
- See Also:
-
TEXT_UNKNOWN
- See Also:
-
APPLICATION_XML
the preferred value for XML!- See Also:
-
APPLICATION_CFF2
zip, maybe?- See Also:
-
APPLICATION_ZIP
- See Also:
-
APPLICATION_XZIP
zip, maybe?- See Also:
-
APPLICATION_PDF
pdf, duh...- See Also:
-
APPLICATION_PS
ps, duh...- See Also:
-
TEXT_XML
- See Also:
-
TEXT_CSV
- See Also:
-
TEXT_JSON
- See Also:
-
APPLICATION_JSON
- See Also:
-
VND_PPF
- See Also:
-
VND_PTK
- See Also:
-
VND_PTK_J
- See Also:
-
VND_JDF
- See Also:
-
VND_JMF
- See Also:
-
VND_XJDF
- See Also:
-
VND_XJMF
- See Also:
-
VND_XJDF_J
- See Also:
-
VND_XJMF_J
- See Also:
-
CONTENT_ID
- See Also:
-
CONTENT_TYPE
more commonly used strings- See Also:
-
CONTENT_LENGTH
more commonly used strings- See Also:
-
BASE64
- See Also:
-
BINARY
- See Also:
-
CONNECTION
- See Also:
-
AUTHORIZATION
- See Also:
-
-
Constructor Details
-
UrlUtil
public UrlUtil()
-
-
Method Details
-
setConnectionTimeout
public static void setConnectionTimeout(int timeout) Returns connection timeout in milliseconds as integer. -
getConnectionTimeout
public static int getConnectionTimeout()Returns connection timeout in milliseconds as integer.- Returns:
- connectionTimeout in milliseconds
-
getRelativeURL
returns the relative URL of a file relative to the current working directory- Parameters:
f- the file to get the relative url forbaseDir- the file that describes cwd, ifnullcwd is calculatedbEscape128- if true, escape > 128 (URL), else retain (IRL)- Returns:
-
createHttpUrl
get a prinect url for this host and path- Parameters:
bSecure- if true, make httpshost- hostnameport- the port, duhpath- may be null- Returns:
-
getRelativePath
returns the relative URL of a file relative to the current working directory
this includes escaping of %20 etc.- Parameters:
f- the file to get the relative path forfCWD- the file that describes cwd, ifnullcwd is calculated from user.dir- Returns:
-
getFileName
get the file name for a url. extract it from a mime package, if appropriate- Parameters:
url-mp-- Returns:
-
getCidURLStream
get a readable inputstream from the CID url- Parameters:
url- the url to get a stream formultipart- the multipart mime to which the cid refers- Returns:
- InputStream - the readable input stream that this filespec refers to,
nullif broken or non-existent
-
extension
get the filename extension of pathName excluding the '.' if no '.' is found, returns null if trailing . is found, returns ""- Parameters:
pathName- the pathName to get the extension for- Returns:
- String - the filename extension
-
removeExtension
Deprecated.use prefix(pathName)get the path name without extension of pathName- Parameters:
pathName- the pathName to get the extension for- Returns:
- String - the filename without extension
-
getURLParts
get an array of urlparts, regardless of whether this was mime or not if the stream is mime/multipart get also extract that- Parameters:
connection-- Returns:
- the array of body parts input stream
-
getURLInputStream
public static InputStream getURLInputStream(String urlString, javax.mail.BodyPart bodyPart, ZipReader zr) get the opened input stream for a given url string- Parameters:
urlString-bodyPart-zr-- Returns:
-
getURLInputStream
get the opened input stream for a given url string- Parameters:
urlString-bodyPart-- Returns:
-
getURLInputStream
- Parameters:
urlString-- Returns:
-
getCreateDirectory
Deprecated.use FileUtil.getCreateDirectory(newDir);create a new directory and return null if the directory could not be created- Parameters:
newDir- the path or URL of the new directory- Returns:
-
fileToUrl
Convert a File to a valid file URL or IRL
note that some internal functions use network protocol and therefor performance may be non-optimal- Parameters:
f- the File to parse,bEscape128- if true, escape non -ascii chars (URI), if false, don't (IRI)- Returns:
- the URL string
-
urlToFileName
- Parameters:
urlString- the string to parse for a file name- Returns:
- the filename
-
urlToFile
Retrieve a file for a relative or absolute file url- Parameters:
urlString- the file url to retrieve a file for- Returns:
- the file located at url
-
urlToUNC
Retrieve a UNC path for a relative or absolute file url, any of '/'or "\\" is assumed a path separator- Parameters:
urlString- the file url to retrieve a UNC path for- Returns:
- the UNC
-
uncToUrl
- Parameters:
unc-escape128- if true escape chars>128- Returns:
-
urlToString
null safe url to string converter- Parameters:
url-- Returns:
-
addParameter
adds a parameter to a given url using either ? or &- Parameters:
baseUrl- the base url - already escaped and ready to gokey- the key to add - NOT escapedval- the value to add - NOT escaped - if null nothing is set- Returns:
- the escaped new url
-
getSecurePath
public static String getSecurePath(String url, boolean allowAbsolute) throws IllegalArgumentException secure check of a file path- Parameters:
baseFile-file-- Returns:
- Throws:
IllegalArgumentException
-
setParameter
adds a parameter to a given url using either ? or &- Parameters:
baseUrl- the base url - already escaped and ready to gokey- the key to add - NOT escapedval- the value to add - NOT escaped - if null nothing is set- Returns:
- the escaped new url
-
getParameter
-
addPath
adds a path to a given url , keeping the parameters- Parameters:
baseUrl- the base url - already escaped and ready to gopath- the path to add- Returns:
- the escaped new url
- Throws:
IllegalArgumentException
-
escape
standard url escaping- Parameters:
toEscape- the string to escapebEscape128- if true, also escape >128, else leave non-ascii7 as is- Returns:
- the escaped string
-
escape
standard url escaping- Parameters:
toEscape- the string to escapebEscape128- if true, also escape >128, else leave non-ascii7 as isescapeSlash- if true also escape '/'- Returns:
- the escaped string
-
unEscape
standard url unescaping- Parameters:
toEscape- the string to unescape- Returns:
- the escaped string
-
stringToURL
Create a URL for any url string using heuristics and escaping- Parameters:
urlString- the file url to retrieve a file for- Returns:
-
isFileOK
checks whether there is a remote chance that the file is useful for reading- Parameters:
f- - File to check- Returns:
- true if the file is ok
-
isEscaped
test whether a given url is escaped as utf-8- Parameters:
url- the url to test- Returns:
-
getExtensionFromMimeType
generates the correct extension for a given mime content type- Parameters:
contentType-- Returns:
-
getMimeTypeFromURL
generates the correct MIMEType for a given URL and sets it- Parameters:
url-- Returns:
-
isCID
test whether a given url is a cid (cid:)- Parameters:
url- the url to test- Returns:
-
isNotCID
test whether a given url is NOT a cid this may be a local identifier- Parameters:
url- the url to test- Returns:
- true if we are either a cid or a local url
-
isFile
is this a valid file url- Parameters:
url-- Returns:
-
isWindowsLocalPath
- Parameters:
pathName-- Returns:
-
isHttp
test whether a given url is an http url (excluding https - @see isHttps)- Parameters:
url- the url to test- Returns:
-
isFtp
test whether a given url is an http url (excluding https - @see isHttps)- Parameters:
url- the url to test- Returns:
-
isHttps
test whether a given url is an https url- Parameters:
url- the url to test- Returns:
-
isNet
test whether a given url is any network style url, currently either http, https or ftp- Parameters:
url- the url to test- Returns:
-
normalize
normalize a url string by grinding it through url- Parameters:
urlString-- Returns:
- the normalized string, null if not a valid url
-
isUNC
test whether a given url is a unc path- Parameters:
pathName-- Returns:
-
isMIME
check whether a file is a mime file only check extensions TODO sniff file rather than check extensions- Parameters:
file- the FILE to check- Returns:
- true if the file is a MIME file
-
getProtocol
returns the general protocol type of a url- Parameters:
url-- Returns:
-
getIPFromBytes
get the ip address as a string with the left byte at pos 0- Parameters:
ip-- Returns:
-
getBytesFromIP
get the ip address as a set of bytes with the left byte at pos 0- Parameters:
ip-- Returns:
-
isMIMEExtenstension
check whether a file is a mime file- Parameters:
lower-- Returns:
-
isIRL
- Parameters:
val-- Returns:
-
removeProtocol
remove the protocol part of a url, if it is specified- Parameters:
url- the url string to work on- Returns:
- the input string without the protocol and ":"
-
isURL
- Parameters:
val-- Returns:
-
getLocalURL
get the local url without directory schemes in the base url are case insensitive, all others are case sensitive- Parameters:
directory- the url of the directoryurl- the absolute url- Returns:
- String - the local URL of url after removing directory
-
addSecure
securely add a subdirectory- Parameters:
baseURL-url-- Returns:
- Throws:
IllegalArgumentException
-
getURLWithDirectory
concatenate directory and url to a single path IF and only IF url is a relative url
relative urls MUST NOT have a scheme (e.g. file:)- Parameters:
directory- the url of the directoryurl- the relative url of the file- Returns:
- String - the concatenated URL of the directory + file
-
getParentDirectory
get the parent directory of a file or url- Parameters:
url-- Returns:
-
cleanDots
remove any internal "../" "./" and "//" from a url- Parameters:
url- the url to clean- Returns:
- String - the clean path
-
cleanHttpURL
create a "real" url from a user input url add http://- Parameters:
url- the input url- Returns:
- the - hopefully - usable url
-
writeToURL
public static UrlPart writeToURL(String strUrl, InputStream stream, String method, String contentType, HTTPDetails det) write a Stream to an output URL File: and http: are currently supported Use HttpURLConnection.getInputStream() to retrieve the http response- Parameters:
strUrl- the URL to write tostream- the input stream to read frommethod- HEAD, GET or POSTcontentType- the contenttype to set, if NULL defaults to TEXT/UNKNOWNdet-- Returns:
UrlPartthe opened http connection, null in case of error
-
writerToURL
public static UrlPart writerToURL(String strUrl, IStreamWriter streamWriter, String method, String contentType, HTTPDetails det) write the contents of an IStreamWriter to an output URL File: and http: are currently supported Use HttpURLConnection.getInputStream() to retrieve the http response- Parameters:
strUrl- the URL to write tostreamWriter- the IStreamWriter to read frommethod- HEAD, GET or POSTcontentType- the contenttype to set, if NULL defaults to TEXT/UNKNOWNdet-- Returns:
UrlPartthe opened http connection, null in case of error
-
moveToDir
Deprecated.use moveToDir(parent, dir, null, overWrite);physically store the file at the location specified in dir and also modify this to reflect the new location- Parameters:
parent- the parent element, trypically a filespec or previewdir- the directory to move to. dir is created if it does not exist. If dir exists and dir is not a directory, the call fails and null is returnedoverWrite- if true, zapp any old files with the same name- Returns:
- the file that corresponds to the moved url reference, null if an error occurred
-
moveToDir
physically store the file at the location specified in dir and also modify parent to reflect the new location- Parameters:
urlSetter- the parent element, typically a filespec or previewdir- the directory to move to. dir is created if it does not exist. If dir exists and dir is not a directory, the call fails and null is returnedcwd- the current working dir for local urlsoverWrite- if true, zapp any old files with the same name- Returns:
- the file that corresponds to the moved url reference, null if an error occurred
-
moveToDir
public static File moveToDir(IURLSetter urlSetter, File dir, String cwd, boolean overWrite, boolean deleteFile) physically store the file at the location specified in dir and also modify parent to reflect the new location- Parameters:
urlSetter- the parent element, typically a filespec or previewdir- the directory to move to. dir is created if it does not exist. If dir exists and dir is not a directory, the call fails and null is returnedcwd- the current working dir for local urlsoverWrite- if true, zapp any old files with the same name- Returns:
- the file that corresponds to the moved url reference, null if an error occurred
- Throws:
IllegalArgumentException- in case the filename contains '..'
-
isRelativeURL
if true this url is relative- Parameters:
url- the url string to test- Returns:
- true if relative
-
isXMLType
check whether the mime type is a known xml dialect- Parameters:
contentType-- Returns:
-
isJSONType
check whether the mime type is a known xml dialect- Parameters:
contentType-- Returns:
-
isZIPType
check whether the mime type is a known zip dialect- Parameters:
contentType-- Returns:
-
isZip
check whether the url type is a known zip- Parameters:
url-- Returns:
-
newExtension
replace the .extension of a file name- Parameters:
strWork- the file pathnewExt- the new extension (works with or without the initial "."- Returns:
- the strWork with a replaced extension
-
prefix
inverse of extension- Parameters:
strWork- the string to work on- Returns:
- the prefix
-
isRedirect
public static boolean isRedirect(int responseCode) return true if the response code should redirect- Parameters:
responseCode-- Returns:
-
isReturnCodeOK
public static boolean isReturnCodeOK(int responseCode) return true if the response code should redirect- Parameters:
responseCode-- Returns:
-
isReturnCodeOK
return true if the response code should redirect- Parameters:
responseCode-- Returns:
-