| <?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" lang=""><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/><link rel="stylesheet" href="../jacoco-resources/report.css" type="text/css"/><link rel="shortcut icon" href="../jacoco-resources/report.gif" type="image/gif"/><title>BaseURI.java</title><link rel="stylesheet" href="../jacoco-resources/prettify.css" type="text/css"/><script type="text/javascript" src="../jacoco-resources/prettify.js"></script></head><body onload="window['PR_TAB_WIDTH']=4;prettyPrint()"><div class="breadcrumb" id="breadcrumb"><span class="info"><a href="../jacoco-sessions.html" class="el_session">Sessions</a></span><a href="../index.html" class="el_report">Apache Turbine</a> > <a href="index.source.html" class="el_package">org.apache.turbine.util.uri</a> > <span class="el_source">BaseURI.java</span></div><h1>BaseURI.java</h1><pre class="source lang-java linenums">package org.apache.turbine.util.uri; |
| |
| |
| /* |
| * Licensed to the Apache Software Foundation (ASF) under one |
| * or more contributor license agreements. See the NOTICE file |
| * distributed with this work for additional information |
| * regarding copyright ownership. The ASF licenses this file |
| * to you under the Apache License, Version 2.0 (the |
| * "License"); you may not use this file except in compliance |
| * with the License. You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, |
| * software distributed under the License is distributed on an |
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| * KIND, either express or implied. See the License for the |
| * specific language governing permissions and limitations |
| * under the License. |
| */ |
| |
| |
| import jakarta.servlet.http.HttpServletResponse; |
| |
| import org.apache.commons.lang3.StringUtils; |
| import org.apache.logging.log4j.LogManager; |
| import org.apache.logging.log4j.Logger; |
| import org.apache.turbine.Turbine; |
| import org.apache.turbine.TurbineConstants; |
| import org.apache.turbine.util.RunData; |
| import org.apache.turbine.util.ServerData; |
| |
| /** |
| * This is the base class for all dynamic URIs in the Turbine System. |
| * |
| * All of the classes used for generating URIs are derived from this. |
| * |
| * @author <a href="mailto:jon@clearink.com">Jon S. Stevens</a> |
| * @author <a href="mailto:jvanzyl@periapt.com">Jason van Zyl</a> |
| * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a> |
| * @author <a href="mailto:quintonm@bellsouth.net">Quinton McCombs</a> |
| * @version $Id$ |
| */ |
| |
| public abstract class BaseURI |
| implements URI, |
| URIConstants |
| { |
| /** Logging */ |
| <span class="nc" id="L51"> private static final Logger log = LogManager.getLogger(BaseURI.class);</span> |
| |
| /** ServerData Object for scheme, name, port etc. */ |
| <span class="nc" id="L54"> private ServerData serverData =</span> |
| new ServerData(null, HTTP_PORT, HTTP, null, null); |
| |
| /** Whether we want to redirect or not. */ |
| <span class="nc" id="L58"> private boolean redirect = false;</span> |
| |
| /** Servlet response interface. */ |
| <span class="nc" id="L61"> private HttpServletResponse response = null;</span> |
| |
| /** Reference Anchor (#ref) */ |
| <span class="nc" id="L64"> private String reference = null;</span> |
| |
| /* |
| * ======================================================================== |
| * |
| * Constructors |
| * |
| * ======================================================================== |
| * |
| */ |
| |
| /** |
| * Empty C'tor. Uses Turbine.getDefaultServerData(). |
| * |
| */ |
| public BaseURI() |
| <span class="nc" id="L80"> {</span> |
| <span class="nc" id="L81"> init(Turbine.getDefaultServerData());</span> |
| <span class="nc" id="L82"> setResponse(null);</span> |
| <span class="nc" id="L83"> }</span> |
| |
| /** |
| * Constructor with a RunData object |
| * |
| * @param runData A RunData object |
| */ |
| public BaseURI(RunData runData) |
| <span class="nc" id="L91"> {</span> |
| <span class="nc" id="L92"> init(runData.getServerData());</span> |
| <span class="nc" id="L93"> setResponse(runData.getResponse());</span> |
| <span class="nc" id="L94"> }</span> |
| |
| /** |
| * Constructor, set explicit redirection |
| * |
| * @param runData A RunData object |
| * @param redirect True if redirection allowed. |
| */ |
| public BaseURI(RunData runData, boolean redirect) |
| <span class="nc" id="L103"> {</span> |
| <span class="nc" id="L104"> init(runData.getServerData());</span> |
| <span class="nc" id="L105"> setResponse(runData.getResponse());</span> |
| <span class="nc" id="L106"> setRedirect(redirect);</span> |
| <span class="nc" id="L107"> }</span> |
| |
| /** |
| * Constructor with a ServerData object |
| * |
| * @param serverData A ServerData object |
| */ |
| public BaseURI(ServerData serverData) |
| <span class="nc" id="L115"> {</span> |
| <span class="nc" id="L116"> init(serverData);</span> |
| <span class="nc" id="L117"> setResponse(null);</span> |
| <span class="nc" id="L118"> }</span> |
| |
| /** |
| * Constructor, set explicit redirection |
| * |
| * @param serverData A ServerData object |
| * @param redirect True if redirection allowed. |
| */ |
| public BaseURI(ServerData serverData, boolean redirect) |
| <span class="nc" id="L127"> {</span> |
| <span class="nc" id="L128"> init(serverData);</span> |
| <span class="nc" id="L129"> setResponse(null);</span> |
| <span class="nc" id="L130"> setRedirect(redirect);</span> |
| <span class="nc" id="L131"> }</span> |
| |
| /* |
| * ======================================================================== |
| * |
| * Init |
| * |
| * ======================================================================== |
| * |
| */ |
| |
| /** |
| * Init with a ServerData object |
| * |
| * @param serverData A ServerData object |
| * |
| */ |
| private void init(ServerData serverData) |
| { |
| <span class="nc" id="L150"> log.debug("init({})", serverData);</span> |
| |
| <span class="nc bnc" id="L152" title="All 2 branches missed."> if (serverData != null)</span> |
| { |
| // We must clone this, because if BaseURI is used in a pull tool, |
| // then the fields might be changed. If we don't clone, this might pull |
| // through to the ServerData object saved at firstRequest() in the |
| // Turbine object. |
| <span class="nc" id="L158"> this.serverData = (ServerData) serverData.clone();</span> |
| } |
| else |
| { |
| <span class="nc" id="L162"> log.error("Passed null ServerData object!");</span> |
| } |
| <span class="nc" id="L164"> reference = null;</span> |
| <span class="nc" id="L165"> }</span> |
| |
| /* |
| * ======================================================================== |
| * |
| * Getter / Setter |
| * |
| * ======================================================================== |
| * |
| */ |
| |
| /** |
| * Set the redirect Flag |
| * |
| * @param redirect The new value of the redirect flag. |
| */ |
| public void setRedirect(boolean redirect) |
| { |
| <span class="nc" id="L183"> this.redirect = redirect;</span> |
| <span class="nc" id="L184"> }</span> |
| |
| /** |
| * Returns the current value of the Redirect flag |
| * |
| * @return True if Redirect is allowed |
| * |
| */ |
| public boolean isRedirect() |
| { |
| <span class="nc" id="L194"> return redirect;</span> |
| } |
| |
| /** |
| * Gets the script name (/servlets/Turbine). |
| * |
| * @return A String with the script name. |
| */ |
| @Override |
| public String getScriptName() |
| { |
| <span class="nc" id="L205"> return serverData.getScriptName();</span> |
| } |
| |
| /** |
| * Sets the script name (/servlets/Turbine). |
| * |
| * @param scriptName A String with the script name. |
| */ |
| public void setScriptName(String scriptName) |
| { |
| <span class="nc" id="L215"> serverData.setScriptName(scriptName);</span> |
| <span class="nc" id="L216"> }</span> |
| |
| /** |
| * Gets the context path. |
| * |
| * @return A String with the context path. |
| */ |
| @Override |
| public String getContextPath() |
| { |
| <span class="nc" id="L226"> return serverData.getContextPath();</span> |
| } |
| |
| /** |
| * Sets the context path. |
| * |
| * @param contextPath A String with the context path |
| */ |
| public void setContextPath(String contextPath) |
| { |
| <span class="nc" id="L236"> serverData.setContextPath(contextPath);</span> |
| <span class="nc" id="L237"> }</span> |
| |
| /** |
| * Gets the server name. |
| * |
| * @return A String with the server name. |
| */ |
| @Override |
| public String getServerName() |
| { |
| <span class="nc" id="L247"> return serverData.getServerName();</span> |
| } |
| |
| /** |
| * Sets the server name. |
| * |
| * @param serverName A String with the server name. |
| */ |
| public void setServerName(String serverName) |
| { |
| <span class="nc" id="L257"> serverData.setServerName(serverName);</span> |
| <span class="nc" id="L258"> }</span> |
| |
| /** |
| * Gets the server port. |
| * |
| * @return A String with the server port. |
| */ |
| @Override |
| public int getServerPort() |
| { |
| <span class="nc" id="L268"> int serverPort = serverData.getServerPort();</span> |
| |
| <span class="nc bnc" id="L270" title="All 2 branches missed."> if (serverPort == 0)</span> |
| { |
| <span class="nc bnc" id="L272" title="All 2 branches missed."> if(getServerScheme().equals(HTTPS))</span> |
| { |
| <span class="nc" id="L274"> serverPort = HTTPS_PORT;</span> |
| } |
| else |
| { |
| <span class="nc" id="L278"> serverPort = HTTP_PORT;</span> |
| } |
| } |
| <span class="nc" id="L281"> return serverPort;</span> |
| } |
| |
| /** |
| * Sets the server port. |
| * |
| * @param serverPort An int with the port. |
| */ |
| public void setServerPort(int serverPort) |
| { |
| <span class="nc" id="L291"> serverData.setServerPort(serverPort);</span> |
| <span class="nc" id="L292"> }</span> |
| |
| /** |
| * Method to specify that a URI should use SSL. The default port |
| * is used. |
| */ |
| public void setSecure() |
| { |
| <span class="nc" id="L300"> setSecure(HTTPS_PORT);</span> |
| <span class="nc" id="L301"> }</span> |
| |
| /** |
| * Method to specify that a URI should use SSL. |
| * Whether or not it does is determined from Turbine.properties. |
| * If use.ssl in the Turbine.properties is set to false, then |
| * http is used in any case. (Default of use.ssl is true). |
| * |
| * @param port An int with the port number. |
| */ |
| public void setSecure(int port) |
| { |
| boolean useSSL = |
| <span class="nc" id="L314"> Turbine.getConfiguration()</span> |
| <span class="nc" id="L315"> .getBoolean(TurbineConstants.USE_SSL_KEY,</span> |
| TurbineConstants.USE_SSL_DEFAULT); |
| |
| <span class="nc bnc" id="L318" title="All 2 branches missed."> setServerScheme(useSSL ? HTTPS : HTTP);</span> |
| <span class="nc" id="L319"> setServerPort(port);</span> |
| <span class="nc" id="L320"> }</span> |
| |
| /** |
| * Sets the scheme (HTTP or HTTPS). |
| * |
| * @param serverScheme A String with the scheme. |
| */ |
| public void setServerScheme(String serverScheme) |
| { |
| <span class="nc bnc" id="L329" title="All 2 branches missed."> serverData.setServerScheme(StringUtils.isNotEmpty(serverScheme)</span> |
| <span class="nc" id="L330"> ? serverScheme : "");</span> |
| <span class="nc" id="L331"> }</span> |
| |
| /** |
| * Returns the current Server Scheme |
| * |
| * @return The current Server scheme |
| * |
| */ |
| @Override |
| public String getServerScheme() |
| { |
| <span class="nc" id="L342"> String serverScheme = serverData.getServerScheme();</span> |
| |
| <span class="nc bnc" id="L344" title="All 2 branches missed."> return StringUtils.isNotEmpty(serverScheme) ? serverScheme : HTTP;</span> |
| } |
| |
| /** |
| * Sets a reference anchor (#ref). |
| * |
| * @param reference A String containing the reference. |
| */ |
| public void setReference(String reference) |
| { |
| <span class="nc" id="L354"> this.reference = reference;</span> |
| <span class="nc" id="L355"> }</span> |
| |
| /** |
| * Returns the current reference anchor. |
| * |
| * @return A String containing the reference. |
| */ |
| @Override |
| public String getReference() |
| { |
| <span class="nc bnc" id="L365" title="All 2 branches missed."> return hasReference() ? reference : "";</span> |
| } |
| |
| /** |
| * Does this URI contain an anchor? (#ref) |
| * |
| * @return True if this URI contains an anchor. |
| */ |
| public boolean hasReference() |
| { |
| <span class="nc" id="L375"> return StringUtils.isNotEmpty(reference);</span> |
| } |
| |
| /* |
| * ======================================================================== |
| * |
| * Protected / Private Methods |
| * |
| * ======================================================================== |
| * |
| */ |
| |
| /** |
| * Set a Response Object to use when creating the |
| * response string. |
| * |
| * @param response the servlet response |
| */ |
| protected void setResponse(HttpServletResponse response) |
| { |
| <span class="nc" id="L395"> this.response = response;</span> |
| <span class="nc" id="L396"> }</span> |
| |
| /** |
| * Returns the Response Object from the Servlet Container. |
| * |
| * @return The Servlet Response object or null |
| * |
| */ |
| protected HttpServletResponse getResponse() |
| { |
| <span class="nc" id="L406"> return response;</span> |
| } |
| |
| /** |
| * Append the Context Path and Script Name to the passed |
| * String Buffer. |
| * |
| * <p> |
| * This is a convenience method to be |
| * used in the Link output routines of derived classes to |
| * easily append the correct path. |
| * </p> |
| * |
| * @param sb The StringBuilder to store context path and script name. |
| */ |
| protected void getContextAndScript(StringBuilder sb) |
| { |
| <span class="nc" id="L423"> String context = getContextPath();</span> |
| |
| <span class="nc bnc" id="L425" title="All 2 branches missed."> if(StringUtils.isNotEmpty(context))</span> |
| { |
| <span class="nc bnc" id="L427" title="All 2 branches missed."> if(context.charAt(0) != '/')</span> |
| { |
| <span class="nc" id="L429"> sb.append('/');</span> |
| } |
| <span class="nc" id="L431"> sb.append (context);</span> |
| } |
| |
| // /servlet/turbine |
| <span class="nc" id="L435"> String script = getScriptName();</span> |
| |
| <span class="nc bnc" id="L437" title="All 2 branches missed."> if(StringUtils.isNotEmpty(script))</span> |
| { |
| <span class="nc bnc" id="L439" title="All 2 branches missed."> if(script.charAt(0) != '/')</span> |
| { |
| <span class="nc" id="L441"> sb.append('/');</span> |
| } |
| <span class="nc" id="L443"> sb.append (script);</span> |
| } |
| <span class="nc" id="L445"> }</span> |
| |
| /** |
| * Appends Scheme, Server and optionally the port to the |
| * supplied String Buffer. |
| * |
| * <p> |
| * This is a convenience method to be |
| * used in the Link output routines of derived classes to |
| * easily append the correct server scheme. |
| * </p> |
| * |
| * @param sb The StringBuilder to store the scheme and port information. |
| */ |
| protected void getSchemeAndPort(StringBuilder sb) |
| { |
| // http(s)://<servername> |
| <span class="nc" id="L462"> sb.append(getServerScheme());</span> |
| <span class="nc" id="L463"> sb.append(URIConstants.URI_SCHEME_SEPARATOR);</span> |
| <span class="nc" id="L464"> sb.append(getServerName());</span> |
| |
| // (:<port>) |
| <span class="nc bnc" id="L467" title="All 2 branches missed."> if ((getServerScheme().equals(HTTP)</span> |
| <span class="nc bnc" id="L468" title="All 2 branches missed."> && getServerPort() != HTTP_PORT)</span> |
| <span class="nc bnc" id="L469" title="All 2 branches missed."> || (getServerScheme().equals(HTTPS)</span> |
| <span class="nc bnc" id="L470" title="All 2 branches missed."> && getServerPort() != HTTPS_PORT))</span> |
| { |
| <span class="nc" id="L472"> sb.append(':');</span> |
| <span class="nc" id="L473"> sb.append(getServerPort());</span> |
| } |
| <span class="nc" id="L475"> }</span> |
| |
| /** |
| * Encodes a Response Uri according to the Servlet Container. |
| * This might add a Java session identifier or do redirection. |
| * The resulting String can be used in a page or template. |
| * |
| * @param uri The Uri to encode |
| * |
| * @return An Uri encoded by the container. |
| */ |
| protected String encodeResponse(String uri) |
| { |
| <span class="nc" id="L488"> String res = uri;</span> |
| |
| <span class="nc" id="L490"> HttpServletResponse response = getResponse();</span> |
| |
| <span class="nc bnc" id="L492" title="All 2 branches missed."> if(response == null)</span> |
| { |
| <span class="nc" id="L494"> log.debug("No Response Object!");</span> |
| } |
| else |
| { |
| try |
| { |
| <span class="nc bnc" id="L500" title="All 2 branches missed."> if(isRedirect())</span> |
| { |
| <span class="nc" id="L502"> log.debug("Should Redirect");</span> |
| <span class="nc" id="L503"> res = response.encodeRedirectURL(uri);</span> |
| } |
| else |
| { |
| <span class="nc" id="L507"> res = response.encodeURL(uri);</span> |
| } |
| } |
| <span class="nc" id="L510"> catch(Exception e)</span> |
| { |
| <span class="nc" id="L512"> log.error("response" + response + ", uri: " + uri);</span> |
| <span class="nc" id="L513"> log.error("While trying to encode the URI: ", e);</span> |
| <span class="nc" id="L514"> }</span> |
| } |
| |
| <span class="nc" id="L517"> log.debug("encodeResponse(): " + res);</span> |
| <span class="nc" id="L518"> return res;</span> |
| } |
| } |
| </pre><div class="footer"><span class="right">Created with <a href="http://www.jacoco.org/jacoco">JaCoCo</a> 0.8.12.202403310830</span></div></body></html> |