Ver Mensaje Individual
  #2 (permalink)  
Antiguo 13/05/2010, 06:35
javibrezmes
 
Fecha de Ingreso: mayo-2010
Mensajes: 19
Antigüedad: 16 años, 5 meses
Puntos: 0
Respuesta: Manejo de protocolos tipo ed2k en mi aplicacion

Mis disculpas se me olvido indicar las clases java que utilizo en mi aplicación:

Haber primero esta el Handler.java que por lo que creo es el que se encarga de recibir la peticion de URL:

Código PHP:
public class Handler extends URLStreamHandler
{ 
    
String ventana;
    
String params;

    
// Metodo para parsear la URL de entrada
    
protected void parseURL(URL u,String string,int begin,int end)
    {
        
System.out.println("ENTRA EN PARSE URL");
        
        
String aux = string.substring(begin+2,end);    
        
int pos =string.indexOf('/'); 
        
ventana = aux.substring(0,pos); 
        
params = aux.substring(pos+1);
         
        try 
        {
            
u.getContent();
        } 
        catch (
IOException e) 
        {
            
e.printStackTrace();
        }
    } 
 
    
// Abrimos la conexion 
    
protected URLConnection openConnection(URL url)throws IOException
    
{     
        
System.out.println("ENTRA EN OPEN CONNECTION");
        
        return new 
org.protocol.myapp.MyappURLConnection(url,ventana,params);
    } 
} 
Luego tenemos la clase que debe extender la URLConnection

Código PHP:
public class MyappURLConnection extends URLConnection 
{

    public static final 
int HTTPport = 80;      
    public static 
String[] headerfields = null;     
    
String window;
    
String resource;
    
Socket socluURL = null;
    
    public 
MyappURLConnection(URL url, String ventana, String params) 
    {
        
super(url);
        
this.setDoOutput(true);
        
resource = params;
        
window = ventana;
    }

    public 
void connect() throws IOException 
    
{
             
URLConnection.connected=true
         this
.connected = true;
    }
    
    public 
InputStream getInputStream() throws IOException
    
{
        if(
connected==false)
        {
            
connect(); 
        }
        
        
InputStream IS = socluURL.getInputStream();

        return 
IS;
    }
    
    public 
Object getContent()throws IOException
    
{  
       
String contentType = getContentType();
   
       
       if(
window.equals("ventanaA"))
       {
           
//Instrucciones abrir ventana A
       
}
       else if(
window.equals("ventanaB"))
       {
           
//Instrucciones abrir ventana B
       
}
       else
       {
           
//Instrucciones abrir ventana por defecto
       
}
       
       return 
null;
    }

    public 
String getContentType()
    {    
        
        
StringTokenizer ST = new StringTokenizer(resource,"|");
           
        
int i = 1;
        while(
ST.hasMoreTokens())
        {
            if((
i % 2) != 0)
            
{
                
//String key = 
                
                
System.out.println("Key-->" + ST.nextToken());
                
/* Son las claves */
               
            
}
            else
            {
                
System.out.println("Value-->" + ST.nextToken());
                
/* Son los valores */
            
}
            
i++;
        }
        
        return 
null;
        
    }
            
    public 
int getContentLength()
    {
        
StringTokenizer ST=new StringTokenizer(headerfields[2],":");
        
ST.nextToken();
    
        return 
Integer.parseInt(ST.nextToken().trim());       
    }  
} 
Y por ultimo la que debe implementar la URLStreamHandlerFactory

Código PHP:
public class MyappURLStreamHandlerFactory implements URLStreamHandlerFactory 
{
    final private static 
String defaultName = "org.protocol.";
    final private static 
String protocol = "myapp";
    
    public 
MyappURLStreamHandlerFactory() 
    {
        
createURLStreamHandler(protocol);
    }

    public 
URLStreamHandler createURLStreamHandler(String protocol) 
    {
        
URLStreamHandler handler;
        
String classPath = defaultName + protocol + ".Handler";
        
        
handler = tryClass(classPath);
        if (
handler != null) 
        {
            return 
handler;
        }
        else
        {
            return 
null;
        }    
    }

    private static 
URLStreamHandler tryClass(String theClass) {
        try 
        {
            Class 
cl = Class.forName(theClass);
            
Object obj = cl.newInstance();
            if (
obj instanceof URLStreamHandler) 
            {
                return (
URLStreamHandler) obj;
            }
        }
        catch (
UnsatisfiedLinkError e) {
        }
        catch (
ClassNotFoundException e) {
        }
        catch (
IllegalAccessException e) {
        }
        catch (
InstantiationException e) {
        }
      
        return 
null;
    }
    
} 
todo ello esta en el paquete: org.protocol.myapp

y para probar utilizo este main y todo correcto

Código PHP:
public static void main(String argv[]) throws Exception 
    
{ 
      
        try
        { 
            
// ESTABLEZCO MANUALMENTE EL PROTOCOLO myapp://
            
URL.setURLStreamHandlerFactory(new MyappURLStreamHandlerFactory()); 
            
            
// EJEMPLO DE URL
            
URL url=new URL("myapp://ventanaA/c|56789|def|hola");     
            
            
// OBTENER EL CONTENIDO DE LA URL
            
url.getContent();
        }
        catch(
Exception e)
        {
            
e.printStackTrace();
        }
    } 
Bueeno pues eso el problema es que cuando hago el .exe de mi aplicacion si abre mi programa pero no recibe los parametros de la url ni el valor de la ventana ni naaaaa.