Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Rework the change made with r1688563. When the war location points to a local path the FileInputStream will be used otherwise URLConnection will be used.

git-svn-id: https://svn.apache.org/repos/asf/tomcat/trunk@1689825 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
violetagg committed Jul 8, 2015
1 parent 9b3b81f commit a64839a
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 2 deletions.
14 changes: 12 additions & 2 deletions java/org/apache/catalina/ant/DeployTask.java
Expand Up @@ -26,6 +26,7 @@
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.regex.Pattern;

import org.apache.tools.ant.BuildException;

Expand All @@ -38,6 +39,7 @@
* @since 4.1
*/
public class DeployTask extends AbstractCatalinaCommandTask {
private static final Pattern PROTOCOL_PATTERN = Pattern.compile("\\w{3,5}\\:");


// ------------------------------------------------------------- Properties
Expand Down Expand Up @@ -140,7 +142,7 @@ public void execute() throws BuildException {
String contentType = null;
int contentLength = -1;
if (war != null) {
if (!war.startsWith("file:")) {
if (PROTOCOL_PATTERN.matcher(war).lookingAt()) {
try {
URL url = new URL(war);
URLConnection conn = url.openConnection();
Expand All @@ -151,8 +153,9 @@ public void execute() throws BuildException {
throw new BuildException(e);
}
} else {
FileInputStream fsInput = null;
try {
FileInputStream fsInput = new FileInputStream(war);
fsInput = new FileInputStream(war);
long size = fsInput.getChannel().size();

if (size > Integer.MAX_VALUE)
Expand All @@ -164,6 +167,13 @@ public void execute() throws BuildException {
stream = new BufferedInputStream(fsInput, 1024);

} catch (IOException e) {
if (fsInput != null) {
try {
fsInput.close();
} catch (IOException ioe) {
// Ignore
}
}
throw new BuildException(e);
}
}
Expand Down
Binary file added test/deployment/context.jar
Binary file not shown.
Binary file added test/deployment/dir with spaces/context.jar
Binary file not shown.
Binary file added test/deployment/dir with spaces/context.war
Binary file not shown.
121 changes: 121 additions & 0 deletions test/org/apache/catalina/ant/TestDeployTask.java
@@ -0,0 +1,121 @@
/*
* 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.
*/
package org.apache.catalina.ant;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

import static org.junit.Assert.assertEquals;

import org.junit.Test;

import org.apache.catalina.startup.Tomcat;
import org.apache.catalina.startup.TomcatBaseTest;
import org.apache.tools.ant.BuildException;

public class TestDeployTask extends TomcatBaseTest {

@Test
public void bug58086a() {
DeployTask deployTask = new DeployTask() {

@Override
public void execute(String command, InputStream istream, String contentType, int contentLength)
throws BuildException {
assertEquals("/deploy?path=somepath", command);
assertEquals("application/octet-stream", contentType);
try {
istream.close();
} catch (IOException e) {
}
}

};

setDefaults(deployTask);

testExecute(deployTask, "file:./test/deployment/context.war");
testExecute(deployTask, "file:.\\test\\deployment\\context.war");
testExecute(deployTask, new File("test/deployment/context.war").toURI().toString());
testExecute(deployTask, new File("test/deployment/context.war").getAbsolutePath());
testExecute(deployTask, "jar:" + new File("test/deployment/context.jar").toURI().toString() + "!/context.war");
testExecute(deployTask, "file:./test/deployment/dir with spaces/context.war");
testExecute(deployTask, "file:.\\test\\deployment\\dir with spaces\\context.war");
testExecute(deployTask, new File("test/deployment/dir with spaces/context.war").toURI().toString());
testExecute(deployTask, new File("test/deployment/dir with spaces/context.war").getAbsolutePath());
testExecute(deployTask, "jar:" + new File("test/deployment/dir with spaces/context.jar").toURI().toString()
+ "!/context.war");
testExecute(deployTask, "file:./test/deployment/dir%20with%20spaces/context.war");
testExecute(deployTask, "file:.\\test\\deployment\\dir%20with%20spaces\\context.war");
}

@Test(expected = BuildException.class)
public void bug58086b() {
DeployTask deployTask = new DeployTask();
setDefaults(deployTask);
testExecute(deployTask, "scheme:./test/deployment/context.war");
}

@Test(expected = BuildException.class)
public void bug58086c() {
DeployTask deployTask = new DeployTask();
setDefaults(deployTask);
testExecute(deployTask, "sc:./test/deployment/context.war");
}

@Test
public void bug58086d() throws Exception {
Tomcat tomcat = getTomcatInstance();

File root = new File("test/deployment");
tomcat.addWebapp("", root.getAbsolutePath());

tomcat.start();

DeployTask deployTask = new DeployTask() {

@Override
public void execute(String command, InputStream istream, String contentType, int contentLength)
throws BuildException {
assertEquals("/deploy?path=somepath", command);
assertEquals("application/octet-stream", contentType);
try {
istream.close();
} catch (IOException e) {
}
}

};

setDefaults(deployTask);

testExecute(deployTask, "http://localhost:" + getPort() + "/context.war");
}

private void setDefaults(DeployTask deployTask) {
deployTask.setUrl("someurl");
deployTask.setUsername("someuser");
deployTask.setPassword("somepassword");
deployTask.setPath("somepath");
}

private void testExecute(DeployTask deployTask, String war) {
deployTask.setWar(war);
deployTask.execute();
}
}

0 comments on commit a64839a

Please sign in to comment.