Ejemplo n.º 1
0
 public static Connection getInstance(
     MerchantConfig mc, DocumentBuilder builder, LoggerWrapper logger) {
   if (mc.getUseHttpClient()) {
     return new HttpClientConnection(mc, builder, logger);
   }
   // HttpClient is not set in properties file then JDKHttpURLConnection class instance.
   return new JDKHttpURLConnection(mc, builder, logger);
 }
Ejemplo n.º 2
0
  /**
   * Validate the Http response for any faults returned from the server.
   *
   * @throws FaultException
   * @throws ClientException
   */
  private void checkForFault() throws FaultException, ClientException {
    try {
      logger.log(Logger.LT_INFO, "Reading response...");

      int responseCode = getHttpResponseCode();

      // if successful, there's nothing left to do here.
      // we'll process the response in a later method.
      if (responseCode == HttpURLConnection.HTTP_OK) return;

      InputStream errorStream = getResponseErrorStream();

      // if there is no error stream, then it is not a fault
      if (errorStream == null) {
        throw new ClientException(responseCode, logger);
      }

      // read error stream into a byte array
      byte[] errorBytes;
      try {
        errorBytes = Utility.read(errorStream);
        errorStream.close();
      } catch (IOException ioe) {
        throw new ClientException(
            responseCode, "Failed to read additional HTTP error", true, logger);
      }

      // server will return HTTP 500 on a fault
      if (responseCode == HttpURLConnection.HTTP_INTERNAL_ERROR) {
        try {
          ByteArrayInputStream bais = new ByteArrayInputStream(errorBytes);

          Document faultDoc = builder.parse(bais);
          bais.close();

          throw new FaultException(faultDoc, mc.getEffectiveNamespaceURI(), logger);

        } catch (IOException ioe) {
          // If an IO exception occurs, we're not sure whether
          // or not it was a fault.  So we mark it as critical.
          String text = new String(errorBytes);
          throw new ClientException(responseCode, text, true, logger);
        } catch (SAXException ioe) {
          // If parsing fails, it means it's not a fault after all.
          String text = new String(errorBytes);
          throw new ClientException(responseCode, text, logger);
        }
      } else {
        // non-500 return codes are definitely not faults
        String text = new String(errorBytes);
        throw new ClientException(responseCode, text, logger);
      }
    }
    // catch other IOException's that we have not already handled.
    catch (IOException e) {
      throw new ClientException(e, true, logger);
    }
  }