viernes, 18 de mayo de 2012

UTILWS PARA JAXWS


Hola en esta oportunidad mostrar les hablare y les compartiré un Utilitario que normalmente manejo para el manejo de Servicios Web y Proxys con el Framework JAXWS.
El utilitario es un clase JAVA que le llamo: UtilWS,java y soporta el manejo lo siguiente:

- setEndpointURL: Permite el seteo de una nueva URL que contenga el WSDL del WS expuesta en el navegador..
- getEndpointURL:
Permite obtener la URL (EndPoint) que el servicio actualmente esta soportando.
- validaUrl:
Valida la URL si esta actualmente activa o no. Esto para ser aplicado antes de la consulta desde un Proxy WS.
- setTimeOutProxyWS:
Setea un tiempo determinado (TimeOut) para las consultas a WS desde un Proxy en base al Binding.
- setLoginProxyWS:
  Setea el Login para la autenticación de acceso a un WS externo.

El utilitario se usa de esta manera:

ESCENARIO:

1. Supongamos que tenemos un Servicio Web desplegado en un SERVER XXX a este para efecto del Dummy le puesto en la operación un: 

public static void main( String[] args ){
           
//------------ PARA TEST [TIMEOUT] ------------//
   long tiempo = (5 * 1000); //5 SEGUNDOS

   try{
       Thread.sleep( tiempo );
   }
   catch( InterruptedException e ){
          e.printStackTrace();
   }
//---------------------------------------------// 
}                                                
}

2. Este SERVER supongamos que generara un WSDL expuesto asi:

http://localhost:8090/GestionDatosUsuarioWS/GestionDatosUsuarioWSService?wsdl

3. Ahora para consumirlo tendré que manejar un PROXY Client y para crearlo usare la IDE:  Oracle Enterprise Pack for Eclipse, ya que viene un unos muy buenos PLUGINs y sobre todo con el de JAXWS.

4. Creamos el APP WS:  file/web/other/weblogic Services/Web Service Project
damos el nombre al Proxy y listo. Ya tenemos nuestro proyecto de tipo WS.

5. Creamos el PROXY Client:   file/new/other/weblogic Services/ Web Service Client, nos mostrara un Wizar donde nos dará la opción para generar los Objetos y Clases .JAVA en base a un WSDL o una URL. Luego, nos pedira que le designemos un nombre al .JAR (Ya que estas librerías las meterá un un .JAR dentro de la carpeta .lib, para su mejor manejo ya que no se deben de modificar dichas clases).

6. Dentro de SRC creamos una Clases Test por ejemplo llamada:  TestGestionDatos.java, en donde crearemos el PROXY en si, en base al JAR generado y apoyándonos en nuestro UtilWS.java.
IMPORTANTE: VALIDAR YA QUE HAY 2 [BindingProperties]:
                             com.sun.xml.internal.ws.client.BindingProperties
                             com.sun.xml.ws.client.BindingProperties


UtilWS.java:

