org.sonatype.nexus.repository.routing.RoutingRule Java Examples

The following examples show how to use org.sonatype.nexus.repository.routing.RoutingRule. 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: RoutingRulesApiResource.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@Override
@DELETE
@Path("/{name}")
@RequiresAuthentication
@RequiresPermissions("nexus:*")
public void deleteRoutingRule(@PathParam("name") final String name) {
  RoutingRule routingRule = getRuleFromStore(name);
  EntityId routingRuleId = routingRule.id();
  Map<EntityId, List<Repository>> assignedRepositories = routingRuleHelper.calculateAssignedRepositories();

  List<Repository> repositories = assignedRepositories.computeIfAbsent(routingRuleId, id -> emptyList());
  if (!repositories.isEmpty()) {
    throw new WebApplicationMessageException(
        Status.BAD_REQUEST,
        "\"Routing rule is still in use by " + repositories.size() + " repositories.\"",
        APPLICATION_JSON);
  }

  routingRuleStore.delete(routingRule);
}
 
Example #2
Source File: RoutingRulesApiResource.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@Override
@PUT
@Path("/{name}")
@RequiresAuthentication
@RequiresPermissions("nexus:*")
public void updateRoutingRule(@PathParam("name") final String name,
                              @NotNull final RoutingRuleXO routingRuleXO)
{
  RoutingRule routingRule = getRuleFromStore(name);

  routingRule.name(routingRuleXO.getName())
      .description(routingRuleXO.getDescription())
      .mode(routingRuleXO.getMode())
      .matchers(routingRuleXO.getMatchers());

  routingRuleStore.update(routingRule);
}
 
Example #3
Source File: RoutingRuleCacheTest.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@Test
public void invalidateRepoCacheOnUpdate() throws Exception {
  mockRule("rule-a");
  Repository repository = createRepository("repo-a", "rule-a");

  // prime the cache
  assertNotNull(routingRuleCache.getRoutingRule(repository));

  // Clear the cache
  routingRuleCache.handle(new RepositoryUpdatedEvent(repository, null));

  // update the assigned rule
  RoutingRule rule = mockRule("rule-b");
  Configuration configuration = repository.getConfiguration();
  when(configuration.getRoutingRuleId()).thenReturn(new DetachedEntityId("rule-b"));

  assertThat(routingRuleCache.getRoutingRule(repository), is(rule));
}
 
Example #4
Source File: RoutingRulesResource.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@DELETE
@Path("/{name}")
@RequiresAuthentication
@RequiresPermissions("nexus:*")
public void deleteRoutingRule(@PathParam("name") final String name) {
  RoutingRule routingRule = routingRuleStore.getByName(name);
  if (null == routingRule) {
    throw new WebApplicationException(Status.NOT_FOUND);
  }

  Map<EntityId, List<Repository>> assignedRepositories = routingRuleHelper.calculateAssignedRepositories();
  List<Repository> repositories = assignedRepositories.getOrDefault(routingRule.id(), emptyList());
  if (repositories.size() > 0) {
    throw new WebApplicationException("Routing rule is still in use by " + repositories.size() + " repositories.", Status.BAD_REQUEST);
  }

  routingRuleStore.delete(routingRule);
}
 
Example #5
Source File: RoutingRuleCacheTest.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@Test
public void invalidateRepoCacheOnDelete() throws Exception {
  mockRule("rule-a");
  Repository repository = createRepository("repo-a", "rule-a");

  // prime the cache
  assertNotNull(routingRuleCache.getRoutingRule(repository));

  // Clear the cache
  routingRuleCache.handle(new RepositoryDeletedEvent(repository));

  // update the assigned rule
  RoutingRule rule = mockRule("rule-b");
  Configuration configuration = repository.getConfiguration();
  when(configuration.getRoutingRuleId()).thenReturn(new DetachedEntityId("rule-b"));

  assertThat(routingRuleCache.getRoutingRule(repository), is(rule));
}
 
