Skip to content

Commit 2122b18

Browse files
lesson09
1 parent c612345 commit 2122b18

28 files changed

Lines changed: 533 additions & 89 deletions

File tree

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11
hosts {
2-
mail = "http://localhost:8080"
2+
mail {
3+
endpoint = "http://localhost:8080"
4+
debug.client = DEBUG
5+
debug.server = INFO
6+
user = "user"
7+
password = "password"
8+
}
39
}
410
include file("/apps/masterjava/config/hosts.conf")

config_templates/wsdl/mailService.wsdl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
xmlns:tns="http://mail.javaops.ru/"
55
xmlns:xs="http://www.w3.org/2001/XMLSchema"
66
xmlns:common="http://common.javaops.ru/"
7+
xmlns:xmime="http://www.w3.org/2005/05/xmlmime"
78
xmlns="http://schemas.xmlsoap.org/wsdl/"
89
targetNamespace="http://mail.javaops.ru/"
910
name="MailServiceImplService">
@@ -22,6 +23,7 @@
2223
<xs:element name="cc" type="tns:addressee" minOccurs="0" maxOccurs="unbounded"/>
2324
<xs:element name="subject" type="xs:string" minOccurs="0"/>
2425
<xs:element name="body" type="xs:string" minOccurs="1"/>
26+
<xs:element name="attaches" type="tns:attach" minOccurs="0" maxOccurs="unbounded"/>
2527
</xs:sequence>
2628
</xs:complexType>
2729
<xs:complexType name="sendToGroupResponse">
@@ -35,6 +37,7 @@
3537
<xs:element name="to" type="tns:addressee" minOccurs="0" maxOccurs="unbounded"/>
3638
<xs:element name="subject" type="xs:string" minOccurs="0"/>
3739
<xs:element name="body" type="xs:string" minOccurs="1"/>
40+
<xs:element name="attaches" type="tns:attach" minOccurs="0" maxOccurs="unbounded"/>
3841
</xs:sequence>
3942
</xs:complexType>
4043
<xs:complexType name="sendBulkResponse">
@@ -69,6 +72,13 @@
6972
<xs:complexType name="sendMailResponse">
7073
<xs:sequence/>
7174
</xs:complexType>
75+
76+
<xs:complexType name="attach">
77+
<xs:sequence>
78+
<xs:element name="name" type="xs:string" minOccurs="0"/>
79+
<xs:element name="dataHandler" xmime:expectedContentTypes="application/octet-stream" type="xs:base64Binary" minOccurs="0"/>
80+
</xs:sequence>
81+
</xs:complexType>
7282
</xs:schema>
7383
</types>
7484
<message name="sendToGroup">

persist/pom.xml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,30 @@
3131
</execution>
3232
</executions>
3333
</plugin>
34+
35+
<!--http://www.liquibase.org/documentation/maven/ -->
36+
<plugin>
37+
<groupId>org.liquibase</groupId>
38+
<artifactId>liquibase-maven-plugin</artifactId>
39+
<version>3.5.3</version>
40+
<configuration>
41+
<changeLogFile>../sql/databaseChangeLog.sql</changeLogFile>
42+
<driver>org.postgresql.Driver</driver>
43+
<url>jdbc:postgresql://localhost:5432/masterjava</url>
44+
<username>user</username>
45+
<password>password</password>
46+
<!--<propertyFile>${masterjava.config}/liquibase.properties</propertyFile>-->
47+
<!--<propertyFileWillOverride>true</propertyFileWillOverride>-->
48+
</configuration>
49+
<executions>
50+
<execution>
51+
<phase>process-resources</phase>
52+
<goals>
53+
<goal>update</goal>
54+
</goals>
55+
</execution>
56+
</executions>
57+
</plugin>
3458
</plugins>
3559
</build>
3660

services/common-ws/pom.xml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@
5050
</exclusions>
5151
</dependency>
5252

