Send a command to the server. Collect all responses until either the corresponding command completion response or a BYE response (indicating server failure). Return all the collected responses. @param command the command @param args the arguments @return array of Response objects returned by the
(String command, Argument args)
| 348 | * @return array of Response objects returned by the server |
| 349 | */ |
| 350 | public synchronized Response[] command(String command, Argument args) { |
| 351 | if (socket == null) |
| 352 | return new Response[]{Response.byeResponse(new SocketException("disconnected"))}; |
| 353 | if ("LOGOUT".equals(command)) |
| 354 | try { |
| 355 | socket.setSoTimeout(10 * 1000); |
| 356 | } catch (SocketException ex) { |
| 357 | eu.faircode.email.Log.e(ex); |
| 358 | } |
| 359 | |
| 360 | commandStart(command); |
| 361 | List<Response> v = new ArrayList<>(); |
| 362 | boolean done = false; |
| 363 | String tag = null; |
| 364 | |
| 365 | // write the command |
| 366 | try { |
| 367 | tag = writeCommand(command, args); |
| 368 | } catch (LiteralException lex) { |
| 369 | v.add(lex.getResponse()); |
| 370 | done = true; |
| 371 | } catch (Exception ex) { |
| 372 | // Convert this into a BYE response |
| 373 | v.add(Response.byeResponse(ex)); |
| 374 | done = true; |
| 375 | } |
| 376 | |
| 377 | Response byeResp = null; |
| 378 | while (!done) { |
| 379 | Response r = null; |
| 380 | try { |
| 381 | r = readResponse(); |
| 382 | } catch (IOException ioex) { |
| 383 | if (byeResp == null) // convert this into a BYE response |
| 384 | byeResp = Response.byeResponse(ioex); |
| 385 | // else, connection closed after BYE was sent |
| 386 | break; |
| 387 | } catch (ProtocolException pex) { |
| 388 | logger.log(Level.FINE, "ignoring bad response", pex); |
| 389 | continue; // skip this response |
| 390 | } |
| 391 | |
| 392 | if (r.isBYE()) { |
| 393 | byeResp = r; |
| 394 | continue; |
| 395 | } |
| 396 | |
| 397 | v.add(r); |
| 398 | |
| 399 | // If this is a matching command completion response, we are done |
| 400 | if (r.isTagged() && r.getTag().equals(tag)) |
| 401 | done = true; |
| 402 | } |
| 403 | |
| 404 | if (byeResp != null) |
| 405 | v.add(byeResp); // must be last |
| 406 | Response[] responses = new Response[v.size()]; |
| 407 | v.toArray(responses); |
no test coverage detected