/** * Should serve by policies. * * @param uid the uid * @return true, if successful */ public boolean shouldServeByPolicies(ResourceStoreRequest request) { if (M1ArtifactRecognizer.isMetadata(request.getRequestPath())) { if (M1ArtifactRecognizer.isSnapshot(request.getRequestPath())) { return RepositoryPolicy.SNAPSHOT.equals(getRepositoryPolicy()); } else { // metadatas goes always return true; } } // we are using Gav to test the path Gav gav = null; try { gav = getGavCalculator().pathToGav(request.getRequestPath()); } catch (IllegalArtifactCoordinateException e) { getLogger() .info("Illegal artifact path: '" + request.getRequestPath() + "'" + e.getMessage()); return false; } if (gav == null) { return true; } else { if (gav.isSnapshot()) { // snapshots goes if enabled return RepositoryPolicy.SNAPSHOT.equals(getRepositoryPolicy()); } else { return RepositoryPolicy.RELEASE.equals(getRepositoryPolicy()); } } }
public void createSnapshotRepo(String repoId) throws IOException { RepositoryResource repo = new RepositoryResource(); repo.setProvider("ivy"); repo.setFormat("maven2"); repo.setRepoPolicy("snapshot"); repo.setChecksumPolicy("ignore"); repo.setBrowseable(false); repo.setId(repoId); repo.setName(repoId); repo.setRepoType("hosted"); repo.setWritePolicy(RepositoryWritePolicy.ALLOW_WRITE.name()); repo.setDownloadRemoteIndexes(true); repo.setBrowseable(true); repo.setRepoPolicy(RepositoryPolicy.SNAPSHOT.name()); repo.setChecksumPolicy(ChecksumPolicy.IGNORE.name()); repo.setIndexable(true); // being sure!!! repoUtil.createRepository(repo); repo = (RepositoryResource) repoUtil.getRepository(repoId); // assert that the repository is created Assert.assertTrue(repo.isIndexable()); }
protected boolean versionMatchesPolicy(String version, RepositoryPolicy policy) { boolean result = false; if ((RepositoryPolicy.SNAPSHOT.equals(policy) && VersionUtils.isSnapshot(version)) || (RepositoryPolicy.RELEASE.equals(policy) && !VersionUtils.isSnapshot(version))) { result = true; } return result; }
/** * Removes the snapshots from maven repository. * * @param repository the repository * @throws Exception the exception */ protected SnapshotRemovalRepositoryResult removeSnapshotsFromMavenRepository( MavenRepository repository, SnapshotRemovalRequest request) { TaskUtil.checkInterruption(); SnapshotRemovalRepositoryResult result = new SnapshotRemovalRepositoryResult(repository.getId(), 0, 0, true); if (!repository.getLocalStatus().shouldServiceRequest()) { return result; } // we are already processed here, so skip repo if (request.isProcessedRepo(repository.getId())) { return new SnapshotRemovalRepositoryResult(repository.getId(), true); } request.addProcessedRepo(repository.getId()); // if this is not snap repo, do nothing if (!RepositoryPolicy.SNAPSHOT.equals(repository.getRepositoryPolicy())) { return result; } if (getLogger().isDebugEnabled()) { getLogger() .debug( "Collecting deletable snapshots on repository " + repository.getId() + " from storage directory " + repository.getLocalUrl()); } request.getMetadataRebuildPaths().clear(); // create a walker to collect deletables and let it loose on collections only SnapshotRemoverWalkerProcessor snapshotRemoveProcessor = new SnapshotRemoverWalkerProcessor(repository, request); DefaultWalkerContext ctxMain = new DefaultWalkerContext( repository, new ResourceStoreRequest("/"), new DottedStoreWalkerFilter()); ctxMain.getProcessors().add(snapshotRemoveProcessor); walker.walk(ctxMain); if (ctxMain.getStopCause() != null) { result.setSuccessful(false); } // and collect results result.setDeletedSnapshots(snapshotRemoveProcessor.getDeletedSnapshots()); result.setDeletedFiles(snapshotRemoveProcessor.getDeletedFiles()); if (getLogger().isDebugEnabled()) { getLogger() .debug( "Collected and deleted " + snapshotRemoveProcessor.getDeletedSnapshots() + " snapshots with alltogether " + snapshotRemoveProcessor.getDeletedFiles() + " files on repository " + repository.getId()); } repository.expireCaches(new ResourceStoreRequest(RepositoryItemUid.PATH_ROOT)); RecreateMavenMetadataWalkerProcessor metadataRebuildProcessor = new RecreateMavenMetadataWalkerProcessor(getLogger()); for (String path : request.getMetadataRebuildPaths()) { DefaultWalkerContext ctxMd = new DefaultWalkerContext( repository, new ResourceStoreRequest(path), new DottedStoreWalkerFilter()); ctxMd.getProcessors().add(metadataRebuildProcessor); try { walker.walk(ctxMd); } catch (WalkerException e) { if (!(e.getCause() instanceof ItemNotFoundException)) { // do not ignore it throw e; } } } return result; }
@Override public Object upload(Context context, Request request, Response response, List<FileItem> files) throws ResourceException { // we have "nibbles": (params,fileA,[fileB])+ // the second file is optional // if two files are present, one of them should be POM String repositoryId = null; boolean hasPom = false; boolean isPom = false; InputStream is = null; String groupId = null; String artifactId = null; String version = null; String classifier = null; String packaging = null; String extension = null; ArtifactCoordinate coords = null; PomArtifactManager pomManager = new PomArtifactManager(getNexus().getNexusConfiguration().getTemporaryDirectory()); try { for (FileItem fi : files) { if (fi.isFormField()) { // a parameter if ("r".equals(fi.getFieldName())) { repositoryId = fi.getString(); } else if ("g".equals(fi.getFieldName())) { groupId = fi.getString(); } else if ("a".equals(fi.getFieldName())) { artifactId = fi.getString(); } else if ("v".equals(fi.getFieldName())) { version = fi.getString(); } else if ("p".equals(fi.getFieldName())) { packaging = fi.getString(); } else if ("c".equals(fi.getFieldName())) { classifier = fi.getString(); } else if ("e".equals(fi.getFieldName())) { extension = fi.getString(); } else if ("hasPom".equals(fi.getFieldName())) { hasPom = Boolean.parseBoolean(fi.getString()); } coords = new ArtifactCoordinate(); coords.setGroupId(groupId); coords.setArtifactId(artifactId); coords.setVersion(version); coords.setPackaging(packaging); } else { // a file isPom = fi.getName().endsWith(".pom") || fi.getName().endsWith("pom.xml"); ArtifactStoreRequest gavRequest = null; if (hasPom) { if (isPom) { // let it "thru" the pomManager to be able to get GAV from it on later pass pomManager.storeTempPomFile(fi.getInputStream()); is = pomManager.getTempPomFileInputStream(); } else { is = fi.getInputStream(); } try { coords = pomManager.getArtifactCoordinateFromTempPomFile(); } catch (IOException e) { getLogger().info(e.getMessage()); throw new ResourceException( Status.CLIENT_ERROR_BAD_REQUEST, "Error occurred while reading the POM file. Malformed POM?"); } if (isPom) { gavRequest = getResourceStoreRequest( request, true, repositoryId, coords.getGroupId(), coords.getArtifactId(), coords.getVersion(), coords.getPackaging(), null, null); } else { gavRequest = getResourceStoreRequest( request, true, repositoryId, coords.getGroupId(), coords.getArtifactId(), coords.getVersion(), coords.getPackaging(), classifier, extension); } } else { is = fi.getInputStream(); gavRequest = getResourceStoreRequest( request, true, repositoryId, groupId, artifactId, version, packaging, classifier, extension); } MavenRepository mr = gavRequest.getMavenRepository(); ArtifactStoreHelper helper = mr.getArtifactStoreHelper(); // temporarily we disable SNAPSHOT upload // check is it a Snapshot repo if (RepositoryPolicy.SNAPSHOT.equals(mr.getRepositoryPolicy())) { getLogger() .info("Upload to SNAPSHOT maven repository attempted, returning Bad Request."); throw new ResourceException( Status.CLIENT_ERROR_BAD_REQUEST, "This is a Maven SNAPSHOT repository, and manual upload against it is forbidden!"); } if (!versionMatchesPolicy(gavRequest.getVersion(), mr.getRepositoryPolicy())) { getLogger() .warn("Version (" + gavRequest.getVersion() + ") and Repository Policy mismatch"); throw new ResourceException( Status.CLIENT_ERROR_BAD_REQUEST, "The version " + gavRequest.getVersion() + " does not match the repository policy!"); } if (isPom) { helper.storeArtifactPom(gavRequest, is, null); isPom = false; } else { if (hasPom) { helper.storeArtifact(gavRequest, is, null); } else { helper.storeArtifactWithGeneratedPom(gavRequest, packaging, is, null); } } } } } catch (Throwable t) { return buildUploadFailedHtmlResponse(t, request, response); } finally { if (hasPom) { pomManager.removeTempPomFile(); } } return coords; }