-
Notifications
You must be signed in to change notification settings - Fork 188
/
AbstractWebServiceClient.java
198 lines (164 loc) · 6.25 KB
/
AbstractWebServiceClient.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/**
* Copyright (c) 2014 Evolveum
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.evolveum.midpoint.infra.wsutil;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
import javax.xml.ws.BindingProvider;
import javax.xml.ws.Service;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.GnuParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.apache.cxf.frontend.ClientProxy;
import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.interceptor.LoggingOutInterceptor;
import org.apache.cxf.ws.security.wss4j.WSS4JOutInterceptor;
import org.apache.wss4j.dom.WSConstants;
import org.apache.wss4j.dom.handler.WSHandlerConstants;
/**
* @author semancik
*
*/
public abstract class AbstractWebServiceClient<P,S extends Service> {
private Options options = new Options();
private CommandLine commandLine;
private boolean verbose = false;
public boolean isVerbose() {
return verbose;
}
public void setVerbose(boolean verbose) {
this.verbose = verbose;
}
protected abstract S createService() throws Exception;
protected abstract Class<P> getPortClass();
protected abstract String getDefaultUsername();
protected String getPasswordType() {
if (commandLine.hasOption('P')) {
String optionValue = commandLine.getOptionValue('P');
if ("text".equals(optionValue)) {
return WSConstants.PW_TEXT;
} else if ("digest".equals(optionValue)) {
return WSConstants.PW_DIGEST;
} else {
throw new IllegalArgumentException("Unknown password type "+optionValue);
}
} else {
return WSConstants.PW_TEXT;
}
}
protected abstract String getDefaultPassword();
protected abstract String getDefaultEndpointUrl();
protected abstract int invoke(P port);
public void main(String[] args) {
try {
init(args);
P port = createPort();
int exitCode = invoke(port);
System.exit(exitCode);
} catch (Exception e) {
e.printStackTrace();
System.exit(-1);
}
}
protected void init(String[] args) throws ParseException {
options.addOption("a", "authentication", true, "Authentication type ("+WSHandlerConstants.USERNAME_TOKEN+", none)");
options.addOption("u", "user", true, "Username");
options.addOption("p", "password", true, "Password");
options.addOption("P", "password-type", true, "Password type (text or digest)");
options.addOption("e", "endpoint", true, "Endpoint URL");
options.addOption("v", "verbose", false, "Verbose mode");
options.addOption("m", "messages", false, "Log SOAP messages");
options.addOption("h", "help", false, "Usage help");
extendOptions(options);
parseCommandLine(args);
}
private void parseCommandLine(String[] args) throws ParseException {
CommandLineParser cliParser = new GnuParser();
commandLine = cliParser.parse(options, args, true);
if (commandLine.hasOption('h')) {
printHelp();
System.exit(0);
}
if (commandLine.hasOption('v')) {
verbose = true;
}
}
protected Options getOptions() {
return options;
}
public CommandLine getCommandLine() {
return commandLine;
}
protected void extendOptions(Options options) {
// nothing here. meant to be overridden
}
protected P createPort() throws Exception {
String password = getDefaultPassword();
String username = getDefaultUsername();
String endpointUrl = getDefaultEndpointUrl();
if (commandLine.hasOption('p')) {
password = commandLine.getOptionValue('p');
}
if (commandLine.hasOption('u')) {
username = commandLine.getOptionValue('u');
}
if (commandLine.hasOption('e')) {
endpointUrl = commandLine.getOptionValue('e');
}
if (verbose) {
System.out.println("Username: "+username);
System.out.println("Password: <not shown>");
System.out.println("Endpoint URL: "+endpointUrl);
}
// uncomment this if you want to use Fiddler or any other proxy
//ProxySelector.setDefault(new MyProxySelector("127.0.0.1", 8888));
P modelPort = createService().getPort(getPortClass());
BindingProvider bp = (BindingProvider)modelPort;
Map<String, Object> requestContext = bp.getRequestContext();
requestContext.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, endpointUrl);
org.apache.cxf.endpoint.Client client = ClientProxy.getClient(modelPort);
org.apache.cxf.endpoint.Endpoint cxfEndpoint = client.getEndpoint();
Map<String,Object> wssProps = new HashMap<String,Object>();
if (!commandLine.hasOption('a') ||
(commandLine.hasOption('a') && WSHandlerConstants.USERNAME_TOKEN.equals(commandLine.getOptionValue('a')))) {
wssProps.put(WSHandlerConstants.ACTION, WSHandlerConstants.USERNAME_TOKEN);
wssProps.put(WSHandlerConstants.USER, username);
wssProps.put(WSHandlerConstants.PASSWORD_TYPE, getPasswordType());
wssProps.put(WSHandlerConstants.PW_CALLBACK_CLASS, ClientPasswordHandler.class.getName());
ClientPasswordHandler.setPassword(password);
WSS4JOutInterceptor wssOut = new WSS4JOutInterceptor(wssProps);
cxfEndpoint.getOutInterceptors().add(wssOut);
} else if (commandLine.hasOption('a') && "none".equals(commandLine.getOptionValue('a'))) {
// Nothing to do
} else {
throw new IllegalArgumentException("Unknown authentication mechanism '"+commandLine.getOptionValue('a')+"'");
}
if (commandLine.hasOption('m')) {
cxfEndpoint.getInInterceptors().add(new LoggingInInterceptor());
cxfEndpoint.getOutInterceptors().add(new LoggingOutInterceptor());
}
return modelPort;
}
protected void printHelp() {
final String commandLineSyntax = System.getProperty("sun.java.command");
final HelpFormatter helpFormatter = new HelpFormatter();
helpFormatter.printHelp(commandLineSyntax, options);
}
}