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

The following examples show how to use org.sonatype.nexus.repository.view.ViewFacet. 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: 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 #2
Source File: AptProxyRecipe.java    From nexus-repository-apt with Eclipse Public License 1.0 6 votes vote down vote up
private ViewFacet configure(final ConfigurableViewFacet facet) {
  Router.Builder builder = new Router.Builder();

  builder.route(new Route.Builder().matcher(new AlwaysMatcher())
      .handler(timingHandler)
      .handler(securityHandler)
      .handler(exceptionHandler)
      .handler(negativeCacheHandler)
      .handler(conditionalRequestHandler)
      .handler(partialFetchHandler)
      .handler(contentHeadersHandler)
      .handler(unitOfWorkHandler)
      .handler(snapshotHandler)
      .handler(lastDownloadedHandler)
      .handler(proxyHandler).create());

  builder.defaultHandlers(notFound());
  facet.configure(builder.create());
  return facet;
}
 
Example #3
Source File: AptHostedRecipe.java    From nexus-repository-apt with Eclipse Public License 1.0 6 votes vote down vote up
private ViewFacet configure(final ConfigurableViewFacet facet) {
  Router.Builder builder = new Router.Builder();

  builder.route(new Route.Builder().matcher(new AlwaysMatcher())
      .handler(timingHandler)
      .handler(securityHandler)
      .handler(exceptionHandler)
      .handler(conditionalRequestHandler)
      .handler(partialFetchHandler)
      .handler(contentHeadersHandler)
      .handler(unitOfWorkHandler)
      .handler(lastDownloadedHandler)
      .handler(snapshotHandler)
      .handler(signingHandler)
      .handler(hostedHandler).create());

  builder.defaultHandlers(notFound());
  facet.configure(builder.create());
  return facet;
}
 
Example #4
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 #5
Source File: RepositoryManagerImpl.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Construct a new repository from configuration.
 */
private Repository newRepository(final Configuration configuration) throws Exception {
  String recipeName = configuration.getRecipeName();
  Recipe recipe = recipe(recipeName);
  log.debug("Using recipe: [{}] {}", recipeName, recipe);

  Repository repository = factory.create(recipe.getType(), recipe.getFormat());

  // attach mandatory facets
  repository.attach(configFacet.get());

  // apply recipe to repository
  recipe.apply(repository);

  // verify required facets
  repository.facet(ViewFacet.class);

  // ensure configuration sanity, once all facets are attached
  repository.validate(configuration);

  // initialize repository
  repository.init(configuration);

  return repository;
}
 
Example #6
Source File: AptHostedRecipe.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
private ViewFacet configure(final ConfigurableViewFacet facet) {
  Router.Builder builder = new Router.Builder();

  builder.route(new Route.Builder().matcher(new AlwaysMatcher())
      .handler(timingHandler)
      .handler(securityHandler)
      .handler(highAvailabilitySupportHandler)
      .handler(exceptionHandler)
      .handler(handlerContributor)
      .handler(conditionalRequestHandler)
      .handler(partialFetchHandler)
      .handler(contentHeadersHandler)
      .handler(unitOfWorkHandler)
      .handler(lastDownloadedHandler)
      .handler(snapshotHandler)
      .handler(signingHandler)
      .handler(hostedHandler).create());

  builder.defaultHandlers(notFound());
  facet.configure(builder.create());
  return facet;
}
 
Example #7
Source File: AptProxyRecipe.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
private ViewFacet configure(final ConfigurableViewFacet facet) {
  Router.Builder builder = new Router.Builder();

  builder.route(new Route.Builder().matcher(new AlwaysMatcher())
      .handler(timingHandler)
      .handler(securityHandler)
      .handler(highAvailabilitySupportHandler)
      .handler(routingRuleHandler)
      .handler(exceptionHandler)
      .handler(handlerContributor)
      .handler(negativeCacheHandler)
      .handler(conditionalRequestHandler)
      .handler(partialFetchHandler)
      .handler(contentHeadersHandler)
      .handler(unitOfWorkHandler)
      .handler(snapshotHandler)
      .handler(lastDownloadedHandler)
      .handler(proxyHandler).create());

  builder.defaultHandlers(notFound());
  facet.configure(builder.create());
  return facet;
}
 
