Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit dc87c0f

Browse filesBrowse files
committed
10_2_HW9_host_config
1 parent 7cddebb commit dc87c0f
Copy full SHA for dc87c0f

File tree

Expand file treeCollapse file tree

7 files changed

+71
-22
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

7 files changed

+71
-22
lines changed
Open diff view settings
Collapse file
+4Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
client.debugLevel = INFO
2+
server.debugLevel = INFO
3+
user = null
4+
password = null
Collapse file
-4Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
hosts {
22
mail {
33
endpoint = "http://localhost:8080"
4-
debug.client = DEBUG
5-
debug.server = INFO
6-
user = "user"
7-
password = "password"
84
}
95
}
106
include file("/apps/masterjava/config/hosts.conf")
Collapse file

‎services/common-ws/src/main/java/ru/javaops/masterjava/web/WsClient.java‎

Copy file name to clipboardExpand all lines: services/common-ws/src/main/java/ru/javaops/masterjava/web/WsClient.java
+49-3Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package ru.javaops.masterjava.web;
22

33
import com.typesafe.config.Config;
4+
import org.slf4j.event.Level;
45
import ru.javaops.masterjava.ExceptionType;
56
import ru.javaops.masterjava.config.Configs;
7+
import ru.javaops.masterjava.web.handler.SoapLoggingHandlers;
68

79
import javax.xml.namespace.QName;
810
import javax.xml.ws.Binding;
@@ -19,7 +21,40 @@ public class WsClient<T> {
1921

2022
private final Class<T> serviceClass;
2123
private final Service service;
22-
private String endpointAddress;
24+
private HostConfig hostConfig;
25+
26+
public static class HostConfig {
27+
public final String endpoint;
28+
public final Level serverDebugLevel;
29+
public final String user;
30+
public final String password;
31+
public final String authHeader;
32+
public final SoapLoggingHandlers.ClientHandler clientLoggingHandler;
33+
34+
public HostConfig(Config config, String endpointAddress) {
35+
endpoint = config.getString("endpoint") + endpointAddress;
36+
serverDebugLevel = config.getEnum(Level.class, "server.debugLevel");
37+
38+
// https://github.com/typesafehub/config/issues/282
39+
if (!config.getIsNull("user") && !config.getIsNull("password")) {
40+
user = config.getString("user");
41+
password = config.getString("password");
42+
authHeader = AuthUtil.encodeBasicAuthHeader(user, password);
43+
} else {
44+
user = password = authHeader = null;
45+
}
46+
clientLoggingHandler = config.getIsNull("client.debugLevel") ? null :
47+
new SoapLoggingHandlers.ClientHandler(config.getEnum(Level.class, "client.debugLevel"));
48+
}
49+
50+
public boolean hasAuthorization() {
51+
return authHeader != null;
52+
}
53+
54+
public boolean hasHandler() {
55+
return clientLoggingHandler != null;
56+
}
57+
}
2358

2459
static {
2560
HOSTS = Configs.getConfig("hosts.conf", "hosts");
@@ -31,15 +66,26 @@ public WsClient(URL wsdlUrl, QName qname, Class<T> serviceClass) {
3166
}
3267

3368
public void init(String host, String endpointAddress) {
34-
this.endpointAddress = HOSTS.getConfig(host).getString("endpoint") + endpointAddress;
69+
this.hostConfig = new HostConfig(
70+
HOSTS.getConfig(host).withFallback(Configs.getConfig("defaults.conf")), endpointAddress);
71+
}
72+
73+
public HostConfig getHostConfig() {
74+
return hostConfig;
3575
}
3676

3777
// Post is not thread-safe (http://stackoverflow.com/a/10601916/548473)
3878
public T getPort(WebServiceFeature... features) {
3979
T port = service.getPort(serviceClass, features);
4080
BindingProvider bp = (BindingProvider) port;
4181
Map<String, Object> requestContext = bp.getRequestContext();
42-
requestContext.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, endpointAddress);
82+
requestContext.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, hostConfig.endpoint);
83+
if (hostConfig.hasAuthorization()) {
84+
setAuth(port, hostConfig.user, hostConfig.password);
85+
}
86+
if (hostConfig.hasHandler()) {
87+
setHandler(port, hostConfig.clientLoggingHandler);
88+
}
4389
return port;
4490
}
4591

Collapse file

‎services/common-ws/src/main/java/ru/javaops/masterjava/web/handler/SoapLoggingHandlers.java‎

