org.sonatype.nexus.repository.view.Request Java Examples

The following examples show how to use org.sonatype.nexus.repository.view.Request. 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: ComposerHostedUploadHandler.java    From nexus-repository-composer with Eclipse Public License 1.0 6 votes vote down vote up
@Nonnull
@Override
public Response handle(@Nonnull final Context context) throws Exception {
  String vendor = getVendorToken(context);
  String project = getProjectToken(context);
  String version = getVersionToken(context);

  Request request = checkNotNull(context.getRequest());
  Payload payload = checkNotNull(request.getPayload());

  Repository repository = context.getRepository();
  ComposerHostedFacet hostedFacet = repository.facet(ComposerHostedFacet.class);

  hostedFacet.upload(vendor, project, version, payload);
  return HttpResponses.ok();
}
 
Example #2
Source File: HandlerContributorTest.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@Test
public void addContributedHandlers() throws Exception {
  final AttributesMap attributes = new AttributesMap();

  final Request request = mock(Request.class);
  final Context context = mock(Context.class);

  when(context.getRequest()).thenReturn(request);
  when(context.getAttributes()).thenReturn(attributes);
  when(context.proceed()).thenReturn(HttpResponses.ok(new Content(new StringPayload("test", "text/plain"))));

  final ContributedHandler handlerA = mock(ContributedHandler.class);
  final ContributedHandler handlerB = mock(ContributedHandler.class);
  final HandlerContributor underTest = new HandlerContributor(asList(handlerA, handlerB));
  final Response response = underTest.handle(context);

  assertThat(response.getStatus().isSuccessful(), is(true));
  assertThat(attributes.get(HandlerContributor.EXTENDED_MARKER), is(Boolean.TRUE));

  // Handle a second time to ensure the contributed handlers aren't injected twice
  underTest.handle(context);

  ArgumentCaptor<ContributedHandler> handlerCaptor = ArgumentCaptor.forClass(ContributedHandler.class);
  verify(context, times(2)).insertHandler(handlerCaptor.capture());
  assertThat(handlerCaptor.getAllValues(), is(asList(handlerB, handlerA))); // Intentionally added in "reverse" order
}
 
Example #3
Source File: HttpConditions.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@Nullable
private static Predicate<Response> ifNoneMatch(final Request request) {
  final String match = request.getHeaders().get(HttpHeaders.IF_NONE_MATCH);
  if (match != null && !"*".equals(match)) {
    return new Predicate<Response>()
    {
      @Override
      public boolean apply(final Response response) {
        final String etag = response.getHeaders().get(HttpHeaders.ETAG);
        if (etag != null) {
          return !match.contains(etag);
        }
        return true;
      }

      @Override
      public String toString() {
        return HttpConditions.class.getSimpleName() + ".ifNoneMatch(" + match + ")";
      }
    };
  }
  return null;
}
 
Example #4
Source File: ViewServletTest.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@Test
@Ignore("disabled, due to unknown heap issue with defaultResponseSender spy")
public void normalRequestReturnsFacetResponse() throws Exception {
  descriptionRequested(null);
  facetThrowsException(false);

  underTest.dispatchAndSend(request, facet, defaultResponseSender, servletResponse);

  verify(underTest, never()).describe(
      any(Request.class),
      any(Response.class),
      any(Exception.class),
      any(String.class)
  );
  verify(defaultResponseSender).send(eq(request), any(Response.class), eq(servletResponse));
}
 
Example #5
Source File: HttpConditions.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@Nullable
private static Predicate<Response> ifMatch(final Request request) {
  final String match = request.getHeaders().get(HttpHeaders.IF_MATCH);
  if (match != null && !"*".equals(match)) {
    return new Predicate<Response>()
    {
      @Override
      public boolean apply(final Response response) {
        final String etag = response.getHeaders().get(HttpHeaders.ETAG);
        if (etag != null) {
          return match.contains(etag);
        }
        return true;
      }

      @Override
      public String toString() {
        return HttpConditions.class.getSimpleName() + ".ifMatch(" + match + ")";
      }
    };
  }
  return null;
}
 
