private <T> T executeRequest(final HttpUriRequest request, final ResponseHandler<T> handler)
      throws IOException {
    final CloseableHttpClient client = createClientInstance();
    try {
      final CloseableHttpResponse response = client.execute(request);
      //  Wrap the response in a buffer to facilitate error handlers re-playing the content if the
      // response
      //  size is smaller than the max allowable buffer
      if (response.getEntity().getContentLength() >= 0
          && response.getEntity().getContentLength() < config.getMaxBufferSize()) {
        EntityUtils.updateEntity(response, new BufferedHttpEntity(response.getEntity()));
      }

      //  Explicit check for the authorization status of the API key
      if (response.getStatusLine().getStatusCode() == HttpStatus.SC_UNAUTHORIZED) {
        throw new HyppoAuthException(config);
      }

      try {
        log.debug(
            "{} - {} : {}",
            request.getMethod(),
            request.getURI().getPath(),
            response.getStatusLine().getStatusCode());
        return handler.handleResponse(response);
      } finally {
        IOUtils.closeQuietly(response);
      }
    } catch (Exception e) {
      log.error(
          "{} - {} : FAILED - {}", request.getMethod(), request.getURI().getPath(), e.toString());
      throw e;
    } finally {
      IOUtils.closeQuietly(client);
    }
  }
예제 #2
0
    public void run() {
      Contact destination = request.getDestination();
      logger.info(
          "Sending message of type "
              + request.getClass().getSimpleName()
              + " to "
              + destination.getIp()
              + ":"
              + destination.getPort());

      ObjectInputStream inStream = null;
      ObjectOutputStream outStream = null;
      Socket socket = new Socket();

      try {
        socket.connect(new InetSocketAddress(destination.getIp(), destination.getPort()));

        try {
          outStream = new ObjectOutputStream(socket.getOutputStream());
        } catch (IOException e) {
          logger.error("IOException occured while trying to open the socket outputStream");
          throw e;
        }

        logger.debug("Writing object " + request + " to socket output stream");
        outStream.writeObject(request);

        try {
          inStream = new ObjectInputStream(socket.getInputStream());
        } catch (IOException e) {
          logger.error("IOException occured while trying to open the socket inputStream");
          throw e;
        }

        Object readObj = null;

        try {
          logger.debug("Reading object from the socket input stream");
          readObj = inStream.readObject();
        } catch (IOException e) {
          logger.error("IOException occured while trying to read the object from the socket");
          throw e;
        } catch (ClassNotFoundException e) {
          logger.error(
              "ClassNotFoundException occured while trying to read object from the socket");
          throw e;
        }

        if (readObj instanceof Response) {
          Response response = (Response) readObj;
          logger.debug("Received response: " + response);
          handler.handleResponse(response);
        } else {
          logger.warn(
              "Object read from the socket is of an unsupported type (not instance of "
                  + Response.class
                  + "): "
                  + readObj.getClass());
        }

      } catch (Exception e) {
        logger.error(null, e);
      } finally {
        closeResources(socket, inStream, outStream);
      }
    }