Author: markt Date: Wed Dec 19 16:13:46 2018 New Revision: 1849311 URL: http://svn.apache.org/viewvc?rev=1849311&view=rev Log: Fix https://bz.apache.org/bugzilla/show_bug.cgi?id=54741 Add a new method, Tomcat.addWebapp(String,URL), that allows a web application to be deployed from a URL when using Tomcat in embedded mode.
Modified: tomcat/trunk/java/org/apache/catalina/startup/LocalStrings.properties tomcat/trunk/java/org/apache/catalina/startup/Tomcat.java tomcat/trunk/test/org/apache/catalina/startup/TestTomcat.java tomcat/trunk/webapps/docs/changelog.xml Modified: tomcat/trunk/java/org/apache/catalina/startup/LocalStrings.properties URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/startup/LocalStrings.properties?rev=1849311&r1=1849310&r2=1849311&view=diff ============================================================================== --- tomcat/trunk/java/org/apache/catalina/startup/LocalStrings.properties [UTF-8] (original) +++ tomcat/trunk/java/org/apache/catalina/startup/LocalStrings.properties [UTF-8] Wed Dec 19 16:13:46 2018 @@ -149,6 +149,8 @@ hostConfig.undeployVersion=Undeploying o passwdUserDatabase.readFail=Failed to obtain a complete set of users from /etc/passwd +tomcat.addWebapp.conflictChild=Unable to deploy WAR at [{0}] to context path [{1}] because of existing context [{2}] +tomcat.addWebapp.conflictFile=Unable to deploy WAR at [{0}] to context path [{1}] because of existing file [{2}] tomcat.baseDirMakeFail=Unable to create the directory [{0}] to use as the base directory tomcat.baseDirNotDir=The location [{0}] specified for the base directory is not a directory tomcat.homeDirMakeFail=Unable to create the directory [{0}] to use as the home directory Modified: tomcat/trunk/java/org/apache/catalina/startup/Tomcat.java URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/startup/Tomcat.java?rev=1849311&r1=1849310&r2=1849311&view=diff ============================================================================== --- tomcat/trunk/java/org/apache/catalina/startup/Tomcat.java (original) +++ tomcat/trunk/java/org/apache/catalina/startup/Tomcat.java Wed Dec 19 16:13:46 2018 @@ -19,10 +19,14 @@ package org.apache.catalina.startup; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; +import java.io.FileOutputStream; import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; import java.net.MalformedURLException; import java.net.URI; import java.net.URL; +import java.net.URLConnection; import java.security.Principal; import java.util.ArrayList; import java.util.HashMap; @@ -63,6 +67,8 @@ import org.apache.catalina.core.Standard import org.apache.catalina.core.StandardWrapper; import org.apache.catalina.realm.GenericPrincipal; import org.apache.catalina.realm.RealmBase; +import org.apache.catalina.util.ContextName; +import org.apache.catalina.util.IOTools; import org.apache.tomcat.util.ExceptionUtils; import org.apache.tomcat.util.buf.UriUtil; import org.apache.tomcat.util.descriptor.web.LoginConfig; @@ -231,6 +237,57 @@ public class Tomcat { } + /** + * Copy the specified WAR file to the Host's appBase and then call + * {@link #addWebapp(String, String)} with the newly copied WAR. The WAR + * will <b>NOT</b> be removed from the Host's appBase when the Tomcat + * instance stops. Note that {@link ExpandWar} provides utility methods that + * may be used to delete the WAR and/or expanded directory if required. + * + * @param contextPath The context mapping to use, "" for root context. + * @param source The location from which the WAR should be copied + * + * @return The deployed Context + * + * @throws IOException If an I/O error occurs while copying the WAR file + * from the specified URL to the appBase + */ + public Context addWebapp(String contextPath, URL source) throws IOException { + + ContextName cn = new ContextName(contextPath, null); + + // Make sure a conflicting web application has not already been deployed + Host h = getHost(); + if (h.findChild(cn.getName()) != null) { + throw new IllegalArgumentException(sm.getString("tomcat.addWebapp.conflictChild", + source, contextPath, cn.getName())); + } + + // Make sure appBase does not contain a conflicting web application + File targetWar = new File(h.getAppBaseFile(), cn.getBaseName() + ".war"); + File targetDir = new File(h.getAppBaseFile(), cn.getBaseName()); + + if (targetWar.exists()) { + throw new IllegalArgumentException(sm.getString("tomcat.addWebapp.conflictFile", + source, contextPath, targetWar.getAbsolutePath())); + } + if (targetDir.exists()) { + throw new IllegalArgumentException(sm.getString("tomcat.addWebapp.conflictFile", + source, contextPath, targetDir.getAbsolutePath())); + } + + // Should be good to copy the WAR now + URLConnection uConn = source.openConnection(); + + try (InputStream is = uConn.getInputStream(); + OutputStream os = new FileOutputStream(targetWar)) { + IOTools.flow(is, os); + } + + return addWebapp(contextPath, targetWar.getAbsolutePath()); + } + + /** * Add a context - programmatic mode, no default web.xml used. This means * that there is no JSP support (no JSP servlet), no default servlet and Modified: tomcat/trunk/test/org/apache/catalina/startup/TestTomcat.java URL: http://svn.apache.org/viewvc/tomcat/trunk/test/org/apache/catalina/startup/TestTomcat.java?rev=1849311&r1=1849310&r2=1849311&view=diff ============================================================================== --- tomcat/trunk/test/org/apache/catalina/startup/TestTomcat.java (original) +++ tomcat/trunk/test/org/apache/catalina/startup/TestTomcat.java Wed Dec 19 16:13:46 2018 @@ -619,4 +619,21 @@ public class TestTomcat extends TomcatBa throw new LifecycleException("Deliberately Broken"); } } + + + @Test + public void testAddWebappUrl() throws Exception { + URL docBase = new URL("jar:" + new File("test/deployment/context.jar").toURI().toString() + "!/context.war"); + + Tomcat tomcat = getTomcatInstance(); + tomcat.addWebapp("", docBase); + tomcat.start(); + + ByteChunk bc = new ByteChunk(); + int rc = getUrl("http://localhost:" + getPort() + "/", bc, null, null); + + Assert.assertEquals(200, rc); + // Index page in sample is 100 bytes + Assert.assertEquals(100, bc.getLength()); + } } Modified: tomcat/trunk/webapps/docs/changelog.xml URL: http://svn.apache.org/viewvc/tomcat/trunk/webapps/docs/changelog.xml?rev=1849311&r1=1849310&r2=1849311&view=diff ============================================================================== --- tomcat/trunk/webapps/docs/changelog.xml (original) +++ tomcat/trunk/webapps/docs/changelog.xml Wed Dec 19 16:13:46 2018 @@ -48,6 +48,11 @@ <subsection name="Catalina"> <changelog> <fix> + <bug>54741</bug>: Add a new method, + <code>Tomcat.addWebapp(String,URL)</code>, that allows a web application + to be deployed from a URL when using Tomcat in embedded mode. (markt) + </fix> + <fix> <bug>63002</bug>: Fix setting rewrite qsdiscard flag. (remm) </fix> <fix> --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org For additional commands, e-mail: dev-h...@tomcat.apache.org