Skip to content

Commit a64839a

Browse files
committed
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
1 parent 9b3b81f commit a64839a

File tree

5 files changed

+133
-2
lines changed

5 files changed

+133
-2
lines changed

java/org/apache/catalina/ant/DeployTask.java

+12-2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import java.net.URL;
2727
import java.net.URLConnection;
2828
import java.net.URLEncoder;
29+
import java.util.regex.Pattern;
2930

3031
import org.apache.tools.ant.BuildException;
3132

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

4244

4345
// ------------------------------------------------------------- Properties
@@ -140,7 +142,7 @@ public void execute() throws BuildException {
140142
String contentType = null;
141143
int contentLength = -1;
142144
if (war != null) {
143-
if (!war.startsWith("file:")) {
145+
if (PROTOCOL_PATTERN.matcher(war).lookingAt()) {
144146
try {
145147
URL url = new URL(war);
146148
URLConnection conn = url.openConnection();
@@ -151,8 +153,9 @@ public void execute() throws BuildException {
151153
throw new BuildException(e);
152154
}
153155
} else {
156+
FileInputStream fsInput = null;
154157
try {
155-
FileInputStream fsInput = new FileInputStream(war);
158+
fsInput = new FileInputStream(war);
156159
long size = fsInput.getChannel().size();
157160

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

166169
} catch (IOException e) {
170+
if (fsInput != null) {
171+
try {
172+
fsInput.close();
173+
} catch (IOException ioe) {
174+
// Ignore
175+
}
176+
}
167177
throw new BuildException(e);
168178
}
169179
}

test/deployment/context.jar

489 Bytes
Binary file not shown.
489 Bytes
Binary file not shown.
565 Bytes
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.catalina.ant;
18+
19+
import java.io.File;
20+
import java.io.IOException;
21+
import java.io.InputStream;
22+
23+
import static org.junit.Assert.assertEquals;
24+
25+
import org.junit.Test;
26+
27+
import org.apache.catalina.startup.Tomcat;
28+
import org.apache.catalina.startup.TomcatBaseTest;
29+
import org.apache.tools.ant.BuildException;
30+
31+
public class TestDeployTask extends TomcatBaseTest {
32+
33+
@Test
34+
public void bug58086a() {
35+
DeployTask deployTask = new DeployTask() {
36+
37+
@Override
38+
public void execute(String command, InputStream istream, String contentType, int contentLength)
39+
throws BuildException {
40+
assertEquals("/deploy?path=somepath", command);
41+
assertEquals("application/octet-stream", contentType);
42+
try {
43+
istream.close();
44+
} catch (IOException e) {
45+
}
46+
}
47+
48+
};
49+
50+
setDefaults(deployTask);
51+
52+
testExecute(deployTask, "file:./test/deployment/context.war");
53+
testExecute(deployTask, "file:.\\test\\deployment\\context.war");
54+
testExecute(deployTask, new File("test/deployment/context.war").toURI().toString());
55+
testExecute(deployTask, new File("test/deployment/context.war").getAbsolutePath());
56+
testExecute(deployTask, "jar:" + new File("test/deployment/context.jar").toURI().toString() + "!/context.war");
57+
testExecute(deployTask, "file:./test/deployment/dir with spaces/context.war");
58+
testExecute(deployTask, "file:.\\test\\deployment\\dir with spaces\\context.war");
59+
testExecute(deployTask, new File("test/deployment/dir with spaces/context.war").toURI().toString());
60+
testExecute(deployTask, new File("test/deployment/dir with spaces/context.war").getAbsolutePath());
61+
testExecute(deployTask, "jar:" + new File("test/deployment/dir with spaces/context.jar").toURI().toString()
62+
+ "!/context.war");
63+
testExecute(deployTask, "file:./test/deployment/dir%20with%20spaces/context.war");
64+
testExecute(deployTask, "file:.\\test\\deployment\\dir%20with%20spaces\\context.war");
65+
}
66+
67+
@Test(expected = BuildException.class)
68+
public void bug58086b() {
69+
DeployTask deployTask = new DeployTask();
70+
setDefaults(deployTask);
71+
testExecute(deployTask, "scheme:./test/deployment/context.war");
72+
}
73+
74+
@Test(expected = BuildException.class)
75+
public void bug58086c() {
76+
DeployTask deployTask = new DeployTask();
77+
setDefaults(deployTask);
78+
testExecute(deployTask, "sc:./test/deployment/context.war");
79+
}
80+
81+
@Test
82+
public void bug58086d() throws Exception {
83+
Tomcat tomcat = getTomcatInstance();
84+
85+
File root = new File("test/deployment");
86+
tomcat.addWebapp("", root.getAbsolutePath());
87+
88+
tomcat.start();
89+
90+
DeployTask deployTask = new DeployTask() {
91+
92+
@Override
93+
public void execute(String command, InputStream istream, String contentType, int contentLength)
94+
throws BuildException {
95+
assertEquals("/deploy?path=somepath", command);
96+
assertEquals("application/octet-stream", contentType);
97+
try {
98+
istream.close();
99+
} catch (IOException e) {
100+
}
101+
}
102+
103+
};
104+
105+
setDefaults(deployTask);
106+
107+
testExecute(deployTask, "http://localhost:" + getPort() + "/context.war");
108+
}
109+
110+
private void setDefaults(DeployTask deployTask) {
111+
deployTask.setUrl("someurl");
112+
deployTask.setUsername("someuser");
113+
deployTask.setPassword("somepassword");
114+
deployTask.setPath("somepath");
115+
}
116+
117+
private void testExecute(DeployTask deployTask, String war) {
118+
deployTask.setWar(war);
119+
deployTask.execute();
120+
}
121+
}

0 commit comments

Comments
 (0)