Basic Authorization in Java | Apache CXF - java

I would like to implement SOAP requests in Java (Client side).
I already created a SOAP project in SOAP 5.6.1 to test and everything works well.
The requests need a Basic Authorization (with username and password).
I generate the code through Apache CXF and now I need to implement the Basic Authorization to my requests.
I am not sure about a good way to implement this on my code. There is a way to SOAP 5.6.1 generate this?
This was the ImplService generated.
import java.net.MalformedURLException;
import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.WebEndpoint;
import javax.xml.ws.WebServiceClient;
import javax.xml.ws.WebServiceFeature;
import javax.xml.ws.Service;
/**
* This class was generated by Apache CXF 3.5.0
* 2022-01-06T12:58:41.736Z
* Generated source version: 3.5.0
*
*/
#WebServiceClient(name = "GuiaAcompanhamentoImplService",
wsdlLocation = "https://qualsiliamb.apambiente.pt/services/egar/GuiaAcompanhamentoWs/v2?wsdl",
targetNamespace = "http://pt.apa.guiaacompanhamento/v2")
public class GuiaAcompanhamentoImplService extends Service {
public final static URL WSDL_LOCATION;
public final static QName SERVICE = new QName("http://pt.apa.guiaacompanhamento/v2", "GuiaAcompanhamentoImplService");
public final static QName GuiaAcompanhamentoWsPort = new QName("http://pt.apa.guiaacompanhamento/v2", "GuiaAcompanhamentoWsPort");
static {
URL url = null;
try {
url = new URL("https://qualsiliamb.apambiente.pt/services/egar/GuiaAcompanhamentoWs/v2?wsdl");
} catch (MalformedURLException e) {
java.util.logging.Logger.getLogger(GuiaAcompanhamentoImplService.class.getName())
.log(java.util.logging.Level.INFO,
"Can not initialize the default wsdl from {0}", "https://qualsiliamb.apambiente.pt/services/egar/GuiaAcompanhamentoWs/v2?wsdl");
}
WSDL_LOCATION = url;
}
public GuiaAcompanhamentoImplService(URL wsdlLocation) {
super(wsdlLocation, SERVICE);
}
public GuiaAcompanhamentoImplService(URL wsdlLocation, QName serviceName) {
super(wsdlLocation, serviceName);
}
public GuiaAcompanhamentoImplService() {
super(WSDL_LOCATION, SERVICE);
}
public GuiaAcompanhamentoImplService(WebServiceFeature ... features) {
super(WSDL_LOCATION, SERVICE, features);
}
public GuiaAcompanhamentoImplService(URL wsdlLocation, WebServiceFeature ... features) {
super(wsdlLocation, SERVICE, features);
}
public GuiaAcompanhamentoImplService(URL wsdlLocation, QName serviceName, WebServiceFeature ... features) {
super(wsdlLocation, serviceName, features);
}
/**
*
* #return
* returns GuiaAcompanhamentoWs
*/
#WebEndpoint(name = "GuiaAcompanhamentoWsPort")
public GuiaAcompanhamentoWs getGuiaAcompanhamentoWsPort() {
return super.getPort(GuiaAcompanhamentoWsPort, GuiaAcompanhamentoWs.class);
}
/**
*
* #param features
* A list of {#link javax.xml.ws.WebServiceFeature} to configure on the proxy. Supported features not in the <code>features</code> parameter will have their default values.
* #return
* returns GuiaAcompanhamentoWs
*/
#WebEndpoint(name = "GuiaAcompanhamentoWsPort")
public GuiaAcompanhamentoWs getGuiaAcompanhamentoWsPort(WebServiceFeature... features) {
return super.getPort(GuiaAcompanhamentoWsPort, GuiaAcompanhamentoWs.class, features);
}
}
And this is an example to one of the implementations:
import com.evox.utils.GuiaAcompanhamentoImplService;
import com.evox.utils.GuiaAcompanhamentoWs;
import com.evox.establishmentsQueries.*;
import javax.xml.namespace.QName;
import javax.xml.ws.WebServiceFeature;
public class ConsultaEstabelecimentosExample {
static GuiaAcompanhamentoImplService service = new GuiaAcompanhamentoImplService(GuiaAcompanhamentoImplService.WSDL_LOCATION, GuiaAcompanhamentoImplService.SERVICE);
static GuiaAcompanhamentoWs guiaAcompanhamentoWs = service.getGuiaAcompanhamentoWsPort();
public static void main(String[] args) throws Exception {
final ConsultaEstabelecimentosInput consultaEstabelecimentosInput = new ConsultaEstabelecimentosInput();
final ConsultaEstabelecimentos consultaEstabelecimentos = new ConsultaEstabelecimentos();
final ConsultaEstabelecimentosOutput consultaEstabelecimentosOutput = new ConsultaEstabelecimentosOutput();
final ConsultaEstabelecimentosResponse consultaEstabelecimentosResponse = new ConsultaEstabelecimentosResponse();
consultaEstabelecimentosInput.setTokenCertificacao("xxxxx");
consultaEstabelecimentosInput.setNif("xxxxx");
consultaEstabelecimentosInput.setCodigoAPA("xxxx");
consultaEstabelecimentos.setArg0(consultaEstabelecimentosInput);
guiaAcompanhamentoWs.consultaEstabelecimentos(consultaEstabelecimentos.getArg0()).getEstabelecimentos();
}
}

