Java Code Examples for org.sonatype.nexus.repository.http.HttpResponses#created()

The following examples show how to use org.sonatype.nexus.repository.http.HttpResponses#created() . 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: NpmTokenFacetImpl.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public Response login(final Context context) {
  final Payload payload = context.getRequest().getPayload();
  if (payload == null) {
    return NpmResponses.badRequest("Missing body");
  }
  StorageFacet storageFacet = facet(StorageFacet.class);
  try (TempBlob tempBlob = storageFacet.createTempBlob(payload, NpmFacetUtils.HASH_ALGORITHMS)) {
    NestedAttributesMap request = NpmJsonUtils.parse(tempBlob);
    String token = npmTokenManager.login(request.get("name", String.class), request.get("password", String.class));
    if (null != token) {
      NestedAttributesMap response = new NestedAttributesMap("response", Maps.newHashMap());
      response.set("ok", Boolean.TRUE.toString());
      response.set("rev", "_we_dont_use_revs_any_more");
      response.set("id", "org.couchdb.user:undefined");
      response.set("token", token);
      return HttpResponses.created(new BytesPayload(NpmJsonUtils.bytes(response), ContentTypes.APPLICATION_JSON));
    }
    else {
      return NpmResponses.badCredentials("Bad username or password");
    }
  }
  catch (IOException e) {
    throw new RuntimeException(e);
  }
}
 
Example 2
Source File: AptHostedHandler.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
private Response doPost(final Context context,
                        final String path,
                        final String method,
                        final AptHostedFacet hostedFacet) throws IOException
{
  if ("rebuild-indexes".equals(path)) {
    hostedFacet.rebuildIndexes();
    return HttpResponses.ok();
  }
  else if ("".equals(path)) {
    hostedFacet.ingestAsset(context.getRequest().getPayload());
    return HttpResponses.created();
  }
  else {
    return HttpResponses.methodNotAllowed(method, GET, HEAD);
  }
}
 
Example 3
Source File: AptHostedHandler.java    From nexus-repository-apt with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public Response handle(Context context) throws Exception {
  String path = assetPath(context);
  String method = context.getRequest().getAction();

  AptFacet aptFacet = context.getRepository().facet(AptFacet.class);
  AptHostedFacet hostedFacet = context.getRepository().facet(AptHostedFacet.class);

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

    case POST: {
      if (path.equals("rebuild-indexes")) {
        hostedFacet.rebuildIndexes();
        return HttpResponses.ok();
      }
      else if (path.equals("")) {
        hostedFacet.ingestAsset(context.getRequest().getPayload());
        return HttpResponses.created();
      }
      else {
        return HttpResponses.methodNotAllowed(method, GET, HEAD);
      }
    }

    default:
      return HttpResponses.methodNotAllowed(method, GET, HEAD, POST);
  }
}
 
Example 4
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 5
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 6
Source File: HostedHandler.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
private Response doPut(@Nonnull final Context context, final MavenPath path, final MavenFacet mavenFacet)
    throws IOException
{
  if (mavenFacet.layoutPolicy() == LayoutPolicy.STRICT
      && isValidSnapshot(path.getCoordinates())
      && !mavenFacet.getMavenPathParser().isRepositoryMetadata(path)) {
    throw new IllegalOperationException("Invalid path for a Maven 2 repository");
  }
  mavenFacet.put(path, context.getRequest().getPayload());
  return HttpResponses.created();
}
 
Example 7
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 8
Source File: AptSnapshotHandler.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
private Response doPut(final Context context, final String id, final AptSnapshotFacet snapshotFacet)
    throws IOException
{
  try (InputStream is = context.getRequest().getPayload().openInputStream()) {
    ControlFile settings = new ControlFileParser().parseControlFile(is);
    snapshotFacet.createSnapshot(id, new FilteredSnapshotComponentSelector(settings));
  }
  return HttpResponses.created();
}
 
Example 9
Source File: AptSnapshotHandler.java    From nexus-public with Eclipse Public License 1.0 4 votes vote down vote up
private Response doMkcol(final String id, final AptSnapshotFacet snapshotFacet) throws IOException {
  snapshotFacet.createSnapshot(id, new AllSnapshotComponentSelector());
  return HttpResponses.created();
}