org.sonatype.nexus.repository.http.HttpMethods Java Examples

The following examples show how to use org.sonatype.nexus.repository.http.HttpMethods. 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: ProxyHandlerTest.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@Test
public void testBypassHttpErrorExceptionPropagatesCodeWithMessageWithHeader() throws Exception {
  final int httpStatus = HttpStatus.FORBIDDEN;
  final String errorMessage = "Error Message";
  final String headerName = "Header Name";
  final String headerValue = "Header Value";

  ListMultimap<String, String> headers = ArrayListMultimap.create();
  headers.put(headerName, headerValue);
  BypassHttpErrorException bypassException = new BypassHttpErrorException(httpStatus, errorMessage, headers);

  when(request.getAction()).thenReturn(HttpMethods.GET);
  doThrow(bypassException).when(proxyFacet).get(context);

  Response response = underTest.handle(context);

  assertStatusCode(response, httpStatus);
  assertStatusMessage(response, errorMessage);
  assertThat(response.getHeaders().get(headerName), is(headerValue));
}
 
Example #2
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 #3
Source File: DefaultHttpResponseSenderTest.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@Test
public void payloadClosedAfterError() throws Exception {
  when(request.getAction()).thenReturn(HttpMethods.GET);

  doThrow(new IOException("Dropped")).when(payload).copy(input, output);

  try {
    underTest.send(request, HttpResponses.ok(payload), httpServletResponse);
    fail("Expected IOException");
  }
  catch (IOException e) {
    assertThat(e.getMessage(), is("Dropped"));
  }

  InOrder order = inOrder(payload, input);

  order.verify(payload).getContentType();
  order.verify(payload, atLeastOnce()).getSize();
  order.verify(payload).openInputStream();
  order.verify(input).close();
  order.verify(payload).close();

  order.verifyNoMoreInteractions();
}
 
Example #4
Source File: DefaultHttpResponseSenderTest.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@Test
public void payloadClosedAfterGET() throws Exception {
  when(request.getAction()).thenReturn(HttpMethods.GET);

  underTest.send(request, HttpResponses.ok(payload), httpServletResponse);

  InOrder order = inOrder(payload, input);

  order.verify(payload).getContentType();
  order.verify(payload, atLeastOnce()).getSize();
  order.verify(payload).openInputStream();
  order.verify(input).close();
  order.verify(payload).close();

  order.verifyNoMoreInteractions();
}
 
Example #5
Source File: BrowseUnsupportedHandler.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public boolean matches(final Context context) {
  checkNotNull(context);
  String action = context.getRequest().getAction();
  String path = context.getRequest().getPath();
  log.debug("Matching: {} {}", action, path);
  if (HttpMethods.GET.equals(action)) {
    path = Strings2.lower(path);
    return path.endsWith("/") || path.endsWith("/index.html") || path.endsWith("/index.htm");
  }
  return false;
}
 
Example #6
Source File: HelmSecurityFacetTest.java    From nexus-repository-helm with Eclipse Public License 1.0 5 votes vote down vote up
@Before
public void setupConfig() throws Exception {
  when(request.getPath()).thenReturn("/some/path.txt");
  when(request.getAction()).thenReturn(HttpMethods.GET);

  when(repository.getFormat()).thenReturn(new Format("helm") { });
  when(repository.getName()).thenReturn("HelmSecurityFacetTest");

  helmSecurityFacet = new HelmSecurityFacet(securityContributor,
      variableResolverAdapter, contentPermissionChecker);

  helmSecurityFacet.attach(repository);
}
 
Example #7
Source File: ProxyHandlerTest.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@Test
public void testCooperationExceptionReturns503ResponseWithMessage() throws Exception {
  when(request.getAction()).thenReturn(HttpMethods.GET);
  doThrow(new CooperationException("Cooperation failed")).when(proxyFacet).get(context);
  Response response = underTest.handle(context);
  assertStatusCode(response, HttpStatus.SERVICE_UNAVAILABLE);
  assertStatusMessage(response, "Cooperation failed");
}
 
