Class UrlUtil

java.lang.Object
org.cip4.jdflib.util.UrlUtil
Direct Known Subclasses:
MimeUtil

public class UrlUtil extends Object
collection of helper routines to convert urls
  • Field Details

  • 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

      public static String getRelativeURL(File f, File baseDir, boolean bEscape128)
      returns the relative URL of a file relative to the current working directory
      Parameters:
      f - the file to get the relative url for
      baseDir - the file that describes cwd, if null cwd is calculated
      bEscape128 - if true, escape > 128 (URL), else retain (IRL)
      Returns:
    • createHttpUrl

      public static String createHttpUrl(boolean bSecure, String host, int port, String path)
      get a prinect url for this host and path
      Parameters:
      bSecure - if true, make https
      host - hostname
      port - the port, duh
      path - may be null
      Returns:
    • getRelativePath

      public static String getRelativePath(File f, File fCWD)
      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 for
      fCWD - the file that describes cwd, if null cwd is calculated from user.dir
      Returns:
    • getFileName

      public static String getFileName(String url, javax.mail.Multipart mp)
      get the file name for a url. extract it from a mime package, if appropriate
      Parameters:
      url -
      mp -
      Returns:
    • getCidURLStream

      public static InputStream getCidURLStream(String url, javax.mail.Multipart multipart)
      get a readable inputstream from the CID url
      Parameters:
      url - the url to get a stream for
      multipart - the multipart mime to which the cid refers
      Returns:
      InputStream - the readable input stream that this filespec refers to, null if broken or non-existent
    • extension

      public static String extension(String pathName)
      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 public static String removeExtension(String pathName)
      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

      public static 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 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

      public static InputStream getURLInputStream(String urlString, javax.mail.BodyPart bodyPart)
      get the opened input stream for a given url string
      Parameters:
      urlString -
      bodyPart -
      Returns:
    • getURLInputStream

      public static InputStream getURLInputStream(String urlString)
      Parameters:
      urlString -
      Returns:
    • getCreateDirectory

      @Deprecated public static File getCreateDirectory(String newDir)
      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

      public static String fileToUrl(File f, boolean bEscape128)
      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

      public static String urlToFileName(String urlString)
      Parameters:
      urlString - the string to parse for a file name
      Returns:
      the filename
    • urlToFile

      public static File urlToFile(String urlString)
      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

      public static String urlToUNC(String urlString)
      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

      public static String uncToUrl(String unc, boolean escape128)
      Parameters:
      unc -
      escape128 - if true escape chars>128
      Returns:
    • urlToString

      public static String urlToString(URL url)
      null safe url to string converter
      Parameters:
      url -
      Returns:
    • addParameter

      public static String addParameter(String baseUrl, String key, String val)
      adds a parameter to a given url using either ? or &
      Parameters:
      baseUrl - the base url - already escaped and ready to go
      key - the key to add - NOT escaped
      val - 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

      public static String setParameter(String baseUrl, String key, String val)
      adds a parameter to a given url using either ? or &
      Parameters:
      baseUrl - the base url - already escaped and ready to go
      key - the key to add - NOT escaped
      val - the value to add - NOT escaped - if null nothing is set
      Returns:
      the escaped new url
    • getParameter

      public static String getParameter(String baseUrl, String key)
    • addPath

      public static String addPath(String baseUrl, String path) throws IllegalArgumentException
      adds a path to a given url , keeping the parameters
      Parameters:
      baseUrl - the base url - already escaped and ready to go
      path - the path to add
      Returns:
      the escaped new url
      Throws:
      IllegalArgumentException
    • escape

      public static String escape(String toEscape, boolean bEscape128)
      standard url escaping
      Parameters:
      toEscape - the string to escape
      bEscape128 - if true, also escape >128, else leave non-ascii7 as is
      Returns:
      the escaped string
    • escape

      public static String escape(String toEscape, boolean bEscape128, boolean escapeSlash)
      standard url escaping
      Parameters:
      toEscape - the string to escape
      bEscape128 - if true, also escape >128, else leave non-ascii7 as is
      escapeSlash - if true also escape '/'
      Returns:
      the escaped string
    • unEscape

      public static String unEscape(String toEscape)
      standard url unescaping
      Parameters:
      toEscape - the string to unescape
      Returns:
      the escaped string
    • stringToURL

      public static URL stringToURL(String urlString)
      Create a URL for any url string using heuristics and escaping
      Parameters:
      urlString - the file url to retrieve a file for
      Returns:
    • isFileOK

      public static boolean isFileOK(File f)
      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

      public static boolean isEscaped(String url)
      test whether a given url is escaped as utf-8
      Parameters:
      url - the url to test
      Returns:
    • getExtensionFromMimeType

      public static String getExtensionFromMimeType(String contentType)
      generates the correct extension for a given mime content type
      Parameters:
      contentType -
      Returns:
    • getMimeTypeFromURL

      public static String getMimeTypeFromURL(String url)
      generates the correct MIMEType for a given URL and sets it
      Parameters:
      url -
      Returns:
    • isCID

      public static boolean isCID(String url)
      test whether a given url is a cid (cid:)
      Parameters:
      url - the url to test
      Returns:
    • isNotCID

      public static boolean isNotCID(String url)
      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

      public static boolean isFile(String url)
      is this a valid file url
      Parameters:
      url -
      Returns:
    • isWindowsLocalPath

      public static boolean isWindowsLocalPath(String pathName)
      Parameters:
      pathName -
      Returns:
    • isHttp

      public static boolean isHttp(String url)
      test whether a given url is an http url (excluding https - @see isHttps)
      Parameters:
      url - the url to test
      Returns:
    • isFtp

      public static boolean isFtp(String url)
      test whether a given url is an http url (excluding https - @see isHttps)
      Parameters:
      url - the url to test
      Returns:
    • isHttps

      public static boolean isHttps(String url)
      test whether a given url is an https url
      Parameters:
      url - the url to test
      Returns:
    • isNet

      public static boolean isNet(String url)
      test whether a given url is any network style url, currently either http, https or ftp
      Parameters:
      url - the url to test
      Returns:
    • normalize

      public static String normalize(String urlString)
      normalize a url string by grinding it through url
      Parameters:
      urlString -
      Returns:
      the normalized string, null if not a valid url
    • isUNC

      public static boolean isUNC(String pathName)
      test whether a given url is a unc path
      Parameters:
      pathName -
      Returns:
    • isMIME

      public static boolean isMIME(File file)
      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

      public static UrlUtil.URLProtocol getProtocol(String url)
      returns the general protocol type of a url
      Parameters:
      url -
      Returns:
    • getIPFromBytes

      public static String getIPFromBytes(byte[] ip)
      get the ip address as a string with the left byte at pos 0
      Parameters:
      ip -
      Returns:
    • getBytesFromIP

      public static byte[] getBytesFromIP(String ip)
      get the ip address as a set of bytes with the left byte at pos 0
      Parameters:
      ip -
      Returns:
    • isMIMEExtenstension

      public static boolean isMIMEExtenstension(String lower)
      check whether a file is a mime file
      Parameters:
      lower -
      Returns:
    • isIRL

      public static boolean isIRL(String val)
      Parameters:
      val -
      Returns:
    • removeProtocol

      public static String removeProtocol(String url)
      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

      public static boolean isURL(String val)
      Parameters:
      val -
      Returns:
    • getLocalURL

      public static String getLocalURL(String directory, String url)
      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 directory
      url - the absolute url
      Returns:
      String - the local URL of url after removing directory
    • addSecure

      public static String addSecure(String baseURL, String url) throws IllegalArgumentException
      securely add a subdirectory
      Parameters:
      baseURL -
      url -
      Returns:
      Throws:
      IllegalArgumentException
    • getURLWithDirectory

      public static String getURLWithDirectory(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. file:)
      Parameters:
      directory - the url of the directory
      url - the relative url of the file
      Returns:
      String - the concatenated URL of the directory + file
    • getParentDirectory

      public static String getParentDirectory(String url)
      get the parent directory of a file or url
      Parameters:
      url -
      Returns:
    • cleanDots

      public static String cleanDots(String url)
      remove any internal "../" "./" and "//" from a url
      Parameters:
      url - the url to clean
      Returns:
      String - the clean path
    • cleanHttpURL

      public static String cleanHttpURL(String url)
      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 to
      stream - the input stream to read from
      method - HEAD, GET or POST
      contentType - the contenttype to set, if NULL defaults to TEXT/UNKNOWN
      det -
      Returns:
      UrlPart the 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 to
      streamWriter - the IStreamWriter to read from
      method - HEAD, GET or POST
      contentType - the contenttype to set, if NULL defaults to TEXT/UNKNOWN
      det -
      Returns:
      UrlPart the opened http connection, null in case of error
    • moveToDir

      @Deprecated public static File moveToDir(IURLSetter parent, File dir, boolean overWrite)
      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 preview
      dir - 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 returned
      overWrite - 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)
      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 preview
      dir - 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 returned
      cwd - the current working dir for local urls
      overWrite - 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 preview
      dir - 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 returned
      cwd - the current working dir for local urls
      overWrite - 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

      public static boolean isRelativeURL(String url)
      if true this url is relative
      Parameters:
      url - the url string to test
      Returns:
      true if relative
    • isXMLType

      public static boolean isXMLType(String contentType)
      check whether the mime type is a known xml dialect
      Parameters:
      contentType -
      Returns:
    • isJSONType

      public static boolean isJSONType(String contentType)
      check whether the mime type is a known xml dialect
      Parameters:
      contentType -
      Returns:
    • isZIPType

      public static boolean isZIPType(String contentType)
      check whether the mime type is a known zip dialect
      Parameters:
      contentType -
      Returns:
    • isZip

      public static boolean isZip(String url)
      check whether the url type is a known zip
      Parameters:
      url -
      Returns:
    • newExtension

      public static String newExtension(String strWork, String newExt)
      replace the .extension of a file name
      Parameters:
      strWork - the file path
      newExt - the new extension (works with or without the initial "."
      Returns:
      the strWork with a replaced extension
    • prefix

      public static String prefix(String strWork)
      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

      public static boolean isReturnCodeOK(UrlPart p)
      return true if the response code should redirect
      Parameters:
      responseCode -
      Returns: