Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -73,16 +73,16 @@
<jersey-media-moxy.version>2.0</jersey-media-moxy.version>
<jersey-json.version>1.19</jersey-json.version>
<file-management.version>1.2</file-management.version>
<apache-commons-text.version>1.8</apache-commons-text.version>

<!-- Plexus component versions -->
<plexus-compiler-api.version>2.5</plexus-compiler-api.version>
<plexus-utils.version>3.0.22</plexus-utils.version>
<plexus-compiler-api.version>2.8.5</plexus-compiler-api.version>
<plexus-utils.version>3.3.0</plexus-utils.version>
<plexus-build-api.version>0.0.7</plexus-build-api.version>
<plexus.version>3.0.22</plexus.version>
<plexus-resources.version>1.0.1</plexus-resources.version>
<plexus-io.version>2.6.1</plexus-io.version>
<plexus-component-annotations.version>1.6</plexus-component-annotations.version>
<plexus-component-metadata.version>1.6</plexus-component-metadata.version>
<plexus-resources.version>1.1.0</plexus-resources.version>
<plexus-io.version>3.2.0</plexus-io.version>
<plexus-component-annotations.version>2.1.0</plexus-component-annotations.version>
<plexus-component-metadata.version>2.1.0</plexus-component-metadata.version>
</properties>

<dependencies>
Expand Down Expand Up @@ -255,6 +255,12 @@
<version>2.2.3</version>
</dependency>

<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-text</artifactId>
<version>${apache-commons-text.version}</version>
</dependency>

</dependencies>

<distributionManagement>
Expand Down
53 changes: 53 additions & 0 deletions src/main/java/com/github/cjnygard/mvn/rest/Plugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
Expand All @@ -39,6 +40,8 @@
import javax.ws.rs.core.Response.Status.Family;

import org.apache.commons.io.IOUtils;
import org.apache.commons.text.StringSubstitutor;

import org.apache.maven.execution.MavenSession;
import org.apache.maven.model.FileSet;
import org.apache.maven.plugin.AbstractMojo;
Expand All @@ -50,7 +53,14 @@
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;
import org.apache.maven.project.MavenProjectHelper;

import org.apache.maven.settings.Settings;
import org.apache.maven.settings.Server;
import org.apache.maven.settings.crypto.DefaultSettingsDecryptionRequest;
import org.apache.maven.settings.crypto.SettingsDecrypter;
import org.apache.maven.settings.crypto.SettingsDecryptionRequest;
import org.apache.maven.settings.crypto.SettingsDecryptionResult;

//import org.codehaus.plexus.components.io.filemappers.AbstractFileMapper;
//import org.codehaus.plexus.components.io.filemappers.IdentityMapper;
import org.codehaus.plexus.components.io.filemappers.FileMapper;
Expand Down Expand Up @@ -201,6 +211,9 @@ public String toString()
@Parameter( defaultValue = "${settings}", readonly = true )
private Settings settings;

@Component
private SettingsDecrypter settingsDecrypter;

@Component
private MavenProjectHelper projectHelper;

Expand All @@ -226,6 +239,16 @@ public String toString()
@Parameter( defaultValue = "${project.build.directory}", readonly = true )
private File target;

/**
* A server Id corresponding to an entry in the settings.xml file, to obtain credentials.
*
* Decrypted credentials will be placed in properties <code>{serverId}.username</code>
* and <code>{serverid}.password</code> and may then be referenced in headers and
* queryParams.
*/
@Parameter( property = "serverId" )
private String serverId;

/**
* A URL path to the base of the REST request resource.
*
Expand Down Expand Up @@ -585,12 +608,39 @@ protected boolean validateOutputDir() throws MojoExecutionException
return true;
}


/**
* Provides access to server credentials encrypted within settings.xml.
*/
private Server decryptServerCredentials(String serverId) throws MojoExecutionException {
SettingsDecryptionRequest request = new DefaultSettingsDecryptionRequest( findServer(serverId) );
SettingsDecryptionResult result = settingsDecrypter.decrypt(request);
return result.getServer();
}

private Server findServer(String serverId) throws MojoExecutionException {
Server server = null;
for (Server s : settings.getServers()) {
if (s.getId().equals(serverId)) {
return( s );
}
}
throw new MojoExecutionException("serverId not found in settings: " + serverId);
}

@Override
public void execute() throws MojoExecutionException
{
validateOutputDir();
getLog().info( String.format( "Output dir [%s]", getOutputDir().toString() ) );

Properties mvnProps = project.getProperties();
if (serverId != null && serverId.length() > 0) {
Server serverCreds = decryptServerCredentials(serverId);
mvnProps.put(serverId+".username", serverCreds.getUsername());
mvnProps.put(serverId+".password", serverCreds.getPassword());
}

Client client = ClientBuilder.newClient();

WebTarget baseTarget = client.target( getEndpoint() );
Expand All @@ -599,12 +649,14 @@ public void execute() throws MojoExecutionException
getLog().debug( String.format( "Setting resource [%s]", getResource() ) );
baseTarget = baseTarget.path( getResource() );
}

// Load up the query parameters if they exist
if ( null != getQueryParams() )
{
for ( String k : getQueryParams().keySet() )
{
String param = getQueryParams().get( k );
param = StringSubstitutor.replace(param, mvnProps);
baseTarget = baseTarget.queryParam( k, param );
getLog().debug( String.format( "Param [%s:%s]", k, param ) );
}
Expand All @@ -617,6 +669,7 @@ public void execute() throws MojoExecutionException
for ( String k : getHeaders().keySet() )
{
String hdr = getHeaders().get( k );
hdr = StringSubstitutor.replace(hdr, mvnProps);
builder = builder.header( k, hdr );
getLog().debug( String.format( "Header [%s:%s]", k, hdr ) );
}
Expand Down