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

The following examples show how to use org.sonatype.nexus.repository.view.Status. 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: PartialFetchHandler.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Mutate the response into one that returns part of the payload.
 */
private Response partialResponse(final Response response,
                                 final Payload payload,
                                 final Range<Long> requestedRange)
{
  Response.Builder builder = new Response.Builder()
      .copy(response)
      .status(Status.success(HttpStatus.PARTIAL_CONTENT));

  Payload partialPayload = new PartialPayload(payload, requestedRange);
  builder.payload(partialPayload);

  // ResponseSender takes care of Content-Length header, via payload.size
  builder.header(HttpHeaders.CONTENT_RANGE,
     "bytes " + requestedRange.lowerEndpoint() + "-" + requestedRange.upperEndpoint() + "/" + payload.getSize());

  return builder.build();
}
 
Example #2
Source File: DescriptionHelper.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
public void describeResponse(final Description desc, final Response response) {
  desc.topic("Response");

  final Status status = response.getStatus();
  desc.addTable("Status", ImmutableMap.of(
      "Code", (Object) status.getCode(),
      "Message", nullToEmpty(status.getMessage())
  ));

  desc.addTable("Headers", toMap(response.getHeaders()));
  desc.addTable("Attributes", toMap(response.getAttributes()));

  Payload payload = response.getPayload();
  if (payload != null) {
    desc.addTable("Payload", toMap(payload));
  }
}
 
Example #3
Source File: ConanTokenFacetImpl.java    From nexus-repository-conan with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public Response login(final Context context) {
  String token = conanTokenManager.login();
  if(null != token) {
    return new Response.Builder()
        .status(Status.success(OK))
        .payload(new StringPayload(token, TEXT_PLAIN))
        .build();
  }
  return badCredentials("Bad username or password");
}
 
Example #4
Source File: HttpResponses.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
public static Response rangeNotSatisfiable(final long contentSize) {
  return new Response.Builder()
      .status(Status.failure(REQUESTED_RANGE_NOT_SATISFIABLE))
      .header(HttpHeaders.CONTENT_LENGTH, "0")
      .header(HttpHeaders.CONTENT_RANGE, "bytes */" + contentSize)
      .build();
}
 
Example #5
Source File: HttpResponses.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
public static Response methodNotAllowed(final String methodName, final String... allowedMethods) {
  checkNotNull(methodName);
  checkNotNull(allowedMethods);
  checkArgument(allowedMethods.length != 0);
  return new Response.Builder()
      .status(Status.failure(METHOD_NOT_ALLOWED, methodName))
      .header(HttpHeaders.ALLOW, Joiner.on(',').join(allowedMethods))
      .build();
}
 
Example #6
Source File: NegativeCacheFacetImpl.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public void invalidateSubset(final NegativeCacheKey key) {
  if (cache != null) {
    invalidate(key);
    for (final Entry<NegativeCacheKey, Status> entry : cache) {
      if (!key.equals(entry.getKey()) && key.isParentOf(entry.getKey())) {
        invalidate(entry.getKey());
      }
    }
  }
}
 
Example #7
Source File: NegativeCacheFacetImpl.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@Override
@Guarded(by = STARTED)
public void put(final NegativeCacheKey key, final Status status) {
  checkNotNull(key);
  checkNotNull(status);
  if (cache != null) {
    log.debug("Adding {}={} to negative-cache of {}", key, status, getRepository());
    cache.put(key, status);
  }
}
 
Example #8
Source File: NegativeCacheFacetImpl.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@Override
@Guarded(by = STARTED)
public Status get(final NegativeCacheKey key) {
  checkNotNull(key);
  if (cache != null) {
    return cache.get(key);
  }
  return null;
}
 
Example #9
Source File: NegativeCacheFacetImpl.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
private void maybeCreateCache() {
  if (cache == null) {
    log.debug("Creating negative-cache for: {}", getRepository());
    cache = cacheHelper.maybeCreateCache(getCacheName(), NegativeCacheKey.class, Status.class,
        CreatedExpiryPolicy.factoryOf(new Duration(TimeUnit.MINUTES, config.timeToLive)));
    log.debug("Created negative-cache: {}", cache);
  }
}
 