Example #6
Source File: HttpConditions.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@Nullable
private static Predicate<Response> ifUnmodifiedSince(final Request request) {
  final DateTime date = parseDateHeader(request.getHeaders().get(HttpHeaders.IF_UNMODIFIED_SINCE));
  if (date != null) {
    return new Predicate<Response>()
    {
      @Override
      public boolean apply(final Response response) {
        final DateTime lastModified = parseDateHeader(response.getHeaders().get(HttpHeaders.LAST_MODIFIED));
        if (lastModified != null) {
          return !lastModified.isAfter(date);
        }
        return true;
      }

      @Override
      public String toString() {
        return HttpConditions.class.getSimpleName() + ".ifUnmodifiedSince(" + date + ")";
      }
    };
  }
  return null;
}
 
Example #7
Source File: DescriptionHelper.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
public void describeRequest(final Description desc, final Request request) {
  desc.topic("Request");

  desc.addTable("Details", ImmutableMap.<String, Object>builder()
          .put("Action", request.getAction())
          .put("path", request.getPath()).build()
  );

  desc.addTable("Parameters", toMap(request.getParameters()));
  desc.addTable("Headers", toMap(request.getHeaders()));
  desc.addTable("Attributes", toMap(request.getAttributes()));

  if (request.isMultipart()) {
    Iterable<PartPayload> parts = request.getMultiparts();
    checkState(parts != null);
    for (Payload payload : parts) {
      desc.addTable("Payload", toMap(payload));
    }
  }
  else {
    if (request.getPayload() != null) {
      desc.addTable("Payload", toMap(request.getPayload()));
    }
  }
}
 
Example #8
Source File: HttpConditions.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@Nullable
private static Predicate<Response> ifModifiedSince(final Request request) {
  final DateTime date = parseDateHeader(request.getHeaders().get(HttpHeaders.IF_MODIFIED_SINCE));
  if (date != null) {
    return new Predicate<Response>()
    {
      @Override
      public boolean apply(final Response response) {
        final DateTime lastModified = parseDateHeader(response.getHeaders().get(HttpHeaders.LAST_MODIFIED));
        if (lastModified != null) {
          return lastModified.isAfter(date);
        }
        return true;
      }

      @Override
      public String toString() {
        return HttpConditions.class.getSimpleName() + ".ifModifiedSince(" + date + ")";
      }
    };
  }
  return null;
}
 
Example #9
Source File: SecurityFacetSupport.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Returns BREAD action for request action.
 */
protected String action(final Request request) {
  switch (request.getAction()) {
    case HttpMethods.OPTIONS:
    case HttpMethods.GET:
    case HttpMethods.HEAD:
    case HttpMethods.TRACE:
      return BreadActions.READ;

    case HttpMethods.POST:
    case HttpMethods.MKCOL:
    case HttpMethods.PATCH:
      return BreadActions.ADD;

    case HttpMethods.PUT:
      return BreadActions.EDIT;

    case HttpMethods.DELETE:
      return BreadActions.DELETE;
  }

  throw new RuntimeException("Unsupported action: " + request.getAction());
}
 
Example #10
Source File: ViewServlet.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Build view request from {@link HttpServletRequest}.
 */
private Request buildRequest(final HttpServletRequest httpRequest, final String path) {
  Request.Builder builder = new Request.Builder()
      .headers(new HttpHeadersAdapter(httpRequest))
      .action(httpRequest.getMethod())
      .path(path)
      .parameters(new HttpParametersAdapter(httpRequest))
      .payload(new HttpRequestPayloadAdapter(httpRequest));

  if (HttpPartIteratorAdapter.isMultipart(httpRequest)) {
    builder.multiparts(new HttpPartIteratorAdapter(httpRequest));
  }

  // copy http-servlet-request attributes
  Enumeration<String> attributes = httpRequest.getAttributeNames();
  while (attributes.hasMoreElements()) {
    String name = attributes.nextElement();
    builder.attribute(name, httpRequest.getAttribute(name));
  }

  return builder.build();
}
 