/**
 * UtilWS  IMPORTANTE: VALIDAR YA QUE HAY 2 [BindingProperties y BindingProvider]:
 *                     com.sun.xml.ws.client.BindingProperties          [SI FUNCIONA]
 *                     com.sun.xml.internal.ws.client.BindingProperties [NO FUNCIONA]
 *                    
 * @author cguerra
 */ public class UtilWS{
   
    private Logger logger = Logger.getLogger( this.getClass().getName() );
   
    //Constructor ...
    public UtilWS(){       
    }
        
   /**
    * EndpointURL
    * @param  objBinding
    * @return actual_URL
    */
    public String getEndpointURL( Object objBinding ){
           String actual_URL = (String)((BindingProvider)objBinding).getRequestContext().get( BindingProvider.ENDPOINT_ADDRESS_PROPERTY );
           return actual_URL;
    }   
   
   /**
    * setEndpointURL
    * @param  URL
    * @param  objBinding
    * @return new_URL
    */
    public String setEndpointURL( String URL, Object objBinding ){
           String new_URL = (String)((BindingProvider)objBinding).getRequestContext().put( BindingProvider.ENDPOINT_ADDRESS_PROPERTY, URL );  
           return new_URL;
    }
   
   /**
    * validaUrl valida la existencia de la 'URL'.
    * @param  cadenaUrl
    * @return int
    * @throws MalformedURLExceptionWS
    * @throws IOExceptionWS
    */
    public int validaUrl( String cadenaUrl ) throws MalformedURLExceptionWS, IOExceptionWS{

        int respuestaURL = 0;

        try{
            URL url = new URL( cadenaUrl );
            URLConnection urlConexion = url.openConnection();

            urlConexion.setDoOutput( true );
            urlConexion.setDoInput(  true );

            if( urlConexion instanceof HttpURLConnection ){

                this.logger.debug( "" );
                this.logger.debug( "====> URL:            " + urlConexion.getURL() );
                this.logger.debug( "====> TIPO CONTENIDO: " + urlConexion.getContentType() );
                this.logger.debug( "" );

                HttpURLConnection httpConexion = (HttpURLConnection)urlConexion;
                httpConexion.connect();

                respuestaURL = httpConexion.getResponseCode(); // 200 = OK
                this.logger.debug( "Respuesta 'URL': " + respuestaURL );

                if( respuestaURL == 200 ){
                    this.logger.debug( "Conexion Exitosa con la 'URL': " + url );
                }
            }
        }
        catch( MalformedURLException e ){
               //this.logger.error( "Error en el Fomato de la 'URL': " + e.getMessage() );
               throw new MalformedURLExceptionWS( "ERROR: [Fomato de la 'URL'] " + e.getMessage(), e );
        }
        catch( IOException e ){
               //this.logger.error( "Error en la conexion con el Servidor: " + e.getMessage() );
               throw new IOExceptionWS( "ERROR: [Conexion con el Servidor] " + e.getMessage(), e );
        }

        return respuestaURL;
    }
   
   /**
    * setTimeOutProxyWS
    * @param objBinding
    * @param requestTimeout
    * @param connectTimeout
    */
    public void setTimeOutProxyWS( Object objBinding, int requestTimeout, int connectTimeout ){          
           Map contextoRequest = ((BindingProvider)objBinding).getRequestContext();
         
           //FORMA #1:
           contextoRequest.put( BindingProviderProperties.CONNECT_TIMEOUT, connectTimeout );
           contextoRequest.put( BindingProviderProperties.REQUEST_TIMEOUT, requestTimeout );
         
           //FORMA #2:
           //contextoRequest.put( "com.sun.xml.ws.connect.timeout", connectTimeout );
           //contextoRequest.put( "com.sun.xml.ws.request.timeout", requestTimeout );
    }
   
   /**
    * setLoginProxyWS
    * @param objBinding
    * @param usuarioParam
    * @param passwParam
    */
    public void setLoginProxyWS( Object objBinding, String usuarioParam, String passwParam ){          
           Map contextoRequest = ((BindingProvider)objBinding).getRequestContext();
           contextoRequest.put (BindingProvider.USERNAME_PROPERTY, usuarioParam );
           contextoRequest.put (BindingProvider.PASSWORD_PROPERTY, passwParam   );
    }
   
 }

Dentro de esta clase manejaremos el control de TIMEOUT y ENDPOINT accediento al UtilWS.java:

TestGestionDatos.java
  
/**
 * @author cguerra.
 * @clase: TestGestionDatos.java
 * @versión 1.0
 */
public class TestGestionDatos{
   
    private Logger logger = Logger.getLogger( this.getClass().getName() );
   
    private UtilWS objUtilWS = null;
   
    /**
     * @param argumentos
     * @throws ServiceException
     * @throws RemoteException
     * @throws MalformedURLException
     */
    public static void main( String... argumentos ){
       
        TestGestionDatos objTest = null;
       
        try{              
            objTest = new TestGestionDatos(); 
            objTest.procesarConsultaUsuarioProxy();
           }
           catch( Exception e ){
                  objTest.logger.error( "ERROR: ", e );
           }          
    }   
  
