예제 #1
0
  /**
   * Get the response from the URL
   *
   * @param url
   * @return
   * @throws TrailerAddictException
   */
  private static DigestedResponse getResponse(String url) throws TrailerAddictException {
    final DigestedResponse response;
    try {
      HttpGet httpGet = new HttpGet(url);
      httpGet.addHeader("accept", "application/xml");
      response = DigestedResponseReader.requestContent(HTTP_CLIENT, httpGet, CHARSET);

      if (response.getStatusCode() >= 500) {
        throw new TrailerAddictException(ApiExceptionType.HTTP_503_ERROR, url);
      } else if (response.getStatusCode() >= 300) {
        throw new TrailerAddictException(ApiExceptionType.HTTP_404_ERROR, url);
      }
    } catch (IOException ex) {
      throw new TrailerAddictException(
          ApiExceptionType.CONNECTION_ERROR, "Unable to read URL", url, ex);
    }
    return response;
  }
예제 #2
0
  /**
   * Get a DOM document from the supplied URL
   *
   * @param url
   * @return
   * @throws TrailerAddictException
   */
  public static synchronized Document getEventDocFromUrl(String url) throws TrailerAddictException {
    final DigestedResponse response = getResponse(url);

    try (InputStream in =
        new ByteArrayInputStream(response.getContent().getBytes(DEFAULT_CHARSET))) {
      DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
      DocumentBuilder db = dbf.newDocumentBuilder();

      Document doc = db.parse(in);
      doc.getDocumentElement().normalize();
      return doc;
    } catch (UnsupportedEncodingException ex) {
      throw new TrailerAddictException(
          ApiExceptionType.INVALID_URL, "Unable to encode URL", url, ex);
    } catch (ParserConfigurationException | SAXException | IOException error) {
      throw new TrailerAddictException(
          ApiExceptionType.MAPPING_FAILED, UNABLE_TO_PARSE, url, error);
    }
  }