Java Code Examples for org.sonatype.nexus.repository.view.Context#getRepository()

The following examples show how to use org.sonatype.nexus.repository.view.Context#getRepository() . 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: ComposerHostedDownloadHandler.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 {
  Repository repository = context.getRepository();
  ComposerHostedFacet hostedFacet = repository.facet(ComposerHostedFacet.class);
  AssetKind assetKind = context.getAttributes().require(AssetKind.class);
  switch (assetKind) {
    case PACKAGES:
      return HttpResponses.ok(hostedFacet.getPackagesJson());
    case LIST:
      throw new IllegalStateException("Unsupported assetKind: " + assetKind);
    case PROVIDER:
      return HttpResponses.ok(hostedFacet.getProviderJson(getVendorToken(context), getProjectToken(context)));
    case ZIPBALL:
      return responseFor(hostedFacet.getZipball(buildZipballPath(context)));
    default:
      throw new IllegalStateException("Unexpected assetKind: " + assetKind);
  }
}
 
Example 3
Source File: ComposerGroupMergingHandler.java    From nexus-repository-composer with Eclipse Public License 1.0 6 votes vote down vote up
@Override
protected final Response doGet(@Nonnull final Context context,
                               @Nonnull final GroupHandler.DispatchedRepositories dispatched)
    throws Exception
{
  Repository repository = context.getRepository();
  GroupFacet groupFacet = repository.facet(GroupFacet.class);

  makeUnconditional(context.getRequest());
  Map<Repository, Response> responses;
  try {
    responses = getAll(context, groupFacet.members(), dispatched);
  }
  finally {
    makeConditional(context.getRequest());
  }

  List<Payload> payloads = responses.values().stream()
      .filter(response -> response.getStatus().getCode() == HttpStatus.OK && response.getPayload() != null)
      .map(Response::getPayload)
      .collect(Collectors.toList());
  if (payloads.isEmpty()) {
    return notFoundResponse(context);
  }
  return HttpResponses.ok(merge(repository, payloads));
}
 
Example 4
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 5
Source File: NpmHandlers.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@Nonnull
@Override
public Response handle(@Nonnull final Context context) throws Exception {
  State state = context.getAttributes().require(TokenMatcher.State.class);
  Repository repository = context.getRepository();
  log.debug("[putDistTags] repository: {} tokens: {}", repository.getName(), state.getTokens());

  try {
    repository.facet(NpmHostedFacet.class)
        .deleteDistTags(packageId(state), state.getTokens().get(T_PACKAGE_TAG), context.getRequest().getPayload());
    return NpmResponses.ok();
  }
  catch (IOException e) { //NOSONAR
    return NpmResponses.badRequest(e.getMessage());
  }
}
 
Example 6
Source File: NpmHandlers.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@Nonnull
@Override
public Response handle(@Nonnull final Context context) throws Exception {
  State state = context.getAttributes().require(TokenMatcher.State.class);
  Repository repository = context.getRepository();
  log.debug("[deletePackage] repository: {} tokens: {}", repository.getName(), state.getTokens());

  NpmPackageId packageId = packageId(state);
  Set<String> deleted = repository.facet(NpmHostedFacet.class).deletePackage(packageId, revision(state));
  if (!deleted.isEmpty()) {
    return NpmResponses.ok();
  }
  else {
    return NpmResponses.packageNotFound(packageId);
  }
}
 
Example 7
Source File: NpmHandlers.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@Nonnull
@Override
public Response handle(@Nonnull final Context context) throws Exception {
  State state = context.getAttributes().require(TokenMatcher.State.class);
  Repository repository = context.getRepository();
  log.debug("[getTarball] repository: {} tokens: {}", repository.getName(), state.getTokens());

  NpmPackageId packageId = packageId(state);
  String tarballName = tarballName(state);
  Content content = repository.facet(NpmHostedFacet.class).getTarball(packageId, tarballName);
  if (content != null) {
    return NpmResponses.ok(content);
  }
  else {
    return NpmResponses.tarballNotFound(packageId, tarballName);
  }
}
 