Example #10
Source File: NegativeCacheHandler.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@Nonnull
@Override
public Response handle(@Nonnull final Context context) throws Exception {
  String action = context.getRequest().getAction();
  if (!NFC_CACHEABLE_ACTIONS.contains(action)) {
    return context.proceed();
  }
  NegativeCacheFacet negativeCache = context.getRepository().facet(NegativeCacheFacet.class);
  NegativeCacheKey key = negativeCache.getCacheKey(context);

  Response response;
  Status status = negativeCache.get(key);
  if (status == null) {
    response = context.proceed();
    if (isNotFound(response)) {
      negativeCache.put(key, response.getStatus());
    }
    else if (response.getStatus().isSuccessful()) {
      negativeCache.invalidate(key);
    }
  }
  else {
    response = buildResponse(status, context);

    log.debug("Found {} in negative cache, returning {}", key, response);
  }
  return response;
}
 
Example #11
Source File: LastDownloadedHandlerTest.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
private void configureHappyPath() throws Exception {
  when(asset.lastDownloaded()).thenReturn(empty());

  attributes = new AttributesMap();
  attributes.set(Asset.class, asset);

  when(context.proceed()).thenReturn(response);
  when(context.getRepository()).thenReturn(repository);
  when(context.getRequest()).thenReturn(request);
  when(request.getAction()).thenReturn(GET);
  when(response.getPayload()).thenReturn(payload);
  when(response.getStatus()).thenReturn(new Status(true, 200));
  when(payload.getAttributes()).thenReturn(attributes);
}
 
Example #12
Source File: LastDownloadedHandlerTest.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@Test
public void shouldMarkAssetAsDownloadedWhenNotModified() throws Exception {
  when(response.getStatus()).thenReturn(new Status(false, 304));
  when(assetManager.maybeUpdateLastDownloaded(asset)).thenReturn(true);
  
  Response handledResponse = underTest.handle(context);

  verify(tx).saveAsset(asset);

  assertThat(handledResponse, is(equalTo(response)));
}
 
Example #13
Source File: DefaultHttpResponseSenderTest.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@Test
public void customStatusMessageIsMaintainedWithPayload() throws Exception {
  when(request.getAction()).thenReturn(HttpMethods.GET);

  Payload detailedReason = new StringPayload("Please authenticate and try again", "text/plain");

  Response response = new Response.Builder()
      .status(Status.failure(FORBIDDEN, "You can't see this"))
      .payload(detailedReason).build();

  underTest.send(request, response, httpServletResponse);

  verify(httpServletResponse).setStatus(403, "You can't see this");
}
 
Example #14
Source File: PackagesGroupHandlerTest.java    From nexus-repository-r with Eclipse Public License 1.0 5 votes vote down vote up
private void setupRepository(final Repository repository) throws Exception {
  ViewFacet viewFacet = mock(ViewFacet.class);
  Response response = mock(Response.class);
  Payload payload = mock(Payload.class);
  GroupFacet groupFacet = mock(GroupFacet.class);
  when(groupFacet.members()).thenReturn(members);
  when(repository.facet(GroupFacet.class)).thenReturn(groupFacet);
  when(repository.facet(ViewFacet.class)).thenReturn(viewFacet);
  when(viewFacet.dispatch(any(), any())).thenReturn(response);
  when(response.getPayload()).thenReturn(payload);
  when(payload.openInputStream()).thenReturn(new FileInputStream(packages));
  when(response.getStatus()).thenReturn(new Status(true, 200));
}
 
Example #15
Source File: NpmResponses.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@Nonnull
public static Response notFound(@Nullable final String message) {
  return new Response.Builder()
      .status(Status.failure(NOT_FOUND, "Not Found"))
      .payload(statusPayload(false, message))
      .build();
}
 
Example #16
Source File: OrientPyPiGroupFacetTest.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@Test
public void whenResponseIsNewerThanLastModifiedShouldBeStale() {
  when(response.getStatus()).thenReturn(Status.success(200));
  when(response.getPayload()).thenReturn(content);
  AttributesMap attributes = new AttributesMap();
  attributes.set(CONTENT_LAST_MODIFIED,
      originalContent.getAttributes().get(CONTENT_LAST_MODIFIED, DateTime.class).plusMinutes(1));

  when(content.getAttributes()).thenReturn(attributes);

  assertTrue(underTest.isStale(null, originalContent, responses));
}
 
