2 Replies Latest reply on Nov 26, 2012 9:02 PM by sannsims

    Unable to communicate between client and Rest server - using User Guide Client Code

    sannsims

      Hello again.  Thanks for reading my post. 

       

      I have my application running on port 8080 communicate with the rest server running on port 8280.  I'm unable to get both of them communicating with each other.  I am using the client side code (mostly) from the User Guide found here: https://docs.jboss.org/author/display/ISPN/Infinispan+REST+Server to access the data on the server, see code below.  When I execute the testRestServerPut() method, the output from the permission shows action: "connect,resolve" and name: localhost:8280, the response code and message produces "405 Method Not allowed" respectively.  When the testRestServerGet() method is executed, a java.io.IOException is thrown:  "Server returned HTTP response code: 405 for URL: http://localhost:8280/infinispan-server-rest/rest/_defaultcache/" (see below for stacktrace).

       

      I am extremely frustrated, I've made very little progress and about to give up on Infinispan cache.  If someone can PLEASE point out the error in the code below OR if you have some code to share that will help me, PLEASE, PLEASE let me know?

       

      Thanks!

       

      private boolean testRestServerPut(){
      
      boolean isSuccessful = true;String value = 
      "Hello";
      
      try {
      URL url = 
      new URL( "http://localhost:8280/infinispan-server-rest/rest/_defaultcache/" );HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 
      Permission p = conn.getPermission();
      
      if( p != null ){
      System.
      out.println( "Permission: " + p.getActions() + " " + p.getName() );}
      conn.setRequestMethod( 
      "PUT" );conn.setRequestProperty( 
      "Content-Type", "text/plain" );conn.setDoOutput( 
      true );conn.connect();
      
      OutputStreamWriter writer = new OutputStreamWriter( conn.getOutputStream() );
      writer.write( value );
      writer.flush();
      
      System.
      out.println( "PUT " + conn.getResponseCode() + " " + conn.getResponseMessage() );conn.disconnect();
      }
      
      catch( Exception ex ){
      isSuccessful = 
      false;System.
      out.println( "ERROR: testRestServerExamplePUT" ); ex.printStackTrace();
      }
      
      return isSuccessful;}
      
      
      private boolean testRestServerGet(){
      
      boolean isSuccessful = true;
      
      try {
      URL url = 
      new URL( "http://localhost:8280/infinispan-server-rest/rest/_defaultcache/" );HttpURLConnection conn = (HttpURLConnection) url.openConnection();
      
      conn.setRequestMethod( 
      "GET" );conn.setRequestProperty( 
      "Content-Type", "text/plain" );conn.setDoOutput( 
      true );conn.connect();
      System.
      out.println( conn.getResponseCode() + " " + conn.getResponseMessage() );
      String x = streamToString( conn.getInputStream() );
      System.
      out.println( "Executed GET method: " + x );
      conn.disconnect();
      }
      
      catch( Exception ex ){
      isSuccessful = 
      false;System.
      out.println( "ERROR: testRestServerExampleGET" );ex.printStackTrace();
      }
      
      return isSuccessful;}
      
      
      private String streamToString(InputStream in) throws IOException {
      StringBuilder out = 
      new StringBuilder();BufferedReader br = 
      new BufferedReader( new InputStreamReader(in) );
      for( String line = br.readLine(); line != null; line = br.readLine() ){
      out.append(line);
      }
      br.close();
      
      return out.toString();}
      

       

      By the way, how do I insert my code so the alignment isn't lost?