I think you mean Basic Authentication, and you can use BindingProvider, something like:
BindingProvider provider = (BindingProvider) client;
Map<String,Object> rc = provider.getRequestContext();
rc.put(BindingProvider.USERNAME_PROPERTY, userName);
rc.put(BindingProvider.PASSWORD_PROPERTY, password);
You can see a complete example here: https://examples.javacodegeeks.com/enterprise-java/jws/jax-ws-bindingprovider-example/ (look at the client section).

Related

Add header to SOAP service client

I need to exchange data with a SOAP service. This service provides many WSDL endpoints so I took one of them to generate Java code (I followed this tutorial: https://www.baeldung.com/maven-wsdl-stubs). Many classes were generated plus an interface and a service. Consider this code:
CodesService Interface:
package my.package.soapconsumer.models.service.getCodes;
import org.springframework.web.bind.annotation.RequestHeader;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import javax.xml.bind.annotation.XmlSeeAlso;
import javax.xml.ws.RequestWrapper;
import javax.xml.ws.ResponseWrapper;
/**
* This class was generated by the JAX-WS RI.
* JAX-WS RI 2.3.2
* Generated source version: 2.2
*
*/
#WebService(name = "CodesService", targetNamespace = "https://thewsdlprovider.xyz/")
#XmlSeeAlso({
ObjectFactory.class
})
public interface CodesService {
/**
*
* #param requestMethodOne
* #return
* returns my.package.soapconsumer.models.service.getCodes.ResponseMethodOne
*/
#WebMethod
#WebResult(name = "ResponseMethodOne", targetNamespace = "")
#RequestWrapper(localName = "methodOne", targetNamespace = "https://thewsdlprovider.xyz/", className = "my.package.soapconsumer.models.service.getCodes.MethodOne")
#ResponseWrapper(localName = "methodOneResponse", targetNamespace = "https://thewsdlprovider.xyz/", className = "my.package.soapconsumer.models.service.getCodes.MethodOneResponse")
public ResponseMethodOne methodOne(
#WebParam(name = "RequestMethodOne", targetNamespace = "")
RequestMethodOne requestMethodOne);
/**
*
* #return
* returns my.package.soapconsumer.models.service.getCodes.ResponseComunicacion
*/
#WebMethod
#WebResult(name = "ResponseTwo", targetNamespace = "")
#RequestWrapper(localName = "methodTwo", targetNamespace = "https://thewsdlprovider.xyz/", className = "my.package.soapconsumer.models.service.getCodes.VerificarComunicacion")
#ResponseWrapper(localName = "methodTwoResponse", targetNamespace = "https://thewsdlprovider.xyz/", className = "my.package.soapconsumer.models.service.getCodes.VerificarComunicacionResponse")
public ResponseComunicacion methodTwo();
/**
*
* #param requestMethodThreeApp
* #return
* returns my.package.soapconsumer.models.service.getCodes.ResponseMethodThree
*/
#WebMethod
#WebResult(name = "ResponseMethodThree", targetNamespace = "")
#RequestWrapper(localName = "methodThree", targetNamespace = "https://thewsdlprovider.xyz/", className = "my.package.soapconsumer.models.service.getCodes.MethodThree")
#ResponseWrapper(localName = "methodThreeResponse", targetNamespace = "https://thewsdlprovider.xyz/", className = "my.package.soapconsumer.models.service.getCodes.MethodThreeResponse")
public ResponseMethodThree methodThree(
#WebParam(name = "RequestMethodThreeApp", targetNamespace = "")
RequestMethodThreeApp requestMethodThreeApp);
/**
*
* #param requestMethodFour
* #return
* returns my.package.soapconsumer.models.service.getCodes.ResponseMethodFour
*/
#WebMethod
#WebResult(name = "ResponseMethodFour", targetNamespace = "")
#RequestWrapper(localName = "methodFour", targetNamespace = "https://thewsdlprovider.xyz/", className = "my.package.soapconsumer.models.service.getCodes.MethodFour")
#ResponseWrapper(localName = "methodFourResponse", targetNamespace = "https://thewsdlprovider.xyz/", className = "my.package.soapconsumer.models.service.getCodes.MethodFourResponse")
public ResponseMethodFour methodFour(
#WebParam(name = "RequestMethodFour", targetNamespace = "")
RequestMethodFour requestMethodFour);
/**
*
* more methods
*
*/
}
CodesService_Service Service
package my.package.soapconsumer.models.service.getCodes;
import org.springframework.web.bind.annotation.RequestHeader;
import java.net.MalformedURLException;
import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import javax.xml.ws.WebEndpoint;
import javax.xml.ws.WebServiceClient;
import javax.xml.ws.WebServiceException;
import javax.xml.ws.WebServiceFeature;
/**
* This class was generated by the JAX-WS RI.
* JAX-WS RI 2.3.2
* Generated source version: 2.2
*
*/
#WebServiceClient(name = "CodesService", targetNamespace = "https://thewsdlprovider.xyz/Codes", wsdlLocation = "https://codes.thewsdlprovider.xyz/v2/Codes?wsdl")
public class CodesService_Service
extends Service
{
private final static URL CODES_SERVICE_WSDL_LOCATION;
private final static WebServiceException CODES_SERVICE_EXCEPTION;
private final static QName CODES_SERVICE_QNAME = new QName("https://thewsdlprovider.xyz/Codes", "CodesService");
static {
URL url = null;
WebServiceException e = null;
try {
url = new URL("https://codes.thewsdlprovider.xyz/v2/Codes?wsdl");
} catch (MalformedURLException ex) {
e = new WebServiceException(ex);
}
CODES_SERVICE_WSDL_LOCATION = url;
CODES_SERVICE_EXCEPTION = e;
}
public CodesService_Service() {
super(__getWsdlLocation(), CODES_SERVICE_QNAME);
}
public CodesService_Service(WebServiceFeature... features) {
super(__getWsdlLocation(), CODES_SERVICE_QNAME, features);
}
public CodesService_Service(URL wsdlLocation) {
super(wsdlLocation, CODES_SERVICE_QNAME);
}
public CodesService_Service(URL wsdlLocation, WebServiceFeature... features) {
super(wsdlLocation, CODES_SERVICE_QNAME, features);
}
public CodesService_Service(URL wsdlLocation, QName serviceName) {
super(wsdlLocation, serviceName);
}
public CodesService_Service(URL wsdlLocation, QName serviceName, WebServiceFeature... features) {
super(wsdlLocation, serviceName, features);
}
/**
*
* #return
* returns CodesService
*/
#WebEndpoint(name = "CodesServicePort")
public CodesService getCodesServicePort() {
return super.getPort(new QName("https://thewsdlprovider.xyz/Codes", "CodesServicePort"), CodesService.class);
}
/**
*
* #param features
* A list of {#link javax.xml.ws.WebServiceFeature} to configure on the proxy. Supported features not in the <code>features</code> parameter will have their default values.
* #return
* returns CodesService
*/
#WebEndpoint(name = "CodesServicePort")
public CodesService getCodesServicePort(WebServiceFeature... features) {
return super.getPort(new QName("https://thewsdlprovider.xyz/Codes", "CodesServicePort"), CodesService.class, features);
}
private static URL __getWsdlLocation() {
if (CODES_SERVICE_EXCEPTION!= null) {
throw CODES_SERVICE_EXCEPTION;
}
return CODES_SERVICE_WSDL_LOCATION;
}
public String getWsdlLocation(){
return CODES_SERVICE_WSDL_LOCATION.toString();
}
}
I need to use the methods or functions provided by the interface so to testing them I'm using a REST controller:
#RestController
#RequestMapping("/codes")
public class CodigosRestController {
#GetMapping
public String obtainCode(){
CodesService_Service service = new CodesService_Service();
String code = service.getCodesServicePort().methodTwo().toString();
return code;
}
}
But when I run it to query the REST endpoint I get the following error:
{
"timestamp": "2022-02-18T13:44:03.234+00:00",
"status": 500,
"error": "Internal Server Error",
"trace": "com.sun.xml.internal.ws.fault.ServerSOAPFaultException: Client received SOAP Fault from server: THE SERVICE REQUIRES API KEY Please see the server log to find more detail regarding exact cause of the failure.\n\tat com.sun.xml.internal.ws.fault.SOAP11Fault.getProtocolException(SOAP11Fault.java:178)\n\tat com.sun.xml.internal.ws.fault.SOAPFaultBuilder.createException(SOAPFaultBuilder.java:116)\n\tat com.sun.xml.internal.ws.client.sei.StubHandler.readResponse(StubHandler.java:238)\n\tat ...
org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1732)\n\tat org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)\n\tat org.apache.tomcat.util.threads.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1191)\n\tat org.apache.tomcat.util.threads.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:659)\n\tat org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)\n\tat java.lang.Thread.run(Thread.java:748)\n",
"message": "Client received SOAP Fault from server: THE SERVICE REQUIRES API KEY Please see the server log to find more detail regarding exact cause of the failure.",
"path": "/codes"
}
It is necessary to send an API KEY in the headers but I don't know how.
So I used SoapUI to test the SOAP endpoint adding this header and I received a correct answer:
What I tried:
Use the WebServiceFeature... features constructor parameter but I
it seems that a header can't be added to this object. In fact, I
don't know what those three points mean.
Use the #RequestHeader but it reports that is not applicable to
web methods
So how can I add a header to my interface or service? Thanks in advance.
To work with the raw request you either need to create a request interceptor or just set the headers on the requested context on the service port. So in your case, it should be as simple as
#RestController
#RequestMapping("/codes")
public class CodigosRestController {
#GetMapping
public String obtainCode(){
CodesService_Service service = new CodesService_Service();
CodesService port = service.getCodesServicePort();
// generate the headers
Map<String, List<String>> requestHeaders = new HashMap<>();
requestHeaders.put("apiKey",Arrays.asList("TokeApi yourtokenhere"));
BindingProvider bindingProvider = (BindingProvider) port;
// set the headers on the request context
bindingProvider.getRequestContext().put(MessageContext.HTTP_REQUEST_HEADERS, requestHeaders);
String code = port.methodTwo().toString();
return code;
}
}

