ArtifactResource extends AbstractResource { @POST public Response postArtifact(@Auth final DbCredential credential, final Artifact artifact){ if(!credential.getRoles().contains(AvailableRoles.DATA_UPDATER)){ throw new WebApplicationException(Response.status(Response.Status.UNAUTHORIZED).build()); } LOG.info("Got a post Artifact request [" + artifact.getGavc() + "]"); if(artifact.getOrigin() == null || artifact.getOrigin().isEmpty()){ artifact.setOrigin("maven"); } DataValidator.validatePostArtifact(artifact); final ArtifactHandler artifactHandler = getArtifactHandler(); final DbArtifact artifactWithSameSHA = artifactHandler.getArtifactUsingSHA256(artifact.getSha256()); if(artifactWithSameSHA != null){ throw new WebApplicationException(Response.serverError().status(HttpStatus.CONFLICT_409) .entity("Artifact with same checksum already exists.").build()); } DbArtifact artifactWithSameGAVC = null; try{ artifactWithSameGAVC = artifactHandler.getArtifact(artifact.getGavc()); }catch(WebApplicationException e){ LOG.info("Validating result for artifact GAVC: No matching artifact found " + e); } if(artifactWithSameGAVC != null){ throw new WebApplicationException(Response.serverError().status(HttpStatus.CONFLICT_409) .entity("Artifact with same GAVC already exists.").build()); } artifact.setCreatedDateTime(new Date()); final DbArtifact dbArtifact = getModelMapper().getDbArtifact(artifact); artifactHandler.store(dbArtifact); for(final String license: artifact.getLicenses()){ artifactHandler.addLicense(dbArtifact.getGavc(), license); } return Response.ok().status(HttpStatus.CREATED_201).build(); } ArtifactResource(final RepositoryHandler repoHandler, final GrapesServerConfig dmConfig); @POST Response postArtifact(@Auth final DbCredential credential, final Artifact artifact); @GET @Produces({MediaType.TEXT_HTML, MediaType.APPLICATION_JSON}) @Path(ServerAPI.GET_GAVCS) Response getGavcs(@Context final UriInfo uriInfo); @GET @Produces({MediaType.TEXT_HTML, MediaType.APPLICATION_JSON}) @Path(ServerAPI.GET_GROUPIDS) Response getGroupIds(@Context final UriInfo uriInfo); @GET @Produces({MediaType.TEXT_HTML, MediaType.APPLICATION_JSON}) @Path("/{gavc}" + ServerAPI.GET_VERSIONS) Response getVersions(@PathParam("gavc") final String gavc); @GET @Produces(MediaType.APPLICATION_JSON) @Path("/{gavc}" + ServerAPI.GET_LAST_VERSION) Response getLastVersion(@PathParam("gavc") final String gavc); @GET @Produces({MediaType.TEXT_HTML, MediaType.APPLICATION_JSON}) @Path("/{gavc}") Response get(@PathParam("gavc") final String gavc); @GET @Produces(MediaType.APPLICATION_JSON) @Path("/isPromoted") Response isPromoted(@QueryParam("user") final String user,
                               @QueryParam("stage") final int stage,
                               @QueryParam("name") final String filename,
                               @QueryParam("sha256") final String sha256,
                               @QueryParam("type") final String type,
                               @QueryParam("location") final String location); @POST @Path("/{gavc}" + ServerAPI.GET_DOWNLOAD_URL) Response updateDownloadUrl(@Auth final DbCredential credential, @PathParam("gavc") final String gavc, @QueryParam(ServerAPI.URL_PARAM) final String downLoadUrl); @POST @Path("/{gavc}" + ServerAPI.GET_PROVIDER) Response updateProvider(@Auth final DbCredential credential, @PathParam("gavc") final String gavc, @QueryParam(ServerAPI.PROVIDER_PARAM) final String provider); @DELETE @Path("/{gavc}") Response delete(@Auth final DbCredential credential, @PathParam("gavc") final String gavc); @POST @Path("/{gavc}" + ServerAPI.SET_DO_NOT_USE) Response postDoNotUse(@Auth final DbCredential credential,
                                 @PathParam("gavc") final String gavc,
                                 @QueryParam(ServerAPI.DO_NOT_USE) final BooleanParam doNotUse,
                                 final String commentText); @GET @Produces(MediaType.APPLICATION_JSON) @Path("/{gavc}" + ServerAPI.SET_DO_NOT_USE) Response getDoNotUse(@PathParam("gavc") final String gavc); @GET @Produces({MediaType.TEXT_HTML, MediaType.APPLICATION_JSON}) @Path("/{gavc}" + ServerAPI.GET_ANCESTORS) Response getAncestors(@PathParam("gavc") final String gavc, @Context final UriInfo uriInfo); @GET @Produces({MediaType.APPLICATION_JSON}) @Path("/{gavc}" + ServerAPI.GET_LICENSES) Response getLicenses(@PathParam("gavc") final String gavc, @Context final UriInfo uriInfo); @POST @Path("/{gavc}" + ServerAPI.GET_LICENSES) Response addLicense(@Auth final DbCredential credential,
                               @PathParam("gavc") final String gavc,
                               @QueryParam(ServerAPI.LICENSE_ID_PARAM) final String licenseId); @DELETE @Path("/{gavc}" + ServerAPI.GET_LICENSES) Response deleteLicense(@Auth final DbCredential credential,
                                  @PathParam("gavc") final String gavc,
                                  @QueryParam(ServerAPI.LICENSE_ID_PARAM) final String licenseString); @GET @Produces({MediaType.TEXT_HTML, MediaType.APPLICATION_JSON}) @Path("/{gavc}" + ServerAPI.GET_MODULE) Response getModule(@PathParam("gavc") final String gavc, @Context final UriInfo uriInfo); @GET @Produces({MediaType.TEXT_HTML, MediaType.APPLICATION_JSON}) @Path("/{gavc}" + ServerAPI.GET_ORGANIZATION) Response getOrganization(@PathParam("gavc") final String gavc, @Context final UriInfo uriInfo); @GET @Produces(MediaType.APPLICATION_JSON) @Path(ServerAPI.GET_ALL) Response getAll(@Context final UriInfo uriInfo);  }
@Test public void postArtifactTest(){ final String sha = "6554ed3d1ab007bd81d3d57ee27027510753d905277d5b5b8813e5bd516e821c"; Artifact artifact = DataModelFactory.createArtifact(GrapesTestUtils.CORPORATE_GROUPID_4TEST, "artifactId", "version", "classifier", "type", "extension"); artifact.setSha256(sha); client().addFilter(new HTTPBasicAuthFilter(USER_4TEST, PASSWORD_4TEST)); final WebResource resource = client().resource("/" + ServerAPI.ARTIFACT_RESOURCE); final ClientResponse response = resource.type(MediaType.APPLICATION_JSON).post(ClientResponse.class, artifact); assertNotNull(response); assertEquals(HttpStatus.CREATED_201, response.getStatus()); final ArgumentCaptor<DbArtifact> captor = ArgumentCaptor.forClass(DbArtifact.class); verify(repositoryHandler, times(1)).store(captor.capture()); assertEquals(sha, captor.getValue().getSha256()); }
@Test public void postArtifact() throws AuthenticationException, UnknownHostException { Artifact artifact = DataModelFactory.createArtifact("groupId", "artifactId", "version", "classifier", "type", "extension"); artifact.setSha256("6554ed3d1ab007bd81d3d57ee27027510753d905277d5b5b8813e5bd516e821c"); artifact.setDownloadUrl("downloadUrl"); artifact.setSize("size"); client().addFilter(new HTTPBasicAuthFilter(USER_4TEST, PASSWORD_4TEST)); WebResource resource = client().resource("/" + ServerAPI.ARTIFACT_RESOURCE); ClientResponse response = resource.type(MediaType.APPLICATION_JSON).post(ClientResponse.class, artifact); assertNotNull(response); assertEquals(HttpStatus.CREATED_201, response.getStatus()); }