Example #6
Source File: RoutingRulesResource.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
private RoutingRulePreviewXO toPreviewXO(final Repository repository,
                                         final List<Repository> childRepositories,
                                         Map<RoutingRule, Boolean> routingRulePathMapping)
{
  Optional<RoutingRule> maybeRule = getRoutingRule(repository);

  boolean allowed = maybeRule.map(routingRulePathMapping::get).orElse(ALLOWED);
  String ruleName = maybeRule.map(RoutingRule::name).orElse(null);

  List<RoutingRulePreviewXO> children = childRepositories == null ? null : childRepositories.stream()
      .map(childRepository -> toPreviewXO(childRepository, null, routingRulePathMapping))
      .collect(toList());

  return RoutingRulePreviewXO.builder()
      .repository(repository.getName())
      .type(repository.getType().getValue())
      .format(repository.getFormat().getValue())
      .allowed(allowed)
      .rule(ruleName)
      .children(children)
      .expanded(children != null && !children.isEmpty())
      .expandable(children != null && !children.isEmpty())
      .build();
}
 
Example #7
Source File: RoutingRuleCacheTest.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@Test
public void testGetRoutingRule() throws Exception {
  RoutingRule ruleA = mockRule("rule-a");
  Repository repository = createRepository("repo-a", "rule-a");

  // verify we get right value back
  assertThat(routingRuleCache.getRoutingRule(repository), is(ruleA));
  verify(store, times(1)).getById("rule-a");

  // verify we don't hit DB twice
  routingRuleCache.getRoutingRule(repository);
  verify(store, times(1)).getById("rule-a");

  // check another repo
  assertThat(routingRuleCache.getRoutingRule(createRepository("repo-b", "rule-a")), is(ruleA));
  verify(store, times(1)).getById("rule-a");

  RoutingRule ruleC = mockRule("rule-c");
  assertThat(routingRuleCache.getRoutingRule(createRepository("repo-c", "rule-c")), is(ruleC));
  verify(store, times(1)).getById("rule-c");
}
 
Example #8
Source File: RoutingRuleStoreImpl.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
private void postEvent(final RoutingRule rule) {
  // trigger invalidation of routing rule cache
  eventManager.post(new RoutingRuleInvalidatedEvent()
  {
    @Override
    public EntityId getRoutingRuleId() {
      return rule.id();
    }
  });
}
 
Example #9
Source File: RoutingRulesApiResource.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
private RoutingRule getRuleFromStore(final String name) {
  RoutingRule routingRule = routingRuleStore.getByName(name);
  if (null == routingRule) {
    throw new WebApplicationMessageException(
        Status.NOT_FOUND,
       "\"Did not find a routing rule with the name '" + name + "'\"",
        APPLICATION_JSON
    );
  }
  return routingRule;
}
 
Example #10
Source File: OrientRoutingRuleStore.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@Override
@Guarded(by = STARTED)
public RoutingRule create(final RoutingRule rule) {
  checkNotNull(rule);
  validate(rule);

  persist(entityAdapter::addEntity, rule);

  return rule;
}
 
Example #11
Source File: OrientRoutingRuleStore.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@Override
@Guarded(by = STARTED)
public void update(final RoutingRule rule) {
  checkNotNull(rule);
  validate(rule);

  persist(entityAdapter::editEntity, rule);
}
 
Example #12
Source File: OrientRoutingRuleStore.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
private void persist(BiConsumer<ODatabaseDocumentTx, OrientRoutingRule> entityFunction, RoutingRule rule) {
  try {
    inTxRetry(databaseInstance).run(db -> entityFunction.accept(db, castToOrientRoutingRule(rule)));
  }
  catch (ORecordDuplicatedException e) {
    if (OrientRoutingRuleEntityAdapter.I_NAME.equals(e.getIndexName())) {
      throw new ValidationErrorsException("name", "A rule with the same name already exists. Name must be unique.");
    }
    throw e;
  }
}
 
Example #13
Source File: RoutingRuleStoreImpl.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@Transactional
@Override
public RoutingRule create(final RoutingRule rule) {
  try {
    dao().create((RoutingRuleData) validate(rule));
    return rule;
  }
  catch (RuntimeException e) {
    if (causedByDuplicateKey(e)) {
      throw new ValidationErrorsException("name", "A rule with the same name already exists. Name must be unique.");
    }
    throw e;
  }
}
 
Example #14
Source File: RoutingRuleRule.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Create a RoutingRule with mode block and a dummy description
 */
