Added template variables for request driven URL calculation. (#416)

Signed-off-by: kaizimmerm <kai.zimmermann@bosch-si.com>
This commit is contained in:
Kai Zimmermann
2017-01-18 18:20:26 +01:00
committed by GitHub
parent 59aba08666
commit 80d11494b6
6 changed files with 172 additions and 29 deletions

View File

@@ -36,6 +36,7 @@ import org.eclipse.hawkbit.repository.model.Artifact;
import org.eclipse.hawkbit.repository.model.Target;
import org.eclipse.hawkbit.tenancy.TenantAware;
import org.springframework.hateoas.Link;
import org.springframework.http.HttpRequest;
import com.google.common.base.Charsets;
@@ -49,10 +50,12 @@ public final class DataConversionHelper {
}
static List<DdiChunk> createChunks(final Target target, final Action uAction,
final ArtifactUrlHandler artifactUrlHandler, final SystemManagement systemManagement) {
final ArtifactUrlHandler artifactUrlHandler, final SystemManagement systemManagement,
final HttpRequest request) {
return uAction.getDistributionSet().getModules().stream()
.map(module -> new DdiChunk(mapChunkLegacyKeys(module.getType().getKey()), module.getVersion(),
module.getName(), createArtifacts(target, module, artifactUrlHandler, systemManagement)))
module.getName(),
createArtifacts(target, module, artifactUrlHandler, systemManagement, request)))
.collect(Collectors.toList());
}
@@ -68,30 +71,18 @@ public final class DataConversionHelper {
return key;
}
/**
* Creates all (rest) artifacts for a given software module.
*
* @param target
* for create URLs for
* @param module
* the software module
* @param artifactUrlHandler
* for creating download URLs
* @param systemManagement
* for access to tenant meta data
* @return a list of artifacts or a empty list. Cannot be <null>.
*/
public static List<DdiArtifact> createArtifacts(final Target target,
static List<DdiArtifact> createArtifacts(final Target target,
final org.eclipse.hawkbit.repository.model.SoftwareModule module,
final ArtifactUrlHandler artifactUrlHandler, final SystemManagement systemManagement) {
final ArtifactUrlHandler artifactUrlHandler, final SystemManagement systemManagement,
final HttpRequest request) {
return module.getArtifacts().stream()
.map(artifact -> createArtifact(target, artifactUrlHandler, artifact, systemManagement))
.map(artifact -> createArtifact(target, artifactUrlHandler, artifact, systemManagement, request))
.collect(Collectors.toList());
}
private static DdiArtifact createArtifact(final Target target, final ArtifactUrlHandler artifactUrlHandler,
final Artifact artifact, final SystemManagement systemManagement) {
final Artifact artifact, final SystemManagement systemManagement, final HttpRequest request) {
final DdiArtifact file = new DdiArtifact();
file.setHashes(new DdiArtifactHash(artifact.getSha1Hash(), artifact.getMd5Hash()));
file.setFilename(artifact.getFilename());
@@ -102,7 +93,7 @@ public final class DataConversionHelper {
systemManagement.getTenantMetadata().getId(), target.getControllerId(), target.getId(),
new SoftwareData(artifact.getSoftwareModule().getId(), artifact.getFilename(), artifact.getId(),
artifact.getSha1Hash())),
ApiType.DDI)
ApiType.DDI, request.getURI())
.forEach(entry -> file.add(new Link(entry.getRef()).withRel(entry.getRel())));
return file;

View File

@@ -55,6 +55,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.http.server.ServletServerHttpRequest;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
@@ -120,7 +121,8 @@ public class DdiRootController implements DdiRootControllerRestApi {
}
return new ResponseEntity<>(
DataConversionHelper.createArtifacts(target, softwareModule, artifactUrlHandler, systemManagement),
DataConversionHelper.createArtifacts(target, softwareModule, artifactUrlHandler, systemManagement,
new ServletServerHttpRequest(requestResponseContextHolder.getHttpServletRequest())),
HttpStatus.OK);
}
@@ -244,7 +246,8 @@ public class DdiRootController implements DdiRootControllerRestApi {
if (!action.isCancelingOrCanceled()) {
final List<DdiChunk> chunks = DataConversionHelper.createChunks(target, action, artifactUrlHandler,
systemManagement);
systemManagement,
new ServletServerHttpRequest(requestResponseContextHolder.getHttpServletRequest()));
final HandlingType handlingType = action.isForce() ? HandlingType.FORCED : HandlingType.ATTEMPT;