Example #8
Source File: SecurityFacetSupportTest.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@Before
public void setupConfig() throws Exception {
  when(request.getPath()).thenReturn("/some/path.txt");
  when(request.getAction()).thenReturn(HttpMethods.GET);

  when(repository.getFormat()).thenReturn(new Format("test") { });
  when(repository.getName()).thenReturn("SecurityFacetSupportTest");

  testSecurityFacetSupport = new TestSecurityFacetSupport(securityContributor,
      variableResolverAdapter, contentPermissionChecker);

  testSecurityFacetSupport.attach(repository);
}
 
Example #9
Source File: MavenSecurityFacetTest.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@Before
public void setupConfig() throws Exception {
  when(request.getPath()).thenReturn("/mygroupid/myartifactid/1.0/myartifactid-1.0.jar");
  when(request.getAction()).thenReturn(HttpMethods.GET);

  when(repository.getFormat()).thenReturn(new Maven2Format());
  when(repository.getName()).thenReturn("MavenSecurityFacetTest");

  mavenSecurityFacet = new MavenSecurityFacet(securityContributor,
      variableResolverAdapter, contentPermissionChecker);

  mavenSecurityFacet.attach(repository);
}
 
Example #10
Source File: GolangSecurityFacetTest.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@Before
public void setupConfig() throws Exception {
  when(request.getPath()).thenReturn("/some/path.txt");
  when(request.getAction()).thenReturn(HttpMethods.GET);

  when(repository.getFormat()).thenReturn(new Format("go") { });
  when(repository.getName()).thenReturn("GoSecurityFacetTest");

  golangSecurityFacet = new GolangSecurityFacet(securityContributor,
      variableResolverAdapter, contentPermissionChecker);

  golangSecurityFacet.attach(repository);
}
 
Example #11
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 #12
Source File: DefaultHttpResponseSenderTest.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@Test
public void customStatusMessageIsMaintained() throws Exception {
  when(request.getAction()).thenReturn(HttpMethods.GET);

  underTest.send(request, HttpResponses.forbidden("You can't see this"), httpServletResponse);

  verify(httpServletResponse).sendError(403, "You can't see this");
}
 
Example #13
Source File: DefaultHttpResponseSenderTest.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@Test
public void payloadClosedAfterHEAD() throws Exception {
  when(request.getAction()).thenReturn(HttpMethods.HEAD);

  underTest.send(request, HttpResponses.ok(payload), httpServletResponse);

  InOrder order = inOrder(payload, input);

  order.verify(payload).getContentType();
  order.verify(payload, atLeastOnce()).getSize();
  order.verify(payload).close();

  order.verifyNoMoreInteractions();
}
 
Example #14
Source File: CondaSecurityFacetTest.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@Before
public void setupConfig() throws Exception{
  when(request.getPath()).thenReturn("/some/path.txt");
  when(request.getAction()).thenReturn(HttpMethods.GET);

  when(repository.getFormat()).thenReturn(new Format(CondaFormat.NAME) { });
  when(repository.getName()).thenReturn(CONDA_SECURITY_FACET_TEST);

  condaSecurityFacet = new CondaSecurityFacet(securityContributor, variableResolverAdapter, contentPermissionChecker);

  condaSecurityFacet.attach(repository);
}
 
Example #15
Source File: ConanSecurityFacetTest.java    From nexus-repository-conan with Eclipse Public License 1.0 5 votes vote down vote up
@Before
public void setupConfig() throws Exception {
  when(request.getPath()).thenReturn("/some/path.txt");
  when(request.getAction()).thenReturn(HttpMethods.GET);

  when(repository.getFormat()).thenReturn(new Format("conan") { });
  when(repository.getName()).thenReturn("ConanSecurityFacetTest");

  conanSecurityFacet = new ConanSecurityFacet(securityContributor,
      variableResolverAdapter, contentPermissionChecker);

  conanSecurityFacet.attach(repository);
}
 