Example 8
Source File: NpmHandlers.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@Nonnull
@Override
public Response handle(@Nonnull final Context context) throws Exception {
  State state = context.getAttributes().require(TokenMatcher.State.class);
  Repository repository = context.getRepository();
  log.debug("[deleteTarball] repository: {} tokens: {}", repository.getName(), state.getTokens());

  NpmPackageId packageId = packageId(state);
  String tarballName = tarballName(state);
  Set<String> deleted = repository.facet(NpmHostedFacet.class).deleteTarball(packageId, tarballName);
  if (!deleted.isEmpty()) {
    return NpmResponses.ok();
  }
  else {
    return NpmResponses.tarballNotFound(packageId, tarballName);
  }
}
 
Example 9
Source File: NpmHandlers.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@Nonnull
@Override
public Response handle(@Nonnull final Context context) throws Exception {
  State state = context.getAttributes().require(TokenMatcher.State.class);
  Repository repository = context.getRepository();
  log.debug("[putDistTags] repository: {} tokens: {}", repository.getName(), state.getTokens());

  try {
    repository.facet(NpmHostedFacet.class)
        .putDistTags(packageId(state), state.getTokens().get(T_PACKAGE_TAG), context.getRequest().getPayload());
    return NpmResponses.ok();
  }
  catch (IOException e) { //NOSONAR
    return NpmResponses.badRequest(e.getMessage());
  }
}
 
Example 10
Source File: NpmHandlers.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@Nonnull
@Override
public Response handle(@Nonnull final Context context) throws Exception {
  State state = context.getAttributes().require(TokenMatcher.State.class);
  Repository repository = context.getRepository();
  log.debug("[getPackage] repository: {} tokens: {}", repository.getName(), state.getTokens());

  NpmPackageId packageId = packageId(state);
  Content content = repository.facet(NpmHostedFacet.class)
      .getDistTags(packageId);
  if (content != null) {
    return NpmResponses.ok(content);
  }
  else {
    return NpmResponses.packageNotFound(packageId);
  }
}
 
Example 11
Source File: OrientArchetypeCatalogHandler.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 {
  MavenPath path = context.getAttributes().require(MavenPath.class);
  Repository repository = context.getRepository();
  String action = context.getRequest().getAction();
  switch (action) {
    case GET:
    case HEAD:
      return doGet(path, repository);

    default:
      return HttpResponses.methodNotAllowed(context.getRequest().getAction(), GET, HEAD);
  }
}
 
Example 12
Source File: RawContentHandler.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 path = contentPath(context);
  String method = context.getRequest().getAction();

  Repository repository = context.getRepository();
  log.debug("{} repository '{}' content-path: {}", method, repository.getName(), path);

  RawContentFacet storage = repository.facet(RawContentFacet.class);

  switch (method) {
    case HEAD:
    case GET: {
      return storage.get(path).map(HttpResponses::ok)
          .orElseGet(() -> HttpResponses.notFound(path));
    }

    case PUT: {
      Payload content = context.getRequest().getPayload();
      storage.put(path, content);
      return HttpResponses.created();
    }

    case DELETE: {
      boolean deleted = storage.delete(path);
      if (deleted) {
        return HttpResponses.noContent();
      }
      return HttpResponses.notFound(path);
    }

    default:
      return HttpResponses.methodNotAllowed(method, GET, HEAD, PUT, DELETE);
  }
}
 
Example 13
Source File: NpmHandlers.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 {
  State state = context.getAttributes().require(TokenMatcher.State.class);
  Repository repository = context.getRepository();
  log.debug("[deleteToken] repository: {} tokens: {}", repository.getName(), state.getTokens());

  return repository.facet(NpmTokenFacet.class).logout(context);
}
 
Example 14
Source File: NpmHandlers.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 {
  State state = context.getAttributes().require(TokenMatcher.State.class);
  Repository repository = context.getRepository();
  log.debug("[createToken] repository: {} tokens: {}", repository.getName(), state.getTokens());

  return repository.facet(NpmTokenFacet.class).login(context);
}
 
