/**
   * (non-javadoc)
   *
   * @see CommunicationsClientConnection#requestListComponentRegistered(PlatformComponentProfile,
   *     DiscoveryQueryParameters)
   */
  @Override
  public void requestListComponentRegistered(
      PlatformComponentProfile networkServiceApplicant,
      DiscoveryQueryParameters discoveryQueryParameters)
      throws CantRequestListException {

    try {

      System.out.println("WsCommunicationsCloudClientConnection - requestListComponentRegistered");

      /*
       * Validate parameter
       */
      if (discoveryQueryParameters == null) {

        throw new IllegalArgumentException(
            "The discoveryQueryParameters is required, can not be null");
      }

      Gson gson = new Gson();
      JsonObject jsonObject = new JsonObject();
      jsonObject.addProperty(
          JsonAttNamesConstants.NETWORK_SERVICE_TYPE,
          networkServiceApplicant.getNetworkServiceType().toString());
      jsonObject.addProperty(
          JsonAttNamesConstants.DISCOVERY_PARAM, discoveryQueryParameters.toJson());

      /*
       * Construct a fermat packet whit the filters
       */
      FermatPacket fermatPacketRespond =
          FermatPacketCommunicationFactory.constructFermatPacketEncryptedAndSinged(
              wsCommunicationsCloudClientChannel.getServerIdentity(), // Destination
              wsCommunicationsCloudClientChannel.getClientIdentity().getPublicKey(), // Sender
              gson.toJson(jsonObject), // Message Content
              FermatPacketType.REQUEST_LIST_COMPONENT_REGISTERED, // Packet type
              wsCommunicationsCloudClientChannel
                  .getClientIdentity()
                  .getPrivateKey()); // Sender private key

      /*
       * Send the encode packet to the server
       */
      wsCommunicationsCloudClientChannel.send(FermatPacketEncoder.encode(fermatPacketRespond));

    } catch (Exception e) {

      CantRequestListException pluginStartException =
          new CantRequestListException(
              CantRequestListException.DEFAULT_MESSAGE,
              e,
              e.getLocalizedMessage(),
              e.getLocalizedMessage());
      throw pluginStartException;
    }
  }
  /**
   * Method that request to the communication cloud server the list of component registered that
   * match whit the discovery query params
   *
   * @param discoveryQueryParameters
   * @throws CantRequestListException this exception means the list receive is empty or a internal
   *     error
   */
  public List<PlatformComponentProfile> requestListComponentRegisteredSocket(
      DiscoveryQueryParameters discoveryQueryParameters) throws CantRequestListException {

    System.out.println(
        "WsCommunicationsCloudClientConnection - new requestListComponentRegistered");
    List<PlatformComponentProfile> resultList = new ArrayList<>();
    Socket clientConnect = null;
    BufferedReader bufferedReader = null;
    PrintWriter printWriter = null;

    try {

      /*
       * Validate parameter
       */
      if (discoveryQueryParameters == null) {
        throw new IllegalArgumentException(
            "The discoveryQueryParameters is required, can not be null");
      }

      /*
       * Construct a jsonObject whit the parameters
       */
      Gson gson = new Gson();
      JsonObject jsonObject = new JsonObject();
      jsonObject.addProperty(
          JsonAttNamesConstants.NAME_IDENTITY,
          wsCommunicationsCloudClientChannel.getIdentityPublicKey());
      jsonObject.addProperty(
          JsonAttNamesConstants.DISCOVERY_PARAM, discoveryQueryParameters.toJson());

      clientConnect = new Socket(WsCommunicationsCloudClientPluginRoot.SERVER_IP, 9001);
      bufferedReader = new BufferedReader(new InputStreamReader(clientConnect.getInputStream()));

      printWriter = new PrintWriter(clientConnect.getOutputStream());
      printWriter.println(gson.toJson(jsonObject));
      printWriter.flush();

      String respondServer = bufferedReader.readLine();

      if (respondServer != null
          && respondServer != ""
          && respondServer.contains(JsonAttNamesConstants.RESULT_LIST)) {

        /*
         * Decode into a json object
         */
        JsonParser parser = new JsonParser();
        JsonObject respondJsonObject = (JsonObject) parser.parse(respondServer);

        /*
         * Get the receivedList
         */
        resultList =
            gson.fromJson(
                respondJsonObject.get(JsonAttNamesConstants.RESULT_LIST).getAsString(),
                new TypeToken<List<PlatformComponentProfileCommunication>>() {}.getType());

        System.out.println(
            "WsCommunicationsCloudClientConnection - resultList.size() = " + resultList.size());
      }

      bufferedReader.close();
      printWriter.close();
      clientConnect.close();

    } catch (Exception e) {

      try {

        if (clientConnect != null) {
          bufferedReader.close();
          printWriter.close();
          clientConnect.close();
        }

      } catch (Exception ex) {
      }

      e.printStackTrace();
      CantRequestListException cantRequestListException =
          new CantRequestListException(
              CantRequestListException.DEFAULT_MESSAGE,
              e,
              e.getLocalizedMessage(),
              e.getLocalizedMessage());
      throw cantRequestListException;
    }

    return resultList;
  }
  /**
   * (non-javadoc)
   *
   * @see CommunicationsClientConnection#requestListComponentRegistered(DiscoveryQueryParameters)
   */
  @Override
  public List<PlatformComponentProfile> requestListComponentRegistered(
      DiscoveryQueryParameters discoveryQueryParameters) throws CantRequestListException {

    System.out.println(
        "WsCommunicationsCloudClientConnection - new requestListComponentRegistered");
    List<PlatformComponentProfile> resultList = new ArrayList<>();

    try {

      /*
       * Validate parameter
       */
      if (discoveryQueryParameters == null) {
        throw new IllegalArgumentException(
            "The discoveryQueryParameters is required, can not be null");
      }

      /*
       * Construct a jsonObject whit the parameters
       */
      Gson gson = new Gson();
      JsonObject jsonObject = new JsonObject();
      jsonObject.addProperty(
          JsonAttNamesConstants.NAME_IDENTITY,
          wsCommunicationsCloudClientChannel.getIdentityPublicKey());
      jsonObject.addProperty(
          JsonAttNamesConstants.DISCOVERY_PARAM, discoveryQueryParameters.toJson());

      // Create a new RestTemplate instance
      HttpHeaders requestHeaders = new HttpHeaders();
      requestHeaders.set("Connection", "Close");
      requestHeaders.setAccept(
          Collections.singletonList(new org.springframework.http.MediaType("application", "json")));

      HttpEntity<?> requestEntity = new HttpEntity<Object>(jsonObject.toString(), requestHeaders);
      RestTemplate restTemplate = new RestTemplate();
      restTemplate
          .getMessageConverters()
          .add(0, new StringHttpMessageConverter(Charset.forName("UTF-8")));

      ResponseEntity<String> responseEntity =
          restTemplate.exchange(WEB_SERVICE_URL, HttpMethod.POST, requestEntity, String.class);

      String respond = responseEntity.getBody();
      // System.out.println("responseEntity = " + respond);

      /*
       * if respond have the result list
       */
      if (respond.contains(JsonAttNamesConstants.RESULT_LIST)) {

        /*
         * Decode into a json object
         */
        JsonParser parser = new JsonParser();
        JsonObject respondJsonObject = (JsonObject) parser.parse(respond.toString());

        /*
         * Get the receivedList
         */
        resultList =
            gson.fromJson(
                respondJsonObject.get(JsonAttNamesConstants.RESULT_LIST).getAsString(),
                new TypeToken<List<PlatformComponentProfileCommunication>>() {}.getType());

        System.out.println(
            "WsCommunicationsCloudClientConnection - resultList.size() = " + resultList.size());

      } else {
        System.out.println(
            "WsCommunicationsCloudClientConnection - Requested list is not available, resultList.size() = "
                + resultList.size());
      }

    } catch (Exception e) {
      e.printStackTrace();
      CantRequestListException cantRequestListException =
          new CantRequestListException(
              CantRequestListException.DEFAULT_MESSAGE,
              e,
              e.getLocalizedMessage(),
              e.getLocalizedMessage());
      throw cantRequestListException;
    }

    return resultList;
  }