Example #16
Source File: ConanRoutesTest.java    From nexus-repository-conan with Eclipse Public License 1.0 5 votes vote down vote up
@Before
public void setUp() {
  attributesMap = new AttributesMap();
  when(context.getRequest()).thenReturn(request);
  when(context.getAttributes()).thenReturn(attributesMap);
  when(request.getAction()).thenReturn(HttpMethods.GET);

  underTest = new ConanRoutes();
}
 
Example #17
Source File: PyPiRecipeSupportTest.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@Test
public void routingForRootIndex() throws Exception {
  exception.expectMessage("rootIndex");
  exception.expect(RuntimeException.class);

  when(request.getAction()).thenReturn(HttpMethods.GET);
  when(request.getPath()).thenReturn("/simple/");

  underTest.dispatch(repository, request, null);
}
 
Example #18
Source File: PyPiRecipeSupportTest.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@Test
public void routingForIndex() throws Exception {
  exception.expectMessage("index");
  exception.expect(RuntimeException.class);

  when(request.getAction()).thenReturn(HttpMethods.GET);
  when(request.getPath()).thenReturn("/simple/foo/");

  underTest.dispatch(repository, request, null);
}
 
Example #19
Source File: PyPiRecipeSupportTest.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@Test
public void routingForIndexVariant() throws Exception {
  exception.expectMessage("index");
  exception.expect(RuntimeException.class);

  when(request.getAction()).thenReturn(HttpMethods.GET);
  when(request.getPath()).thenReturn("/simple/foo");

  underTest.dispatch(repository, request, null);
}
 
Example #20
Source File: PyPiRecipeSupportTest.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@Test
public void routingForPackages() throws Exception {
  exception.expectMessage("packages");
  exception.expect(RuntimeException.class);

  when(request.getAction()).thenReturn(HttpMethods.GET);
  when(request.getPath()).thenReturn("/packages/bar/1/bar-1.tar.gz");

  underTest.dispatch(repository, request, null);
}
 
Example #21
Source File: PyPiRecipeSupportTest.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@Test
public void routingForSearch() throws Exception {
  exception.expectMessage("search");
  exception.expect(RuntimeException.class);

  when(request.getAction()).thenReturn(HttpMethods.POST);
  when(request.getPath()).thenReturn("/pypi");

  underTest.dispatch(repository, request, null);
}
 
Example #22
Source File: RawSecurityFacetTest.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@Before
public void setupConfig() throws Exception {
  when(request.getPath()).thenReturn("/some/path.txt");
  when(request.getAction()).thenReturn(HttpMethods.GET);

  when(repository.getFormat()).thenReturn(new Format("raw") { });
  when(repository.getName()).thenReturn("RawSecurityFacetTest");

  rawSecurityFacet = new RawSecurityFacet(securityContributor,
      variableResolverAdapter, contentPermissionChecker);

  rawSecurityFacet.attach(repository);
}
 
Example #23
Source File: PyPiPathUtils.java    From nexus-public with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Returns whether or not the request is a PyPI search request.
 */
public static boolean isSearchRequest(final Request request) {
  return HttpMethods.POST.equals(request.getAction()) && request.getPath().endsWith("/pypi");
}
 
