I have written custom soaphandler class which extends javax.xml.rpc.GenericHandler. And my requirement is to pass a primitive variable into this handler class from my other caller java class. This variable should pass at run time and should be thread safe because multiple thread is going to access that handler same time.
I tried to store the value into HandlerConfig object and injected it to the HandlerInfo, but I couldn't found this value in my handler.
This Soap Handler concept is new for me, so please help me to fix this issue.
below I'm posting my raw code for handler class and the class from where I'm calling it.
public class MilerHandler extends GenericHandler {
private HandlerInfo info;
private static final String AUTHORIZATION = "Authorization";
private static final String DATE = "Date";
private static final String URI = "http://-----.com";
public MilerHandler() {
}
public void init(HandlerInfo info) {
this.info = info;
}
public QName[] getHeaders() {
return info.getHeaders();
}
public boolean handleRequest(MessageContext context) {
SOAPMessageContext smc = (SOAPMessageContext)context;
SOAPMessage message = smc.getMessage();
try {
SOAPEnvelope envelope = smc.getMessage().getSOAPPart().getEnvelope();
SOAPFactory factory = SOAPFactory.newInstance();
SOAPElement authorization = factory.createElement(AUTHORIZATION, PCMilerClientService.PREFIX, URI);
SOAPElement date = factory.createElement(DATE, PCMilerClientService.PREFIX, URI);
authorization.addTextNode((String)value1); //Value1 need to be pass from my business class.
date.addTextNode((int)value2); //Value2 need to be pass from my business class.
SOAPElement authHeader = factory.createElement(PCMilerClientService.AUTH_HEADER, PCMilerClientService.PREFIX, URI);
authHeader.addChildElement(authorization);
authHeader.addChildElement(date);
SOAPHeader header = envelope.getHeader();
header.addChildElement(authHeader);
message.saveChanges();
if(log.debug()) {
log.debug(message);
}
}
catch (Exception ex) {
log.error(ex);
}
return true;
}
public boolean handleResponse(javax.xml.rpc.handler.MessageContext context) {
SOAPMessageContext smc = (SOAPMessageContext)context;
SOAPMessage message = smc.getMessage();
if(log.debug()) {
log.debug(message);
}
return true;
}
public boolean handleFault(javax.xml.rpc.handler.MessageContext context) {
SOAPMessageContext smc = (SOAPMessageContext)context;
SOAPMessage message = smc.getMessage();
if(log.debug()) {
log.debug(message);
}
return true;
}
}
public class MilerDistanceLookupWorker {
public void run() {
IService_Stub stub = null;
Service_Impl impl = null;
try {
impl = new Service_Impl();
setPCMilerHandler(impl);
stub = (IService_Stub) impl.getBasicHttpBinding_IService();
} catch (ServiceException e) {
-----------------
}
}
private void setMilerHandler(Service_Impl impl) {
HandlerInfo handlerInfo = new HandlerInfo();
handlerInfo.setHandlerClass(MilerHandler.class);
QName authHeader = new QName(NAMESPACE, AUTH_HEADER, PREFIX);
List<HandlerInfo> headerList = impl.getHandlerRegistry().getHandlerChain(authHeader);
headerList.add(handlerInfo);
impl.getHandlerRegistry().setHandlerChain(authHeader, headerList);
}
}
If you are trying to implement SOAP auth handler then you need to do something like this:
public class SOAPAuthenticationHandler implements SOAPHandler<SOAPMessageContext> {
private String username;
private String password;
public SOAPAuthenticationHandler (String username, String password) {
this.username = username;
this.password = password;
}
#Override
public boolean handleMessage(SOAPMessageContext smc) {
Boolean outboundProperty = (Boolean) smc.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
if (!outboundProperty) {
return outboundProperty;
}
try {
SOAPEnvelope envelope = smc.getMessage().getSOAPPart().getEnvelope();
SOAPHeader header = envelope.addHeader();
SOAPElement security = header.addChildElement("Security", "wsse", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");
security.addNamespaceDeclaration("common", "some xmlns");
SOAPElement usernameToken = security.addChildElement("UsernameToken", "wsse");
usernameToken.addAttribute(new QName("xmlns:wsu"), "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd");
SOAPElement usernameElement = usernameToken.addChildElement("Username", "wsse");
usernameElement.addTextNode(username);
SOAPElement passwordElement = usernameToken.addChildElement("PasswordSaltedDigest", "common");
passwordElement.setAttribute("Type", "http://www.w3.org/2001/04/xmldsig-more#gostr341194");
passwordElement.addTextNode(password);
} catch (SOAPException | DOMException e) {
getLogger().error(e.getMessage());
}
return outboundProperty;
}
#Override
public boolean handleFault(SOAPMessageContext context) {
return false;
}
#Override
public void close(MessageContext context) {
}
#Override
public Set<QName> getHeaders() {
return null;
}
}
And after that you need to add this handler to port' HandlerChain:
List handlerChain = ((BindingProvider) port).getBinding().getHandlerChain();
if (handlerChain == null) {
handlerChain = new ArrayList();
}
handlerChain.add(new SOAPAuthenticationHandler(username, password));
((BindingProvider) port).getBinding().setHandlerChain(handlerChain);
Related
I want to use this webflux client code to send POST requests with reply and without reply. I tried this code implementation:
public class RestClientBuilder {
private String token;
private String username;
private String password;
private URL gatewayUrl;
private SslContextBuilder sslContextBuilder;
public static RestClientBuilder builder() {
return new RestClientBuilder();
}
public RestClientBuilder token(String token) {
this.token = validateAndTrim(token, "Token");
return this;
}
public RestClientBuilder usernamePassword(String username, String password) {
this.username = validateAndTrim(username, "Username");
this.password = validateAndTrim(password, "Password");
return this;
}
private String validateAndTrim(String value, final String parameter) {
if (value == null || value.trim().isEmpty()) {
throw new IllegalArgumentException(parameter + " is empty");
}
return value.trim();
}
public RestClientBuilder gatewayUrl(String gatewayUrl) {
String urlSt = validateAndTrim(gatewayUrl, "Gateway URL");
try {
this.gatewayUrl = new URL(urlSt);
} catch (MalformedURLException e) {
throw new IllegalArgumentException("Malformed URL: " + urlSt, e);
}
return this;
}
public RestClientBuilder truststore(File truststoreFile) {
getSslContextBuilder().trustManager(truststoreFile);
return this;
}
public RestClientBuilder sslCertificate(File keyCertChainFile, File keyFile, String keyPassword) {
getSslContextBuilder().keyManager(keyCertChainFile, keyFile, keyPassword);
return this;
}
public RestClient build() throws SSLException {
SslContext sslContext = sslContextBuilder != null ? sslContextBuilder.build() : null;
return new RestClient(gatewayUrl.toString(), token, username, password, sslContext);
}
private SslContextBuilder getSslContextBuilder() {
if (sslContextBuilder == null) {
sslContextBuilder = SslContextBuilder.forClient();
}
return sslContextBuilder;
}
}
Implementation of the rest client:
public class RestClient {
private WebClient client;
private String gatewayUrl;
public RestClient(String gatewayUrl, String token, String username, String password, SslContext sslContext) {
this.gatewayUrl = gatewayUrl;
WebClient.Builder builder = WebClient.builder().baseUrl(gatewayUrl);
if (sslContext != null) {
HttpClient httpClient = HttpClient.create().secure(sslContextSpec -> sslContextSpec.sslContext(sslContext));
ClientHttpConnector httpConnector = new ReactorClientHttpConnector(httpClient);
builder.clientConnector(httpConnector);
}
if (username != null && password != null) {
builder.filter(basicAuthentication(username, password));
}
client = builder.build();
}
public Mono<Void> executeOnly(ReportRequest transaction) {
Mono<ReportRequest> transactionMono = Mono.just(transaction);
return client.post().uri(gatewayUrl)
.accept(MediaType.APPLICATION_XML)
.contentType(MediaType.APPLICATION_XML)
.body(transactionMono, ReportRequest.class)
.retrieve()
.bodyToMono(Void.class);
}
}
Make remote calls:
public class ReportingProcessor {
private String URL2 = "......";
public void collectEnvironmentData() throws JAXBException {
ReportRequest report = new ReportRequest();
report.setVersion("1.0");
RestClient client = null;
try {
client = RestClientBuilder.builder()
.gatewayUrl(URL2)
// .token(contract.getTerminal_token())
// .usernamePassword("user", "password")
// .truststore(new File("server.pem"))
// .sslCertificate(new File("client.pem"), new File("clientKey.p8"), "secret")
.build();
} catch (SSLException e) {
e.printStackTrace();
}
Mono<Void> result = client.executeOnly(report);
Void response = result.block();
}
When I remove Void response = result.block(); the request is not send. I Can't find why. Can you give me some advice how to make the client code working without using block().
Whenever you work with Spring-webflux you have to keep one thing in mind. i.e You don't have to break your chain. because it is necessary to, someone should call subscribe on your chain. as it works on RXJava specification.
if you break the chain then you have to call block() which not recommended.
you have to modify your code in the below manner.
Let's Consider you have a handler which is making a call to your collectEnvironmentData() method and your method is making a call to remote service.
public Mono<ServerResponse> handelerMethod(ServerRequest request){
return collectEnvironmentData().flatMap(aVoid -> ServerResponse.ok().build());
}
your method should be modified to
public Mono<Void> collectEnvironmentData() throws JAXBException {
ReportRequest report = new ReportRequest();
report.setVersion("1.0");
RestClient client = null;
try {
client = RestClientBuilder.builder()
.gatewayUrl(URL2)
// .token(contract.getTerminal_token())
// .usernamePassword("user", "password")
// .truststore(new File("server.pem"))
// .sslCertificate(new File("client.pem"), new File("clientKey.p8"),
//"secret").build();
} catch (SSLException e) {
e.printStackTrace();
}
return client.executeOnly(report);
}
Change your implementation in the above manner, hope it will work.
How I would implement your method is:
public Mono<Void> executeOnly(ReportRequest transaction) {
Mono<ReportRequest> transactionMono = Mono.just(transaction);
return client.post().uri(gatewayUrl)
.accept(MediaType.APPLICATION_XML)
.contentType(MediaType.APPLICATION_XML)
.body(transaction, ReportRequest.class)
.exchange()
.then();
}
And then I would use it as follows:
client.executeOnly(report).subscribe()
Change the method return type to Mono<Void> for end to end streaming.
public void collectEnvironmentData() throws JAXBException {
ReportRequest report = new ReportRequest();
report.setVersion("1.0");
RestClient client = null;
try {
client = RestClientBuilder.builder()
.gatewayUrl(URL2)
.build();
} catch (SSLException e) {
e.printStackTrace();
}
return client.executeOnly(report);
}
Or you can also subscribe the Mono
client.executeOnly(report).subscribe();
I have a retrofit 2 response by which i gets data from server. my code is working fine.
But i want cache this response using SharedPreferences and keep it till that activity is running and after activity is destroyed. i want to delete this response from SharedPreferences.
This is my Code:
public class SampleClass {
private DataInterface mListener;
public SampleClass() {
super();
}
public void getDataForId(final String id) {
ApiInterface apiInterface = APIClient.getApiInterface();
Call<MyResponse> call = apiInterface.getResponse();
call.enqueue(new Callback<MyResponse>() {
#Override
public void onResponse(Call<MyResponse> call, Response<MyResponse> response) {
if (response!=null && response.body() != null && mListener != null) {
mListener.responseData(response.body());
}
}
#Override
public void onFailure(Call<MyResponse> call, Throwable t) {
}
});
}
public void setOnDataListener(DataInterface listener) {
mListener = listener;
}
public interface DataInterface {
void responseData( MyResponse myResponse );
}
}
SecondData class file
sampleClass.setOnDataListener(new SampleClass.DataInterface() {
#Override
public void responseData(MyResponse myResponse) {
// i wanna store this response into SharedPreferences for temp and delete after activity is destroyed.
List<Detail> details = myResponse.getDetails();
for (Detail d : details) {
if (d.getId().equals(id)) {
reqDetail = d;
name.setText(reqDetail.getName());
Picasso.with(SecondData.this)
.load(reqDetail.getName())
.placeholder(R.mipmap.ic_launcher)
.error(R.mipmap.ic_launcher)
.into(image);
}
}
}
});
ApiInterface
public interface ApiInterfaceNew {
#GET("/display.php")
Call<MyResponse> getResponse();//imp to include MyResponse as a call
}
Api class
private static final String ROOT_URL = "";
private static Retrofit retrofit1 = null;
private static final String CACHE_CONTROL = "Cache-Control";
public static Retrofit getClient() {
if (retrofit1 == null) {
retrofit1 = new Retrofit.Builder()
.baseUrl(ROOT_URL)
.client(provideOkHttpClient())
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit1;
}
public static ApiInterfaceNew getApiInterface() {
return getClient().create(ApiInterfaceNew.class);
}
private static OkHttpClient provideOkHttpClient() {
return new OkHttpClient.Builder()
.addInterceptor(provideHttpLoggingInterceptor())
.addInterceptor(provideOfflineCacheInterceptor())
.addNetworkInterceptor(provideCacheInterceptor())
.cache(provideCache())
.build();
}
private static Cache provideCache() {
Cache cache = null;
try {
cache = new Cache(new File(AppControler.getInstance().getCacheDir(), "http-cache"),
10 * 1024 * 1024); // 10 MB
} catch (Exception e) {
Timber.e("Could not create Cache!");
}
return cache;
}
private static HttpLoggingInterceptor provideHttpLoggingInterceptor() {
HttpLoggingInterceptor httpLoggingInterceptor =
new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
#Override
public void log(String message) {
Timber.e(message);
}
});
httpLoggingInterceptor.setLevel(BuildConfig.DEBUG ? HEADERS : NONE);
return httpLoggingInterceptor;
}
public static Interceptor provideCacheInterceptor() {
return new Interceptor() {
#Override
public Response intercept(Chain chain) throws IOException {
Response response = chain.proceed(chain.request());
// re-write response header to force use of cache
CacheControl cacheControl = new CacheControl.Builder()
.maxAge(2, TimeUnit.MINUTES)
.build();
return response.newBuilder()
.header(CACHE_CONTROL, cacheControl.toString())
.build();
}
};
}
public static Interceptor provideOfflineCacheInterceptor() {
return new Interceptor() {
#Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
if (!AppControler.hasNetwork()) {
CacheControl cacheControl = new CacheControl.Builder()
.maxStale(7, TimeUnit.DAYS)
.build();
request = request.newBuilder()
.cacheControl(cacheControl)
.build();
}
return chain.proceed(request);
}
};
}
AppControler class
public class AppControler extends Application {
private static AppControler instance;
#Override
public void onCreate()
{
super.onCreate();
instance = this;
if (BuildConfig.DEBUG)
{
Timber.plant(new Timber.DebugTree());
}
Timber.i("Creating our Application");
}
public static AppControler getInstance ()
{
return instance;
}
public static boolean hasNetwork ()
{
return instance.checkIfHasNetwork();
}
public boolean checkIfHasNetwork()
{
ConnectivityManager cm = (ConnectivityManager) getSystemService( Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = cm.getActiveNetworkInfo();
return networkInfo != null && networkInfo.isConnected();
}
Add below code in your Retrofit response when a response is success.
First convert JSON to string and store.
Gson gson = new Gson();
String favData = gson.toJson(response.body());
save strings to prefrance.
preferenceManager is my SharedPref class
preferenceManager.setStringPreference(Global.OFFLINE_WORD, favData);
now when you want to get pref data call below method.
public ArrayList<MyResponse> getData(String key) {
String data = getStringPreference(key);
Gson gson = new Gson();
Type type = new TypeToken<ArrayList<MyResponse>>() {
}.getType();
return gson.fromJson(data, type);
}
You can use either ObjectMapper or Gson.
For ObjectMapper you can refer the below code
public static void updateUserInfo(UserInfo userInfo, Context context) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
Crashlytics.log("updating User Info "+(userInfo!=null?userInfo.toString():"UserInfo is null"));
final SharedPreferences.Editor edit = preferences.edit();
ObjectMapper objectMapper = new ObjectMapper();
try {
String value = objectMapper.writeValueAsString(userInfo);
edit.putString("USER_INFO_MODEL", value);
edit.commit();
} catch (JsonProcessingException e) {
Exceptions.propagate(e);
}
}
you can also get the stored response from shared preferences
public static UserInfo getUserInfo(Context context) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
String userDetatails = preferences.getString(AppConstants.USER_INFO_MODEL, "");
Crashlytics.log("get UserInfo "+userDetatails);
ObjectMapper mapper = new ObjectMapper();
if (StringUtils.isEmpty(userDetatails)) {
return null;
}
UserInfo userInfo = null;
try {
userInfo = mapper.readValue(userDetatails, UserInfo.class);
} catch (IOException e) {
Exceptions.propagate(e);
}
return userInfo;
}
I am trying to consume data from local web service in an android emulator using ksoap2 library, I test the same app with a online webservice http://www.webservicex.net/New/Home/ServiceDetail/17 and It works,
the webService has been made it on Csharp, it has a method to return a column from a DB, call neptuno(spanish adventure works),when execute dont show the data and after a moment show the error message programmed, I change the localhost for my local IP
[WebService(Namespace = "http://testapi.idat/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]
public class Service : System.Web.Services.WebService
{
SqlConnection cn = new SqlConnection("data source=.;initial catalog = neptuno; integrated security=true");
public Service () {
//InitializeComponent();
}
[WebMethod]
public string HelloWorld() {
return "Hello World";
}
[WebMethod]
public DataSet mostrar(){
DataSet ds = new DataSet();
cn.Open();
String sql = "select idProducto from productos";
SqlDataAdapter da = new SqlDataAdapter(sql,cn);
da.Fill(ds);
return ds;
}
}
the service works and return the column IDProduct, the problem is call with the method, I already add the permission on manifest, and the layout is just a textview for error message and edittext multiline for show the data
package com.example.webservice;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.EditText;
import android.widget.TextView;
import android.os.AsyncTask;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
public class MainActivity extends Activity {
EditText resultMultiline;
TextView message;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
resultMultiline = (EditText)findViewById(R.id.editText1);
message = (TextView) findViewById(R.id.textView);
CallWebservice webservice = new CallWebservice();
webservice.execute();
}
public class CallWebservice extends AsyncTask<Integer,Integer,Boolean>{
String resultText = "";
protected Boolean doInBackground(Integer... params){
boolean result = true;
final String SOAP_ACTION = "http://testapi.idat/mostrar";
final String NAMESPACE = "http://testapi.idat/";
final String METHOD_NAME = "mostrar";
final String URL = "http://192.168.1.45:51582/Webservice/Service.asmx?WSDL";
HttpTransportSE transport = new HttpTransportSE(URL);
SoapObject request = new SoapObject(NAMESPACE,METHOD_NAME);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
try {
transport.call(SOAP_ACTION,envelope);
SoapPrimitive respSoap = (SoapPrimitive)envelope.getResponse();
resultText = respSoap.toString();
}catch (Exception e){
result = false;
Log.d("Debug", e.getMessage().toString());
}
return result;
}
protected void onPostExecute(Boolean result){
if (result){
resultMultiline.setText(resultText);
Log.d("Debug","Web service works");
}else{
message.setText("ERROR");
}
}
}
}
Use the Following function it is working in my case this is normal Login soap call you need to add internet permission in your menifest
/*
* Vishal Mokal 05-MAR-2015
* soapCALL() function returns String Array.
* Make A sope call and returns response.
* if responce is success full then user will get responce array
* first element status = 1 if syccessfull 0 = if any exception of faliur
* second element = response srtring if successful or error message.
* */
public String[] soapCALL(RequestDetails requestDetails, String wsdlUserName, String wsdlPassword , String headerName) {
String url = requestDetails.getUrl().toString();
String nameSpace = requestDetails.getNameSpace().toString();
String methodName = requestDetails.getMethodName().toString();
String soapAction = requestDetails.getSoapAction().toString();
String[] responses = new String[2];
Element[] header = new Element[1];
// header[0] = new Element().createElement(nameSpace, "AuthenticationHeader");
header[0] = new Element().createElement(nameSpace, headerName);
try {
Element UserName = new Element().createElement(nameSpace, "UserName");
UserName.addChild(Node.TEXT, wsdlUserName);
header[0].addChild(Node.ELEMENT, UserName);
Element Password = new Element().createElement(nameSpace, "Password");
Password.addChild(Node.TEXT, wsdlPassword);
header[0].addChild(Node.ELEMENT, Password);
SoapObject request = requestDetails.getSoapObject();
SoapSerializationEnvelope soapSerilizationEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
soapSerilizationEnvelope.dotNet = true;
soapSerilizationEnvelope.headerOut = header;
soapSerilizationEnvelope.setOutputSoapObject(request);
Object env = soapSerilizationEnvelope.bodyOut;
HttpTransportSE httptransport = new HttpTransportSE(url);
httptransport.call(soapAction, soapSerilizationEnvelope);
SoapPrimitive response = (SoapPrimitive) soapSerilizationEnvelope.getResponse();
responses[0] = "1";
responses[1] = response.toString();
Log.d("Respons", response.toString());
return responses;
}
catch (SocketTimeoutException e)
{
responses[0] = "0";
responses[1] = "Sorry!Unable To Connect Server Please Check Your Internet Connection Or Try After Some Time.";
return responses;
}
catch (SocketException e)
{
responses[0] = "0";
responses[1] = "Sorry!Unable To Connect Server Please Try After Some Time.";
return responses;
}
catch (Exception e) {
responses[0] = "0";
responses[1] = "Sorry!Unable To Connect Server Please Try After Some Time.";
return responses;
}
}
Following is the structure for the Requestdetail class
public class RequestDetails {
private String nameSpace;
private String url;
private String methodName;
private String SoapAction;
private SoapObject soapObject;
public RequestDetails(String nameSpace, String url, String methodName, String soapAction) {
this.nameSpace = nameSpace;
this.url = url;
this.methodName = methodName;
SoapAction = soapAction;
}
public String getNameSpace() {
return nameSpace;
}
public String getUrl() {
return url;
}
public String getMethodName() {
return methodName;
}
public String getSoapAction() {
return SoapAction;
}
public SoapObject getSoapObject() {
return soapObject;
}
public void setSoapObject(SoapObject soapObject) {
this.soapObject = soapObject;
}
}
I have a SOAP service which I need to talk to. The first SOAP request receives a response with a sessionID element in the SOAP header which I need to send in the final release command.
To grab this sessionID value I plan on using a ClientInterceptor. In my implementation of WebServiceGatewaySupport I register my interceptor:
this.setInterceptors(new ClientInterceptor[] { new MyWebServiceClientInterceptor() });
My Interceptor:
public class MyWebServiceClientInterceptor implements ClientInterceptor {
public final QName SessionID_QNAME = new QName("http://xml.example.com/ws/session", "sessionID");
public boolean handleFault(MessageContext context) throws WebServiceClientException {
logger.info("Handle Fault");
return true;
}
public boolean handleResponse(MessageContext context) throws WebServiceClientException {
logger.info("Handle Response");
SoapMessage soapMessage = (SoapMessage) context.getRequest();
SoapHeader soapHeader = soapMessage.getSoapHeader();
logger.info("Response Header: " + soapHeader.getName());
Iterator<SoapHeaderElement> qn = soapHeader.examineAllHeaderElements();
while (qn.hasNext()) {
SoapElement elem = qn.next();
logger.info(elem.toString());
}
Iterator<QName> an = soapHeader.getAllAttributes();
while (an.hasNext()) {
QName elem = an.next();
logger.info(elem.toString());
}
return true;
}
public boolean handleRequest(MessageContext context) throws WebServiceClientException {
logger.info("Handle Request");
return true;
}
}
However I keep getting back an empty SOAP header. I'm running Wireshark and can clearly see the sessionID token in the returned SOAP packet so at a lost as to what's going on. Any ideas?
I'm an idiot. 'Twas a typo. I needed to call:
SoapMessage soapMessage = (SoapMessage) context.getResponse();
Not:
SoapMessage soapMessage = (SoapMessage) context.getRequest();
Full code for those wanting to set headers on requests and receive headers on responses:
ClientInterceptor to receive custom SOAP header values:
public class MyWebServiceClientInterceptor implements ClientInterceptor {
public boolean handleFault(MessageContext context) throws WebServiceClientException {
return true;
}
public boolean handleResponse(MessageContext context) throws WebServiceClientException {
SoapMessage soapMessage = (SoapMessage) context.getResponse();
SoapHeader soapHeader = soapMessage.getSoapHeader();
Iterator<SoapHeaderElement> qn = soapHeader.examineHeaderElements(MY_SESSIONID_QNAME);
while (qn.hasNext()) {
SoapElement elem = qn.next();
SoapHeaderElement headerElem = (SoapHeaderElement) elem;
if (StringUtil.validString(headerElem.getText())) {
if (!headerElem.getText().equals(sessionId)) {
sessionId = headerElem.getText();
logger.info("Bound to Session ID: " + sessionId);
}
}
}
return true;
}
public boolean handleRequest(MessageContext context) throws WebServiceClientException {
return true;
}
}
Where I use the interceptor:
public class MySoapClient extends WebServiceGatewaySupport {
public MySoapClient() {
this.setInterceptors(new ClientInterceptor[] { new MyWebServiceClientInterceptor() });
...
}
...
}
Callback to set custom SOAP Header:
class MySoapActionCallback implements WebServiceMessageCallback {
public void doWithMessage(WebServiceMessage message) throws IOException, TransformerException {
SaajSoapMessage soapMessage = (SaajSoapMessage) message;
SoapHeaderElement messageId = soapMessage.getSoapHeader().addHeaderElement(SessionID_QNAME);
if (StringUtil.validString(sessionId)) {
messageId.setText(sessionId);
}
}
}
Where I use the callback:
JAXBElement<ReturnType> result = (JAXBElement<ReturnType>) getWebServiceTemplate().marshalSendAndReceive(of.createRelease(null), new MySoapActionCallback());
I have an AsyncTask which calls a webservice method and sends the returned data back to the main thread. This task does not affect any UI elements yet throws CalledFromWrongThreadException on the line calling onResultSuccess. I have many other AsyncTask which work in basically the exact same way, changing the called web service method and parameters, etc, but this is the only one that fails. Anyone spot what might be the problem? The AsyncTask looks like:
public static class ClockOff extends AsyncTask<String, Void, Void>
{
public OnAsyncResultClockOff onAsyncResultClockOff;
public void setOnResultListener(OnAsyncResultClockOff onAsyncResultClockOff) {
if(onAsyncResultClockOff != null) {
ResultClockOff = onAsyncResultClockOff;
}
}
private static final String SOAP_ACTION = "http://tempuri.org/ClockOff";
private static final String METHOD_NAME = "ClockOff";
private static final String NAMESPACE = "http://tempuri.org/";
private static final String URL = GlobalFunction.URL;
#Override
protected Void doInBackground(String... strArgs)
{
try
{
SoapObject Request = new SoapObject(NAMESPACE, METHOD_NAME);
Request.addProperty("Items", strArgs[0]);
Request.addProperty("ID", strArgs[1]);
SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
soapEnvelope.dotNet = true;
soapEnvelope.setOutputSoapObject(Request);
HttpTransportSE transport= new HttpTransportSE(URL);
transport.call(SOAP_ACTION, soapEnvelope);
SoapObject result = (SoapObject) soapEnvelope.bodyIn;
String ReturnMessage = ((SoapObject) result.getProperty(0)).getPropertyAsString(0);
String ReturnMessageHeader = ((SoapObject) result.getProperty(0)).getPropertyAsString(1);
String ActionType = ((SoapObject) result.getProperty(0)).getPropertyAsString(2);
String SelectedCompleted = ((SoapObject) result.getProperty(0)).getPropertyAsString(3);
String ClockOffItems = ((SoapObject) result.getProperty(0)).getPropertyAsString(4);
//Exception thrown on the below line
onAsyncResultClockOffItem.onResultSuccess(ReturnMessage, ReturnMessageHeader, ActionType, SelectedCompleted, ClockOffItems);
return null;
}
catch (Exception e)
{
onAsyncResultClockOffItem.onResultFail();
return null;
}
}
}
OnAsyncResultClockOff is simply:
public interface OnAsyncResultClockOff {
public abstract void onResultFail();
public abstract void onResultSuccess(String returnMessage,
String returnMessageHeader, String actionType,
String selectedCompleted, String clockOffItems);
}
And it is called via:
ClockOff co= new ClockOffItem();
co.setOnResultListener(onAsyncResultClockOff);
co.execute(items, ID);
Where onAsyncResultClockOff implements the OnResultFail() and OnResultSuccess methods.