Example #17
Source File: NpmResponses.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@Nonnull
public static Response badRequest(@Nullable final String message) {
  return new Response.Builder()
      .status(Status.failure(BAD_REQUEST))
      .payload(statusPayload(false, message))
      .build();
}
 
Example #18
Source File: NpmResponses.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@Nonnull
public static Response badCredentials(@Nullable final String message) {
  return new Response.Builder()
      .status(Status.failure(UNAUTHORIZED))
      .payload(statusPayload(false, message))
      .build();
}
 
Example #19
Source File: NpmResponses.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@Nonnull
static Response npmErrorAuditResponse(
    final int statusCode,
    @Nonnull final String message)
{
  String newLineDelimiter = String.format("%s%s", lineSeparator(), DELIMITER);
  String errorMsg = String.format("%s%s%s%s", newLineDelimiter, lineSeparator(), message, newLineDelimiter);
  return new Response.Builder()
      .status(Status.failure(statusCode))
      .payload(new StringPayload(errorMsg, APPLICATION_JSON))
      .build();
}
 
Example #20
Source File: OrientPyPiGroupFacetTest.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@Test
public void whenResponseHasNoContentShouldNotBeStale() {
  when(response.getStatus()).thenReturn(Status.success(200));
  when(response.getPayload()).thenReturn(null);

  assertFalse(underTest.isStale(null, originalContent, responses));
}
 
Example #21
Source File: NpmNegativeCacheHandler.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@Override
protected Response buildResponse(final Status status, final Context context) {
  if (status.getCode() == HttpStatus.SC_NOT_FOUND) {
    State state = context.getAttributes().require(TokenMatcher.State.class);
    NpmPackageId packageId = NpmHandlers.packageId(state);
    return NpmResponses.packageNotFound(packageId);
  }
  return super.buildResponse(status, context);
}
 
Example #22
Source File: HttpResponses.java    From nexus-public with Eclipse Public License 1.0 4 votes vote down vote up
public static Response created(final Payload payload) {
  return new Response.Builder()
      .status(Status.success(CREATED))
      .payload(payload)
      .build();
}
 
Example #23
Source File: PartialFetchHandlerTest.java    From nexus-public with Eclipse Public License 1.0 4 votes vote down vote up
private static Response.Builder createResponseBuilderForStatus(final Status status) {
  return new Response.Builder().status(status);
}
 
Example #24
Source File: HttpResponses.java    From nexus-public with Eclipse Public License 1.0 4 votes vote down vote up
public static Response accepted() {
  return new Response.Builder()
      .status(Status.success(ACCEPTED))
      .build();
}
 
Example #25
Source File: HttpResponses.java    From nexus-public with Eclipse Public License 1.0 4 votes vote down vote up
public static Response noContent(@Nullable final String message) {
  return new Response.Builder()
      .status(Status.success(NO_CONTENT, message))
      .build();
}
 
Example #26
Source File: HttpResponses.java    From nexus-public with Eclipse Public License 1.0 4 votes vote down vote up
public static Response notFound(@Nullable final String message) {
  return new Response.Builder()
      .status(Status.failure(NOT_FOUND, message))
      .build();
}
 
Example #27
Source File: HttpResponses.java    From nexus-public with Eclipse Public License 1.0 4 votes vote down vote up
public static Response badRequest(@Nullable final String message) {
  return new Response.Builder()
      .status(Status.failure(BAD_REQUEST, message))
      .build();
}
 
Example #28
Source File: HttpResponses.java    From nexus-public with Eclipse Public License 1.0 4 votes vote down vote up
public static Response unauthorized(@Nullable final String message) {
  return new Response.Builder()
      .status(Status.failure(UNAUTHORIZED, message))
      .build();
}
 
Example #29
Source File: HttpResponses.java    From nexus-public with Eclipse Public License 1.0 4 votes vote down vote up
public static Response forbidden(@Nullable final String message) {
  return new Response.Builder()
      .status(Status.failure(FORBIDDEN, message))
      .build();
}
 
Example #30
Source File: ConanTokenFacetImpl.java    From nexus-repository-conan with Eclipse Public License 1.0 4 votes vote down vote up
static Response badCredentials(final String message) {
  return new Response.Builder()
      .status(Status.failure(UNAUTHORIZED))
      .payload(new StringPayload(message, TEXT_PLAIN))
      .build();
}