Example #24
Source File: CocoapodsProxyRecipeTest.java    From nexus-public with Eclipse Public License 1.0 4 votes vote down vote up
@Before
public void setup() throws Exception
{
  AttributesMap attributesMap = new AttributesMap();
  attributesMap.set(LOCAL_ATTRIBUTE_PREFIX + "some_key", "some_value");
  when(context.getRepository()).thenReturn(repository);
  when(context.getAttributes()).thenReturn(attributesMap);
  when(request.getAction()).thenReturn(HttpMethods.GET);
  when(viewFacet.get()).thenReturn(cocoapodsViewFacet);
  when(format.getValue()).thenReturn(COCOAPODS_NAME);
  timingHandler = spy(new TimingHandler(null));

  underTest = new CocoapodsProxyRecipe(new ProxyType(), format);
  underTest.timingHandler = timingHandler;
  underTest.securityFacet = securityFacet;
  underTest.viewFacet = viewFacet;
  underTest.securityHandler = securityHandler;
  underTest.routingHandler = routingHandler;
  underTest.exceptionHandler = exceptionHandler;
  underTest.negativeCacheHandler = negativeCacheHandler;
  underTest.conditionalRequestHandler = conditionalRequestHandler;
  underTest.partialFetchHandler = partialFetchHandler;
  underTest.contentHeadersHandler = ontentHeadersHandler;
  underTest.unitOfWorkHandler = unitOfWorkHandler;
  underTest.lastDownloadedHandler = lastDownloadedHandler;
  underTest.proxyHandler = proxyHandler;
  underTest.httpClientFacet = httpClientFacet;
  underTest.negativeCacheFacet = negativeCacheFacet;
  underTest.proxyFacet = proxyFacet;
  underTest.cocoapodsFacet = cocoapodsFacet;
  underTest.storageFacet = storageFace;
  underTest.attributesFacet = attributesFacet;
  underTest.componentMaintenance = componentMaintenance;
  underTest.searchFacet = searchFacet;
  underTest.purgeUnusedFacet = purgeUnusedFacet;
  underTest.highAvailabilitySupportHandler = highAvailabilitySupportHandler;
  underTest.highAvailabilitySupportChecker = highAvailabilitySupportChecker;
  underTest.handlerContributor = handlerContributor;

  underTest.apply(repository);
  cocoapodsViewFacet.attach(repository);
}
 
Example #25
Source File: CondaProxyRecipeTest.java    From nexus-public with Eclipse Public License 1.0 4 votes vote down vote up
@Before
public void setup() throws Exception
{
  condaViewFacet = new ConfigurableViewFacet();
  AttributesMap attributesMap = new AttributesMap();
  attributesMap.set(LOCAL_ATTRIBUTE_PREFIX + "some_key", "some_value");
  when(format.getValue()).thenReturn(CONDA_NAME);
  when(viewFacet.get()).thenReturn(condaViewFacet);
  when(context.getRepository()).thenReturn(repository);
  when(context.getAttributes()).thenReturn(attributesMap);
  when(request.getAction()).thenReturn(HttpMethods.GET);
  when(securityHandler.handle(any())).thenAnswer(createPropagationAnswer());
  when(timingHandler.handle(any())).thenAnswer(createPropagationAnswer());
  when(browseUnsupportedHandler.getRoute()).thenReturn(
      new Route(mock(Matcher.class), ImmutableList.of(securityHandler, browseUnsupportedHandler)));

  proxyRecipe = new CondaProxyRecipe(new ProxyType(), format);

  proxyRecipe.setTimingHandler(timingHandler);
  proxyRecipe.setHighAvailabilitySupportHandler(highAvailabilitySupportHandler);
  proxyRecipe.setSecurityHandler(securityHandler);
  proxyRecipe.setExceptionHandler(exceptionHandler);
  proxyRecipe.setHandlerContributor(handlerContributor);
  proxyRecipe.setNegativeCacheHandler(negativeCacheHandler);
  proxyRecipe.setConditionalRequestHandler(conditionalRequestHandler);
  proxyRecipe.setPartialFetchHandler(partialFetchHandler);
  proxyRecipe.setContentHeadersHandler(contentHeadersHandler);
  proxyRecipe.setUnitOfWorkHandler(unitOfWorkHandler);
  proxyRecipe.setLastDownloadedHandler(lastDownloadedHandler);
  proxyRecipe.setProxyHandler(proxyHandler);
  proxyRecipe.setBrowseUnsupportedHandler(browseUnsupportedHandler);
  proxyRecipe.setRoutingHandler(routingRuleHandler);

  proxyRecipe.setSecurityFacet(securityFacet);
  proxyRecipe.setViewFacet(viewFacet);
  proxyRecipe.setHttpClientFacet(httpClientFacet);
  proxyRecipe.setNegativeCacheFacet(negativeCacheFacet);
  proxyRecipe.setProxyFacet(proxyFacet);
  proxyRecipe.setCondaFacet(condaFacet);
  proxyRecipe.setStorageFacet(storageFacet);
  proxyRecipe.setAttributesFacet(attributesFacet);
  proxyRecipe.setComponentMaintenanceFacet(componentMaintenanceFacet);
  proxyRecipe.setSearchFacet(searchFacet);
  proxyRecipe.setPurgeUnusedFacet(purgeUnusedFacet);
  proxyRecipe.setHighAvailabilitySupportChecker(highAvailabilitySupportChecker);

  proxyRecipe.apply(repository);
  condaViewFacet.attach(repository);
}
 