Copy file name to clipboardExpand all lines: services/common-ws/src/main/java/ru/javaops/masterjava/web/handler/SoapLoggingHandlers.java
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,8 @@ protected boolean isRequest(boolean isOutbound) {
137137

138138
public static class ServerHandler extends SoapLoggingHandlers {
139139

140-
public ServerHandler() {
141-
super(Level.INFO);
140+
public ServerHandler(Level loggingLevel) {
141+
super(loggingLevel);
142142
}
143143

144144
@Override
Collapse file

‎services/mail-api/src/main/java/ru/javaops/masterjava/service/mail/MailWSClient.java‎

Copy file name to clipboardExpand all lines: services/mail-api/src/main/java/ru/javaops/masterjava/service/mail/MailWSClient.java
+5-9Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,8 @@
55
import com.google.common.collect.Iterables;
66
import com.google.common.io.Resources;
77
import lombok.extern.slf4j.Slf4j;
8-
import org.slf4j.event.Level;
98
import ru.javaops.masterjava.web.WebStateException;
109
import ru.javaops.masterjava.web.WsClient;
11-
import ru.javaops.masterjava.web.handler.SoapLoggingHandlers;
1210

1311
import javax.xml.namespace.QName;
1412
import javax.xml.ws.soap.MTOMFeature;
@@ -18,9 +16,6 @@
1816
@Slf4j
1917
public class MailWSClient {
2018
private static final WsClient<MailService> WS_CLIENT;
21-
public static final String USER = "user";
22-
public static final String PASSWORD = "password";
23-
private static final SoapLoggingHandlers.ClientHandler LOGGING_HANDLER = new SoapLoggingHandlers.ClientHandler(Level.DEBUG);
2419

2520
static {
2621
WS_CLIENT = new WsClient<>(Resources.getResource("wsdl/mailService.wsdl"),
@@ -46,14 +41,15 @@ public static GroupResult sendBulk(final Set<Addressee> to, final String subject
4641
}
4742

4843
private static MailService getPort() {
49-
MailService port = WS_CLIENT.getPort(new MTOMFeature(1024));
50-
WsClient.setAuth(port, USER, PASSWORD);
51-
WsClient.setHandler(port, LOGGING_HANDLER);
52-
return port;
44+
return WS_CLIENT.getPort(new MTOMFeature(1024));
5345
}
5446

5547
public static Set<Addressee> split(String addressees) {
5648
Iterable<String> split = Splitter.on(',').trimResults().omitEmptyStrings().split(addressees);
5749
return ImmutableSet.copyOf(Iterables.transform(split, Addressee::new));
5850
}
51+
52+
public static WsClient.HostConfig getHostConfig() {
53+
return WS_CLIENT.getHostConfig();
54+
}
5955
}
Collapse file
+8-1Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
11
package ru.javaops.masterjava.service.mail;
22

3+
import ru.javaops.masterjava.web.handler.SoapLoggingHandlers;
34
import ru.javaops.masterjava.web.handler.SoapServerSecurityHandler;
45

56
public class MailHandlers {
67
public static class SecurityHandler extends SoapServerSecurityHandler {
78
public SecurityHandler() {
8-
super(MailWSClient.USER, MailWSClient.PASSWORD);
9+
super(MailWSClient.getHostConfig().authHeader);
10+
}
11+
}
12+
13+
public static class LoggingHandler extends SoapLoggingHandlers.ServerHandler {
14+
public LoggingHandler() {
15+
super(MailWSClient.getHostConfig().serverDebugLevel);
916
}
1017
}
1118
}
Collapse file

‎services/mail-service/src/main/resources/mailWsHandlers.xml‎

Copy file name to clipboardExpand all lines: services/mail-service/src/main/resources/mailWsHandlers.xml
+3-3Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
<handler-chains xmlns="http://java.sun.com/xml/ns/javaee">
22
<handler-chain>
33
<handler>
4-
<handler-name>SoapLoggingHandler</handler-name>
5-
<handler-class>ru.javaops.masterjava.web.handler.SoapLoggingHandlers$ServerHandler</handler-class>
4+
<handler-name>MailLoggingHandler</handler-name>
5+
<handler-class>ru.javaops.masterjava.service.mail.MailHandlers$LoggingHandler</handler-class>
66
</handler>
77
<handler>
88
<handler-name>SoapStatisticHandler</handler-name>
99
<handler-class>ru.javaops.masterjava.web.handler.SoapStatisticHandler</handler-class>
1010
</handler>
1111
<handler>
12-
<handler-name>SoapStatisticHandler</handler-name>
12+
<handler-name>MailSecurityHandler</handler-name>
1313
<handler-class>ru.javaops.masterjava.service.mail.MailHandlers$SecurityHandler</handler-class>
1414
</handler>
1515
</handler-chain>

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.