53+
<dependency>
54+
<groupId>org.jvnet.mimepull</groupId>
55+
<artifactId>mimepull</artifactId>
56+
<version>1.9.4</version>
57+
</dependency>
58+
5359
<dependency>
5460
<groupId>javax.activation</groupId>
5561
<artifactId>activation</artifactId>
@@ -74,4 +80,4 @@
7480
<scope>provided</scope>
7581
</dependency>
7682
</dependencies>
77-
</project>
83+
</project>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package ru.javaops.web;
2+
3+
import lombok.extern.slf4j.Slf4j;
4+
5+
import javax.servlet.http.HttpServletResponse;
6+
import javax.xml.bind.DatatypeConverter;
7+
import java.util.List;
8+
import java.util.Map;
9+
10+
@Slf4j
11+
public class AuthUtil {
12+
private static final String AUTHORIZATION = "Authorization";
13+
14+
public static String encodeBasicAuthHeader(String name, String passw) {
15+
String authString = name + ":" + passw;
16+
return "Basic " + DatatypeConverter.printBase64Binary(authString.getBytes());
17+
}
18+
19+
public static int checkBasicAuth(Map<String, List<String>> headers, String basicAuthCredentials) {
20+
List<String> autHeaders = headers.get(AUTHORIZATION);
21+
if ((autHeaders == null || autHeaders.isEmpty())) {
22+
log.warn("Unauthorized access");
23+
return HttpServletResponse.SC_UNAUTHORIZED;
24+
} else {
25+
if (!autHeaders.get(0).equals(basicAuthCredentials)) {
26+
log.warn("Wrong password access");
27+
return HttpServletResponse.SC_FORBIDDEN;
28+
}
29+
return 0;
30+
}
31+
}
32+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package ru.javaops.web;
2+
3+
import lombok.extern.slf4j.Slf4j;
4+
5+
/**
6+
* gkislin
7+
* 09.01.2017
8+
*/
9+
@Slf4j
10+
public class Statistics {
11+
public enum RESULT {
12+
SUCCESS, FAIL
13+
}
14+
15+
public static void count(String payload, long startTime, RESULT result) {
16+
long now = System.currentTimeMillis();
17+
int ms = (int) (now - startTime);
18+
log.info(payload + " " + result.name() + " execution time(ms): " + ms);
19+
// place for statistics staff
20+
21+
}
22+
23+
}

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

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,13 @@
55
import ru.javaops.masterjava.config.Configs;
66

77
import javax.xml.namespace.QName;
8+
import javax.xml.ws.Binding;
89
import javax.xml.ws.BindingProvider;
910
import javax.xml.ws.Service;
11+
import javax.xml.ws.WebServiceFeature;
12+
import javax.xml.ws.handler.Handler;
1013
import java.net.URL;
14+
import java.util.List;
1115
import java.util.Map;
1216

1317
public class WsClient<T> {
@@ -31,14 +35,27 @@ public void init(String host, String endpointAddress) {
3135
}
3236

3337
// Post is not thread-safe (http://stackoverflow.com/a/10601916/548473)
34-
public T getPort() {
35-
T port = service.getPort(serviceClass);
38+
public T getPort(WebServiceFeature... features) {
39+
T port = service.getPort(serviceClass, features);
3640
BindingProvider bp = (BindingProvider) port;
3741
Map<String, Object> requestContext = bp.getRequestContext();
3842
requestContext.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, endpointAddress);
3943
return port;
4044
}
4145

46+
public static <T> void setAuth(T port, String user, String password) {
47+
Map<String, Object> requestContext = ((BindingProvider) port).getRequestContext();
48+
requestContext.put(BindingProvider.USERNAME_PROPERTY, user);
49+
requestContext.put(BindingProvider.PASSWORD_PROPERTY, password);
50+
}
51+
52+
public static <T> void setHandler(T port, Handler handler) {
53+
Binding binding = ((BindingProvider) port).getBinding();
54+
List<Handler> handlerList = binding.getHandlerChain();
55+
handlerList.add(handler);
56+
binding.setHandlerChain(handlerList);
57+
}
58+
4259
public static WebStateException getWebStateException(Exception e) {
4360
return (e instanceof WebStateException) ?
4461
(WebStateException) e : new WebStateException(ExceptionType.NETWORK, e);
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package ru.javaops.web.handler;
2+
3+
import com.sun.xml.ws.api.handler.MessageHandler;
4+
import com.sun.xml.ws.api.handler.MessageHandlerContext;
5+
6+
import javax.xml.namespace.QName;
7+
import javax.xml.ws.handler.MessageContext;
8+
import java.util.Set;
9+
10+
public abstract class SoapBaseHandler implements MessageHandler<MessageHandlerContext> {
11+
12+
public Set<QName> getHeaders() {
13+
return null;
14+
}
15+
16+
@Override
17+
public void close(MessageContext context) {
18+
}
19+
20+
protected static boolean isOutbound(MessageHandlerContext context) {
21+
return (Boolean) context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
22+
}
23+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package ru.javaops.web.handler;
2+
3+
4+
import org.slf4j.event.Level;
5+
6+
public class SoapClientLoggingHandler extends SoapLoggingHandler {
7+
public SoapClientLoggingHandler(Level loggingLevel) {
8+
super(loggingLevel);
9+
}
10+
11+
@Override
12+
protected boolean isRequest(boolean isOutbound) {
13+
return isOutbound;
14+
}
15+
}
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
package ru.javaops.web.handler;
2+
3+
4+
import com.sun.xml.txw2.output.IndentingXMLStreamWriter;
5+
import com.sun.xml.ws.api.handler.MessageHandlerContext;
6+
import com.sun.xml.ws.api.message.Message;
7+
import com.sun.xml.ws.api.streaming.XMLStreamWriterFactory;
8+
import lombok.extern.slf4j.Slf4j;
9+
import org.slf4j.event.Level;
10+
11+
import javax.xml.stream.XMLStreamWriter;
12+
import java.io.ByteArrayOutputStream;
13+
import java.nio.charset.StandardCharsets;
14+
import java.util.EnumMap;
15+
import java.util.Map;
16+
17+
/**
18+
* Refactored from:
19+
*
20+
* @see {http://weblogs.java.net/blog/ramapulavarthi/archive/2007/12/extend_your_web.html
21+
* http://fisheye5.cenqua.com/browse/jax-ws-sources/jaxws-ri/samples/efficient_handler/src/efficient_handler/common/LoggingHandler.java?r=MAIN}
22+
* <p/>
23+
* This simple LoggingHandler will log the contents of incoming
24+
* and outgoing messages. This is implemented as a MessageHandler
25+
* for better performance over SOAPHandler.
26+
*/
27+
@Slf4j
28+
public abstract class SoapLoggingHandler extends SoapBaseHandler {
29+
30+
private final Level loggingLevel;
31+
32+
protected SoapLoggingHandler(Level loggingLevel) {
33+
this.loggingLevel = loggingLevel;
34+
}
35+
36+
private static final Map<Level, HANDLER> HANDLER_MAP = new EnumMap<Level, HANDLER>(Level.class) {
37+
{
38+
put(Level.TRACE, HANDLER.DEBUG);
39+
put(Level.DEBUG, HANDLER.DEBUG);
40+
put(Level.INFO, HANDLER.INFO);
41+
put(Level.WARN, HANDLER.ERROR);
42+
put(Level.ERROR, HANDLER.ERROR);
43+
}
44+
};
45+
46+
protected enum HANDLER {
47+
NONE {
48+
@Override
49+
public void handleFault(MessageHandlerContext mhc) {
50+
}
51+
52+
@Override
53+
public void handleMessage(MessageHandlerContext mhc, boolean isRequest) {
54+
}
55+
},
56+
ERROR {
57+
private static final String REQUEST_MSG = "REQUEST_MSG";
58+
59+
public void handleFault(MessageHandlerContext context) {
60+
log.error("Fault SOAP request:\n" + getMessageText(((Message) context.get(REQUEST_MSG))));
61+
}
62+
63+
public void handleMessage(MessageHandlerContext context, boolean isRequest) {
64+
if (isRequest) {
65+
context.put(REQUEST_MSG, context.getMessage().copy());
66+
}
67+
}
68+
},
69+
INFO {
70+
public void handleFault(MessageHandlerContext context) {
71+
ERROR.handleFault(context);
72+
}
73+
74+
public void handleMessage(MessageHandlerContext context, boolean isRequest) {
75+
ERROR.handleMessage(context, isRequest);
76+
log.info((isRequest ? "SOAP request: " : "SOAP response: ") + context.getMessage().getPayloadLocalPart());
77+
}
78+
},
79+
DEBUG {
80+
public void handleFault(MessageHandlerContext context) {
81+
log.error("Fault SOAP message:\n" + getMessageText(context.getMessage().copy()));
82+
}
83+
84+
public void handleMessage(MessageHandlerContext context, boolean isRequest) {
85+
log.info((isRequest ? "SOAP request:\n" : "SOAP response:\n") + getMessageText(context.getMessage().copy()));
86+
}
87+
};
88+
89+
public abstract void handleMessage(MessageHandlerContext mhc, boolean isRequest);
90+
91+
public abstract void handleFault(MessageHandlerContext mhc);
92+
93+
protected static String getMessageText(Message msg) {
94+
try {
95+
ByteArrayOutputStream out = new ByteArrayOutputStream();
96+
XMLStreamWriter writer = XMLStreamWriterFactory.create(out, "UTF-8");
97+
IndentingXMLStreamWriter wrap = new IndentingXMLStreamWriter(writer);
98+
msg.writeTo(wrap);
99+
return out.toString(StandardCharsets.UTF_8.name());
100+
} catch (Exception e) {
101+
log.warn("Coudn't get SOAP message for logging", e);
102+
return null;
103+
}
104+
}
105+
}
106+
107+
abstract protected boolean isRequest(boolean isOutbound);
108+
109+
@Override
110+
public boolean handleMessage(MessageHandlerContext mhc) {
111+
HANDLER_MAP.get(loggingLevel).handleMessage(mhc, isRequest(isOutbound(mhc)));
112+
return true;
113+
}
114+
115+
@Override
116+
public boolean handleFault(MessageHandlerContext mhc) {
117+
HANDLER_MAP.get(loggingLevel).handleFault(mhc);
118+
return true;
119+
}
120+
}

0 commit comments

Comments
 (0)