Class Url
- java.lang.Object
-
- com.facilita.fc.web.Url
-
public class Url extends java.lang.Object
Represents 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#reference
A
Url
object is immutable - that is, it cannot be changed after it has been created. To modify a URL, variouswith*()
methods can be used on an existingUrl
object to create a copy of the object with different attributes.A
Url
object 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 aUrl
object with the specifiedprotocol
andipEndPoint
(host/port).Url(Protocol protocol, IpEndPoint ipEndPoint, java.lang.String path)
Creates aUrl
object with the specifiedprotocol
,ipEndPoint
(host/port) andpath
.Url(Protocol protocol, IpEndPoint ipEndPoint, java.lang.String path, QueryData queryData)
Creates aUrl
object with the specifiedprotocol
,ipEndPoint
(host/port),path
andQueryData
object.Url(Protocol protocol, IpEndPoint ipEndPoint, java.lang.String path, QueryData queryData, java.lang.String unsafeCharacters)
Creates aUrl
object with the specifiedprotocol
,ipEndPoint
(host/port),path
andQueryData
object.Url(Protocol protocol, IpEndPoint ipEndPoint, java.lang.String path, java.lang.String queryText)
Creates aUrl
object with the specifiedprotocol
,ipEndPoint
(host/port),path
andqueryText String
.Url(Protocol protocol, java.lang.String host)
Creates aUrl
object with the specifiedprotocol
andhost
.Url(Protocol protocol, java.lang.String host, java.lang.String path)
Creates aUrl
object with the specifiedprotocol
,host
andpath
.Url(Url url, java.lang.String relativeUrlText, boolean checkSlash)
For internal use only.Url(java.lang.String urlText)
Creates aUrl
object from the specifiedurlText
.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description Url
appendPath(java.lang.String path)
Creates a newUrl
object using the attributes of thisUrl
, but with the specifiedpath
appended to the existing path.void
delete()
static long
getCPtr(Url obj)
For internal use only.java.lang.String
getDirectoryPath()
Gets the directory components of the path in thisUrl
, excluding the filename.java.lang.String
getEncodedPathAndQuery()
Gets the section of the URL used in an HTTP request.java.lang.String
getFileName()
Gets the name of the file at the end of the path in thisUrl
.IpEndPoint
getIpEndPoint()
Gets theIpEndPoint
pointed to by thisUrl
.java.lang.String
getPassword()
Gets the password specified in thisUrl
.java.lang.String
getPath()
Gets the path component of thisUrl
, excluding the first/
after the hostname.Protocol
getProtocol()
Gets theProtocol
used for thisUrl
.java.lang.String
getQuery()
Gets the percent-encoded query data section of thisUrl
.QueryData
getQueryData()
Get a QueryData object representing the query data section of thisUrl
.java.lang.String
getReference()
Gets the reference (also known as the fragment ID) of thisUrl
.java.lang.String
getUser()
Gets the username specified in thisUrl
.boolean
isRelativePath()
boolean
isStandardPortForProtocol()
Returnstrue
if a standard port is being used.java.lang.String
toString()
Returns aString
representation of thisUrl
.java.lang.String
toString(boolean encodedPath)
Returns aString
representation of thisUrl
, specifying whether to percent-encode the path.java.lang.String
toString(boolean encodedPath, boolean outputStandardPorts)
Returns aString
representation of thisUrl
, specifying whether to percent-encode the path and include the port component of the URL if it is a standard port.Url
withCredentials(java.lang.String user, java.lang.String password)
Creates a newUrl
object using the attributes of thisUrl
, but with the specifieduser
andpassword
(replacing the existing credentials if present).Url
withPath(java.lang.String path)
Creates a newUrl
object using the attributes of thisUrl
, but with the specifiedpath
(replacing the existing path if present).Url
withPort(int port)
Creates a newUrl
object using the attributes of thisUrl
, but with the specifiedport
(replacing the existing port if present).Url
withQuery(Form form)
Creates a newUrl
object using the attributes of thisUrl
, but with query data from the specifiedForm
object (replacing the existing query data if present).Url
withQuery(Form form, java.lang.String unsafeCharacters)
Creates a newUrl
object using the attributes of thisUrl
, but with query data from the specifiedForm
object percent-encoded using the supplied list ofunsafeCharacters
(replacing the existing query data if present).Url
withQuery(QueryData queryData)
Creates a newUrl
object using the attributes of thisUrl
, but with the specifiedQueryData
object (replacing the existing query data if present).Url
withQuery(QueryData queryData, java.lang.String unsafeCharacters)
Creates a newUrl
object using the attributes of thisUrl
, but with the specifiedQueryData
object (replacing the existing query data if present).Url
withQuery(java.lang.String queryText)
Creates a newUrl
object using the attributes of thisUrl
, but with the specifiedqueryText String
(replacing the existing query data if present).Url
withReference(java.lang.String reference)
Creates a newUrl
object 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 aUrl
object 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
- aString
representation of a URL
-
Url
public Url(Protocol protocol, java.lang.String host) throws NoSuchValueException
Creates aUrl
object with the specifiedprotocol
andhost
.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 aUrl
object with the specifiedprotocol
andipEndPoint
(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 aUrl
object 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 aUrl
object with the specifiedprotocol
,host
andpath
.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 aUrl
object with the specifiedprotocol
,ipEndPoint
(host/port),path
andQueryData
object.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 aUrl
object with the specifiedprotocol
,ipEndPoint
(host/port),path
andQueryData
object.The
QueryData
object 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 aUrl
object with the specifiedprotocol
,ipEndPoint
(host/port),path
andqueryText 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-encodedString
of 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 newUrl
object using the attributes of thisUrl
, but with the specifiedpath
appended to the existing path.- Parameters:
path
- the path to append- Returns:
- a new
Url
object which is the same as thisUrl
but with the specifiedpath
appended to the existing path
-
withPort
public Url withPort(int port)
Creates a newUrl
object using the attributes of thisUrl
, but with the specifiedport
(replacing the existing port if present).- Parameters:
port
- the port to add- Returns:
- a new
Url
object which is the same as thisUrl
but with the specifiedport
-
withPath
public Url withPath(java.lang.String path)
Creates a newUrl
object using the attributes of thisUrl
, but with the specifiedpath
(replacing the existing path if present).- Parameters:
path
- the path to add- Returns:
- a new
Url
object which is the same as thisUrl
but with the specifiedpath
-
withCredentials
public Url withCredentials(java.lang.String user, java.lang.String password)
Creates a newUrl
object using the attributes of thisUrl
, but with the specifieduser
andpassword
(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
Url
object which is the same as thisUrl
but with the specifieduser
andpassword
-
withQuery
public Url withQuery(java.lang.String queryText)
Creates a newUrl
object 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-encodedString
of key/value pairs to be appended to the URL, separated by the&
character- Returns:
- a new
Url
object which is the same as thisUrl
but with the specified query data - See Also:
for percent-encoding
-
withQuery
public Url withQuery(QueryData queryData)
Creates a newUrl
object using the attributes of thisUrl
, but with the specifiedQueryData
object (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
Url
object which is the same as thisUrl
but with the specifiedqueryData
-
withQuery
public Url withQuery(QueryData queryData, java.lang.String unsafeCharacters)
Creates a newUrl
object using the attributes of thisUrl
, but with the specifiedQueryData
object (replacing the existing query data if present).The
QueryData
object 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
Url
object which is the same as thisUrl
but with the specifiedqueryData
-
withQuery
public Url withQuery(Form form)
Creates a newUrl
object using the attributes of thisUrl
, but with query data from the specifiedForm
object (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
Url
object which is the same as thisUrl
but with query data from the specifiedform
-
withQuery
public Url withQuery(Form form, java.lang.String unsafeCharacters)
Creates a newUrl
object using the attributes of thisUrl
, but with query data from the specifiedForm
object 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
Url
object which is the same as thisUrl
but with query data from the specifiedform
-
getProtocol
public Protocol getProtocol() throws NoSuchValueException
Gets theProtocol
used 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 theIpEndPoint
pointed 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 emptyString
if 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 emptyString
if 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 emptyString
if 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 emptyString
if thisUrl
has 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 emptyString
if thisUrl
has no path component
-
isRelativePath
public boolean isRelativePath()
- Returns:
true
if the return value ofgetPath()
doesn't begin with/
-
isStandardPortForProtocol
public boolean isStandardPortForProtocol()
Returnstrue
if a standard port is being used.- For
Protocol.HTTP
the standard port is 80 - For
Protocol.HTTPS
the standard port is 443
- Returns:
true
if a standard port is being used
- For
-
toString
public java.lang.String toString(boolean encodedPath, boolean outputStandardPorts)
Returns aString
representation 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
-true
if the path component of the URL should be percent-encodedoutputStandardPorts
-true
if the port component of the URL should be included even if it is a standard port for thisUrl
object's protocol- Returns:
- a
String
representation of thisUrl
- See Also:
Coder.urlEncode(String)
-
toString
public java.lang.String toString(boolean encodedPath)
Returns aString
representation of thisUrl
, specifying whether to percent-encode the path.- Parameters:
encodedPath
-true
if the path component of the URL should be percent-encoded- Returns:
- a
String
representation of thisUrl
- See Also:
Coder.urlEncode(String)
-
toString
public java.lang.String toString()
Returns aString
representation of thisUrl
.- Overrides:
toString
in classjava.lang.Object
- Returns:
- a
String
representation of thisUrl
-
withReference
public Url withReference(java.lang.String reference)
Creates a newUrl
object 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
Url
object which is the same as thisUrl
but with the specified reference
-
-