package net.sourceforge.stripes.util; /** * HttpUrlInfo holds the various parts of a parsed URL. It provides nearly the * same variables java.net.URL class provides. However, HttpUrlInfo provides an * extra field for the session and some convenience methods, e.g. * {@link #isSecure()} * * @author Christian Schwanke */ public class HttpUrlInfo { /** * Defines the default port used for SSL connections */ public static final int DEFAULT_HTTPS_PORT = 443; /** * Defines the default port used for non SSL connections */ public static final int DEFAULT_HTTP_PORT = 80; private String host; private String protocol; private int port; private String path; private String query; private String ref; private String session; /** * Retrieves the host value * * @return the host */ public String getHost() { return this.host; } /** * Retrieves the path value. In contrast to the java.net.URL class, the path * value will not contain the sessionid. The session id part is available as * a separate field. * * @return the path */ public String getPath() { return this.path; } /** * Retrieves the port value * * @return the port */ public int getPort() { return this.port; } /** * Retrieves the protocol value * * @return the protocol */ public String getProtocol() { return this.protocol; } /** * Retrieves the query value * * @return the query */ public String getQuery() { return this.query; } /** * Retrieves the ref value * * @return the ref */ public String getRef() { return this.ref; } /** * Retrieves the session value * * @return the session */ public String getSession() { return this.session; } /** * Sets the host value * * @param host * the host to set */ public void setHost(String host) { this.host = host; } /** * Sets the path value * * @param path * the path to set */ public void setPath(String path) { this.path = path; } /** * Sets the port value * * @param port * the port to set */ public void setPort(int port) { this.port = port; } /** * Sets the protocol value * * @param protocol * the protocol to set */ public void setProtocol(String protocol) { this.protocol = protocol; } /** * Sets the query value * * @param query * the query to set */ public void setQuery(String query) { this.query = query; } /** * Sets the ref value * * @param ref * the ref to set */ public void setRef(String ref) { this.ref = ref; } /** * Sets the session value * * @param session * the session to set */ public void setSession(String session) { this.session = session; } /** * Checks wether the current data represent a secure URL. An URL is * considered secure if the protocol starts with {@literal https} * * @return true if the current instance represents a secure URL */ public boolean isSecure() { return protocol != null && protocol.toLowerCase().startsWith("https"); } /** * Retrieves the host and port combined. If the port equals either one of * the default ports (80 or 443) it is omitted. Otherwise, the port will be * added to the result. * * @return a string containing the host and the port, if the port is not a * default port */ public String getCompleteHost() { if (port == DEFAULT_HTTP_PORT || port == DEFAULT_HTTPS_PORT) { return host; } return host + ":" + port; } /** * Retrieves the full jsessionid part of the url including the ; character * and the session id value. If the session value is not present, an empty * string is returned. This method is intended to be used during string * concatenation in order to rebuilt an URL. * * @return The complete session value including the ; character or an empty * string */ public String getCompleteSession() { if (!isEmpty(session)) { return ";" + session; } return ""; } /** * Retrieves the full querystring of the url including the ? character. If * the query value is not present, an empty string is returned. This method * is intended to be used during string concatenation in order to rebuilt an * URL. * * @return The complete querystring including the ? character or an empty * string */ public String getCompleteQuery() { if (!isEmpty(query)) { return "?" + query; } return ""; } /** * Retrieves the full anchor part of the url including the # character. If * the ref value is not present, an empty string is returned. This method is * intended to be used during string concatenation in order to rebuilt an * URL. * * @return The complete ref value including the # character or an empty * string */ public String getCompleteRef() { if (!isEmpty(ref)) { return "#" + ref; } return ""; } private boolean isEmpty(String value) { return value == null || value.length() == 0; } }