Example #26
Source File: DefaultHttpResponseSender.java    From nexus-public with Eclipse Public License 1.0 4 votes vote down vote up
@Override
public void send(@Nullable final Request request, final Response response, final HttpServletResponse httpResponse)
    throws ServletException, IOException
{
  log.debug("Sending response: {}", response);

  // add response headers
  for (Map.Entry<String, String> header : response.getHeaders()) {
    httpResponse.addHeader(header.getKey(), header.getValue());
  }

  // add status followed by payload if we have one
  Status status = response.getStatus();
  String statusMessage = status.getMessage();
  try (Payload payload = response.getPayload()) {
    if (status.isSuccessful() || payload != null) {

      if (statusMessage == null) {
        httpResponse.setStatus(status.getCode());
      }
      else {
        httpResponse.setStatus(status.getCode(), statusMessage);
      }

      if (payload != null) {
        log.trace("Attaching payload: {}", payload);

        if (payload.getContentType() != null) {
          httpResponse.setContentType(payload.getContentType());
        }
        if (payload.getSize() != Payload.UNKNOWN_SIZE) {
          httpResponse.setContentLengthLong(payload.getSize());
        }

        if (request != null && !HttpMethods.HEAD.equals(request.getAction())) {
          try (InputStream input = payload.openInputStream(); OutputStream output = httpResponse.getOutputStream()) {
            payload.copy(input, output);
          }
        }
      }
    }
    else {
      httpResponse.sendError(status.getCode(), statusMessage);
    }
  }
}
 
Example #27
Source File: ProxyHandlerTest.java    From nexus-public with Eclipse Public License 1.0 4 votes vote down vote up
@Test
public void testMethodNotAllowedReturns405Response() throws Exception {
  when(request.getAction()).thenReturn(HttpMethods.LOCK);
  assertStatusCode(underTest.handle(context), HttpStatus.METHOD_NOT_ALLOWED);
}
 
Example #28
Source File: ProxyHandlerTest.java    From nexus-public with Eclipse Public License 1.0 4 votes vote down vote up
@Test
public void testPayloadPresentReturns200Response() throws Exception {
  when(request.getAction()).thenReturn(HttpMethods.GET);
  when(proxyFacet.get(context)).thenReturn(content);
  assertStatusCode(underTest.handle(context), HttpStatus.OK);
}
 
Example #29
Source File: ProxyHandlerTest.java    From nexus-public with Eclipse Public License 1.0 4 votes vote down vote up
@Test
public void testPayloaAbsentReturns404Response() throws Exception {
  when(request.getAction()).thenReturn(HttpMethods.GET);
  assertStatusCode(underTest.handle(context), HttpStatus.NOT_FOUND);
}
 
Example #30
Source File: ProxyHandlerTest.java    From nexus-public with Eclipse Public License 1.0 4 votes vote down vote up
@Test
public void testProxyServiceExceptionReturns503Response() throws Exception {
  when(request.getAction()).thenReturn(HttpMethods.GET);
  doThrow(new ProxyServiceException(httpResponse)).when(proxyFacet).get(context);
  assertStatusCode(underTest.handle(context), HttpStatus.SERVICE_UNAVAILABLE);
}