Example #8
Source File: ViewServlet.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@VisibleForTesting
void dispatchAndSend(final Request request,
                     final ViewFacet facet,
                     final HttpResponseSender sender,
                     final HttpServletResponse httpResponse)
    throws Exception
{
  Response response = null;
  Exception failure = null;
  try {
    response = facet.dispatch(request);
  }
  catch (Exception e) {
    failure = e;
  }

  String describeFlags = request.getParameters().get(P_DESCRIBE);
  log.trace("Describe flags: {}", describeFlags);
  if (describeFlags != null) {
    send(request, describe(request, response, failure, describeFlags), httpResponse);
  }
  else {
    if (failure != null) {
      throw failure;
    }
    log.debug("Request: {}", request);
    sender.send(request, response, httpResponse);
  }
}
 
Example #9
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 #10
Source File: GroupHandlerTest.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@Before
public void setUp() throws Exception {
  underTest = new GroupHandler();

  when(context.getRequest()).thenReturn(request);
  when(proxy1.getName()).thenReturn("Proxy 1");
  when(proxy1.facet(ViewFacet.class)).thenReturn(viewFacet1);
  when(proxy2.getName()).thenReturn("Proxy 2");
  when(proxy2.facet(ViewFacet.class)).thenReturn(viewFacet2);
}
 
Example #11
Source File: CocoapodsProxyRecipe.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
private ViewFacet configure(final ConfigurableViewFacet facet) {
  Router.Builder builder = new Router.Builder();

  createRoutes(builder);

  builder.defaultHandlers(notFound());
  facet.configure(builder.create());
  return facet;
}
 
Example #12
Source File: LegacyViewServletTest.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
private void mockRepository() {
  when(repositoryManager.get(REPOSITORY_NAME)).thenReturn(repository);
  when(repository.getConfiguration()).thenReturn(configuration);
  when(repository.facet(ViewFacet.class)).thenReturn(viewFacet);
  when(repository.getFormat()).thenReturn(new Format("maven") { });
  when(configuration.isOnline()).thenReturn(true);
}
 
Example #13
Source File: ComposerGroupPackagesJsonHandlerTest.java    From nexus-repository-composer with Eclipse Public License 1.0 5 votes vote down vote up
@Before
public void setUp() throws Exception {
  when(context.getRepository()).thenReturn(repository);
  when(context.getRequest()).thenReturn(request);
  when(context.getAttributes()).thenReturn(new AttributesMap());

  when(request.getAction()).thenReturn(GET);
  when(request.getAttributes()).thenReturn(new AttributesMap());
  when(request.getHeaders()).thenReturn(new Headers());

  when(repository.facet(GroupFacet.class)).thenReturn(groupFacet);
  when(groupFacet.members()).thenReturn(asList(memberRepository1, memberRepository2));

  when(memberRepository1.getName()).thenReturn("member1");
  when(memberRepository1.facet(ViewFacet.class)).thenReturn(memberRepository1ViewFacet);

  when(memberRepository2.getName()).thenReturn("member2");
  when(memberRepository2.facet(ViewFacet.class)).thenReturn(memberRepository2ViewFacet);

  when(memberRepository1ViewFacet.dispatch(request, context)).thenReturn(response1);
  when(memberRepository2ViewFacet.dispatch(request, context)).thenReturn(response2);

  when(response1.getStatus()).thenReturn(status1);
  when(response1.getPayload()).thenReturn(payload1);

  when(response2.getStatus()).thenReturn(status2);
  when(response2.getPayload()).thenReturn(payload2);

  when(status1.getCode()).thenReturn(OK);
  when(status2.getCode()).thenReturn(OK);

  underTest = new ComposerGroupPackagesJsonHandler(composerJsonProcessor);
}
 