Example 15
Source File: NpmHandlers.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 {
  Repository repository = context.getRepository();
  Parameters parameters = context.getRequest().getParameters();
  log.debug("[searchV1] repository: {} parameters: {}", repository.getName(), parameters);

  return NpmResponses.ok(repository.facet(NpmSearchFacet.class).searchV1(parameters));
}
 
Example 16
Source File: NpmHandlers.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 {
  Repository repository = context.getRepository();
  Parameters parameters = context.getRequest().getParameters();
  log.debug("[searchIndex] repository: {} parameters: {}", repository.getName(), parameters);

  return NpmResponses.ok(repository.facet(NpmSearchIndexFacet.class).searchIndex(indexSince(parameters)));
}
 
Example 17
Source File: MavenContentHandler.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public Response handle(@Nonnull final Context context) throws Exception {
  MavenPath mavenPath = contentPath(context);
  String path = mavenPath.getPath();
  String method = context.getRequest().getAction();
  Repository repository = context.getRepository();

  log.debug("{} repository '{}' content-path: {}", method, repository.getName(), path);

  MavenContentFacet storage = repository.facet(MavenContentFacet.class);

  switch (method) {
    case HEAD:
    case GET:
      return doGet(path, storage);

    case PUT:
      doPut(context, mavenPath, storage);
      return HttpResponses.created();

    case DELETE:
      return doDelete(mavenPath, storage);

    default:
      return HttpResponses.methodNotAllowed(method, GET, HEAD, PUT, DELETE);
  }
}
 
Example 18
Source File: AptSnapshotHandler.java    From nexus-repository-apt with Eclipse Public License 1.0 5 votes vote down vote up
private Response handleSnapshotFetchRequest(Context context, String id, String path) throws Exception {
  Repository repository = context.getRepository();
  AptSnapshotFacet snapshotFacet = repository.facet(AptSnapshotFacet.class);
  if (snapshotFacet.isSnapshotableFile(path)) {
    Content content = snapshotFacet.getSnapshotFile(id, path);
    return content == null ? HttpResponses.notFound() : HttpResponses.ok(content);
  }
  context.getAttributes().set(AptSnapshotHandler.State.class, new AptSnapshotHandler.State(path));
  return context.proceed();
}
 
Example 19
Source File: AptSnapshotHandler.java    From nexus-repository-apt with Eclipse Public License 1.0 5 votes vote down vote up
private Response handleSnapshotAdminRequest(Context context, String id) throws Exception {
  String method = context.getRequest().getAction();
  Repository repository = context.getRepository();
  AptSnapshotFacet snapshotFacet = repository.facet(AptSnapshotFacet.class);

  switch (method) {
    case MKCOL: {
      snapshotFacet.createSnapshot(id, new AllSnapshotComponentSelector());
      return HttpResponses.created();
    }

    case PUT: {
      try (InputStream is = context.getRequest().getPayload().openInputStream()) {
        ControlFile settings = new ControlFileParser().parseControlFile(is);
        snapshotFacet.createSnapshot(id, new FilteredSnapshotComponentSelector(settings));
      }
      return HttpResponses.created();
    }

    case DELETE: {
      snapshotFacet.deleteSnapshot(id);
      return HttpResponses.noContent();
    }

    default:
      return HttpResponses.methodNotAllowed(method, DELETE, MKCOL, PUT);
  }
}
 
Example 20
Source File: MavenArchetypeCatalogHandler.java    From nexus-public with Eclipse Public License 1.0 4 votes vote down vote up
private Response generateArchetypeCatalog(final Context context) throws Exception {
  Repository repository = context.getRepository();
  MavenArchetypeCatalogFacet archetypeCatalogFacet = repository.facet(MavenArchetypeCatalogFacet.class);
  archetypeCatalogFacet.rebuildArchetypeCatalog();
  return context.proceed();
}