Is there a way to automatically generate the java test code using swagger codegen?

I've been playing with swagger codegen for a springboot project. I am now able to automatically generate the client code in java, ignore the generation of the data models (importing my own in the swagger-codegen-maven-plugin via importMapping).
Furthermore, I am also able to:
specify tags directly in the springboot code to help organize the openapi contract,
use the #Operation annotation to specify method names,
use the #ApiResponses annotation to detail the expected response codes and descriptions,etc.
Example below:
#RestController
#RequestMapping(value="/api/external/")
#Tag(name = "addResources", description = "The Add Resources API")
public class ControllerAddResources {
private final ServiceInterAddResources externalSources;
public ControllerAddResources(
ServiceInterAddResources externalSources){
this.externalSources = externalSources;
}
#Operation(operationId="importDataV3", summary = "Import Data V3", description = "Import a Data V3 file from source", tags = { "addResources" })
#PostMapping(path="/{source}/datav3", consumes = {MediaType.MULTIPART_FORM_DATA_VALUE})
public #ResponseBody String importDataV3(
#PathVariable String source,
MultipartFile file) throws ExceptionInvalidDataV3, IOException{
return externalSources.importDataV3(source, file);
}
All of this is built into the openapi contract which is then used by codegen to generate the client library (in this case, I'm using resttemplate), such as the one below:
#javax.annotation.Generated(value = "io.swagger.codegen.v3.generators.java.JavaClientCodegen", date = "2020-03-05T12:15:11.537Z[Europe/Lisbon]")#Component("com.xxx.connector.api.AddResourcesApi")
public class AddResourcesApi {
private ApiClient apiClient;
public AddResourcesApi() {
this(new ApiClient());
}
#Autowired
public AddResourcesApi(ApiClient apiClient) {
this.apiClient = apiClient;
}
public ApiClient getApiClient() {
return apiClient;
}
public void setApiClient(ApiClient apiClient) {
this.apiClient = apiClient;
}
/**
* Import Data V3
* Import a Data V3 file from source
* <p><b>412</b> - default response
* <p><b>409</b> - default response
* <p><b>200</b> - default response
* #param source The source parameter
* #param file The file parameter
* #return String
* #throws RestClientException if an error occurs while attempting to invoke the API
*/
public String importDataV3(String source, File file) throws RestClientException {
Object postBody = null;
// verify the required parameter 'source' is set
if (source == null) {
throw new HttpClientErrorException(HttpStatus.BAD_REQUEST, "Missing the required parameter 'source' when calling importDataV3");
}
// create path and map variables
final Map<String, Object> uriVariables = new HashMap<String, Object>();
uriVariables.put("source", source);
String path = UriComponentsBuilder.fromPath("/api/external/{source}/datav3").buildAndExpand(uriVariables).toUriString();
final MultiValueMap<String, String> queryParams = new LinkedMultiValueMap<String, String>();
final HttpHeaders headerParams = new HttpHeaders();
final MultiValueMap<String, Object> formParams = new LinkedMultiValueMap<String, Object>();
if (file != null)
formParams.add("file", new FileSystemResource(file));
final String[] accepts = {
"*/*", "application/json"
};
final List<MediaType> accept = apiClient.selectHeaderAccept(accepts);
final String[] contentTypes = {
"multipart/form-data"
};
final MediaType contentType = apiClient.selectHeaderContentType(contentTypes);
String[] authNames = new String[] { };
ParameterizedTypeReference<String> returnType = new ParameterizedTypeReference<String>() {};
return apiClient.invokeAPI(path, HttpMethod.POST, queryParams, postBody, headerParams, formParams, accept, contentType, authNames, returnType);
}
In the generated sources, codegen also includes scaffolds for junit tests, like the one below:
/**
* API tests for AddResourcesApi
*/
#Ignore
public class AddResourcesApiTest {
private final AddResourcesApi api = new AddResourcesApi();
/**
* Import Data V2
*
* Import an Data V2 file from source
*
* #throws ApiException
* if the Api call fails
*/
#Test
public void imporDataV2Test() {
String source = null;
File file = null;
String response = api.importDataV2(source, file);
// TODO: test validations
}
/**
* Import Data V3
*
* Import an Data V3 file from source [%source%]
*
* #throws ApiException
* if the Api call fails
*/
#Test
public void importDataV3Test() {
String source = null;
File file = null;
String response = api.importDataV3(source, file);
// TODO: test validations
}
}
However, the validation code is empty, as expected. Since the client is being continuously generated in a CI/CD environment and being deployed to a dependency management system that I'm running internally (Artifactory), this defeats the purpose of manually writing the tests each time I execute codegen.
Is there a way of specifying the validation code (or validation requisites) directly using java annotations (or a templating mechanism) at the springboot project level? This would allow for fully automated client library generation with testing included.

CXF Client - unexpected XML tag. expected: {http://schemas.xmlsoap.org/soap/envelope/}Envelope but found: {null}html

I am trying to call webservice with Apache CXF.
I generated java classes from wsdl file located here.
Here is my main method:
public static void main(String[] args) throws Exception {
ObslugaKomunikatowService service = new ObslugaKomunikatowService();
ObslugaKomunikatow client = service.getObslugaKomunikatowPort();
client.zapiszKomunikatZB(new KomunikatZBMT());
}
This code crashes with this exception:
Exception in thread "main" com.sun.xml.internal.ws.protocol.soap.MessageCreationException: Couldn't create SOAP message due to exception: unexpected XML tag. expected: {http://schemas.xmlsoap.org/soap/envelope/}Envelope but found: {null}html
at com.sun.xml.internal.ws.encoding.SOAPBindingCodec.decode(SOAPBindingCodec.java:304)
at com.sun.xml.internal.ws.transport.http.client.HttpTransportPipe.createResponsePacket(HttpTransportPipe.java:268)
at com.sun.xml.internal.ws.transport.http.client.HttpTransportPipe.process(HttpTransportPipe.java:217)
at com.sun.xml.internal.ws.transport.http.client.HttpTransportPipe.processRequest(HttpTransportPipe.java:130)
at com.sun.xml.internal.ws.transport.DeferredTransportPipe.processRequest(DeferredTransportPipe.java:95)
at com.sun.xml.internal.ws.api.pipe.Fiber.__doRun(Fiber.java:1121)
at com.sun.xml.internal.ws.api.pipe.Fiber._doRun(Fiber.java:1035)
at com.sun.xml.internal.ws.api.pipe.Fiber.doRun(Fiber.java:1004)
at com.sun.xml.internal.ws.api.pipe.Fiber.runSync(Fiber.java:862)
at com.sun.xml.internal.ws.client.Stub.process(Stub.java:448)
at com.sun.xml.internal.ws.client.sei.SEIStub.doProcess(SEIStub.java:178)
at com.sun.xml.internal.ws.client.sei.SyncMethodHandler.invoke(SyncMethodHandler.java:93)
at com.sun.xml.internal.ws.client.sei.SyncMethodHandler.invoke(SyncMethodHandler.java:77)
at com.sun.xml.internal.ws.client.sei.SEIStub.invoke(SEIStub.java:147)
at com.sun.proxy.$Proxy34.zapiszKomunikatZB(Unknown Source)
at pl.opencare.zsmoplclient.ZSMOPLClient.main(ZSMOPLClient.java:98)
Caused by: com.sun.xml.internal.ws.streaming.XMLStreamReaderException: unexpected XML tag. expected: {http://schemas.xmlsoap.org/soap/envelope/}Envelope but found: {null}html
at com.sun.xml.internal.ws.streaming.XMLStreamReaderUtil.verifyTag(XMLStreamReaderUtil.java:261)
at com.sun.xml.internal.ws.encoding.StreamSOAPCodec.decode(StreamSOAPCodec.java:205)
at com.oracle.webservices.internal.impl.encoding.StreamDecoderImpl.decode(StreamDecoderImpl.java:49)
at com.sun.xml.internal.ws.encoding.StreamSOAPCodec.decode(StreamSOAPCodec.java:234)
at com.sun.xml.internal.ws.encoding.StreamSOAPCodec.decode(StreamSOAPCodec.java:151)
at com.sun.xml.internal.ws.encoding.SOAPBindingCodec.decode(SOAPBindingCodec.java:299)
... 15 more
There is generated service class:
package pl.gov.csioz.zsmopl.ws;
import java.net.MalformedURLException;
import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.WebEndpoint;
import javax.xml.ws.WebServiceClient;
import javax.xml.ws.WebServiceFeature;
import javax.xml.ws.Service;
/**
* This class was generated by Apache CXF 3.2.5
* 2018-07-11T11:38:29.954+02:00
* Generated source version: 3.2.5
*
*/
#WebServiceClient(name = "ObslugaKomunikatowService",
wsdlLocation = "file:ZSMOPL.wsdl",
targetNamespace = "http://csioz.gov.pl/zsmopl/ws/")
public class ObslugaKomunikatowService extends Service {
public final static URL WSDL_LOCATION;
public final static QName SERVICE = new QName("http://csioz.gov.pl/zsmopl/ws/", "ObslugaKomunikatowService");
public final static QName ObslugaKomunikatowPort = new QName("http://csioz.gov.pl/zsmopl/ws/", "ObslugaKomunikatowPort");
static {
URL url = null;
try {
url = new URL("file:ZSMOPL.wsdl");
} catch (MalformedURLException e) {
java.util.logging.Logger.getLogger(ObslugaKomunikatowService.class.getName())
.log(java.util.logging.Level.INFO,
"Can not initialize the default wsdl from {0}", "file:ZSMOPL.wsdl");
}
WSDL_LOCATION = url;
}
public ObslugaKomunikatowService(URL wsdlLocation) {
super(wsdlLocation, SERVICE);
}
public ObslugaKomunikatowService(URL wsdlLocation, QName serviceName) {
super(wsdlLocation, serviceName);
}
public ObslugaKomunikatowService() {
super(WSDL_LOCATION, SERVICE);
}
public ObslugaKomunikatowService(WebServiceFeature ... features) {
super(WSDL_LOCATION, SERVICE, features);
}
public ObslugaKomunikatowService(URL wsdlLocation, WebServiceFeature ... features) {
super(wsdlLocation, SERVICE, features);
}
public ObslugaKomunikatowService(URL wsdlLocation, QName serviceName, WebServiceFeature ... features) {
super(wsdlLocation, serviceName, features);
}
/**
*
* #return
* returns ObslugaKomunikatow
*/
#WebEndpoint(name = "ObslugaKomunikatowPort")
public ObslugaKomunikatow getObslugaKomunikatowPort() {
return super.getPort(ObslugaKomunikatowPort, ObslugaKomunikatow.class);
}
/**
*
* #param features
* A list of {#link javax.xml.ws.WebServiceFeature} to configure on the proxy. Supported features not in the <code>features</code> parameter will have their default values.
* #return
* returns ObslugaKomunikatow
*/
#WebEndpoint(name = "ObslugaKomunikatowPort")
public ObslugaKomunikatow getObslugaKomunikatowPort(WebServiceFeature... features) {
return super.getPort(ObslugaKomunikatowPort, ObslugaKomunikatow.class, features);
}
}
There is generted port class:
package pl.gov.csioz.zsmopl.ws;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import javax.xml.bind.annotation.XmlSeeAlso;
import javax.xml.ws.RequestWrapper;
import javax.xml.ws.ResponseWrapper;
/**
* This class was generated by Apache CXF 3.2.5
* 2018-07-11T11:38:29.942+02:00
* Generated source version: 3.2.5
*
*/
#WebService(targetNamespace = "http://csioz.gov.pl/zsmopl/ws/", name = "ObslugaKomunikatow")
#XmlSeeAlso({pl.gov.csioz.zsmopl.ws.obslugakomunikatow.ObjectFactory.class, pl.gov.csioz.zsmopl.mt.ObjectFactory.class})
public interface ObslugaKomunikatow {
#WebMethod(action = "zapiszKomunikatZB")
#RequestWrapper(localName = "zapiszKomunikatZB", targetNamespace = "http://csioz.gov.pl/zsmopl/ws/obslugakomunikatow/", className = "pl.gov.csioz.zsmopl.ws.obslugakomunikatow.ZapiszKomunikatZB")
#ResponseWrapper(localName = "zapiszKomunikatZBResponse", targetNamespace = "http://csioz.gov.pl/zsmopl/ws/obslugakomunikatow/", className = "pl.gov.csioz.zsmopl.ws.obslugakomunikatow.ZapiszKomunikatZBResponse")
#WebResult(name = "identyfikatorKomunikatu", targetNamespace = "")
public pl.gov.csioz.zsmopl.mt.IdentyfikatorKomunikatuMT zapiszKomunikatZB(
#WebParam(name = "komunikatZB", targetNamespace = "")
pl.gov.csioz.zsmopl.mt.KomunikatZBMT komunikatZB
) throws BladTworzeniaIdentyfikatoraKomunikatu, BladDostepnosci;
#WebMethod(action = "zapiszKomunikatOS")
#RequestWrapper(localName = "zapiszKomunikatOS", targetNamespace = "http://csioz.gov.pl/zsmopl/ws/obslugakomunikatow/", className = "pl.gov.csioz.zsmopl.ws.obslugakomunikatow.ZapiszKomunikatOS")
#ResponseWrapper(localName = "zapiszKomunikatOSResponse", targetNamespace = "http://csioz.gov.pl/zsmopl/ws/obslugakomunikatow/", className = "pl.gov.csioz.zsmopl.ws.obslugakomunikatow.ZapiszKomunikatOSResponse")
#WebResult(name = "identyfikatorKomunikatu", targetNamespace = "")
public pl.gov.csioz.zsmopl.mt.IdentyfikatorKomunikatuMT zapiszKomunikatOS(
#WebParam(name = "komunikatOS", targetNamespace = "")
pl.gov.csioz.zsmopl.mt.KomunikatOSMT komunikatOS
) throws BladTworzeniaIdentyfikatoraKomunikatu, BladDostepnosci;
#WebMethod(action = "zapiszKomunikatPD")
#RequestWrapper(localName = "zapiszKomunikatPD", targetNamespace = "http://csioz.gov.pl/zsmopl/ws/obslugakomunikatow/", className = "pl.gov.csioz.zsmopl.ws.obslugakomunikatow.ZapiszKomunikatPD")
#ResponseWrapper(localName = "zapiszKomunikatPDResponse", targetNamespace = "http://csioz.gov.pl/zsmopl/ws/obslugakomunikatow/", className = "pl.gov.csioz.zsmopl.ws.obslugakomunikatow.ZapiszKomunikatPDResponse")
#WebResult(name = "identyfikatorKomunikatu", targetNamespace = "")
public pl.gov.csioz.zsmopl.mt.IdentyfikatorKomunikatuMT zapiszKomunikatPD(
#WebParam(name = "komunikatPD", targetNamespace = "")
pl.gov.csioz.zsmopl.mt.KomunikatPDMT komunikatPD
) throws BladTworzeniaIdentyfikatoraKomunikatu, BladDostepnosci;
}
And generated class that I am trying to send:
package pl.gov.csioz.zsmopl.mt;
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlType;
/**
* Komunikat zg�oszenia brak�w.
*
*
* <p>Java class for KomunikatZBMT complex type.
*
* <p>The following schema fragment specifies the expected content contained within this class.
*
* <pre>
* <complexType name="KomunikatZBMT">
* <complexContent>
* <extension base="{http://csioz.gov.pl/zsmopl/mt/}KomunikatMT">
* <sequence>
* <element name="idMPDPodmiotuRaportujacego" type="{http://csioz.gov.pl/zsmopl/mt/}IdentyfikatorMPDPodmiotuMT"/>
* <element name="komunikatTransakcja" type="{http://csioz.gov.pl/zsmopl/mt/}KomunikatTransakcjaZBMT" maxOccurs="unbounded"/>
* </sequence>
* </extension>
* </complexContent>
* </complexType>
* </pre>
*
*
*/
#XmlAccessorType(XmlAccessType.FIELD)
#XmlType(name = "KomunikatZBMT", propOrder = {
"idMPDPodmiotuRaportujacego",
"komunikatTransakcja"
})
public class KomunikatZBMT
extends KomunikatMT
{
#XmlElement(required = true)
protected IdentyfikatorMPDPodmiotuMT idMPDPodmiotuRaportujacego;
#XmlElement(required = true)
protected List<KomunikatTransakcjaZBMT> komunikatTransakcja;
/**
* Gets the value of the idMPDPodmiotuRaportujacego property.
*
* #return
* possible object is
* {#link IdentyfikatorMPDPodmiotuMT }
*
*/
public IdentyfikatorMPDPodmiotuMT getIdMPDPodmiotuRaportujacego() {
return idMPDPodmiotuRaportujacego;
}
/**
* Sets the value of the idMPDPodmiotuRaportujacego property.
*
* #param value
* allowed object is
* {#link IdentyfikatorMPDPodmiotuMT }
*
*/
public void setIdMPDPodmiotuRaportujacego(IdentyfikatorMPDPodmiotuMT value) {
this.idMPDPodmiotuRaportujacego = value;
}
/**
* Gets the value of the komunikatTransakcja property.
*
* <p>
* This accessor method returns a reference to the live list,
* not a snapshot. Therefore any modification you make to the
* returned list will be present inside the JAXB object.
* This is why there is not a <CODE>set</CODE> method for the komunikatTransakcja property.
*
* <p>
* For example, to add a new item, do as follows:
* <pre>
* getKomunikatTransakcja().add(newItem);
* </pre>
*
*
* <p>
* Objects of the following type(s) are allowed in the list
* {#link KomunikatTransakcjaZBMT }
*
*
*/
public List<KomunikatTransakcjaZBMT> getKomunikatTransakcja() {
if (komunikatTransakcja == null) {
komunikatTransakcja = new ArrayList<KomunikatTransakcjaZBMT>();
}
return this.komunikatTransakcja;
}
}
Do you know what's causing this exception?
Maybe this helps others:
I also got the error message
Exception in thread "main" com.sun.xml.internal.ws.protocol.soap.MessageCreationException: Couldn't create SOAP message due to exception: unexpected XML tag. expected: {http://schemas.xmlsoap.org/soap/envelope/}Envelope but found: {null}html
Or in german:
Unerwartetes Tag. Erwartet: {http://schemas.xmlsoap.org/soap/envelope/}Envelope, gefunden: {null}html
I didn't change anything and the request was answered before without any problems until the error occured.
Finally the reason was, that the server was not available and returned an internal server error (500) response and a HTML error message. If you want to see the HTTP errors you can turn HTTP logging on by setting this system properties in your code:
System.setProperty("com.sun.xml.ws.transport.http.client.HttpTransportPipe.dump", "true")
System.setProperty("com.sun.xml.ws.transport.http.HttpAdapter.dump", "true")
System.setProperty("com.sun.xml.internal.ws.transport.http.client.HttpTransportPipe.dump", "true")
System.setProperty("com.sun.xml.internal.ws.transport.http.HttpAdapter.dump", "true")
System.setProperty("com.sun.xml.internal.ws.transport.http.HttpAdapter.dumpTreshold", "999999")

How to use Holder<Payload> holder in SOAP web client

I am trying to figure out how to use a webservice which was generated from a wsdl file. In this project I am going send request by using the generated webservice client. The interface is shown below:
public interface IConnectService {
public void processMessage(
#WebParam(name = "payload", mode =
WebParam.Mode.INOUT)
Holder<Payload> payload);
}
The client is like this:
public class ConnectService
extends Service
{
private final static URL CONNECTSERVICE_WSDL_LOCATION;
private final static WebServiceException CONNECTSERVICE_EXCEPTION;
private final static QName CONNECTSERVICE_QNAME = new
QName("http://xxxxxx-asi.com/services", "ConnectService");
static {
URL url = null;
WebServiceException e = null;
try {
url = new URL("http://localhost/wsdl/xxxxx");
} catch (MalformedURLException ex) {
e = new WebServiceException(ex);
}
CONNECTSERVICE_WSDL_LOCATION = url;
CONNECTSERVICE_EXCEPTION = e;
}
public ConnectService() {
super(__getWsdlLocation(), CONNECTSERVICE_QNAME);
}
public ConnectService(WebServiceFeature... features) {
super(__getWsdlLocation(), CONNECTSERVICE_QNAME, features);
}
public ConnectService(URL wsdlLocation) {
super(wsdlLocation, CONNECTSERVICE_QNAME);
}
public ConnectService(URL wsdlLocation, WebServiceFeature...
features) {
super(wsdlLocation, CONNECTSERVICE_QNAME, features);
}
public ConnectService(URL wsdlLocation, QName serviceName) {
super(wsdlLocation, serviceName);
}
public ConnectService(URL wsdlLocation, QName serviceName,
WebServiceFeature... features) {
super(wsdlLocation, serviceName, features);
}
#WebEndpoint(name = "BasicHttpBinding_IConnectService")
public IConnectService getBasicHttpBindingIConnectService() {
return super.getPort(new QName("http://xxxxxxx-
asi.com/services", "BasicHttpBinding_IConnectService"),
IConnectService.class);
}
#WebEndpoint(name = "BasicHttpBinding_IConnectService")
public IConnectService
getBasicHttpBindingIConnectService(WebServiceFeature... features) {
return super.getPort(new QName("http://xxxxxx-asi.com/services",
"BasicHttpBinding_IConnectService"), IConnectService.class,
features);
}
private static URL __getWsdlLocation() {
if (CONNECTSERVICE_EXCEPTION!= null) {
throw CONNECTSERVICE_EXCEPTION;
}
return CONNECTSERVICE_WSDL_LOCATION;
}
}
The soap request message structure is going to be like in the picture: enter image description here
So my question is how can I use the client to make call which includes my soapmessage using the interface method? To my understand, the Holder object can only take Payload object, which is an childelement of ProcessMessege (as shown in the structure), and ProcessMessage is a childelement of SOAP body. I need to put the security credentials in the SOAP header and I already did that. So right now if I use the webservice method, I only can pass the payload object, but the web server will not accept the request because no credentials inside of the payload part. Anybody can help out for this problem? I really appreciate your help!
I solved this problem by using soapmessage handler and handler resolver. The handler can insert the credentials in the soap header and modifier the soap body to satisfy all my requirements for the soap message.

Connection to web service through proxy

I've created java code from wsdl file using wsimport.
I want to connect to a web service using proxy, i got this code (generated):
package siri.siriservices;
import java.net.MalformedURLException;
import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import javax.xml.ws.WebEndpoint;
import javax.xml.ws.WebServiceClient;
import javax.xml.ws.WebServiceException;
import javax.xml.ws.WebServiceFeature;
/**
* This class was generated by the JAX-WS RI.
* JAX-WS RI 2.2.4-b01
* Generated source version: 2.2
*
*/
#WebServiceClient(name = "SiriServices", targetNamespace = "http://someip/Siri/SiriServices/", wsdlLocation = "file:/C:/Users/Pavel/git/stop-scanner/StopScanner/SIRI%20-%20ISRAEL/siri_wsProducer%20-%20Israel.wsdl")
public class SiriServices
extends Service
{
private final static URL SIRISERVICES_WSDL_LOCATION;
private final static WebServiceException SIRISERVICES_EXCEPTION;
private final static QName SIRISERVICES_QNAME = new QName("http://someip/Siri/SiriServices/", "SiriServices");
static {
URL url = null;
WebServiceException e = null;
try {
url = new URL("file:/C:/Users/Pavel/git/stop-scanner/StopScanner/SIRI%20-%20ISRAEL/siri_wsProducer%20-%20Israel.wsdl");
} catch (MalformedURLException ex) {
e = new WebServiceException(ex);
}
SIRISERVICES_WSDL_LOCATION = url;
SIRISERVICES_EXCEPTION = e;
}
public SiriServices() {
super(__getWsdlLocation(), SIRISERVICES_QNAME);
}
public SiriServices(WebServiceFeature... features) {
super(__getWsdlLocation(), SIRISERVICES_QNAME, features);
}
public SiriServices(URL wsdlLocation) {
super(wsdlLocation, SIRISERVICES_QNAME);
}
public SiriServices(URL wsdlLocation, WebServiceFeature... features) {
super(wsdlLocation, SIRISERVICES_QNAME, features);
}
public SiriServices(URL wsdlLocation, QName serviceName) {
super(wsdlLocation, serviceName);
}
public SiriServices(URL wsdlLocation, QName serviceName, WebServiceFeature... features) {
super(wsdlLocation, serviceName, features);
}
/**
*
* #return
* returns SOAPPort
*/
#WebEndpoint(name = "SiriWSPort")
public SOAPPort getSiriWSPort() {
return super.getPort(new QName("http://someip/Siri/SiriServices/", "SiriWSPort"), SOAPPort.class);
}
/**
*
* #param features
* A list of {#link javax.xml.ws.WebServiceFeature} to configure on the proxy. Supported features not in the <code>features</code> parameter will have their default values.
* #return
* returns SOAPPort
*/
#WebEndpoint(name = "SiriWSPort")
public SOAPPort getSiriWSPort(WebServiceFeature... features) {
return super.getPort(new QName("http://someip/Siri/SiriServices/", "SiriWSPort"), SOAPPort.class, features);
}
private static URL __getWsdlLocation() {
if (SIRISERVICES_EXCEPTION!= null) {
throw SIRISERVICES_EXCEPTION;
}
return SIRISERVICES_WSDL_LOCATION;
}
}
I have an option to configure the proxy passing WebServiceFeature to getSiriWSPort constructor as described in the comment:
/**
*
* #param features
* A list of {#link javax.xml.ws.WebServiceFeature} to configure on the proxy. Supported features not in the <code>features</code> parameter will have their default values.
* #return
* returns SOAPPort
*/
#WebEndpoint(name = "SiriWSPort")
public SOAPPort getSiriWSPort(WebServiceFeature... features) {
return super.getPort(new QName("http://someip/Siri/SiriServices/", "SiriWSPort"), SOAPPort.class, features);
}
I found some info about the configuration here, but i can't find jar containing ClientProxyFeature.
Thanks!
You should have a look at CXF.
I'm using CXF-based client with http proxy and certificate authentication and it works quite good. I don't have IDE by hand, but in your case I guess it could look something like this:
SiriServices siri = new SiriServices();
SOAPPort port = siri.getSiriWSPort();
org.apache.cxf.endpoint.Client client = ClientProxy.getClient(port);
HTTPConduit conduit = (HTTPConduit) client.getConduit();
conduit.getClient().setProxyServer("...");
conduit.getClient().setProxyServerPort(...);

Categories

Resources