Example #14
Source File: ViewServlet.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
protected void doService(final HttpServletRequest httpRequest, final HttpServletResponse httpResponse)
    throws Exception
{
  if (sandboxEnabled) {
    httpResponse.setHeader(HttpHeaders.CONTENT_SECURITY_POLICY, SANDBOX);
  }
  httpResponse.setHeader(HttpHeaders.X_XSS_PROTECTION, "1; mode=block");

  // resolve repository for request
  RepositoryPath path = RepositoryPath.parse(httpRequest.getPathInfo());
  log.debug("Parsed path: {}", path);

  Repository repo = repository(path.getRepositoryName());
  if (repo == null) {
    send(null, HttpResponses.notFound(REPOSITORY_NOT_FOUND_MESSAGE), httpResponse);
    return;
  }
  log.debug("Repository: {}", repo);

  if (!repo.getConfiguration().isOnline()) {
    send(null, HttpResponses.serviceUnavailable("Repository offline"), httpResponse);
    return;
  }

  ViewFacet facet = repo.facet(ViewFacet.class);
  log.debug("Dispatching to view facet: {}", facet);

  // Dispatch the request
  Request request = buildRequest(httpRequest, path.getRemainingPath());
  dispatchAndSend(request, facet, httpResponseSenderSelector.sender(repo), httpResponse);
}
 
Example #15
Source File: OrientNpmGroupPackageHandlerTest.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@Before
@SuppressWarnings("unchecked")
public void setUp() throws Exception {
  underTest = new OrientNpmGroupPackageHandler();

  AttributesMap attributesMap = new AttributesMap();
  attributesMap.set(TokenMatcher.State.class, state);
  when(context.getAttributes()).thenReturn(attributesMap);
  when(context.getRepository()).thenReturn(group);
  when(context.getRequest()).thenReturn(request);

  when(group.getName()).thenReturn(OrientNpmGroupFacetTest.class.getSimpleName() + "-group");
  when(group.facet(GroupFacet.class)).thenReturn(groupFacet);
  when(group.facet(StorageFacet.class)).thenReturn(storageFacet);

  when(groupFacet.buildPackageRoot(anyMap(), eq(context)))
      .thenReturn(new Content(new BytesPayload("test".getBytes(), "")));

  when(viewFacet.dispatch(request, context))
      .thenReturn(new Response.Builder().status(success(OK)).build());

  when(proxy.facet(ViewFacet.class)).thenReturn(viewFacet);
  when(hosted.facet(ViewFacet.class)).thenReturn(viewFacet);

  when(request.getHeaders()).thenReturn(new Headers());
  when(request.getAttributes()).thenReturn(new AttributesMap());

  when(storageFacet.txSupplier()).thenReturn(() -> storageTx);

  UnitOfWork.beginBatch(storageTx);
}
 
Example #16
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 #17
Source File: ComposerProxyFacetImplTest.java    From nexus-repository-composer with Eclipse Public License 1.0 5 votes vote down vote up
@Before
public void setUp() throws Exception {
  underTest = new ComposerProxyFacetImpl(composerJsonProcessor);
  underTest.attach(repository);

  when(repository.facet(ComposerContentFacet.class)).thenReturn(composerContentFacet);
  when(repository.facet(ViewFacet.class)).thenReturn(viewFacet);

  when(context.getAttributes()).thenReturn(contextAttributes);
  when(context.getRequest()).thenReturn(request);
  when(context.getRepository()).thenReturn(repository);

  when(response.getPayload()).thenReturn(payload);
}
 
Example #18
Source File: ComposerGroupProviderJsonHandlerTest.java    From nexus-repository-composer with Eclipse Public License 1.0 5 votes vote down vote up
@Before
public void setUp() throws Exception {
  when(context.getRepository()).thenReturn(repository);
  when(context.getRequest()).thenReturn(request);
  when(context.getAttributes()).thenReturn(new AttributesMap());

  when(request.getAction()).thenReturn(GET);
  when(request.getAttributes()).thenReturn(new AttributesMap());
  when(request.getHeaders()).thenReturn(new Headers());

  when(repository.facet(GroupFacet.class)).thenReturn(groupFacet);
  when(groupFacet.members()).thenReturn(asList(memberRepository1, memberRepository2));

  when(memberRepository1.getName()).thenReturn("member1");
  when(memberRepository1.facet(ViewFacet.class)).thenReturn(memberRepository1ViewFacet);

  when(memberRepository2.getName()).thenReturn("member2");
  when(memberRepository2.facet(ViewFacet.class)).thenReturn(memberRepository2ViewFacet);

  when(memberRepository1ViewFacet.dispatch(request, context)).thenReturn(response1);
  when(memberRepository2ViewFacet.dispatch(request, context)).thenReturn(response2);

  when(response1.getStatus()).thenReturn(status1);
  when(response1.getPayload()).thenReturn(payload1);

  when(response2.getStatus()).thenReturn(status2);
  when(response2.getPayload()).thenReturn(payload2);

  when(status1.getCode()).thenReturn(OK);
  when(status2.getCode()).thenReturn(OK);

  underTest = new ComposerGroupProviderJsonHandler(composerJsonProcessor);
}
 