public RoutingRule create(final String name, final String pattern) {
  final RoutingRuleStore routingRuleStore = ruleStoreProvider.get();
  return create(routingRuleStore.newRoutingRule()
      .name(name)
      .description("some description")
      .mode(RoutingMode.BLOCK)
      .matchers(Collections.singletonList(pattern))
  );
}
 
Example #15
Source File: OrientRoutingRuleStoreTest.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@Test
public void testCreate() throws Exception {
  underTest.start();
  createRoutingRule("asdf", "asdf2");

  RoutingRule rule = underTest.getByName("asdf");
  assertThat(rule.name(), is("asdf"));
  assertThat(rule.description(), is("some description"));
  assertThat(rule.mode(), is(RoutingMode.BLOCK));
  assertThat(rule.matchers(), contains("asdf2"));
}
 
Example #16
Source File: OrientRoutingRuleStoreTest.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@Test
public void testUpdate() throws Exception {
  underTest.start();
  RoutingRule routingRule = createRoutingRule("asdf", "asdf");
  routingRule.matchers(Arrays.asList("asdf2"));
  underTest.update(routingRule);

  routingRule = underTest.getByName("asdf");
  assertThat(routingRule.name(), is("asdf"));
  assertThat(routingRule.mode(), is(RoutingMode.BLOCK));
  assertThat(routingRule.matchers(), contains("asdf2"));
}
 
Example #17
Source File: OrientRoutingRuleStoreTest.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@Test
public void testUpdate_duplicateName() throws Exception {
  underTest.start();
  createRoutingRule("dup", "bar");
  RoutingRule routingRule = createRoutingRule("foo", "bar");
  routingRule.name("dup");
  thrown.expect(ValidationErrorsException.class);
  thrown.expectMessage("A rule with the same name already exists. Name must be unique.");
  underTest.update(routingRule);
}
 
Example #18
Source File: OrientRoutingRuleStoreTest.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@Test
public void testList() throws Exception {
  underTest.start();
  createRoutingRule("asdf1", "asdf1");
  createRoutingRule("asdf2", "asdf2");
  createRoutingRule("asdf3", "asdf3");

  List<RoutingRule> matchers = underTest.list();
  assertThat(matchers.size(), is(3));
}
 
Example #19
Source File: OrientRoutingRuleStoreTest.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@Test
public void testDelete() throws Exception {
  underTest.start();
  RoutingRule rule = createRoutingRule("asdf", "asdf2");
  assertThat(underTest.list(), hasSize(1));

  underTest.delete(rule);

  assertThat(underTest.list(), hasSize(0));
}
 
Example #20
Source File: OrientRoutingRuleStoreTest.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@Test
public void testGetByName() throws Exception {
  underTest.start();
  createRoutingRule("foo", "foo");
  createRoutingRule("bar", "bar");

  RoutingRule rule = underTest.getByName("foo");
  assertThat(rule.name(), is("foo"));
}
 
Example #21
Source File: OrientRoutingRuleStoreTest.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@Test(expected = ValidationErrorsException.class)
public void testValidate_onUpdate() throws Exception {
  underTest.start();
  RoutingRule rule = null;
  try {
    rule = createRoutingRule("name", "asdf");
    rule.name("a   a");
  }
  catch (Exception e) {
    fail("Unexpected exception occurred");
  }

  underTest.update(rule);
}
 
Example #22
Source File: OrientRoutingRuleStoreTest.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
private static void validate(final RoutingRule rule, final String id, final String message) {
  try {
    OrientRoutingRuleStore.validate(rule);
    fail("Expected exception");
  }
  catch (ValidationErrorsException e) {
    assertValidationException(e, id, message);
  }
}
 
Example #23
Source File: RoutingRulesApiResource.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@Override
@POST
@RequiresAuthentication
@RequiresPermissions("nexus:*")
public void createRoutingRule(@NotNull final RoutingRuleXO routingRuleXO)
{
  final RoutingRule routingRule = routingRuleStore.newRoutingRule();
  routingRule.name(routingRuleXO.getName());
  routingRule.description(routingRuleXO.getDescription());
  routingRule.mode(routingRuleXO.getMode());
  routingRule.matchers(routingRuleXO.getMatchers());

  routingRuleStore.create(routingRule);
}
 
Example #24
Source File: RoutingRuleHelperImpl.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public boolean isAllowed(final Repository repository, final String path) {
  RoutingRule routingRule = routingRuleCache.getRoutingRule(repository);

  if (routingRule == null) {
    return true;
  }

  return isAllowed(routingRule, path);
}
 
