Showing posts with label Web Service. Show all posts
Showing posts with label Web Service. Show all posts

Monday, March 14, 2022

Remove the <return> tag from SOAP response in JAVA

 Following JAVA annotation can be used to avoid <return> tag from SOAP web service response.

@WebService(serviceName = "Operations")

public class Service {

    @WebMethod(operationName = "processUser")

    @SOAPBinding(parameterStyle=ParameterStyle.BARE)

    public Result processUser(@WebParam(name = "userName")String userName) {

        Result result = new Result();

        result.setResultCode("1");

        result.setResultDesc("success");

        return result;

    }

}

ParameterStyle.BARE will return SOAP response with your defined object attributes.


Friday, April 9, 2021

Edit XSD files to resolve AXIS web service invalid element error

 When executing webservices using AXIS, you can receive following error due to invalid elements.

                {http://xml.apache.org/axis/}stackTrace:org.xml.sax.SAXException: Invalid element in com.mobitel.axis.AdjustmentResult - AdjustmentSerialNo

You can edit XSD file for this WSDL and add AdjustmentSerialNo element to AdjustmentResult element to resolve this. You have to download XSD files and generate webservice clients.



Thursday, September 6, 2018

Informix 12.10 docker image (Rest Services)

You can find the docker image for "informix 12.10" database at following location. Informix 12.10 is rest enabled. That means CRUD opreations can be done using rest services without JDBC connectivity.


For demonstration purposes, you can use above docker image as a informix database.

You can use standard http requests to CRUD operations.

insert = POST
get = GET
update = PUT 
delete = DELETE etc

Sample requests

Sample Testing

database = test_db
table = test_tbl
table schema = id int(11), name varchar(20)

Insert request

http://192.168.111.132:27018/test_db/test_tbl

{
  "id":0,
  "name":"sujith"
}

Get request

http://192.168.111.132:27018/test_db/test_tbl

{
{
  "id":0,
  "name":"sujith"
},
{
  "id":1,
  "name":"delachithra"
}
}

Update request

http://192.168.111.132:27018/test_db/test_tbl/id/2

{
  "id":2,
  "name":"test update"
}

Delete request

http://192.168.111.132:27018/test_db/test_tbl/id/2

Thursday, April 4, 2013

Verify HOST servers entrusted certificates in JAVA

In sometimes, it has to be trusted the certificates in JAVA client when applications are developed. This may occur when hosting URL doesn't match with certificate URL.

Following JAVA code can be used to verify host as a trusted host.

   static {
   javax.net.ssl.HttpsURLConnection.setDefaultHostnameVerifier(
new javax.net.ssl.HostnameVerifier(){

   public boolean verify(String hostname,
               javax.net.ssl.SSLSession sslSession) {
       if (hostname.equals("192.168.11.123")) {
          return true;
       }
          return false;
       }
   });
     } 

Monday, November 19, 2012

Call AXIS web service with security

AXIS web service uses .wsdd XML file to read username & passwordCallbackClass. Here, it shown sample .wsdd file.

client_deploy.wsdd

<deployment xmlns="http://xml.apache.org/axis/wsdd/"
xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">
<transport name="http" pivot="java:org.apache.axis.transport.http.HTTPSender"/>
<globalConfiguration >
<requestFlow >
<handler type="java:org.apache.ws.axis.security.WSDoAllSender" >
<parameter name="action" value="UsernameToken"/>
<parameter name="user" value="M_TICKET"/>
<parameter name="passwordType" value="PasswordText" />
<parameter name="passwordCallbackClass"
value="PWCallBackClass"/>
</handler>
</requestFlow >
</globalConfiguration >
</deployment>

Then you have to implement "PWCallBackClass" class as below.

PWCallBackClass .java


public class PWCallBackClass implements CallbackHandler {

     private static final byte[] key = {
   (byte)0x31, (byte)0xfd, (byte)0xcb, (byte)0xda,
   (byte)0xfb, (byte)0xcd, (byte)0x6b, (byte)0xa8,
   (byte)0xe6, (byte)0x19, (byte)0xa7, (byte)0xbf,
   (byte)0x51, (byte)0xf7, (byte)0xc7, (byte)0x3e,
   (byte)0x80, (byte)0xae, (byte)0x98, (byte)0x51,
   (byte)0xc8, (byte)0x51, (byte)0x34, (byte)0x04,
     };
public void handle(Callback[] callbacks)
   throws IOException, UnsupportedCallbackException {
   for (int i = 0; i < callbacks.length; i++) {
     if (callbacks[i] instanceof WSPasswordCallback) {
       WSPasswordCallback pc = (WSPasswordCallback) callbacks[i];
       /*
        * here call a function/method to lookup the password for
        * the given identifier (e.g. a user name or keystore alias)
        * e.g.: pc.setPassword(passStore.getPassword(pc.getIdentfifier))
        * for testing we supply a fixed name/fixed key here.
        */
       if (pc.getUsage() == WSPasswordCallback.KEY_NAME) {
         pc.setKey(key);
       }
       else {
         pc.setPassword("Hsy84#ep$8&@v");
       }
     } else {
       throw new UnsupportedCallbackException(
         callbacks[i], "Unrecognized Callback");
     }
   }
}

Following code can be used to call web service with security implemented with AXIS.

TestWS.java

import java.rmi.Remote;
import java.rmi.RemoteException;

import javax.xml.rpc.ServiceException;

import org.apache.axis.AxisFault;
import org.apache.axis.EngineConfiguration;
import org.apache.axis.client.Stub;
import org.apache.axis.configuration.FileProvider;

import com.comverse_in.prepaid.ccws.ServiceLocator;
import com.comverse_in.prepaid.ccws.ServiceSoap;
import com.comverse_in.prepaid.ccws.ServiceSoapProxy;
import com.comverse_in.prepaid.ccws.ServiceSoapStub;

import org.apache.ws.security.WSConstants;
import org.apache.ws.security.handler.WSHandlerConstants;
import org.apache.ws.security.message.token.UsernameToken;

public class TestWS {

public static void main(String args[]) throws ServiceException, RemoteException{

EngineConfiguration config = new FileProvider("client_deploy.wsdd");
ServiceLocator locator = new ServiceLocator(config);
Remote remote = locator.getPort(ServiceSoap.class);
Stub axisPort = (Stub)remote;

axisPort._setProperty(WSHandlerConstants.ACTION, WSHandlerConstants.USERNAME_TOKEN);
axisPort._setProperty(UsernameToken.PASSWORD_TYPE, WSConstants.PASSWORD_TEXT);
axisPort._setProperty(WSHandlerConstants.USER, "sujith");

axisPort._setProperty(WSHandlerConstants.PW_CALLBACK_CLASS, "PWCallBackClass");

ServiceSoap service = (ServiceSoapStub)axisPort;
System.out.println("Calling service...");
int ver = service.getVersionId();
System.out.println("get version id service returned " + ver);
}
}

Reference :- http://ws.apache.org/wss4j/package.html

Following jars needed to run web service client than AXIS jars.

  1. opensaml-1.0.1.jar
  2. wss4j-2.1.jar
  3. ws-security-5.1.0-M1.jar
  4. xmlsec-1.3.0.jar

Thursday, November 26, 2009

Use AXIS library to create web service and client

Download AXIS binary and extract it to your hard disk.
Set up AXIS2_HOME and path environment veriables.

Create service using WSDL file

1) Open terminal.

2) Go to the directory you want to create service.

