hc-httpclient-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Giuseppe De Marco <peppeli...@yahoo.it>
Subject RouterTracker deadlock inside java web applet?
Date Wed, 19 Jan 2011 17:06:16 GMT
Hi all,

I wrote a simple class to store a jpeg file to a remote server, via Http Post 
method, using the code from the Tutorial.
It works very fine, so I found httpclient 4.03 very interesting.

This is the code, it's very simple (without imports):

<CODE>
public class PostAFile_cleaned {
    private static String tmp_dir = System.getProperty("java.io.tmpdir");

    public static void main(String[] args) throws IOException {
        DefaultHttpClient httpclient = new DefaultHttpClient();

        String url = args[0];
        String[] indirizzo = url.split("/");
        String file_inviabile = tmp_dir + "/" + "scansione_imageJ_"+ 
indirizzo[4] +"_" + indirizzo[5] +".jpg";
  
        System.out.println(url);

        System.out.println("instanziamento POST");        
        HttpPost httpost = new HttpPost(url);
        System.out.println("Post instanziato");
        
        // [Tipo Dato] [tipo dato dei dati lista] nomelista = nuovo oggetto 
ArrayList con eleenti NameValuePairs :)
        List <NameValuePair> nvps = new ArrayList <NameValuePair>();

        // Aggiungo il post invisibile del csrf token, gli headers e via dicendo
        httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
        
        // Apro il file e costruisco il tipo di dato adatto
        File file = new File(file_inviabile);        
        MultipartEntity mpEntity = new MultipartEntity();
        ContentBody cbFile = new FileBody(file, "image/jpeg");
        mpEntity.addPart("file", cbFile);
        
        System.out.println("Lunghezza file = " + file.length());
        httpost.setEntity(mpEntity);

        HttpResponse response = httpclient.execute(httpost);

        HttpEntity entity = response.getEntity();    

        if (entity != null) {
                    entity.consumeContent();
                }
        System.out.println("79        entity.consumeContent(); ");
        System.out.println("executing request " + httpost.getRequestLine());
        System.out.println("response.getStatusLine: " + 
response.getStatusLine());
    }
}
</CODE>

as input arguments I use the Url where the POST will be, from this value I 
create a temporary file used for upload, but it doesn't interest to us. From 
command line everything works really fine ! I used this code to create a 
Prototipe of my needs.

Now I integrated this very simple procedure inside a ImageJ plugin, in a class 
that implements Plugin, here is the code:

<CODE>

public class Aquarius_Plugin implements PlugIn {
    static boolean showArgs = true;
    private Applet applet = IJ.getApplet();
    private String Url_path = String.valueOf(applet.getCodeBase());
    private String url_completo = 
java.util.Arrays.toString(Url_path.split("/"));
    private String[] url_splittato = Url_path.split("/");            

//    int length = Url_path.length();
    
    public void run(String arg) {                
        String msg = "";
        String tmp_dir = "";
        String Line_Separator = System.getProperty("file.separator");
        
        if (arg.equals("run"))
            msg = "Allega Scansione";

            tmp_dir = System.getProperty("java.io.tmpdir");
            
            String file_salvato = tmp_dir + "/" + "scansione_imageJ_"+ 
url_splittato[4] +"_" + url_splittato[5] +".jpg";
            IJ.saveAs("JPG", file_salvato);        

            String post_url = url_splittato[0] +"/" +  url_splittato[1]+ "/"+ 
url_splittato[2]+ "/"+ 
url_splittato[3]+"/"+url_splittato[4]+"/"+url_splittato[5]+"/"+"acquisisci_scansione/salva/";


            
            IJ.showMessage(post_url);

            IJ.showMessage(file_salvato);
            //IJ.showMessage(url_completo);            
            //IJ.showMessage(java.util.Arrays.toString(url_splittato));        
    

                        
             DefaultHttpClient httpclient = new DefaultHttpClient();
    
             ProxySelectorRoutePlanner routePlanner = new 
ProxySelectorRoutePlanner(
             httpclient.getConnectionManager().getSchemeRegistry(), 
ProxySelector.getDefault()); 

            
             httpclient.setRoutePlanner(routePlanner);            
            
            try {
/*
                IJ.showMessage("GET");
                HttpUriRequest request = new HttpGet("http://192.168.0.23/");
                HttpResponse response = httpclient.execute(request);
                IJ.showMessage("response GET ok !");
*/
            
            
                IJ.showMessage("Avvio procedura salvataggio Scansione...");
            
                String url = post_url;
                String[] indirizzo = url.split("/");
                String file_inviabile = tmp_dir + "/" + "scansione_imageJ_"+ 
indirizzo[4] +"_" + indirizzo[5] +".jpg";
            
                // Prima faccio il GET per recuperare il token csrf
                // HttpGet httpget = new HttpGet(url);
                
                IJ.showMessage("Mi connetto qui:" + url);

                IJ.showMessage("instanziamento POST");        
                HttpPost httpost = new HttpPost(url);
                IJ.showMessage("Post instanziato");
                
                // [Tipo Dato] [tipo dato dei dati lista] nomelista = nuovo 
oggetto ArrayList con eleenti NameValuePairs :)
                List <NameValuePair> nvps = new ArrayList <NameValuePair>();
            
                // Aggiungo il post invisibile del csrf token, gli headers e via 

dicendo
                httpost.setEntity(new UrlEncodedFormEntity(nvps, 
HTTP.UTF_8));    

                
                // Apro il file e costruisco il tipo di dato adatto
                    File file = new File(file_inviabile);        
                    MultipartEntity mpEntity = new MultipartEntity();
                    ContentBody cbFile = new FileBody(file, "image/jpeg");
                    mpEntity.addPart("file", cbFile);
                    
                    
                IJ.showMessage("Lunghezza file = " + file.length());
                httpost.setEntity(mpEntity);
                IJ.showMessage("70        httpost.setEntity(mpEntity);");
                
                HttpResponse response = httpclient.execute(httpost);
                IJ.showMessage("74        HttpResponse response = 
httpclient.execute(httpost);");
                
                HttpEntity entity = response.getEntity();    
                IJ.showMessage("76        response.getEntity(); ");
            
                if (entity != null) {
                        entity.consumeContent();
                    }
                IJ.showMessage("79        entity.consumeContent(); ");
                IJ.showMessage("executing request " + httpost.getRequestLine());
                IJ.showMessage("response.getStatusLine: " + 
response.getStatusLine());
            }
            catch (Exception e){
                e.printStackTrace();
                String a = e.toString();
                IJ.showMessage(a);
                IJ.showMessage("mannaia");
            
            }
            } // fine public  void run 
}
</CODE>

Now the question.
As you could simply read by the code theese procedure are identicals, but the 
web applet fails on:

                HttpResponse response = httpclient.execute(httpost);

But No one Exception is raised, from the server I know that the last called 
class, by my plugin, is:

"GET 
/protocollo/0/382/scansiona/imageJA_web_embedded/org/apache/http/conn/routing/RouteTracker.class

HTTP/1.1"

If I change the url host, to another from the same address where the applet was 
downloaded, the connection goes fine. If I leave the host as I need, pointing it 

to the Httpd Address, all remains mute. No connection time out, no exceptions...

The applet is signed, java.policy were modified to permits everything, Paths 
were configured and tested.

It seems a problem of Routing to the httpd address, where the applet was 
downloaded from. Or what?

If someone could help me I'll appreciate,
tomorrow morning I'll try to use Http Client 3.1 because I read that in 3.1 
version Routing it's not so evolute.

Anticipate Thanks
giuseppe



      

---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org


Mime
View raw message