Skip to content

Commit 665864d

Browse files
committed
Finish SSI handler
1 parent 3c49d11 commit 665864d

10 files changed

Lines changed: 316 additions & 87 deletions

File tree

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package org.jooby.issues;
2+
3+
import org.jooby.handlers.SSIHandler;
4+
import org.jooby.test.ServerFeature;
5+
import org.junit.Test;
6+
7+
public class Issue644 extends ServerFeature {
8+
9+
{
10+
get("/i644/*", new SSIHandler());
11+
12+
get("/644/index.html",
13+
new SSIHandler("/i644/ssi-jooby-include.html")
14+
.delimiters("<!-- JOOBY-INCLUDE", "-->"));
15+
}
16+
17+
@Test
18+
public void ssi() throws Exception {
19+
request()
20+
.get("/i644/ssi.html")
21+
.expect("<!-- The template for the note object -->\n" +
22+
"<script type=\"text/x-template\" id=\"custom-note-template\">\n" +
23+
" <ul><li>Item</li></ul>\n" +
24+
"</script>")
25+
.header("Content-Type", "text/html;charset=utf-8");
26+
27+
request()
28+
.get("/644/index.html")
29+
.expect("<!-- The template for the note object -->\n" +
30+
"<script type=\"text/x-template\" id=\"custom-note-template\">\n" +
31+
" <ul><li>Item</li></ul>\n" +
32+
"</script>")
33+
.header("Content-Type", "text/html;charset=utf-8");
34+
}
35+
36+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<ul><li>Item</li></ul>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<!-- The template for the note object -->
2+
<script type="text/x-template" id="custom-note-template">
3+
<!-- JOOBY-INCLUDE /i644/chunk.html -->
4+
</script>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<!-- The template for the note object -->
2+
<script type="text/x-template" id="custom-note-template">
3+
<!-- /i644/chunk.html -->
4+
</script>

jooby-assets/src/test/java/org/jooby/assets/PropsTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,18 +51,18 @@ public void missingProp() throws Exception {
5151
new Props().process("/j.s", "$.ajax(${cpath}/service);", ConfigFactory.empty());
5252
fail();
5353
} catch (AssetException ex) {
54-
assertEquals("\t/j.s:1:8: No configuration setting found for key 'cpath' at 1:8",
54+
assertEquals("\t/j.s:1:8: Missing ${cpath} at 1:8",
5555
ex.getMessage());
56-
assertEquals("No configuration setting found for key 'cpath' at 1:8", ex.get());
56+
assertEquals("Missing ${cpath} at 1:8", ex.get());
5757
}
5858

5959
try {
6060
new Props().process("/j.s", "$.ajax(\n\n ${cpath}/service);", ConfigFactory.empty());
6161
fail();
6262
} catch (AssetException ex) {
63-
assertEquals("\t/j.s:3:4: No configuration setting found for key 'cpath' at 3:4",
63+
assertEquals("\t/j.s:3:4: Missing ${cpath} at 3:4",
6464
ex.getMessage());
65-
assertEquals("No configuration setting found for key 'cpath' at 3:4", ex.get());
65+
assertEquals("Missing ${cpath} at 3:4", ex.get());
6666
}
6767
}
6868

jooby-run/TODO

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
how do I detect a hotswap? and only then bounce the app?
2+
-- JavaHotCodeReplaceManager at eclipse.jdt.debug
23

34
ideal implementation?
45
+ bounce when hotswap fail

jooby/src/main/java/org/jooby/Env.java

Lines changed: 84 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141
import com.google.inject.Key;
4242
import com.google.inject.name.Names;
4343
import com.typesafe.config.Config;
44-
import com.typesafe.config.ConfigFactory;
4544

4645
import javaslang.API;
4746
import javaslang.control.Option;
@@ -69,6 +68,73 @@
6968
*/
7069
public interface Env extends LifeCycle {
7170

71+
/**
72+
* Property source for {@link Resolver}
73+
*
74+
* @author edgar
75+
* @since 1.1.0
76+
*/
77+
interface PropertySource {
78+
79+
/**
80+
* Get a property value or throw {@link NoSuchElementException}.
81+
*
82+
* @param key Property key/name.
83+
* @return Value or throw {@link NoSuchElementException}.
84+
* @throws NoSuchElementException If property is missing.
85+
*/
86+
String get(String key) throws NoSuchElementException;
87+
}
88+
89+
/**
90+
* {@link PropertySource} for {@link Config}.
91+
*
92+
* @author edgar
93+
* @since 1.1.0
94+
*/
95+
class ConfigSource implements PropertySource {
96+
97+
private Config source;
98+
99+
public ConfigSource(final Config source) {
100+
this.source = source;
101+
}
102+
103+
@Override
104+
public String get(final String key) throws NoSuchElementException {
105+
if (source.hasPath(key)) {
106+
return source.getString(key);
107+
}
108+
throw new NoSuchElementException(key);
109+
}
110+
111+
}
112+
113+
/**
114+
* {@link PropertySource} for {@link Map}.
115+
*
116+
* @author edgar
117+
* @since 1.1.0
118+
*/
119+
class MapSource implements PropertySource {
120+
121+
private Map<String, Object> source;
122+
123+
public MapSource(final Map<String, Object> source) {
124+
this.source = source;
125+
}
126+
127+
@Override
128+
public String get(final String key) throws NoSuchElementException {
129+
Object value = source.get(key);
130+
if (value != null) {
131+
return value.toString();
132+
}
133+
throw new NoSuchElementException(key);
134+
}
135+
136+
}
137+
72138
/**
73139
* Template literal implementation, replaces <code>${expression}</code> from a String using a
74140
* {@link Config} object.
@@ -80,7 +146,7 @@ class Resolver {
80146

81147
private String endDelim = "}";
82148

83-
private Config source;
149+
private PropertySource source;
84150

85151
private boolean ignoreMissing;
86152

@@ -91,8 +157,7 @@ class Resolver {
91157
* @return This resolver.
92158
*/
93159
public Resolver source(final Map<String, Object> source) {
94-
this.source = ConfigFactory.parseMap(source);
95-
return this;
160+
return source(new MapSource(source));
96161
}
97162

98163
/**
@@ -101,11 +166,21 @@ public Resolver source(final Map<String, Object> source) {
101166
* @param source Source.
102167
* @return This resolver.
103168
*/
104-
public Resolver source(final Config source) {
169+
public Resolver source(final PropertySource source) {
105170
this.source = source;
106171
return this;
107172
}
108173

174+
/**
175+
* Set property source.
176+
*
177+
* @param source Source.
178+
* @return This resolver.
179+
*/
180+
public Resolver source(final Config source) {
181+
return source(new ConfigSource(source));
182+
}
183+
109184
/**
110185
* Set start and end delimiters.
111186
*
@@ -166,14 +241,14 @@ public String resolve(final String text) {
166241
buffer.append(text.substring(offset, start));
167242
String key = text.substring(start + startDelim.length(), end);
168243
Object value;
169-
if (source.hasPath(key)) {
170-
value = source.getAnyRef(key);
171-
} else {
244+
try {
245+
value = source.get(key);
246+
} catch (NoSuchElementException x) {
172247
if (ignoreMissing) {
173248
value = text.substring(start, end + endDelim.length());
174249
} else {
175250
throw err.apply(start, (line, column) -> new NoSuchElementException(
176-
"No configuration setting found for key '" + key + "' at " + line + ":" + column));
251+
"Missing " + startDelim + key + endDelim + " at " + line + ":" + column));
177252
}
178253
}
179254
buffer.append(value);

jooby/src/main/java/org/jooby/SSIHandler.java

Lines changed: 0 additions & 67 deletions
This file was deleted.

0 commit comments

Comments
 (0)