Example #11
Source File: GroupHandler.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Returns the first OK response from member repositories or {@link HttpResponses#notFound()} if none of the members
 * responded with OK.
 */
protected Response getFirst(@Nonnull final Context context,
                            @Nonnull final List<Repository> members,
                            @Nonnull final DispatchedRepositories dispatched)
    throws Exception
{
  final Request request = context.getRequest();
  for (Repository member : members) {
    log.trace("Trying member: {}", member);
    // track repositories we have dispatched to, prevent circular dispatch for nested groups
    if (dispatched.contains(member)) {
      log.trace("Skipping already dispatched member: {}", member);
      continue;
    }
    dispatched.add(member);

    final ViewFacet view = member.facet(ViewFacet.class);
    final Response response = view.dispatch(request, context);
    log.trace("Member {} response {}", member, response.getStatus());
    if (isValidResponse(response)) {
      return response;
    }
  }
  return notFoundResponse(context);
}
 
Example #12
Source File: GroupHandler.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Similar to {@link #getAll(Context, Iterable, DispatchedRepositories)}, but allows for using a
 * different request then provided by the {@link Context#getRequest()} while still using the
 * same {@link Context} to execute the request in.
 *
 * @param request  {@link Request} that could be different then the {@link Context#getRequest()}
 * @param context  {@link Context}
 * @param members  {@link Repository}'s
 * @param dispatched {@link DispatchedRepositories}
 * @return LinkedHashMap of all responses from all members where order is group member order.
 * @throws Exception throw for any issues dispatching the request
 */
protected LinkedHashMap<Repository, Response> getAll(@Nonnull final Request request,
                                                     @Nonnull final Context context,
                                                     @Nonnull final Iterable<Repository> members,
                                                     @Nonnull final DispatchedRepositories dispatched)
    throws Exception
{
  final LinkedHashMap<Repository, Response> responses = Maps.newLinkedHashMap();
  for (Repository member : members) {
    log.trace("Trying member: {}", member);
    // track repositories we have dispatched to, prevent circular dispatch for nested groups
    if (dispatched.contains(member)) {
      log.trace("Skipping already dispatched member: {}", member);
      continue;
    }
    dispatched.add(member);

    final ViewFacet view = member.facet(ViewFacet.class);
    final Response response = view.dispatch(request, context);
    log.trace("Member {} response {}", member, response.getStatus());

    responses.put(member, response);
  }
  return responses;
}
 
Example #13
Source File: SearchGroupHandler.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Builds a context that contains a request that can be "replayed" with its post body content. Since we're repeating
 * the same post request to all the search endpoints, we need to have a new request instance with a payload we can
 * read multiple times.
 */
private Context buildReplayableContext(final Context context) throws IOException {
  checkNotNull(context);
  Request request = checkNotNull(context.getRequest());
  Payload payload = checkNotNull(request.getPayload());
  try (InputStream in = payload.openInputStream()) {
    byte[] content = ByteStreams.toByteArray(in);
    Context replayableContext = new Context(context.getRepository(),
        new Builder()
            .attributes(request.getAttributes())
            .headers(request.getHeaders())
            .action(request.getAction())
            .path(request.getPath())
            .parameters(request.getParameters())
            .payload(new BytesPayload(content, payload.getContentType()))
            .build());

    copyLocalContextAttributes(context, replayableContext);

    return replayableContext;
  }
}
 
Example #14
Source File: OrientPyPiProxyFacetImpl.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@Override
protected HttpRequestBase buildFetchHttpRequest(final URI uri, final Context context) {
  Request request = context.getRequest();
  // If we're doing a search operation, we have to proxy the content of the XML-RPC POST request to the PyPI server...
  if (isSearchRequest(request)) {
    Payload payload = checkNotNull(request.getPayload());
    try {
      ContentType contentType = ContentType.parse(payload.getContentType());
      HttpPost post = new HttpPost(uri);
      post.setEntity(new InputStreamEntity(payload.openInputStream(), payload.getSize(), contentType));
      return post;
    }
    catch (IOException e) {
      throw new RuntimeException(e);
    }
  }
  return super.buildFetchHttpRequest(uri, context); // URI needs to be replaced here
}
 
Example #15
Source File: OrientPyPiHostedHandlers.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Utility method to extract the uploaded content. Populates the attributes map with any other metadata. One and only
 * one file upload with a field name of "content" should be present in the upload, or {@link IllegalStateException}
 * is thrown to indicate unexpected input.
 */
private SignablePyPiPackage extractPayloads(final Context context) throws IOException {
  checkNotNull(context);
  Map<String, String> attributes = new LinkedHashMap<>();
  Request request = context.getRequest();
  TempBlobPartPayload contentBlob = null;
  TempBlobPartPayload signatureBlob = null;

  for (PartPayload payload : checkNotNull(request.getMultiparts())) {
    if (payload.isFormField()) {
      addAttribute(attributes, payload);
    }
    else if (FIELD_CONTENT.equals(payload.getFieldName())) {
      checkState(contentBlob == null);
      contentBlob = createBlobFromPayload(payload, context.getRepository());
    } else if (GPG_SIGNATURE.equals(payload.getFieldName())) {
      checkState(signatureBlob == null);
      signatureBlob = createBlobFromPayload(payload, context.getRepository());
    }
  }
  checkState (contentBlob != null);
  return new SignablePyPiPackage(contentBlob, attributes, signatureBlob);
}
 
Example #16
Source File: NpmSearchIndexFacetProxy.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@Nonnull
@Override
public Content searchIndex(@Nullable final DateTime since) throws IOException {
  try {
    final Request getRequest = new Request.Builder()
        .action(GET)
        .path("/" + NpmFacetUtils.REPOSITORY_ROOT_ASSET)
        .build();
    Context context = new Context(getRepository(), getRequest);
    context.getAttributes().set(ProxyTarget.class, ProxyTarget.SEARCH_INDEX);
    Content fullIndex = getRepository().facet(ProxyFacet.class).get(context);
    if (fullIndex == null) {
      throw new IOException("Could not retrieve registry root");
    }
    return NpmSearchIndexFilter.filterModifiedSince(fullIndex, since);
  }
  catch (Exception e) {
    throw new IOException(e);
  }
}
 
Example #17
Source File: NpmSearchGroupHandler.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@Override
protected Response doGet(@Nonnull final Context context,
                         @Nonnull final DispatchedRepositories dispatched)
    throws Exception
{
  checkNotNull(context);
  checkNotNull(dispatched);

  Request request = context.getRequest();
  Parameters parameters = request.getParameters();
  String text = npmSearchParameterExtractor.extractText(parameters);

  NpmSearchResponse response;
  if (text.isEmpty()) {
    response = npmSearchResponseFactory.buildEmptyResponse();
  }
  else {
    response = searchMembers(context, dispatched, parameters);
  }

  String content = npmSearchResponseMapper.writeString(response);
  return HttpResponses.ok(new StringPayload(content, ContentTypes.APPLICATION_JSON));
}
 
Example #18
Source File: NpmSearchFacetProxy.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public Content searchV1(final Parameters parameters) throws IOException {
  try {
    final Request getRequest = new Request.Builder()
        .action(GET)
        .path("/" + NpmFacetUtils.REPOSITORY_SEARCH_ASSET)
        .parameters(parameters)
        .build();
    Context context = new Context(getRepository(), getRequest);
    context.getAttributes().set(ProxyTarget.class, ProxyTarget.SEARCH_V1_RESULTS);
    Content searchResults = getRepository().facet(ProxyFacet.class).get(context);
    if (searchResults == null) {
      throw new IOException("Could not retrieve registry search");
    }
    return searchResults;
  }
  catch (Exception e) {
    throw new IOException(e);
  }
}
 
Example #19
Source File: ComposerProxyFacetImplTest.java    From nexus-repository-composer with Eclipse Public License 1.0 6 votes vote down vote up
@Test(expected = NonResolvableProviderJsonException.class)
public void getUrlZipballMissingProviderJson() throws Exception {
  when(contextAttributes.require(AssetKind.class)).thenReturn(ZIPBALL);
  when(contextAttributes.require(TokenMatcher.State.class)).thenReturn(state);

  when(viewFacet.dispatch(any(Request.class), eq(context))).thenReturn(response);
  when(response.getPayload()).thenReturn(null);

  when(state.getTokens()).thenReturn(new ImmutableMap.Builder<String, String>()
      .put("vendor", "vendor")
      .put("project", "project")
      .put("version", "version")
      .put("name", "project-version")
      .build());

  underTest.getUrl(context);
}
 
Example #20
Source File: ComposerProxyFacetImplTest.java    From nexus-repository-composer with Eclipse Public License 1.0 6 votes vote down vote up
@Test
public void getUrlZipball() throws Exception {
  when(contextAttributes.require(AssetKind.class)).thenReturn(ZIPBALL);
  when(contextAttributes.require(TokenMatcher.State.class)).thenReturn(state);

  when(viewFacet.dispatch(any(Request.class), eq(context))).thenReturn(response);
  when(composerJsonProcessor.getDistUrl("vendor", "project", "version", payload)).thenReturn("distUrl");

  when(state.getTokens()).thenReturn(new ImmutableMap.Builder<String, String>()
      .put("vendor", "vendor")
      .put("project", "project")
      .put("version", "version")
      .put("name", "project-version")
      .build());

  assertThat(underTest.getUrl(context), is("distUrl"));
}
 
Example #21
Source File: PartialFetchHandlerTest.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@Test
public void testHandleWhenIfRangeCheckFailsBecauseIfRangeIsNotValidDateTime() throws Exception {
  Request request = createGetRequestBuilder()
      .header(RANGE, RANGE_HEADER).header(IF_RANGE, "notValidDateTime").build();
  Response response = createOkResponseBuilder()
      .header(ETAG, ETAG_VALUE).header(LAST_MODIFIED, LAST_MODIFIED_VALUE).payload(PAYLOAD).build();
  when(rangeParser.parseRangeSpec(RANGE_HEADER, PAYLOAD.getSize())).thenReturn(singletonList(ZERO_TO_TWO_RANGE));

  assertThat(doHandle(request, response), is(sameInstance(response)));
}
 
Example #22
Source File: HttpConditions.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Un-stash the conditions originally found in {@link Request}. This method accepts only requests returned by {@link
 * #makeUnconditional(Request)} method, otherwise will throw {@link IllegalStateException}. This method must be used
 * in pair with the method above.
 */
@Nonnull
public static Request makeConditional(@Nonnull final Request request) {
  checkNotNull(request);
  final Headers stashedHeaders = request.getAttributes().require(HTTP_CONDITIONS, Headers.class);
  for (Entry<String, String> entry : stashedHeaders.entries()) {
    request.getHeaders().set(entry.getKey(), stashedHeaders.getAll(entry.getKey()));
  }
  return request;
}
 
Example #23
Source File: PartialFetchHandlerTest.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@Test
public void testHandleWhenIfRangeCheckFailsToMatchOnLastModified() throws Exception {
  Request request = createGetRequestBuilder()
      .header(RANGE, RANGE_HEADER).header(IF_RANGE, "Wed, 17 Jun 2015 11:31:00 GMT").build();
  Response response = createOkResponseBuilder()
      .header(ETAG, ETAG_VALUE).header(LAST_MODIFIED, LAST_MODIFIED_VALUE).payload(PAYLOAD).build();
  when(rangeParser.parseRangeSpec(RANGE_HEADER, PAYLOAD.getSize())).thenReturn(singletonList(ZERO_TO_TWO_RANGE));

  assertThat(doHandle(request, response), is(sameInstance(response)));
}
 
Example #24
Source File: PartialFetchHandlerTest.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@Test
public void testHandleWhenIfRangeCheckMatchesOnEtag() throws Exception {
  Request request = createGetRequestBuilder().header(RANGE, RANGE_HEADER).header(IF_RANGE, ETAG_VALUE).build();
  Response response = createOkResponseBuilder()
      .header(ETAG, ETAG_VALUE).header(LAST_MODIFIED, LAST_MODIFIED_VALUE).payload(PAYLOAD).build();
  when(rangeParser.parseRangeSpec(RANGE_HEADER, PAYLOAD.getSize())).thenReturn(singletonList(ZERO_TO_TWO_RANGE));

  verifyHandleReturnsPartialResponse(request, response);
}
 
Example #25
Source File: PartialFetchHandlerTest.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@Test
public void testHandleWhenMethodIsNotGet() throws Exception {
  Request requestWithNonGetAction = createRequestBuilderForAction(POST).build();
  Response response = createOkResponseBuilder().build();

  assertThat(doHandle(requestWithNonGetAction, response), is(sameInstance(response)));
}
 
Example #26
Source File: ConcurrentProxyTest.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@Test
public void limitCooperatingThreads() throws Exception {
  int threadLimit = 10;

  underTest.configureCooperation(cooperationFactory, true, Time.seconds(60), Time.seconds(10), threadLimit);
  underTest.buildCooperation();

  Request request = new Request.Builder().action(GET).path("some/fixed/path").build();

  ConcurrentRunner runner = new ConcurrentRunner(1, 60);
  runner.addTask(NUM_CLIENTS, verifyThreadLimit(request));
  runner.addTask(() -> {

    // only the limited number of threads should be cooperating
    waitForThreadCooperation(threadLimit);

    // the other threads should all receive cooperation exceptions
    waitForCooperationExceptionCount(NUM_CLIENTS - threadLimit);

    // and only one thread should be waiting on the upstream
    waitForAssetDownloads(1);
    releaseAssetDownloads(1);
    waitForAssetDownloads(0);

    waitForThreadCooperation(0);
  });
  runner.go();

  assertThat(runner.getRunInvocations(), is(runner.getTaskCount() * runner.getIterations()));

  // only one request should have made it upstream
  assertThat(upstreamRequestLog.count(ASSET_PREFIX + "some/fixed/path"), is(1));

  // majority of requests should have been cancelled to maintain thread limit
  assertThat(cooperationExceptionCount.get(), is(NUM_CLIENTS - threadLimit));
}
 
Example #27
Source File: ComposerProxyFacetImplTest.java    From nexus-repository-composer with Eclipse Public License 1.0 5 votes vote down vote up
@Test
public void storePackages() throws Exception {
  when(contextAttributes.require(AssetKind.class)).thenReturn(PACKAGES);
  when(composerContentFacet.put(PACKAGES_PATH, content, PACKAGES)).thenReturn(content);

  when(viewFacet.dispatch(any(Request.class), eq(context))).thenReturn(response);
  when(composerJsonProcessor.generatePackagesFromList(repository, payload)).thenReturn(content);

  assertThat(underTest.store(context, content), is(content));

  verify(composerContentFacet).put(PACKAGES_PATH, content, PACKAGES);
}
 
Example #28
Source File: IndexHtmlForwardHandler.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
private Response forward(final Context context, final String path) throws Exception {
  log.trace("Forwarding request to path: {}", path);

  Request request = new Request.Builder()
      .copy(context.getRequest())
      .path(path)
      .build();

  return context.getRepository()
      .facet(ViewFacet.class)
      .dispatch(request);
}
 
Example #29
Source File: HttpConditions.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Builds a {@link Predicate} that contains conditions in passed in {@link Request} or {@code null} if
 * request does not contains any condition. The predicate applies to {@link Response} if it meets all the conditions
 * found (they all are logically AND bound).
 */
@Nullable
public static Predicate<Response> requestPredicate(@Nonnull final Request request) {
  checkNotNull(request);
  final List<Predicate<Response>> predicates = new ArrayList<>();
  final Predicate<Response> ifModifiedSince = ifModifiedSince(request);
  if (ifModifiedSince != null) {
    predicates.add(ifModifiedSince);
  }
  final Predicate<Response> ifUnmodifiedSince = ifUnmodifiedSince(request);
  if (ifUnmodifiedSince != null) {
    predicates.add(ifUnmodifiedSince);
  }
  final Predicate<Response> ifMatch = ifMatch(request);
  if (ifMatch != null) {
    predicates.add(ifMatch);
  }
  final Predicate<Response> ifNoneMatch = ifNoneMatch(request);
  if (ifNoneMatch != null) {
    predicates.add(ifNoneMatch);
  }

  if (!predicates.isEmpty()) {
    return Predicates.and(predicates);
  }
  return null;
}
 
Example #30
Source File: HttpConditions.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Stashes the conditions of the passed in request, making it non-conditional request. To reverse this change,
 * use {@link #makeConditional(Request)} method.
 */
@Nonnull
public static Request makeUnconditional(@Nonnull final Request request) {
  checkNotNull(request);
  final Headers stashedHeaders = new Headers();
  for (String httpHeader : SUPPORTED_HEADERS) {
    List<String> headerValues = request.getHeaders().getAll(httpHeader);
    if (headerValues != null) {
      stashedHeaders.set(httpHeader, headerValues);
    }
    request.getHeaders().remove(httpHeader);
  }
  request.getAttributes().set(HTTP_CONDITIONS, stashedHeaders);
  return request;
}