Skip to content

Commit

Permalink
For destination path without filename information use source file name
Browse files Browse the repository at this point in the history
 * Uses filename from nexus redirect location header

refs #10
  • Loading branch information
Daniel Hammann committed Aug 16, 2016
1 parent ee4c639 commit 2c61c8f
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 3 deletions.
Expand Up @@ -97,7 +97,10 @@ public class NexusDeliverArtifactStepPlugin implements StepPlugin {

String customDestinationPath = destinationPath;
if (destinationPath.endsWith("/")) {
customDestinationPath = customDestinationPath + tempFile.getName();

def originalFileName = NexusHttpRequestHandler.resolveArtifactFileName(nexus, nexusUser, nexusPassword, query)

customDestinationPath = customDestinationPath + (originalFileName ? originalFileName : tempFile.getName());
}
context.getNodes().each { node ->
def nodedata = DataContextUtils.nodeData(node)
Expand Down
Expand Up @@ -6,7 +6,8 @@ import groovyx.net.http.HTTPBuilder
import groovyx.net.http.Method
import groovyx.net.http.URIBuilder
import groovyx.net.http.ContentType

import org.apache.http.impl.client.AbstractHttpClient
import org.apache.http.params.BasicHttpParams

class NexusHttpRequestHandler {

Expand All @@ -16,9 +17,51 @@ class NexusHttpRequestHandler {
FileNotFound, UnexepectedFailure
}

static resolveArtifactFileName(host, user, password, query) {

def redirectLocation
def uri = buildUri(host, query)

def http = new HTTPBuilder(uri)

AbstractHttpClient ahc = http.client
BasicHttpParams params = new BasicHttpParams();
params.setParameter("http.protocol.handle-redirects",false)
ahc.setParams(params)

if (user && password) {
http.auth.basic user, password
}

http.request( Method.GET, ContentType.ANY ) {

response.'307' = { rsp ->
redirectLocation = rsp.headers?.Location
}

response.failure = { resp ->
throw new StepException("Status code: ${resp.status}. Uri: ${uri}", Reason.UnexepectedFailure)
}

response.'404' = { resp ->
throw new StepException("Artifact not found in matching query: ${uri}", Reason.FileNotFound)
}
}

if (redirectLocation) {
def fileName = redirectLocation.substring(redirectLocation.lastIndexOf("/") + 1)
println "Nexus artifact filename: ${fileName}"

return fileName
}

throw new StepException("Location Header was not set for given uri: ${uri}", Reason.UnexepectedFailure)

}

static handleRequest(host, user, password, query, successHandler) {

def uri = new URIBuilder(host + REDIRECT_URL).setQuery(query);
def uri = buildUri(host, query)

println "Nexus request URL: ${uri}"

Expand All @@ -42,6 +85,10 @@ class NexusHttpRequestHandler {
}
}

static buildUri (host, query) {
return new URIBuilder(host + REDIRECT_URL).setQuery(query)
}

static buildQuery(group , artifact, version, repo, packaging, classifier) {

def query = [g: group, a: artifact, v: version, r: repo, p: packaging]
Expand Down

0 comments on commit 2c61c8f

Please sign in to comment.