Exemple #1
0
  @Test
  public void testDummyProvider() throws Exception {
    ClientResponse resp1 = ldclient.retrieveResource("http://localhost/resource1");
    RepositoryConnection con1 = ModelCommons.asRepository(resp1.getData()).getConnection();
    try {
      con1.begin();
      Assert.assertEquals(3, con1.size());
      con1.commit();
    } finally {
      con1.close();
    }

    ClientResponse resp2 = ldclient.retrieveResource("http://localhost/resource2");
    RepositoryConnection con2 = ModelCommons.asRepository(resp2.getData()).getConnection();
    try {
      con2.begin();
      Assert.assertEquals(2, con2.size());
      con2.commit();
    } finally {
      con2.close();
    }

    ClientResponse resp3 = ldclient.retrieveResource("http://localhost/resource3");
    RepositoryConnection con3 = ModelCommons.asRepository(resp3.getData()).getConnection();
    try {
      con3.begin();
      Assert.assertEquals(2, con3.size());
      con3.commit();
    } finally {
      con3.close();
    }
  }
Exemple #2
0
 @After
 public void shutdownClient() {
   ldclient.shutdown();
 }
  /**
   * Retrieve the data for a resource using the given http client and endpoint definition. The
   * service is supposed to manage the connection handling itself. See {@link AbstractHttpProvider}
   * for a generic implementation of this method.
   *
   * @param resource the resource to be retrieved
   * @param endpoint the endpoint definition
   * @return a completely specified client response, including expiry information and the set of
   *     triples
   */
  @Override
  public ClientResponse retrieveResource(String resource, LDClientService client, Endpoint endpoint)
      throws DataRetrievalException {

    try {

      String contentType;
      if (endpoint != null && endpoint.getContentTypes().size() > 0) {
        contentType =
            CollectionUtils.fold(
                endpoint.getContentTypes(),
                new CollectionUtils.StringSerializer<ContentType>() {
                  @Override
                  public String serialize(ContentType contentType) {
                    return contentType.toString("q");
                  }
                },
                ",");
      } else {
        contentType = CollectionUtils.fold(Arrays.asList(listMimeTypes()), ",");
      }

      long defaultExpires = client.getClientConfiguration().getDefaultExpiry();
      if (endpoint != null && endpoint.getDefaultExpiry() != null) {
        defaultExpires = endpoint.getDefaultExpiry();
      }

      final ResponseHandler handler = new ResponseHandler(resource, endpoint);

      // a queue for queuing the request URLs needed to build the query response
      Queue<String> requestUrls = new LinkedList<String>();
      requestUrls.addAll(buildRequestUrl(resource, endpoint));

      Set<String> visited = new HashSet<String>();

      String requestUrl = requestUrls.poll();
      while (requestUrl != null) {

        if (!visited.contains(requestUrl)) {
          HttpGet get = new HttpGet(requestUrl);
          try {
            get.setHeader("Accept", contentType);
            get.setHeader("Accept-Language", "*"); // PoolParty compatibility

            log.info(
                "retrieving resource data for {} from '{}' endpoint, request URI is <{}>",
                new Object[] {resource, getName(), get.getURI().toASCIIString()});

            handler.requestUrl = requestUrl;
            List<String> additionalRequestUrls = client.getClient().execute(get, handler);
            requestUrls.addAll(additionalRequestUrls);

            visited.add(requestUrl);
          } finally {
            get.releaseConnection();
          }
        }

        requestUrl = requestUrls.poll();
      }

      Date expiresDate = handler.expiresDate;
      if (expiresDate == null) {
        expiresDate = new Date(System.currentTimeMillis() + defaultExpires * 1000);
      }

      long min_expires =
          System.currentTimeMillis() + client.getClientConfiguration().getMinimumExpiry() * 1000;
      if (expiresDate.getTime() < min_expires) {
        log.info(
            "expiry time returned by request lower than minimum expiration time; using minimum time instead");
        expiresDate = new Date(min_expires);
      }

      if (log.isInfoEnabled()) {
        RepositoryConnection con = handler.triples.getConnection();
        log.info(
            "retrieved {} triples for resource {}; expiry date: {}",
            new Object[] {con.size(), resource, expiresDate});
        con.close();
      }

      ClientResponse result = new ClientResponse(handler.httpStatus, handler.triples);
      result.setExpires(expiresDate);
      return result;
    } catch (RepositoryException e) {
      log.error("error while initialising Sesame repository; classpath problem?", e);
      throw new DataRetrievalException(
          "error while initialising Sesame repository; classpath problem?", e);
    } catch (ClientProtocolException e) {
      log.error(
          "HTTP client error while trying to retrieve resource {}: {}", resource, e.getMessage());
      throw new DataRetrievalException(
          "I/O error while trying to retrieve resource " + resource, e);
    } catch (IOException e) {
      log.error("I/O error while trying to retrieve resource {}: {}", resource, e.getMessage());
      throw new DataRetrievalException(
          "I/O error while trying to retrieve resource " + resource, e);
    } catch (RuntimeException ex) {
      log.error(
          "Unknown error while trying to retrieve resource {}: {}", resource, ex.getMessage());
      throw new DataRetrievalException(
          "Unknown error while trying to retrieve resource " + resource, ex);
    }
  }