protected Model getPom(Variant variant, Request request, Response response) throws ResourceException { Form form = request.getResourceRef().getQueryAsForm(); // TODO: enable only one section retrieval of POM, ie. only mailing lists, or team members String groupId = form.getFirstValue("g"); String artifactId = form.getFirstValue("a"); String version = form.getFirstValue("v"); String repositoryId = form.getFirstValue("r"); if (groupId == null || artifactId == null || version == null || repositoryId == null) { throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST); } ArtifactStoreRequest gavRequest = getResourceStoreRequest( request, false, repositoryId, groupId, artifactId, version, null, null, "pom"); try { MavenRepository mavenRepository = getMavenRepository(repositoryId); ArtifactStoreHelper helper = mavenRepository.getArtifactStoreHelper(); InputStream pomContent = null; InputStreamReader ir = null; Model pom = null; try { StorageFileItem file = helper.retrieveArtifactPom(gavRequest); pomContent = file.getInputStream(); MavenXpp3Reader reader = new MavenXpp3Reader(); ir = new InputStreamReader(pomContent); pom = reader.read(ir); } finally { IOUtil.close(pomContent); IOUtil.close(ir); } return pom; } catch (Exception e) { handleException(request, response, e); } return null; }
@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; }
protected Object getContent( Variant variant, boolean redirectTo, Request request, Response response) throws ResourceException { Form form = request.getResourceRef().getQueryAsForm(); String groupId = form.getFirstValue("g"); String artifactId = form.getFirstValue("a"); String version = form.getFirstValue("v"); String packaging = form.getFirstValue("p"); String classifier = form.getFirstValue("c"); String repositoryId = form.getFirstValue("r"); String extension = form.getFirstValue("e"); if (groupId == null || artifactId == null || version == null || repositoryId == null) { throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST); } // default the packaging if (StringUtils.isBlank(packaging)) { packaging = "jar"; } ArtifactStoreRequest gavRequest = getResourceStoreRequest( request, false, repositoryId, groupId, artifactId, version, packaging, classifier, extension); try { MavenRepository mavenRepository = getMavenRepository(repositoryId); ArtifactStoreHelper helper = mavenRepository.getArtifactStoreHelper(); StorageFileItem file = helper.retrieveArtifact(gavRequest); if (redirectTo) { Reference fileReference = createRepositoryReference( request, file.getRepositoryItemUid().getRepository().getId(), file.getRepositoryItemUid().getPath()); response.setLocationRef(fileReference); response.setStatus(Status.REDIRECTION_PERMANENT); String redirectMessage = "If you are not automatically redirected use this url: " + fileReference.toString(); return redirectMessage; } else { Representation result = new StorageFileItemRepresentation(file); result.setDownloadable(true); result.setDownloadName(file.getName()); return result; } } catch (Exception e) { handleException(request, response, e); } return null; }