org.sonatype.nexus.repository.httpclient.HttpClientFacet Java Examples

The following examples show how to use org.sonatype.nexus.repository.httpclient.HttpClientFacet. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AptProxyFacet.java    From nexus-repository-apt with Eclipse Public License 1.0 5 votes vote down vote up
private Optional<SnapshotItem> fetchLatest(ContentSpecifier spec) throws IOException {
  AptFacet aptFacet = getRepository().facet(AptFacet.class);
  ProxyFacet proxyFacet = facet(ProxyFacet.class);
  HttpClientFacet httpClientFacet = facet(HttpClientFacet.class);
  HttpClient httpClient = httpClientFacet.getHttpClient();
  CacheController cacheController = cacheControllerHolder.getMetadataCacheController();
  CacheInfo cacheInfo = cacheController.current();
  Content oldVersion = aptFacet.get(spec.path);

  URI fetchUri = proxyFacet.getRemoteUrl().resolve(spec.path);
  HttpGet getRequest = buildFetchRequest(oldVersion, fetchUri);

  HttpResponse response = httpClient.execute(getRequest);
  StatusLine status = response.getStatusLine();

  if (status.getStatusCode() == HttpStatus.SC_OK) {
    HttpEntity entity = response.getEntity();
    Content fetchedContent = new Content(new HttpEntityPayload(response, entity));
    AttributesMap contentAttrs = fetchedContent.getAttributes();
    contentAttrs.set(Content.CONTENT_LAST_MODIFIED, getDateHeader(response, HttpHeaders.LAST_MODIFIED));
    contentAttrs.set(Content.CONTENT_ETAG, getQuotedStringHeader(response, HttpHeaders.ETAG));
    contentAttrs.set(CacheInfo.class, cacheInfo);
    Content storedContent = getAptFacet().put(spec.path, fetchedContent);
    return Optional.of(new SnapshotItem(spec, storedContent));
  }

  try {
    if (status.getStatusCode() == HttpStatus.SC_NOT_MODIFIED) {
      checkState(oldVersion != null, "Received 304 without conditional GET (bad server?) from %s", fetchUri);
      doIndicateVerified(oldVersion, cacheInfo, spec.path);
      return Optional.of(new SnapshotItem(spec, oldVersion));
    }
    throwProxyExceptionForStatus(response);
  }
  finally {
    HttpClientUtils.closeQuietly(response);
  }

  return Optional.empty();
}
 
Example #2
Source File: OrientNpmProxyFacet.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Execute http client request.
 */
protected HttpResponse execute(final Context context, final HttpClient client, final HttpRequestBase request)
    throws IOException
{
  String bearerToken = getRepository().facet(HttpClientFacet.class).getBearerToken();
  if (StringUtils.isNotBlank(bearerToken)) {
    request.setHeader("Authorization", "Bearer " + bearerToken);
  }
  return super.execute(context, client, request);
}
 
Example #3
Source File: AptProxyFacet.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
private Optional<SnapshotItem> fetchLatest(final ContentSpecifier spec) throws IOException {
  AptFacet aptFacet = getRepository().facet(AptFacet.class);
  ProxyFacet proxyFacet = facet(ProxyFacet.class);
  HttpClientFacet httpClientFacet = facet(HttpClientFacet.class);
  HttpClient httpClient = httpClientFacet.getHttpClient();
  CacheController cacheController = cacheControllerHolder.getMetadataCacheController();
  CacheInfo cacheInfo = cacheController.current();
  Content oldVersion = aptFacet.get(spec.path);

  URI fetchUri = proxyFacet.getRemoteUrl().resolve(spec.path);
  HttpGet getRequest = buildFetchRequest(oldVersion, fetchUri);

  HttpResponse response = httpClient.execute(getRequest);
  StatusLine status = response.getStatusLine();

  if (status.getStatusCode() == HttpStatus.SC_OK) {
    HttpEntity entity = response.getEntity();
    Content fetchedContent = new Content(new HttpEntityPayload(response, entity));
    AttributesMap contentAttrs = fetchedContent.getAttributes();
    contentAttrs.set(Content.CONTENT_LAST_MODIFIED, getDateHeader(response, HttpHeaders.LAST_MODIFIED));
    contentAttrs.set(Content.CONTENT_ETAG, getQuotedStringHeader(response, HttpHeaders.ETAG));
    contentAttrs.set(CacheInfo.class, cacheInfo);
    Content storedContent = getAptFacet().put(spec.path, fetchedContent);
    return Optional.of(new SnapshotItem(spec, storedContent));
  }

  try {
    if (status.getStatusCode() == HttpStatus.SC_NOT_MODIFIED) {
      checkState(oldVersion != null, "Received 304 without conditional GET (bad server?) from %s", fetchUri);
      doIndicateVerified(oldVersion, cacheInfo, spec.path);
      return Optional.of(new SnapshotItem(spec, oldVersion));
    }
    throwProxyExceptionForStatus(response);
  }
  finally {
    HttpClientUtils.closeQuietly(response);
  }

  return Optional.empty();
}
 
Example #4
Source File: ProxyFacetSupport.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@Override
protected void doStart() throws Exception {
  httpClient = facet(HttpClientFacet.class);

  if (remoteUrlChanged) {
    remoteUrlChanged = false;

    optionalFacet(NegativeCacheFacet.class).ifPresent((nfc) -> nfc.invalidate());
  }
}
 
Example #5
Source File: ProxyFacetSupportTest.java    From nexus-public with Eclipse Public License 1.0 4 votes vote down vote up
@PrepareForTest(HttpClientUtils.class)
@Test
public void leak() throws Exception {
  HttpClientFacet httpClientFacet = mock(HttpClientFacet.class);
  HttpClient httpClient = mock(HttpClient.class);
  ConfigurationFacet configurationFacet = mock(ConfigurationFacet.class);
  ProxyFacetSupport.Config config = new ProxyFacetSupport.Config();
  config.remoteUrl = new URI("http://example.com");

  when(repository.facet(HttpClientFacet.class)).thenReturn(httpClientFacet);
  when(httpClientFacet.getHttpClient()).thenReturn(httpClient);
  when(repository.facet(ConfigurationFacet.class)).thenReturn(configurationFacet);
  when(configurationFacet.readSection(any(Configuration.class), anyString(), eq(ProxyFacetSupport.Config.class)))
      .thenReturn(config);

  HttpResponse httpResponse = new BasicHttpResponse(
      new BasicHttpResponse(new ProtocolVersion("HTTP", 1, 1), 304, "NOT MODIFIED").getStatusLine());
  httpResponse.addHeader(BYPASS_HTTP_ERRORS_HEADER_NAME, BYPASS_HTTP_ERRORS_HEADER_VALUE);
  doReturn("http://example.com").when(underTest).getUrl(cachedContext);
  doReturn(httpResponse).when(underTest).execute(eq(cachedContext), eq(httpClient), any(HttpRequestBase.class));

  mockStatic(HttpClientUtils.class);

  Configuration configuration = mock(Configuration.class);
  when(configuration.attributes("proxy")).thenReturn(new NestedAttributesMap(
      "proxy",
      singletonMap("remoteUrl", "http://example.com")
  ));
  underTest.doConfigure(configuration);
  underTest.doStart();

  try {
    underTest.get(cachedContext);
    fail("Expected BypassHttpErrorException to be thrown");
  }
  catch (BypassHttpErrorException expected) {
    // expected
  }

  verifyStatic();
  HttpClientUtils.closeQuietly(httpResponse);
  verifyNoMoreInteractions(HttpClientUtils.class);
}