Example #19
Source File: NpmSearchGroupHandlerTest.java    From nexus-public with Eclipse Public License 1.0 4 votes vote down vote up
@Before
public void setUp() throws Exception {
  underTest = new NpmSearchGroupHandler(npmSearchParameterExtractor, npmSearchResponseFactory,
      npmSearchResponseMapper, MAX_SEARCH_RESULTS);

  when(context.getRepository()).thenReturn(repository);
  when(context.getRequest()).thenReturn(request);
  when(request.getParameters()).thenReturn(parameters);

  when(repository.facet(GroupFacet.class)).thenReturn(groupFacet);

  when(groupFacet.members()).thenReturn(asList(memberRepository1, memberRepository2));

  when(memberRepository1.facet(ViewFacet.class)).thenReturn(viewFacet1);
  when(memberRepository2.facet(ViewFacet.class)).thenReturn(viewFacet2);

  when(npmSearchResponseFactory.buildEmptyResponse()).thenReturn(searchResponse);
  when(npmSearchResponseFactory.buildResponseForObjects(any(List.class))).then(invocation -> {
    List<NpmSearchResponseObject> objects = (List<NpmSearchResponseObject>) invocation.getArguments()[0];
    NpmSearchResponse response = new NpmSearchResponse();
    response.setObjects(objects);
    response.setTime("Wed Jan 25 2017 19:23:35 GMT+0000 (UTC)");
    response.setTotal(objects.size());
    return response;
  });

  when(npmSearchParameterExtractor.extractText(parameters)).thenReturn("text");
  when(npmSearchParameterExtractor.extractSize(parameters)).thenReturn(20);
  when(npmSearchParameterExtractor.extractFrom(parameters)).thenReturn(0);

  when(npmSearchResponseMapper.writeString(any(NpmSearchResponse.class))).thenReturn("response");

  when(viewFacet1.dispatch(request, context)).thenReturn(response1);
  when(viewFacet2.dispatch(request, context)).thenReturn(response2);

  when(response1.getPayload()).thenReturn(payload1);
  when(response1.getStatus()).thenReturn(status1);

  when(response2.getPayload()).thenReturn(payload2);
  when(response2.getStatus()).thenReturn(status2);

  when(status1.getCode()).thenReturn(OK);
  when(status2.getCode()).thenReturn(OK);

  when(payload1.openInputStream()).thenReturn(payloadInputStream1);
  when(payload2.openInputStream()).thenReturn(payloadInputStream2);

  when(npmSearchResponseMapper.readFromInputStream(payloadInputStream1)).thenReturn(npmSearchResponse1);
  when(npmSearchResponseMapper.readFromInputStream(payloadInputStream2)).thenReturn(npmSearchResponse2);

  when(npmSearchResponse1.getObjects()).thenReturn(singletonList(npmSearchResponseObject1));
  when(npmSearchResponse2.getObjects()).thenReturn(singletonList(npmSearchResponseObject2));

  when(npmSearchResponseObject1.getSearchScore()).thenReturn(0.8);
  when(npmSearchResponseObject1.getPackageEntry()).thenReturn(npmSearchResponsePackage1);

  when(npmSearchResponseObject2.getSearchScore()).thenReturn(0.9);
  when(npmSearchResponseObject2.getPackageEntry()).thenReturn(npmSearchResponsePackage2);

  when(npmSearchResponsePackage1.getName()).thenReturn("package-1");
  when(npmSearchResponsePackage2.getName()).thenReturn("package-2");
}