Skip to content

Commit a2a3642

Browse files
authored
Redirect DTD http request to the resource file (#850)
* Redirect DTD http request to the resource file * Add JobSpec unittests * Remove Cuebot TCP 8080 port from Docker
1 parent 6d84794 commit a2a3642

File tree

6 files changed

+209
-6
lines changed

6 files changed

+209
-6
lines changed

cuebot/src/main/java/com/imageworks/spcue/service/JobSpec.java

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
package com.imageworks.spcue.service;
2121

2222
import java.io.File;
23+
import java.io.IOException;
24+
import java.io.InputStream;
2325
import java.io.StringReader;
2426
import java.util.ArrayList;
2527
import java.util.HashSet;
@@ -36,6 +38,9 @@
3638
import org.jdom.Element;
3739
import org.jdom.input.SAXBuilder;
3840
import org.springframework.dao.EmptyResultDataAccessException;
41+
import org.xml.sax.EntityResolver;
42+
import org.xml.sax.InputSource;
43+
import org.xml.sax.SAXException;
3944

4045
import com.imageworks.spcue.BuildableDependency;
4146
import com.imageworks.spcue.BuildableJob;
@@ -102,6 +107,8 @@ public class JobSpec {
102107

103108
public static final String DEFAULT_SERVICE = "default";
104109

110+
public static final String SPCUE_DTD_URL = "http://localhost:8080/spcue/dtd/";
111+
105112
private List<BuildableJob> jobs = new ArrayList<BuildableJob>();
106113

107114
private List<BuildableDependency> depends = new ArrayList<BuildableDependency>();
@@ -836,9 +843,30 @@ public void parse(File file) {
836843
handleDependsTags();
837844
}
838845

846+
private class DTDRedirector implements EntityResolver {
847+
public InputSource resolveEntity(String publicId,
848+
String systemId) throws SAXException, IOException {
849+
if (systemId.startsWith(SPCUE_DTD_URL)) {
850+
// Redirect to resource file.
851+
try {
852+
String filename = systemId.substring(SPCUE_DTD_URL.length());
853+
InputStream dtd = getClass().getResourceAsStream("/public/dtd/" + filename);
854+
return new InputSource(dtd);
855+
} catch (Exception e) {
856+
throw new SpecBuilderException("Failed to redirect DTD " + systemId + ", " + e);
857+
}
858+
} else {
859+
// Use default resolver.
860+
return null;
861+
}
862+
}
863+
}
864+
839865
public void parse(String cjsl) {
840866
try {
841-
doc = new SAXBuilder(true).build(new StringReader(cjsl));
867+
SAXBuilder builder = new SAXBuilder(true);
868+
builder.setEntityResolver(new DTDRedirector());
869+
doc = builder.build(new StringReader(cjsl));
842870

843871
} catch (Exception e) {
844872
throw new SpecBuilderException("Failed to parse job spec XML, " + e);
Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,2 @@
1-
server.contextPath=/spcue
2-
server.port=8080
3-
41
spring.main.allow-bean-definition-overriding=true
5-
spring.mvc.static-path-pattern=/spcue/**
2+
spring.main.web-application-type=none
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
2+
/*
3+
* Copyright Contributors to the OpenCue Project
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* 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+
18+
19+
package com.imageworks.spcue.test.service;
20+
21+
import java.io.IOException;
22+
import java.nio.charset.StandardCharsets;
23+
import java.nio.file.Files;
24+
import java.nio.file.Paths;
25+
import javax.annotation.Resource;
26+
27+
import org.junit.Test;
28+
import org.springframework.test.context.ContextConfiguration;
29+
import org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests;
30+
import org.springframework.test.context.support.AnnotationConfigContextLoader;
31+
32+
import com.imageworks.spcue.SpecBuilderException;
33+
import com.imageworks.spcue.config.TestAppConfig;
34+
import com.imageworks.spcue.service.JobLauncher;
35+
import com.imageworks.spcue.service.JobSpec;
36+
37+
import static org.junit.Assert.assertEquals;
38+
import static org.junit.Assert.assertTrue;
39+
import static org.junit.Assert.fail;
40+
41+
42+
@ContextConfiguration(classes=TestAppConfig.class, loader=AnnotationConfigContextLoader.class)
43+
public class JobSpecTests extends AbstractTransactionalJUnit4SpringContextTests {
44+
45+
@Resource
46+
JobLauncher jobLauncher;
47+
48+
private static String readJobSpec(String name)
49+
{
50+
String path = "src/test/resources/conf/jobspec/" + name;
51+
byte[] encoded = null;
52+
53+
try {
54+
encoded = Files.readAllBytes(Paths.get(path));
55+
} catch (IOException e) {
56+
fail("readJobSpec should succeed to read jobspec file");
57+
}
58+
59+
return new String(encoded, StandardCharsets.UTF_8);
60+
}
61+
62+
@Test
63+
public void testParseSuccess() {
64+
String xml = readJobSpec("jobspec_1_10.xml");
65+
JobSpec spec = jobLauncher.parse(xml);
66+
assertEquals(spec.getDoc().getDocType().getPublicID(),
67+
"SPI Cue Specification Language");
68+
assertEquals(spec.getDoc().getDocType().getSystemID(),
69+
"http://localhost:8080/spcue/dtd/cjsl-1.10.dtd");
70+
assertEquals(spec.getJobs().size(), 1);
71+
assertEquals(spec.getJobs().get(0).detail.name, "testing-default-testuser_test");
72+
}
73+
74+
@Test
75+
public void testParseNonExistent() {
76+
String xml = readJobSpec("jobspec_nonexistent_dtd.xml");
77+
try {
78+
jobLauncher.parse(xml);
79+
fail("Expected exception");
80+
} catch (SpecBuilderException e) {
81+
assertEquals(e.getMessage(),
82+
"Failed to parse job spec XML, java.net.MalformedURLException");
83+
}
84+
}
85+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
Copyright Contributors to the OpenCue Project
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
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+
18+
19+
20+
21+
<!DOCTYPE spec PUBLIC "SPI Cue Specification Language" "http://localhost:8080/spcue/dtd/cjsl-1.10.dtd">
22+
<spec>
23+
<facility>local</facility>
24+
<show>testing</show>
25+
<shot>default</shot>
26+
<user>testuser</user>
27+
<uid>9860</uid>
28+
29+
<job name="test">
30+
<paused>False</paused>
31+
<maxretries>2</maxretries>
32+
<autoeat>False</autoeat>
33+
<env/>
34+
<layers>
35+
<layer name="test" type="Render">
36+
<cmd>echo hello</cmd>
37+
<range>1</range>
38+
<chunk>1</chunk>
39+
<env/>
40+
<services>
41+
<service>shell</service>
42+
</services>
43+
</layer>
44+
</layers>
45+
</job>
46+
<depends/>
47+
</spec>
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
Copyright Contributors to the OpenCue Project
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
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+
18+
19+
20+
21+
<!DOCTYPE spec PUBLIC "SPI Cue Specification Language" "http://localhost:8080/spcue/dtd/cjsl-nonexistent.dtd">
22+
<spec>
23+
<facility>local</facility>
24+
<show>testing</show>
25+
<shot>default</shot>
26+
<user>testuser</user>
27+
<uid>9860</uid>
28+
29+
<job name="test">
30+
<paused>False</paused>
31+
<maxretries>2</maxretries>
32+
<autoeat>False</autoeat>
33+
<env/>
34+
<layers>
35+
<layer name="test" type="Render">
36+
<cmd>echo hello</cmd>
37+
<range>1</range>
38+
<chunk>1</chunk>
39+
<env/>
40+
<services>
41+
<service>shell</service>
42+
</services>
43+
</layer>
44+
</layers>
45+
</job>
46+
<depends/>
47+
</spec>

sandbox/docker-compose.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ services:
3434
- db
3535
ports:
3636
- "8443:8443"
37-
- "8080:8080"
3837
depends_on:
3938
- db
4039
- flyway

0 commit comments

Comments
 (0)