3) Run the below command.

wsdl2java -uri "test.wsdl" -ss -sd -d xmlbeans -o service

4) This will create a folder called "service" and created the service using the .wsdl file.

5) Go to the "service" folder and run "ant" command to build the .aar file.

6) Go to AXIS bin and run "axis2server.sh".

7) Put the created .aar file into AXIS repository\services folder.

8) It will auto deployed as a service.

9) You can access the service using "http://127.0.0.1:8080".

Create client using WSDL file

1) Open terminal.

2) Go to the directory you want to create client.

3) Run the below command.

wsdl2java -uri "test.wsdl" -d xmlbeans -o client

4) This will create a folder called "client" and created the client code using the .wsdl file.

Thursday, October 15, 2009

Create web service clients in java

There is a tool to create web service clients called as "cxf". This is a free tool. You must have the wsdl file to create java clients.

Download URL :- http://cxf.apache.org/download.html
User guide :- http://ws.apache.org/axis/java/user-guide.html
Reference :- http://cxf.apache.org/docs/wsdl-to-java.html

  • Extract cxf zip file and change directory to /din in your terminal
  • use the follwing command to create java clients
wsdl2java HelloWorld.wsdl

Namespace error in JAX-WS

Here is the error, i got when i am going to creat my web service client.

oracle.jdeveloper.webservices.model.WebServiceException: Error creating model from wsdl "http://127.0.0.1/Service.asmx?wsdl": undefined element declaration 's:schema'

I tried many ways to create my client using jdeveloper, but failed.

After that, i used axis web service for client implementation. It works fine in eclipse.

Create XML file in java

This is a simple example of creating standard XML file in java.

DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
Document document = documentBuilder.newDocument();
Element rootElement = document.createElement("root");

This code segment creates a root element of the XML file.

Now, use this code to create child nodes to XML document.

Element em = document.createElement("element");
em.appendChild(document.createTextNode("data"));
rootElement.appendChild(em);