Skip to content

Commit 910f063

Browse files
committed
allow to set port, lang, charset, dateFormat... programmatically fix #441
1 parent 06ada34 commit 910f063

File tree

5 files changed

+215
-15
lines changed

5 files changed

+215
-15
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package org.jooby.issues;
2+
3+
import java.net.URISyntaxException;
4+
import java.nio.charset.StandardCharsets;
5+
import java.time.ZoneId;
6+
7+
import org.jooby.test.ServerFeature;
8+
import org.junit.Test;
9+
10+
import com.typesafe.config.Config;
11+
12+
public class Issue441 extends ServerFeature {
13+
14+
{
15+
16+
port(9000);
17+
18+
charset(StandardCharsets.US_ASCII);
19+
dateFormat("YYYY-MM-dd");
20+
numberFormat("000000.000");
21+
lang("es");
22+
timezone(ZoneId.of("Europe/Paris"));
23+
24+
get("/441", req -> {
25+
Config conf = req.require(Config.class);
26+
return req.charset() + ";" + req.locale() + ";" + conf.getString("application.dateFormat")
27+
+ ";" + conf.getString("application.numberFormat") + ";"
28+
+ conf.getString("application.tz") + ":" + req.port();
29+
});
30+
}
31+
32+
@Test
33+
public void harcodeCodeOptions() throws URISyntaxException, Exception {
34+
request()
35+
.get("/441")
36+
.expect("US-ASCII;es;YYYY-MM-dd;000000.000;Europe/Paris:9000");
37+
}
38+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package org.jooby.issues;
2+
3+
import org.jooby.test.ServerFeature;
4+
import org.junit.AfterClass;
5+
import org.junit.BeforeClass;
6+
import org.junit.Test;
7+
8+
public class Issue441b extends ServerFeature {
9+
10+
{
11+
securePort(8877);
12+
13+
get("/441", req -> req.port());
14+
}
15+
16+
@BeforeClass
17+
public static void httpsOn() {
18+
protocol = "https";
19+
}
20+
21+
@AfterClass
22+
public static void httpsOff() {
23+
protocol = "http";
24+
}
25+
26+
@Test
27+
public void hello() throws Exception {
28+
request()
29+
.get("/441")
30+
.expect("8877");
31+
32+
}
33+
34+
}

jooby/src/main/java/org/jooby/Jooby.java

Lines changed: 112 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@
126126
import org.slf4j.Logger;
127127
import org.slf4j.LoggerFactory;
128128

129+
import com.google.common.base.Joiner;
129130
import com.google.common.base.Strings;
130131
import com.google.common.collect.ImmutableList;
131132
import com.google.common.collect.ImmutableMap;
@@ -655,6 +656,20 @@ public EnvDep(final Predicate<String> predicate, final Consumer<Config> callback
655656

656657
private ServerLookup server = new ServerLookup();
657658

659+
private String dateFormat;
660+
661+
private Charset charset;
662+
663+
private String[] languages;
664+
665+
private ZoneId zoneId;
666+
667+
private Integer port;
668+
669+
private Integer securePort;
670+
671+
private String numberFormat;
672+
658673
public Jooby() {
659674
this(null);
660675
}
@@ -3773,6 +3788,83 @@ public <T> Jooby bind(final Function<Config, T> provider) {
37733788
return this;
37743789
}
37753790

3791+
/**
3792+
* Set application date format.
3793+
*
3794+
* @param dateFormat A date format.
3795+
* @return This instance.
3796+
*/
3797+
public Jooby dateFormat(final String dateFormat) {
3798+
this.dateFormat = requireNonNull(dateFormat, "DateFormat required.");
3799+
return this;
3800+
}
3801+
3802+
/**
3803+
* Set application number format.
3804+
*
3805+
* @param numberFormat A number format.
3806+
* @return This instance.
3807+
*/
3808+
public Jooby numberFormat(final String numberFormat) {
3809+
this.numberFormat = requireNonNull(numberFormat, "NumberFormat required.");
3810+
return this;
3811+
}
3812+
3813+
/**
3814+
* Set application/default charset.
3815+
*
3816+
* @param charset A charset.
3817+
* @return This instance.
3818+
*/
3819+
public Jooby charset(final Charset charset) {
3820+
this.charset = requireNonNull(charset, "Charset required.");
3821+
return this;
3822+
}
3823+
3824+
/**
3825+
* Set application locale (first listed are higher priority).
3826+
*
3827+
* @param Languages list of locale using the language tag format.
3828+
* @return This instance.
3829+
*/
3830+
public Jooby lang(final String... languages) {
3831+
this.languages = languages;
3832+
return this;
3833+
}
3834+
3835+
/**
3836+
* Set application time zone.
3837+
*
3838+
* @param zoneId ZoneId.
3839+
* @return This instance.
3840+
*/
3841+
public Jooby timezone(final ZoneId zoneId) {
3842+
this.zoneId = requireNonNull(zoneId, "ZoneId required.");
3843+
return this;
3844+
}
3845+
3846+
/**
3847+
* Set the HTTP port.
3848+
*
3849+
* @param port HTTP port.
3850+
* @return This instance.
3851+
*/
3852+
public Jooby port(final int port) {
3853+
this.port = port;
3854+
return this;
3855+
}
3856+
3857+
/**
3858+
* Set the HTTPS port.
3859+
*
3860+
* @param port HTTPS port.
3861+
* @return This instance.
3862+
*/
3863+
public Jooby securePort(final int port) {
3864+
this.securePort = port;
3865+
return this;
3866+
}
3867+
37763868
/**
37773869
* Run app in javascript.
37783870
*
@@ -4337,7 +4429,9 @@ private Config defaultConfig(final Config config) {
43374429
// locale
43384430
final List<Locale> locales;
43394431
if (!config.hasPath("application.lang")) {
4340-
locales = ImmutableList.of(Locale.getDefault());
4432+
locales = Optional.ofNullable(this.languages)
4433+
.map(langs -> LocaleUtils.parse(Joiner.on(",").join(langs)))
4434+
.orElse(ImmutableList.of(Locale.getDefault()));
43414435
} else {
43424436
locales = LocaleUtils.parse(config.getString("application.lang"));
43434437
}
@@ -4347,15 +4441,16 @@ private Config defaultConfig(final Config config) {
43474441
// time zone
43484442
final String tz;
43494443
if (!config.hasPath("application.tz")) {
4350-
tz = ZoneId.systemDefault().getId();
4444+
tz = Optional.ofNullable(zoneId).orElse(ZoneId.systemDefault()).getId();
43514445
} else {
43524446
tz = config.getString("application.tz");
43534447
}
43544448

43554449
// number format
43564450
final String nf;
43574451
if (!config.hasPath("application.numberFormat")) {
4358-
nf = ((DecimalFormat) DecimalFormat.getInstance(locale)).toPattern();
4452+
nf = Optional.ofNullable(numberFormat)
4453+
.orElseGet(() -> ((DecimalFormat) DecimalFormat.getInstance(locale)).toPattern());
43594454
} else {
43604455
nf = config.getString("application.numberFormat");
43614456
}
@@ -4378,6 +4473,20 @@ private Config defaultConfig(final Config config) {
43784473
.withValue("runtime.concurrencyLevel", ConfigValueFactory
43794474
.fromAnyRef(Math.max(4, processors)));
43804475

4476+
if (charset != null) {
4477+
defs = defs.withValue("application.charset", ConfigValueFactory.fromAnyRef(charset.name()));
4478+
}
4479+
if (port != null) {
4480+
defs = defs.withValue("application.port", ConfigValueFactory.fromAnyRef(port.intValue()));
4481+
}
4482+
if (securePort != null) {
4483+
defs = defs.withValue("application.securePort",
4484+
ConfigValueFactory.fromAnyRef(securePort.intValue()));
4485+
}
4486+
if (dateFormat != null) {
4487+
defs = defs.withValue("application.dateFormat", ConfigValueFactory.fromAnyRef(dateFormat));
4488+
}
4489+
43814490
return defs;
43824491
}
43834492

jooby/src/test/java/org/jooby/test/JoobyRunner.java

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
*/
1919
package org.jooby.test;
2020

21-
import java.io.IOException;
2221
import java.util.ArrayList;
2322
import java.util.Arrays;
2423
import java.util.List;
@@ -42,7 +41,9 @@ public class JoobyRunner extends BlockJUnit4ClassRunner {
4241

4342
private Jooby app;
4443

45-
private int port;
44+
private int port = 9999;
45+
46+
private int securePort = 9943;
4647

4748
private Class<?> server;
4849

@@ -76,7 +77,6 @@ private void prepare(final Class<?> klass, final Class<?> server) throws Initial
7677
try {
7778
this.server = server;
7879
Class<?> appClass = klass;
79-
port = freePort();
8080
if (!Jooby.class.isAssignableFrom(appClass)) {
8181
throw new InitializationError("Invalid jooby app: " + appClass);
8282
}
@@ -98,9 +98,22 @@ private void prepare(final Class<?> klass, final Class<?> server) throws Initial
9898
.withValue("server.module", ConfigValueFactory.fromAnyRef(server.getName())));
9999
}
100100

101-
Config testConfig = config;
102-
103101
app = (Jooby) appClass.newInstance();
102+
if (app instanceof ServerFeature) {
103+
int appport = ((ServerFeature) app).port;
104+
if (appport > 0) {
105+
config = config.withValue("application.port", ConfigValueFactory.fromAnyRef(appport));
106+
this.port = appport;
107+
}
108+
109+
int sappport = ((ServerFeature) app).securePort;
110+
if (sappport > 0) {
111+
config = config.withValue("application.securePort",
112+
ConfigValueFactory.fromAnyRef(sappport));
113+
this.securePort = sappport;
114+
}
115+
}
116+
Config testConfig = config;
104117
app.use(new Jooby.Module() {
105118
@Override
106119
public void configure(final Env mode, final Config config, final Binder binder) {
@@ -135,7 +148,7 @@ protected Object createTest() throws Exception {
135148
Object test = super.createTest();
136149
Guice.createInjector(binder -> {
137150
binder.bind(Integer.class).annotatedWith(Names.named("port")).toInstance(port);
138-
binder.bind(Integer.class).annotatedWith(Names.named("securePort")).toInstance(9943);
151+
binder.bind(Integer.class).annotatedWith(Names.named("securePort")).toInstance(securePort);
139152
}).injectMembers(test);
140153

141154
return test;
@@ -171,10 +184,4 @@ public void evaluate() throws Throwable {
171184
};
172185
}
173186

174-
private int freePort() throws IOException {
175-
// try (ServerSocket socket = new ServerSocket(0)) {
176-
// return socket.getLocalPort();
177-
// }
178-
return 9999;
179-
}
180187
}

jooby/src/test/java/org/jooby/test/ServerFeature.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,16 @@ protected URIBuilder ws(final String... parts) throws Exception {
7474
return builder;
7575
}
7676

77+
@Override
78+
public Jooby securePort(final int port) {
79+
this.securePort = port;
80+
return super.securePort(port);
81+
}
82+
83+
@Override
84+
public Jooby port(final int port) {
85+
this.port = port;
86+
return super.port(port);
87+
}
88+
7789
}

0 commit comments

Comments
 (0)