Example #25
Source File: RoutingRulesResource.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
private static RoutingRuleXO toXO(RoutingRule routingRule) {
  RoutingRuleXO routingRuleXO = new RoutingRuleXO();
  routingRuleXO.setId(routingRule.id().getValue());
  routingRuleXO.setName(routingRule.name());
  routingRuleXO.setDescription(routingRule.description());
  routingRuleXO.setMode(routingRule.mode());
  routingRuleXO.setMatchers(routingRule.matchers());
  return routingRuleXO;
}
 
Example #26
Source File: RoutingRulesResource.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
private RoutingRule fromXO(RoutingRuleXO routingRuleXO) {
  final RoutingRule routingRule = routingRuleStore.newRoutingRule();
  routingRule.name(routingRuleXO.getName());
  routingRule.description(routingRuleXO.getDescription());
  routingRule.mode(routingRuleXO.getMode());
  routingRule.matchers(routingRuleXO.getMatchers());
  return routingRule;
}
 
Example #27
Source File: RoutingRuleCache.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Retrieves the routing rule assigned to a repository or null if one is not assigned.
 *
 * @param repository
 * @return
 */
@Nullable
public RoutingRule getRoutingRule(final Repository repository) {
  try {
    return repositoryAssignedCache.get(repository).map(this::getRoutingRule).orElse(null);
  }
  catch (ExecutionException e) {
    log.error("An error occurred retrieving the routing rule for repository: {}", repository.getName(), e);
    return null;
  }
}
 
Example #28
Source File: RoutingRulesResource.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@GET
@Path("/preview")
@RequiresPermissions("nexus:*")
@RequiresAuthentication
public RoutingRulePreviewXO getRoutingRulesPreview(@QueryParam("path") final String path,
                                                   @QueryParam("filter") final String filter)
{
  Map<Class<?>, List<Repository>> repositoriesByType = stream(repositoryManager.browse())
      .collect(groupingBy(r -> r.getType().getClass()));
  List<Repository> groupRepositories = repositoriesByType.get(GroupType.class);
  List<Repository> proxyRepositories = repositoriesByType.get(ProxyType.class);

  Map<RoutingRule, Boolean> routingRulePathMapping = routingRuleStore.list().stream()
      .collect(toMap(identity(), (RoutingRule rule) -> routingRuleHelper.isAllowed(rule, path)));

  final Stream<Repository> repositories;
  if (GROUPS.equals(filter)) {
    repositories = groupRepositories.stream();
  }
  else if (PROXIES.equals(filter)) {
    repositories = proxyRepositories.stream();
  }
  else {
    repositories = Stream.of(groupRepositories, proxyRepositories).flatMap(Collection::stream);
  }

  List<RoutingRulePreviewXO> rootRepositories = repositories.map(repository -> {
    List<Repository> children = repository.optionalFacet(GroupFacet.class)
        .map(facet -> facet.members()).orElse(null);
    return toPreviewXO(repository, children, routingRulePathMapping);
  }).collect(toList());

  return RoutingRulePreviewXO.builder().children(rootRepositories).expanded(!rootRepositories.isEmpty()).expandable(true).build();
}
 
Example #29
Source File: RoutingRulesResource.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@PUT
@Path("/{name}")
@RequiresAuthentication
@RequiresPermissions("nexus:*")
public void updateRoutingRule(@PathParam("name") final String name, RoutingRuleXO routingRuleXO) {
  RoutingRule routingRule = routingRuleStore.getByName(name);
  if (null == routingRule) {
    throw new WebApplicationException(Status.NOT_FOUND);
  }
  routingRule.name(routingRuleXO.getName());
  routingRule.description(routingRuleXO.getDescription());
  routingRule.mode(routingRuleXO.getMode());
  routingRule.matchers(routingRuleXO.getMatchers());
  routingRuleStore.update(routingRule);
}
 
Example #30
Source File: RoutingRuleCache.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
private RoutingRule getRoutingRule(final EntityId id) {
  try {
    return routingRuleCache.get(id).orElse(null);
  }
  catch (ExecutionException e) {
    log.error("An error occurred retrieving the routing rule id {}", id, e);
    return null;
  }
}