cstamas commented on code in PR #712:
URL: https://github.com/apache/maven/pull/712#discussion_r847184452


##########
maven-core/src/main/java/org/apache/maven/artifact/transform/ConsumerPomTransformer.java:
##########
@@ -0,0 +1,140 @@
+package org.apache.maven.artifact.transform;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import javax.inject.Inject;
+import javax.inject.Named;
+import javax.inject.Provider;
+import javax.inject.Singleton;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.UncheckedIOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.nio.file.attribute.FileTime;
+import java.util.ArrayList;
+import java.util.Collection;
+
+import org.apache.maven.execution.MavenSession;
+import org.apache.maven.feature.Features;
+import org.apache.maven.model.building.TransformerContext;
+import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
+import org.eclipse.aether.RepositorySystemSession;
+import org.eclipse.aether.artifact.Artifact;
+import org.eclipse.aether.deployment.DeployRequest;
+import org.eclipse.aether.installation.InstallRequest;
+
+/**
+ * Consumer POM install and deploy transformer.
+ */
+@Named( ConsumerPomTransformer.NAME )
+@Singleton
+public class ConsumerPomTransformer implements InstallRequestTransformer, 
DeployRequestTransformer
+{
+    public static final String NAME = "consumer-pom";
+
+    private final Provider<MavenSession> mavenSessionProvider;
+
+    @Inject
+    public ConsumerPomTransformer( Provider<MavenSession> mavenSessionProvider 
)
+    {
+        this.mavenSessionProvider = mavenSessionProvider;
+    }
+
+    @Override
+    public DeployRequest transformDeployRequest( RepositorySystemSession 
session,
+                                                 DeployRequest deployRequest )
+    {
+        MavenSession mavenSession = mavenSessionProvider.get();
+        if ( !Features.buildConsumer( mavenSession.getUserProperties() 
).isActive() )
+        {
+            return deployRequest;
+        }
+        return deployRequest.setArtifacts( transformArtifacts( mavenSession, 
deployRequest.getArtifacts() ) );
+    }
+
+    @Override
+    public InstallRequest transformInstallRequest( RepositorySystemSession 
session,
+                                                   InstallRequest 
installRequest )
+    {
+        MavenSession mavenSession = mavenSessionProvider.get();
+        if ( !Features.buildConsumer( mavenSession.getUserProperties() 
).isActive() )
+        {
+            return installRequest;
+        }
+        return installRequest.setArtifacts( transformArtifacts( mavenSession, 
installRequest.getArtifacts() ) );
+    }
+
+    private Collection<Artifact> transformArtifacts( MavenSession 
mavenSession, Collection<Artifact> artifacts )
+    {
+        TransformerContext context = (TransformerContext) mavenSession
+                .getRepositorySession().getData().get( TransformerContext.KEY 
);
+        if ( context == null )
+        {
+            return artifacts;
+        }
+        ArrayList<Artifact> result = new ArrayList<>( artifacts.size() );
+        for ( Artifact artifact : artifacts )
+        {
+            if ( "pom".equals( artifact.getExtension() ) )
+            {
+                Path buildOutputDirectory = Paths.get(
+                        
mavenSession.getCurrentProject().getBuild().getDirectory() );
+                Path originalPom = artifact.getFile().toPath();
+                Path transformedPom = buildOutputDirectory.resolve( NAME + 
".xml" );
+                try
+                {
+                    FileTime originalPomTs = Files.getLastModifiedTime( 
originalPom );
+                    FileTime transformedPomTs = Files.isRegularFile( 
transformedPom )
+                            ? Files.getLastModifiedTime( transformedPom ) : 
null;
+
+                    if ( !originalPomTs.equals( transformedPomTs ) )
+                    {
+                        // save it: either does not exist or TS differ
+                        Files.createDirectories( transformedPom.getParent() );
+                        try ( InputStream inputStream = new 
ConsumerModelSourceTransformer()
+                                .transform( originalPom, context ) )
+                        {
+                            Files.copy( inputStream, transformedPom );

Review Comment:
   Am unsure what backdoor would be solved by not using temporary file. True, 
here am using component name, but the file name could be randomized instead, 
and effectively it would prevent any code to tamper with it.
   
   Resolver is all about `java.io.Files`, so no, resolver does not allow 
passing over InputStream. Moreover, misuse of it's internal APIs (like it 
happened with FileTransformer), where 
`org.eclipse.aether.spi.connector.transport.PutTask#dataBytes` (meant and used 
for checksums only, by resolver itself) was used to transfer rewritten POM is 
just IMHO merely wrong: it misuses resolver API to prevent Maven API misuse....?
   
   This whole "do not save the file" to me looks like some sort of "premature 
optimization" parallel: it is assumed, that not saving transformed POM prevents 
some sort of backdoor, that we are actually not even sure about what it is....  
Let's solve problems when we actually hit them IMHO.
   
   Finally, IMHO all this feature is really Maven concern, not Resolver 
concern. And true, maven-3.x does not have any "callback" to transform "just in 
time" when m-install-p or m-deploy-p calls (directly) into resolver, but IMHO 
this is much cleaner solution, and you are using proper APIs just to "swap out" 
(replace) the artifact about to be deploy to another one. Also, it works, as 
ITs are showing. Hence, I think this should be solved within realm of Maven, 
and not have Resolver involved into this at all.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to