   /**
    * procesarConsultaUsuarioProxy
    * @throws TimeOutExceptionWS
    * @throws WSException
    * @throws GenericExceptionWS
    * @throws MalformedURLExceptionWS
    * @throws IOExceptionWS
    */
    public void procesarConsultaUsuarioProxy() throws TimeOutExceptionWS, WSException, GenericExceptionWS, MalformedURLExceptionWS, IOExceptionWS{
       
       GestionDatosUsuarioWSSoapImplService objGestionDatosUsuario = new GestionDatosUsuarioWSSoapImplService();
       GestionDatosUsuarioWSSoapImpl        objServicioWS          = objGestionDatosUsuario.getGestionDatosUsuarioWSSoapImplPort();
      
       this.objUtilWS = new UtilWS();       
       int validaURL  = this.objUtilWS.validaUrl( ConstantesWS.URL_WSDL_GESTION_DATOS_USUARIO_WS );
      
       if( validaURL == ConstantesWS.COD_ENDPOINT_OK ){   
          
           //---------- ENDPOINT ----------//
           this.objUtilWS.setEndpointURL( ConstantesWS.URL_WSDL_GESTION_DATOS_USUARIO_WS, objServicioWS );
           //---------- ENDPOINT ----------//
          
           try{
               ConsultarUsuarioInput  objConsultaInput  = new ConsultarUsuarioInput();
               ConsultarUsuarioOutput objConsultaOutPut = new ConsultarUsuarioOutput();
               Usuario                objUsuario        = new Usuario();
               Generico               objGenerico       = null;
               Arg0                   objArgumento      = null;
               StringBuffer           objCadena         = null;           
               String                 saltoLine         = new String( "\n" ); 
                             
               objUsuario.setCodigo( 1 );       
               this.logger.debug( "[DATOS REQUEST]: " );
               this.logger.debug( "CODIGO: [" + objUsuario.getCodigo() + "]" );   
              
               objConsultaInput.setConsultarUsuarioInput( objUsuario );
              
               objArgumento = new Arg0();
               objArgumento.setConsultarUsuarioInput( objConsultaInput );
              
               //---------- TIMEOUT ----------//
               this.objUtilWS.setTimeOutProxyWS( objServicioWS, ConstantesWS.REQUEST_TIMEOUT_GESTION_DATOS_USUARIO,
                                                                ConstantesWS.CONECTION_TIMEOUT_GESTION_DATOS_USUARIO );
               //---------- TIMEOUT ----------//
              
               objConsultaOutPut = objServicioWS.consultarUsuario( objArgumento );
              
               objUsuario  = objConsultaOutPut.getConsultarUsuarioOutput();
               objGenerico = objConsultaOutPut.getMensajeGenericoOutput();
              
               String   codigoGene     = objGenerico.getCodigo();
               String   mensajeGene    = objGenerico.getMensaje();
               Integer  codigo         = objUsuario.getCodigo();
               String   nombres        = objUsuario.getNombres();
               String   apellidos      = objUsuario.getApellidos();
               String   nombreApellido = objUsuario.getNombreApellido();
               String   dni            = objUsuario.getDni();
               String   telefono       = objUsuario.getTelefono();
               String   email          = objUsuario.getEmail();
               XMLGregorianCalendar cumpleanos     = objUsuario.getCumpleanos();
               XMLGregorianCalendar fecRegDateTime = objUsuario.getFecRegDateTime();
               String   fecRegString   = objUsuario.getFecRegString();
              
               this.logger.debug( "[DATOS RESPONSE]: " );
              
               objCadena = new StringBuffer();
               objCadena.append( "- CODIGO GENERICO:  ["  + codigoGene + "]" + saltoLine );
               objCadena.append( "- MENSAJE GENERICO: [" + mensajeGene + "]" + saltoLine );
               objCadena.append( "- CODIGO: ["       + codigo      + "]" + saltoLine );
               objCadena.append( "- NOMBRES: ["      + nombres     + "]" + saltoLine );
               objCadena.append( "- APELLIDOS: ["    + apellidos   + "]" + saltoLine );
               objCadena.append( "- NOMBRES-APELLIDOS: [" + nombreApellido + "]" + saltoLine );
               objCadena.append( "- DNI: ["          + dni         + "]" + saltoLine );
               objCadena.append( "- TELEFONO: ["     + telefono    + "]" + saltoLine );
               objCadena.append( "- EMAIL: ["        + email       + "]" + saltoLine );
               objCadena.append( "- CUMPLEANIOS: ["  + cumpleanos  + "]" + saltoLine );
               objCadena.append( "- FECHA DATETIME: [" + fecRegDateTime + "]" + saltoLine );
               objCadena.append( "- FECHA STRING: ["   + fecRegString   + "]" + saltoLine );
              
               this.logger.debug( objCadena.toString() );              
           }
           catch( Exception e ){
                  if( (e instanceof java.net.SocketTimeoutException) ){
                throw new TimeOutExceptionWS( "ERROR: [SocketTimeoutException] " + e.getMessage(), e );
                  }
                  else if( (e instanceof javax.xml.ws.WebServiceException) ){
                            throw new WSException( "ERROR: [WebServiceException] " + e.getMessage(), e );
                  }
                  else{
                       throw new GenericExceptionWS( "ERROR: " + e.getMessage(), e );
                  }
           }          
       }
   }    
 }

