title: Wait for Response on REST Request JAVA
body: <p>I created a java project with glassfish and posted a simple REST GET service like this:</p>
  
  <pre><code>@Path("/get") 
  public class Rest {
       @Path("test/{user}/")
       @GET
       public String getTest(@PathParam("user") String id) throws IOException {
          //send message to websocket client and wait for response
  
           //return "websocket client response";
       }
  }
  </code></pre>
  
  <p>this works fine.</p>
  
  <p>I also have a websocket server implementation in the same project. This implementation allows me to send data to the connected clients.</p>
  
  <p>This is my WebSocket implementation:</p>
  
  <pre><code>@ServerEndpoint("/websocket")
  public class WebSocketServer {
  
  
      @OnOpen
      public void onOpen(Session userSession){
          System.out.println("Se conecto un nuevo cliente");
  
          Modelo.getInstance().users.add(userSession);
  
      }
      @OnMessage
      public void onMessage(String message,Session userSession) throws IOException{
          String username=(String) userSession.getUserProperties().get("username");
  
          if(username==null){
              userSession.getUserProperties().put("username", message);
              userSession.getBasicRemote().sendText(Modelo.getInstance().buildJsonData("Servidor","nuevo cliente conectado como: "+message));
  
          }else{
              Iterator&lt;Session&gt; iterator=Modelo.getInstance().users.iterator();
              while(iterator.hasNext()){
                  iterator.next().getBasicRemote().sendText(Modelo.getInstance().buildJsonData(username,message));
              }
          }
      }
      @OnClose
      public void onClose(Session userSession){
          Modelo.getInstance().users.remove(userSession);
  
      }
      @OnError
      public void onError(Throwable t){
          t.printStackTrace();
      }
  }
  </code></pre>
  
  <p>this works fine too. </p>
  
  <p>When the REST method is called i can send successfully a message to one of my websockets clients.</p>
  
  <p>The thing is that i want to return as the REST response, the data that the WebSocket client sends me.</p>
  
  <p>So...
  1)Receive REST GET request in Java Server
  2)Send via websocket to the client i want to get the info from
  3)Respond the REST GET request with the message the websocket client send me.</p>
  
  <p>How can i accomplish this?</p>
  
  <p>[SOLVED]?</p>
  
  <p>I found a way to do this, please i would like to know what do you think.</p>
  
  <p>I found this article: <a href="https://jersey.java.net/documentation/latest/async.html#d0e8611" rel="nofollow">here</a> about async rest reponses.</p>
  
  <p>So i implemented, its the first thing come to my mind, i save the websocket client message in an array, and the REST request is responded when the array has a message.</p>
  
  <pre><code>@Path("/resource")
       @GET
       public void asyncGet(@Suspended final AsyncResponse asyncResponse) throws IOException {
           Modelo.getInstance().enviarMensaje("5", "escenas");
  
           new Thread(new Runnable() {
               @Override
               public void run() {
                   String result = veryExpensiveOperation();
                   asyncResponse.resume(result);
               }
  
               private String veryExpensiveOperation() {
                  while(Modelo.getInstance().responses.size()==0){
  
                  }
                  String result=Modelo.getInstance().responses.get(0);
                  Modelo.getInstance().responses.clear();
                  return result;
                   // ... very expensive operation
               }
           }).start();
       }
  </code></pre>
  
  <p>I know there a more things to validate this reponses, but at first it works.</p>
  
  <p>I also edit the websockerserver.java to save in the array the response.</p>
  
  <p>Thank you very much</p>