Class Url
- java.lang.Object
-
- com.facilita.fc.web.Url
-
public class Url extends java.lang.ObjectRepresents a URL, specified by a protocol, optional credentials host, port, path, optional query data, and optional reference.e.g.
protocol://user:password@host:port/path?query_data#referenceA
Urlobject is immutable - that is, it cannot be changed after it has been created. To modify a URL, variouswith*()methods can be used on an existingUrlobject to create a copy of the object with different attributes.A
Urlobject may not necessarily represent a valid URL.
-
-
Constructor Summary
Constructors Constructor Description Url(long cPtr, boolean cMemoryOwn)For internal use only.Url(Protocol protocol, IpEndPoint ipEndPoint)Creates aUrlobject with the specifiedprotocolandipEndPoint(host/port).Url(Protocol protocol, IpEndPoint ipEndPoint, java.lang.String path)Creates aUrlobject with the specifiedprotocol,ipEndPoint(host/port) andpath.Url(Protocol protocol, IpEndPoint ipEndPoint, java.lang.String path, QueryData queryData)Creates aUrlobject with the specifiedprotocol,ipEndPoint(host/port),pathandQueryDataobject.Url(Protocol protocol, IpEndPoint ipEndPoint, java.lang.String path, QueryData queryData, java.lang.String unsafeCharacters)Creates aUrlobject with the specifiedprotocol,ipEndPoint(host/port),pathandQueryDataobject.Url(Protocol protocol, IpEndPoint ipEndPoint, java.lang.String path, java.lang.String queryText)Creates aUrlobject with the specifiedprotocol,ipEndPoint(host/port),pathandqueryText String.Url(Protocol protocol, java.lang.String host)Creates aUrlobject with the specifiedprotocolandhost.Url(Protocol protocol, java.lang.String host, java.lang.String path)Creates aUrlobject with the specifiedprotocol,hostandpath.Url(Url url, java.lang.String relativeUrlText, boolean checkSlash)For internal use only.Url(java.lang.String urlText)Creates aUrlobject from the specifiedurlText.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description UrlappendPath(java.lang.String path)Creates a newUrlobject using the attributes of thisUrl, but with the specifiedpathappended to the existing path.voiddelete()static longgetCPtr(Url obj)For internal use only.java.lang.StringgetDirectoryPath()Gets the directory components of the path in thisUrl, excluding the filename.java.lang.StringgetEncodedPathAndQuery()Gets the section of the URL used in an HTTP request.java.lang.StringgetFileName()Gets the name of the file at the end of the path in thisUrl.IpEndPointgetIpEndPoint()Gets theIpEndPointpointed to by thisUrl.java.lang.StringgetPassword()Gets the password specified in thisUrl.java.lang.StringgetPath()Gets the path component of thisUrl, excluding the first/after the hostname.ProtocolgetProtocol()Gets theProtocolused for thisUrl.java.lang.StringgetQuery()Gets the percent-encoded query data section of thisUrl.QueryDatagetQueryData()Get a QueryData object representing the query data section of thisUrl.java.lang.StringgetReference()Gets the reference (also known as the fragment ID) of thisUrl.java.lang.StringgetUser()Gets the username specified in thisUrl.booleanisRelativePath()booleanisStandardPortForProtocol()Returnstrueif a standard port is being used.java.lang.StringtoString()Returns aStringrepresentation of thisUrl.java.lang.StringtoString(boolean encodedPath)Returns aStringrepresentation of thisUrl, specifying whether to percent-encode the path.java.lang.StringtoString(boolean encodedPath, boolean outputStandardPorts)Returns aStringrepresentation of thisUrl, specifying whether to percent-encode the path and include the port component of the URL if it is a standard port.UrlwithCredentials(java.lang.String user, java.lang.String password)Creates a newUrlobject using the attributes of thisUrl, but with the specifieduserandpassword(replacing the existing credentials if present).UrlwithPath(java.lang.String path)Creates a newUrlobject using the attributes of thisUrl, but with the specifiedpath(replacing the existing path if present).UrlwithPort(int port)Creates a newUrlobject using the attributes of thisUrl, but with the specifiedport(replacing the existing port if present).UrlwithQuery(Form form)Creates a newUrlobject using the attributes of thisUrl, but with query data from the specifiedFormobject (replacing the existing query data if present).UrlwithQuery(Form form, java.lang.String unsafeCharacters)Creates a newUrlobject using the attributes of thisUrl, but with query data from the specifiedFormobject percent-encoded using the supplied list ofunsafeCharacters(replacing the existing query data if present).UrlwithQuery(QueryData queryData)Creates a newUrlobject using the attributes of thisUrl, but with the specifiedQueryDataobject (replacing the existing query data if present).UrlwithQuery(QueryData queryData, java.lang.String unsafeCharacters)Creates a newUrlobject using the attributes of thisUrl, but with the specifiedQueryDataobject (replacing the existing query data if present).UrlwithQuery(java.lang.String queryText)Creates a newUrlobject using the attributes of thisUrl, but with the specifiedqueryText String(replacing the existing query data if present).UrlwithReference(java.lang.String reference)Creates a newUrlobject using the attributes of thisUrl, but with the specifiedreference String(replacing the existing reference, also known as the fragment ID, if present).
-
-
-
Constructor Detail
-
Url
public Url(long cPtr, boolean cMemoryOwn)For internal use only. Of no interest to the user.- Parameters:
cPtr- pointer to the CPP wrapped objectcMemoryOwn- indicates if this object is responsible for memory management of the CPP object
-
Url
public Url(java.lang.String urlText)
Creates aUrlobject from the specifiedurlText.e.g.
// This will have the default HTTP port (80) Url url = new Url("http://www.myCompany.co.uk/index.php");- Parameters:
urlText- aStringrepresentation of a URL
-
Url
public Url(Protocol protocol, java.lang.String host) throws NoSuchValueException
Creates aUrlobject with the specifiedprotocolandhost.e.g.
// These will have the default HTTP port (80) Url url1 = new Url(Protocol.HTTP, "www.myCompany.co.uk"); Url url2 = new Url(Protocol.HTTP, "192.168.01.123");- Parameters:
protocol- the protocol to usehost- a dotted IP address or host name- Throws:
NoSuchValueException- Invalid protocol
-
Url
public Url(Protocol protocol, IpEndPoint ipEndPoint) throws NoSuchValueException
Creates aUrlobject with the specifiedprotocolandipEndPoint(host/port).e.g.
IpEndPoint myServer = new IpEndPoint("www.myCompany.co.uk", 8080); Url url = new Url(Protocol.HTTP, myServer); // This refers to http://www.myCompany.co.uk:8080- Parameters:
protocol- the protocol to useipEndPoint- the hostname/IP address and port- Throws:
NoSuchValueException- Invalid protocol
-
Url
public Url(Protocol protocol, IpEndPoint ipEndPoint, java.lang.String path) throws NoSuchValueException
Creates aUrlobject with the specifiedprotocol,ipEndPoint(host/port) andpath.e.g.
IpEndPoint myServer = new IpEndPoint("www.myCompany.co.uk", 8080); Url url = new Url(Protocol.HTTP, myServer, "index.php"); // This refers to http://www.myCompany.co.uk:8080/index.php- Parameters:
protocol- the protocol to useipEndPoint- the hostname/IP address and portpath- the path to be appended to the URL- Throws:
NoSuchValueException- Invalid protocol
-
Url
public Url(Protocol protocol, java.lang.String host, java.lang.String path) throws NoSuchValueException
Creates aUrlobject with the specifiedprotocol,hostandpath.e.g.
// This will have the default HTTP port (80) Url url = new Url(Protocol.HTTP, "www.myCompany.co.uk", "index.php");- Parameters:
protocol- the protocol to usehost- a dotted IP address or host namepath- the path to be appended to the URL- Throws:
NoSuchValueException- Invalid protocol
-
Url
public Url(Protocol protocol, IpEndPoint ipEndPoint, java.lang.String path, QueryData queryData) throws NoSuchValueException
Creates aUrlobject with the specifiedprotocol,ipEndPoint(host/port),pathandQueryDataobject.e.g.
QueryData queryData = new QueryData(); queryData.add("sessionID", "12345"); IpEndPoint myServer = new IpEndPoint("www.myCompany.co.uk", 8080); Url url = new Url(Protocol.HTTP, myServer, "index.php", queryData); // This refers to http://www.myCompany.co.uk:8080/index.php?sessionID=12345- Parameters:
protocol- the protocol to useipEndPoint- the hostname/IP address and portpath- the path to be appended to the URLqueryData- a collection of key/value pairs to be appended to the URL- Throws:
NoSuchValueException- Invalid protocol
-
Url
public Url(Protocol protocol, IpEndPoint ipEndPoint, java.lang.String path, QueryData queryData, java.lang.String unsafeCharacters) throws NoSuchValueException
Creates aUrlobject with the specifiedprotocol,ipEndPoint(host/port),pathandQueryDataobject.The
QueryDataobject will be percent-encoded using the supplied list ofunsafeCharacters.e.g.
QueryData queryData = new QueryData(); queryData.add("sessionID", "12345"); IpEndPoint myServer = new IpEndPoint("www.myCompany.co.uk", 8080); Url url = new Url(Protocol.HTTP, myServer, "index.php", queryData, "<>\"%{}|\\^[]` \n\t\r&"); // This refers to http://www.myCompany.co.uk:8080/index.php?sessionID=12345- Parameters:
protocol- the protocol to useipEndPoint- the hostname/IP address and portpath- the path to be appended to the URLqueryData- a collection of key/value pairs to be appended to the URLunsafeCharacters- a list of unsafe characters that will be percent-encoded in the query data of the URL- Throws:
NoSuchValueException- Invalid protocol
-
Url
public Url(Protocol protocol, IpEndPoint ipEndPoint, java.lang.String path, java.lang.String queryText) throws NoSuchValueException
Creates aUrlobject with the specifiedprotocol,ipEndPoint(host/port),pathandqueryText String.e.g.
IpEndPoint myServer = new IpEndPoint("www.myCompany.co.uk", 8080); Url url = new Url(Protocol.HTTP, myServer, "index.php", "sessionID=12345"); // This refers to http://www.myCompany.co.uk:8080/index.php?sessionID=12345- Parameters:
protocol- the protocol to useipEndPoint- the hostname/IP address and portpath- the path to be appended to the URLqueryText- a percent-encodedStringof key/value pairs to be appended to the URL, separated by the&character- Throws:
NoSuchValueException- Invalid protocol- See Also:
Coder.urlEncode(String)
-
Url
public Url(Url url, java.lang.String relativeUrlText, boolean checkSlash)
For internal use only. Of no interest to the user.- Parameters:
url- internal use onlyrelativeUrlText- internal use onlycheckSlash- internal use only
-
-
Method Detail
-
getCPtr
public static long getCPtr(Url obj)
For internal use only. Of no interest to the user.- Parameters:
obj- a reference to an object of this class- Returns:
- a long containing the address of the CPP wrapped object
-
delete
public void delete()
-
appendPath
public Url appendPath(java.lang.String path)
Creates a newUrlobject using the attributes of thisUrl, but with the specifiedpathappended to the existing path.- Parameters:
path- the path to append- Returns:
- a new
Urlobject which is the same as thisUrlbut with the specifiedpathappended to the existing path
-
withPort
public Url withPort(int port)
Creates a newUrlobject using the attributes of thisUrl, but with the specifiedport(replacing the existing port if present).- Parameters:
port- the port to add- Returns:
- a new
Urlobject which is the same as thisUrlbut with the specifiedport
-
withPath
public Url withPath(java.lang.String path)
Creates a newUrlobject using the attributes of thisUrl, but with the specifiedpath(replacing the existing path if present).- Parameters:
path- the path to add- Returns:
- a new
Urlobject which is the same as thisUrlbut with the specifiedpath
-
withCredentials
public Url withCredentials(java.lang.String user, java.lang.String password)
Creates a newUrlobject using the attributes of thisUrl, but with the specifieduserandpassword(replacing the existing credentials if present).If a username and password are present in a URL, they appear before the host.
e.g.
http://username:password@www.myCompany.co.uk- Parameters:
user- a usernamepassword- a password- Returns:
- a new
Urlobject which is the same as thisUrlbut with the specifieduserandpassword
-
withQuery
public Url withQuery(java.lang.String queryText)
Creates a newUrlobject using the attributes of thisUrl, but with the specifiedqueryText String(replacing the existing query data if present).e.g.
IpEndPoint myServer = new IpEndPoint("www.myCompany.co.uk", 8080); Url url = new Url(Protocol.HTTP, myServer, "index.php"); Url url2 = url.withQuery("sessionID=12345"); // This refers to http://www.myCompany.co.uk:8080/index.php?sessionID=12345- Parameters:
queryText- a percent-encodedStringof key/value pairs to be appended to the URL, separated by the&character- Returns:
- a new
Urlobject which is the same as thisUrlbut with the specified query data - See Also:
for percent-encoding
-
withQuery
public Url withQuery(QueryData queryData)
Creates a newUrlobject using the attributes of thisUrl, but with the specifiedQueryDataobject (replacing the existing query data if present).e.g.
IpEndPoint myServer = new IpEndPoint("www.myCompany.co.uk", 8080); Url url = new Url(Protocol.HTTP, myServer, "index.php"); QueryData queryData = new QueryData(); queryData.add("sessionID", "12345"); Url url2 = url.withQuery(queryData); // This refers to http://www.myCompany.co.uk:8080/index.php?sessionID=12345- Parameters:
queryData- a collection of key/value pairs to be appended to the URL- Returns:
- a new
Urlobject which is the same as thisUrlbut with the specifiedqueryData
-
withQuery
public Url withQuery(QueryData queryData, java.lang.String unsafeCharacters)
Creates a newUrlobject using the attributes of thisUrl, but with the specifiedQueryDataobject (replacing the existing query data if present).The
QueryDataobject will be percent-encoded using the supplied list ofsafeCharacter's.e.g.
IpEndPoint myServer = new IpEndPoint("www.myCompany.co.uk", 8080); Url url = new Url(Protocol.HTTP, myServer, "index.php"); QueryData queryData = new QueryData(); queryData.add("sessionID", "~12345"); Url url2 = url.withQuery(queryData, Coder.getDefaultUnsafeCharacters() + "~"); // This refers to http://www.myCompany.co.uk:8080/index.php?sessionID=12345- Parameters:
queryData- a collection of key/value pairs to be appended to the URLunsafeCharacters- a list of unsafe characters that will be percent-encoded in the query data of the URL- Returns:
- a new
Urlobject which is the same as thisUrlbut with the specifiedqueryData
-
withQuery
public Url withQuery(Form form)
Creates a newUrlobject using the attributes of thisUrl, but with query data from the specifiedFormobject (replacing the existing query data if present).e.g.
IpEndPoint myServer = new IpEndPoint("www.myCompany.co.uk", 8080); Url url = new Url(Protocol.HTTP, myServer, "index.php"); Form form = response.extractForm("contactForm"); form.getInputElement("sessionID").setValue("12345"); Url url2 = url.withQuery(form); // This refers to http://www.myCompany.co.uk:8080/index.php?sessionID=12345- Parameters:
form- a collection of key/value pairs represented as a HTML form- Returns:
- a new
Urlobject which is the same as thisUrlbut with query data from the specifiedform
-
withQuery
public Url withQuery(Form form, java.lang.String unsafeCharacters)
Creates a newUrlobject using the attributes of thisUrl, but with query data from the specifiedFormobject percent-encoded using the supplied list ofunsafeCharacters(replacing the existing query data if present).e.g.
IpEndPoint myServer = new IpEndPoint("www.myCompany.co.uk", 8080); Url url = new Url(Protocol.HTTP, myServer, "index.php"); Form form = response.extractForm("contactForm"); form.getInputElement("sessionID").setValue("~12345"); Url url2 = url.withQuery(form, Coder.getDefaultUnsafeCharacters() + "~"); // This refers to http://www.myCompany.co.uk:8080/index.php?sessionID=12345- Parameters:
form- a collection of key/value pairs represented as a HTML formunsafeCharacters- a list of unsafe characters that will be percent-encoded in the query data of the URL- Returns:
- a new
Urlobject which is the same as thisUrlbut with query data from the specifiedform
-
getProtocol
public Protocol getProtocol() throws NoSuchValueException
Gets theProtocolused for thisUrl. This appears before the://at the start of a URL, and is also known as the scheme.- Returns:
- the protocol used for this
Url - Throws:
NoSuchValueException- Invalid protocol
-
getIpEndPoint
public IpEndPoint getIpEndPoint()
Gets theIpEndPointpointed to by thisUrl.- Returns:
- the host/port combination pointed to by this
Url - See Also:
withPort(int)
-
getUser
public java.lang.String getUser()
Gets the username specified in thisUrl.If a username and password are present, they appear before the host in the URL.
e.g.
http://username:password@www.myCompany.co.uk- Returns:
- the username specified in this
Url, or an emptyStringif no username is specified - See Also:
withCredentials(String, String)
-
getPassword
public java.lang.String getPassword()
Gets the password specified in thisUrl.If a username and password are present, they appear before the host in the URL.
e.g.
http://username:password@www.myCompany.co.uk- Returns:
- the password specified in this
Url, or an emptyStringif no password is specified - See Also:
withCredentials(String, String)
-
getPath
public java.lang.String getPath()
Gets the path component of thisUrl, excluding the first/after the hostname.- Returns:
- the path component of this
Url, or an emptyStringif no path is present - See Also:
withPath(String)
-
getEncodedPathAndQuery
public java.lang.String getEncodedPathAndQuery()
Gets the section of the URL used in an HTTP request.- Returns:
- the section of the URL used in an HTTP request
- See Also:
Coder.urlEncode(String),withPath(String),withQuery(QueryData)
-
getQuery
public java.lang.String getQuery()
Gets the percent-encoded query data section of thisUrl.- Returns:
- the percent-encoded query data section of this
Url - See Also:
Coder.urlEncode(String),withQuery(QueryData)
-
getQueryData
public QueryData getQueryData()
Get a QueryData object representing the query data section of thisUrl.- Returns:
- the query data section of this Url.
- See Also:
getQuery(),withQuery(QueryData)
-
getReference
public java.lang.String getReference()
Gets the reference (also known as the fragment ID) of thisUrl. The reference follows a#at the end of a URL, and is often used to refer to a specific location in a HTML document.e.g.
http://www.myCompany.co.uk/index.php#chapter3- Returns:
- the reference of this
Url - See Also:
withReference(String)
-
getFileName
public java.lang.String getFileName()
Gets the name of the file at the end of the path in thisUrl.e.g.
new Url("http://www.myCompany.co.uk/").getFileName(); // returns "" new Url("http://www.myCompany.co.uk/index.php").getFileName(); // returns "index.php" new Url("http://www.myCompany.co.uk/directory/subdirectory/").getFileName(); // returns ""- Returns:
- the name of the file at the end of the path in this
Url, or an emptyStringif thisUrlhas no path component or the path ends with a/character
-
getDirectoryPath
public java.lang.String getDirectoryPath()
Gets the directory components of the path in thisUrl, excluding the filename.e.g.
new Url("http://www.myCompany.co.uk/").getDirectoryPath(); // returns "/" new Url("http://www.myCompany.co.uk/directory/index.php").getDirectoryPath(); // returns "/directory" new Url("http://www.myCompany.co.uk/directory/subdirectory/").getDirectoryPath(); // returns "/directory/subdirectory"- Returns:
- the directory components of the path in this
Url, excluding the filename, or an emptyStringif thisUrlhas no path component
-
isRelativePath
public boolean isRelativePath()
- Returns:
trueif the return value ofgetPath()doesn't begin with/
-
isStandardPortForProtocol
public boolean isStandardPortForProtocol()
Returnstrueif a standard port is being used.- For
Protocol.HTTPthe standard port is 80 - For
Protocol.HTTPSthe standard port is 443
- Returns:
trueif a standard port is being used
- For
-
toString
public java.lang.String toString(boolean encodedPath, boolean outputStandardPorts)Returns aStringrepresentation of thisUrl, specifying whether to percent-encode the path and include the port component of the URL if it is a standard port.- Parameters:
encodedPath-trueif the path component of the URL should be percent-encodedoutputStandardPorts-trueif the port component of the URL should be included even if it is a standard port for thisUrlobject's protocol- Returns:
- a
Stringrepresentation of thisUrl - See Also:
Coder.urlEncode(String)
-
toString
public java.lang.String toString(boolean encodedPath)
Returns aStringrepresentation of thisUrl, specifying whether to percent-encode the path.- Parameters:
encodedPath-trueif the path component of the URL should be percent-encoded- Returns:
- a
Stringrepresentation of thisUrl - See Also:
Coder.urlEncode(String)
-
toString
public java.lang.String toString()
Returns aStringrepresentation of thisUrl.- Overrides:
toStringin classjava.lang.Object- Returns:
- a
Stringrepresentation of thisUrl
-
withReference
public Url withReference(java.lang.String reference)
Creates a newUrlobject using the attributes of thisUrl, but with the specifiedreference String(replacing the existing reference, also known as the fragment ID, if present).The reference follows a
#at the end of a URL, and is often used to refer to a specific location in a HTML document.e.g.
http://www.myCompany.co.uk/index.php#chapter3- Parameters:
reference- the new reference- Returns:
- a new
Urlobject which is the same as thisUrlbut with the specified reference
-
-