Si en la constante:  REQUEST_TIMEOUT_GESTION_DATOS_USUARIO, le seteamos un tiempo de 6 segundos (6000 milisegundos) se obtendría un resultado OK ya que el SERVER dará 5 segundos de espera:

[2012-05-18 | 22:53:24] [pe.com.javaman.services.jaxws.util.UtilWS] [DEBUG]:
[2012-05-18 | 22:53:24] [pe.com.javaman.services.jaxws.util.UtilWS] [DEBUG]: ====> URL:            http://127.0.0.1:8090/GestionDatosUsuarioWS/GestionDatosUsuarioWSService?wsdl
[2012-05-18 | 22:53:24] [pe.com.javaman.services.jaxws.util.UtilWS] [DEBUG]: ====> TIPO CONTENIDO: text/xml;charset="utf-8"
[2012-05-18 | 22:53:24] [pe.com.javaman.services.jaxws.util.UtilWS] [DEBUG]:
[2012-05-18 | 22:53:24] [pe.com.javaman.services.jaxws.util.UtilWS] [DEBUG]: Respuesta 'URL': 200
[2012-05-18 | 22:53:24] [pe.com.javaman.services.jaxws.util.UtilWS] [DEBUG]: Conexion Exitosa con la 'URL': http://127.0.0.1:8090/GestionDatosUsuarioWS/GestionDatosUsuarioWSService?wsdl
[2012-05-18 | 22:53:24] [pe.com.javaman.services.jaxws.dummy.proxy.TestGestionDatos] [DEBUG]: [DATOS REQUEST]:
[2012-05-18 | 22:53:24] [pe.com.javaman.services.jaxws.dummy.proxy.TestGestionDatos] [DEBUG]: CODIGO: [1]
[2012-05-18 | 22:53:29] [pe.com.javaman.services.jaxws.dummy.proxy.TestGestionDatos] [DEBUG]: [DATOS RESPONSE]:
[2012-05-18 | 22:53:29] [pe.com.javaman.services.jaxws.dummy.proxy.TestGestionDatos] [DEBUG]: - CODIGO GENERICO:  [1]
- MENSAJE GENERICO: [PROCESO EXITOSO]
- CODIGO: [1]
- NOMBRES: [Cesar Ricardo]
- APELLIDOS: [Guerra Arnaiz]
- NOMBRES-APELLIDOS: [null]
- DNI: [41816133]
- TELEFONO: [5214952]
- EMAIL: [null]
- CUMPLEANIOS: [null]
- FECHA DATETIME: [null]
- FECHA STRING: [null]

Pero si en la constante:  REQUEST_TIMEOUT_GESTION_DATOS_USUARIO, le seteamos un tiempo de 4 segundos (4000 milisegundos) se obtendría un resultado NOK ya es MENOR al tiempo del SERVER de 5 segundos de espera. El ERROR será de tipo TIMEOUT:

pe.com.javaman.services.jaxws.exceptions.WSException: ERROR: [WebServiceException] java.net.SocketTimeoutException: Read timed out
    at pe.com.javaman.services.jaxws.dummy.proxy.TestGestionDatos.procesarConsultaUsuarioProxy(TestGestionDatos.java:139)
    at pe.com.javaman.services.jaxws.dummy.proxy.TestGestionDatos.main(TestGestionDatos.java:46)

Para descargar UtilWS.java, pulsar:  AQUI 

Espero que el post les sea de utilidad ..!

No hay comentarios: