Removed http servlet response and request form method signature

Signed-off-by: Jonathan Philip Knoblauch <JonathanPhilip.Knoblauch@bosch-si.com>
This commit is contained in:
Jonathan Philip Knoblauch
2016-04-22 13:36:21 +02:00
parent 8995195e44
commit 810900eee7
11 changed files with 105 additions and 84 deletions

View File

@@ -10,7 +10,7 @@ package org.eclipse.hawkbit.app;
import org.eclipse.hawkbit.RepositoryApplicationConfiguration; import org.eclipse.hawkbit.RepositoryApplicationConfiguration;
import org.eclipse.hawkbit.autoconfigure.security.EnableHawkbitManagedSecurityConfiguration; import org.eclipse.hawkbit.autoconfigure.security.EnableHawkbitManagedSecurityConfiguration;
import org.eclipse.hawkbit.ddi.annotation.EnableDdiApi; import org.eclipse.hawkbit.ddi.EnableDdiApi;
import org.eclipse.hawkbit.mgmt.annotation.EnableMgmtApi; import org.eclipse.hawkbit.mgmt.annotation.EnableMgmtApi;
import org.eclipse.hawkbit.system.annotation.EnableSystemApi; import org.eclipse.hawkbit.system.annotation.EnableSystemApi;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;

View File

@@ -3,9 +3,13 @@
*/ */
package org.eclipse.hawkbit.ddi.client; package org.eclipse.hawkbit.ddi.client;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.netflix.feign.EnableFeignClients; import org.springframework.cloud.netflix.feign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import feign.Contract;
/** /**
* @author Jonathan Knoblauch * @author Jonathan Knoblauch
@@ -15,8 +19,31 @@ import org.springframework.cloud.netflix.feign.EnableFeignClients;
@EnableFeignClients @EnableFeignClients
public class Application { public class Application {
@Autowired
private DdiClient ddiClient;
public static void main(final String[] args) { public static void main(final String[] args) {
new SpringApplicationBuilder().showBanner(false).sources(Application.class).run(args); new SpringApplicationBuilder().showBanner(false).sources(Application.class).run(args);
// TODO .encoder(new JacksonEncoder())
// .decoder(new ResponseEntityDecoder(new JacksonDecoder()));
}
// @Bean
// public BasicAuthRequestInterceptor basicAuthRequestInterceptor() {
// return new BasicAuthRequestInterceptor(configuration.getUsername(),
// configuration.getPassword());
// }
@Bean
public ApplicationJsonRequestHeaderInterceptor jsonHeaderInterceptor() {
return new ApplicationJsonRequestHeaderInterceptor();
}
@Bean
public Contract feignContract() {
return new IgnoreMultipleConsumersProducersSpringMvcContract();
} }
} }

View File

@@ -3,15 +3,12 @@
*/ */
package org.eclipse.hawkbit.ddi.client; package org.eclipse.hawkbit.ddi.client;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.hawkbit.ddi.client.resource.RootControllerResourceClient; import org.eclipse.hawkbit.ddi.client.resource.RootControllerResourceClient;
import org.eclipse.hawkbit.ddi.json.model.DdiControllerBase; import org.eclipse.hawkbit.ddi.json.model.DdiControllerBase;
import org.eclipse.hawkbit.ddi.rest.api.DdiRestConstants; import org.eclipse.hawkbit.ddi.rest.api.DdiRestConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.netflix.feign.support.ResponseEntityDecoder; import org.springframework.cloud.netflix.feign.support.ResponseEntityDecoder;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import feign.Feign; import feign.Feign;
import feign.Logger; import feign.Logger;
@@ -19,13 +16,12 @@ import feign.Logger.Level;
import feign.jackson.JacksonDecoder; import feign.jackson.JacksonDecoder;
import feign.jackson.JacksonEncoder; import feign.jackson.JacksonEncoder;
@Component
public class DdiClient { public class DdiClient {
@Autowired //
private HttpServletRequest request; // @Autowired
// private HttpServletResponse response;
@Autowired
private HttpServletResponse response;
private final String controllerId; private final String controllerId;
private final String name; private final String name;
@@ -65,14 +61,7 @@ public class DdiClient {
public void startDdiClient() { public void startDdiClient() {
// final HttpServletRequest request; final ResponseEntity<DdiControllerBase> response = rootControllerResourceClient.getControllerBase("test");
// final HttpSession mySession = request.getSession();
// final HttpServletRequest request = new;
final ResponseEntity<DdiControllerBase> response = rootControllerResourceClient.getControllerBase("test",
request);
final DdiControllerBase controllerBase = response.getBody(); final DdiControllerBase controllerBase = response.getBody();
// TODO notify every 10 seconds on the rollout server // TODO notify every 10 seconds on the rollout server

View File

@@ -11,7 +11,7 @@ public class AppTest {
@Test @Test
public void AppTest() { public void AppTest() {
final DdiClient ddiClient = new DdiClient("localhost:8080", "mytest", "mytest", "desc", "DEFAULT"); final DdiClient ddiClient = new DdiClient("http://localhost:8080", "mytest", "mytest", "desc", "DEFAULT");
ddiClient.startDdiClient(); ddiClient.startDdiClient();
} }

View File

@@ -8,7 +8,7 @@
*/ */
package org.eclipse.hawkbit.autoconfigure.web; package org.eclipse.hawkbit.autoconfigure.web;
import org.eclipse.hawkbit.ddi.annotation.EnableDdiApi; import org.eclipse.hawkbit.ddi.EnableDdiApi;
import org.eclipse.hawkbit.mgmt.annotation.EnableMgmtApi; import org.eclipse.hawkbit.mgmt.annotation.EnableMgmtApi;
import org.eclipse.hawkbit.system.annotation.EnableSystemApi; import org.eclipse.hawkbit.system.annotation.EnableSystemApi;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;

View File

@@ -8,9 +8,6 @@
*/ */
package org.eclipse.hawkbit.ddi.rest.api; package org.eclipse.hawkbit.ddi.rest.api;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.hawkbit.ddi.json.model.DdiArtifact; import org.eclipse.hawkbit.ddi.json.model.DdiArtifact;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
@@ -43,11 +40,10 @@ public interface DdiArtifactStoreControllerRestApi {
* {@link HttpStatus#OK} or in case of partial download * {@link HttpStatus#OK} or in case of partial download
* {@link HttpStatus#PARTIAL_CONTENT}. * {@link HttpStatus#PARTIAL_CONTENT}.
*/ */
@RequestMapping(method = RequestMethod.GET, value = DdiRestConstants.ARTIFACT_DOWNLOAD_BY_FILENAME @RequestMapping(method = RequestMethod.GET, value = DdiRestConstants.ARTIFACT_DOWNLOAD_BY_FILENAME + "/{fileName}")
+ "/{fileName}")
@ResponseBody @ResponseBody
public ResponseEntity<Void> downloadArtifactByFilename(@PathVariable("fileName") final String fileName, public ResponseEntity<Void> downloadArtifactByFilename(@PathVariable("fileName") final String fileName,
final HttpServletResponse response, final HttpServletRequest request,
@AuthenticationPrincipal final String targetid); @AuthenticationPrincipal final String targetid);
/** /**
@@ -60,10 +56,9 @@ public interface DdiArtifactStoreControllerRestApi {
* *
* @return response of the servlet * @return response of the servlet
*/ */
@RequestMapping(method = RequestMethod.GET, value = DdiRestConstants.ARTIFACT_DOWNLOAD_BY_FILENAME @RequestMapping(method = RequestMethod.GET, value = DdiRestConstants.ARTIFACT_DOWNLOAD_BY_FILENAME + "/{fileName}"
+ "/{fileName}" + DdiRestConstants.ARTIFACT_MD5_DWNL_SUFFIX) + DdiRestConstants.ARTIFACT_MD5_DWNL_SUFFIX)
@ResponseBody @ResponseBody
public ResponseEntity<Void> downloadArtifactMD5ByFilename(@PathVariable("fileName") final String fileName, public ResponseEntity<Void> downloadArtifactMD5ByFilename(@PathVariable("fileName") final String fileName);
final HttpServletResponse response);
} }

View File

@@ -6,8 +6,6 @@ package org.eclipse.hawkbit.ddi.rest.api;
import java.lang.annotation.Target; import java.lang.annotation.Target;
import java.util.List; import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid; import javax.validation.Valid;
import org.eclipse.hawkbit.ddi.json.model.DdiActionFeedback; import org.eclipse.hawkbit.ddi.json.model.DdiActionFeedback;
@@ -60,8 +58,7 @@ public interface DdiRootControllerRestApi {
*/ */
@RequestMapping(method = RequestMethod.GET, value = "/{targetid}", produces = { "application/hal+json", @RequestMapping(method = RequestMethod.GET, value = "/{targetid}", produces = { "application/hal+json",
MediaType.APPLICATION_JSON_VALUE }) MediaType.APPLICATION_JSON_VALUE })
ResponseEntity<DdiControllerBase> getControllerBase(@PathVariable("targetid") final String targetid, ResponseEntity<DdiControllerBase> getControllerBase(@PathVariable("targetid") final String targetid);
final HttpServletRequest request);
/** /**
* Handles GET {@link DdiArtifact} download request. This could be full or * Handles GET {@link DdiArtifact} download request. This could be full or
@@ -85,8 +82,7 @@ public interface DdiRootControllerRestApi {
@RequestMapping(method = RequestMethod.GET, value = "/{targetid}/softwaremodules/{softwareModuleId}/artifacts/{fileName}") @RequestMapping(method = RequestMethod.GET, value = "/{targetid}/softwaremodules/{softwareModuleId}/artifacts/{fileName}")
ResponseEntity<Void> downloadArtifact(@PathVariable("targetid") final String targetid, ResponseEntity<Void> downloadArtifact(@PathVariable("targetid") final String targetid,
@PathVariable("softwareModuleId") final Long softwareModuleId, @PathVariable("softwareModuleId") final Long softwareModuleId,
@PathVariable("fileName") final String fileName, final HttpServletResponse response, @PathVariable("fileName") final String fileName);
final HttpServletRequest request);
/** /**
* Handles GET {@link DdiArtifact} MD5 checksum file download request. * Handles GET {@link DdiArtifact} MD5 checksum file download request.
@@ -109,8 +105,7 @@ public interface DdiRootControllerRestApi {
+ DdiRestConstants.ARTIFACT_MD5_DWNL_SUFFIX, produces = MediaType.TEXT_PLAIN_VALUE) + DdiRestConstants.ARTIFACT_MD5_DWNL_SUFFIX, produces = MediaType.TEXT_PLAIN_VALUE)
ResponseEntity<Void> downloadArtifactMd5(@PathVariable("targetid") final String targetid, ResponseEntity<Void> downloadArtifactMd5(@PathVariable("targetid") final String targetid,
@PathVariable("softwareModuleId") final Long softwareModuleId, @PathVariable("softwareModuleId") final Long softwareModuleId,
@PathVariable("fileName") final String fileName, final HttpServletResponse response, @PathVariable("fileName") final String fileName);
final HttpServletRequest request);
/** /**
* Resource for {@link SoftwareModule} {@link UpdateAction}s. * Resource for {@link SoftwareModule} {@link UpdateAction}s.
@@ -134,8 +129,7 @@ public interface DdiRootControllerRestApi {
ResponseEntity<DdiDeploymentBase> getControllerBasedeploymentAction( ResponseEntity<DdiDeploymentBase> getControllerBasedeploymentAction(
@PathVariable("targetid") @NotEmpty final String targetid, @PathVariable("targetid") @NotEmpty final String targetid,
@PathVariable("actionId") @NotEmpty final Long actionId, @PathVariable("actionId") @NotEmpty final Long actionId,
@RequestParam(value = "c", required = false, defaultValue = "-1") final int resource, @RequestParam(value = "c", required = false, defaultValue = "-1") final int resource);
final HttpServletRequest request);
/** /**
* This is the feedback channel for the {@link DdiDeploymentBase} action. * This is the feedback channel for the {@link DdiDeploymentBase} action.
@@ -155,8 +149,7 @@ public interface DdiRootControllerRestApi {
@RequestMapping(value = "/{targetid}/" + DdiRestConstants.DEPLOYMENT_BASE_ACTION + "/{actionId}/" @RequestMapping(value = "/{targetid}/" + DdiRestConstants.DEPLOYMENT_BASE_ACTION + "/{actionId}/"
+ DdiRestConstants.FEEDBACK, method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE) + DdiRestConstants.FEEDBACK, method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
ResponseEntity<Void> postBasedeploymentActionFeedback(@Valid @RequestBody final DdiActionFeedback feedback, ResponseEntity<Void> postBasedeploymentActionFeedback(@Valid @RequestBody final DdiActionFeedback feedback,
@PathVariable("targetid") final String targetid, @PathVariable("actionId") @NotEmpty final Long actionId, @PathVariable("targetid") final String targetid, @PathVariable("actionId") @NotEmpty final Long actionId);
final HttpServletRequest request);
/** /**
* This is the feedback channel for the config data action. * This is the feedback channel for the config data action.
@@ -173,7 +166,7 @@ public interface DdiRootControllerRestApi {
@RequestMapping(value = "/{targetid}/" @RequestMapping(value = "/{targetid}/"
+ DdiRestConstants.CONFIG_DATA_ACTION, method = RequestMethod.PUT, consumes = MediaType.APPLICATION_JSON_VALUE) + DdiRestConstants.CONFIG_DATA_ACTION, method = RequestMethod.PUT, consumes = MediaType.APPLICATION_JSON_VALUE)
ResponseEntity<Void> putConfigData(@Valid @RequestBody final DdiConfigData configData, ResponseEntity<Void> putConfigData(@Valid @RequestBody final DdiConfigData configData,
@PathVariable("targetid") final String targetid, final HttpServletRequest request); @PathVariable("targetid") final String targetid);
/** /**
* {@link RequestMethod.GET} method for the {@link DdiCancel} action. * {@link RequestMethod.GET} method for the {@link DdiCancel} action.
@@ -190,7 +183,7 @@ public interface DdiRootControllerRestApi {
@RequestMapping(value = "/{targetid}/" + DdiRestConstants.CANCEL_ACTION @RequestMapping(value = "/{targetid}/" + DdiRestConstants.CANCEL_ACTION
+ "/{actionId}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE) + "/{actionId}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
ResponseEntity<DdiCancel> getControllerCancelAction(@PathVariable("targetid") @NotEmpty final String targetid, ResponseEntity<DdiCancel> getControllerCancelAction(@PathVariable("targetid") @NotEmpty final String targetid,
@PathVariable("actionId") @NotEmpty final Long actionId, final HttpServletRequest request); @PathVariable("actionId") @NotEmpty final Long actionId);
/** /**
* {@link RequestMethod.POST} method receiving the {@link DdiActionFeedback} * {@link RequestMethod.POST} method receiving the {@link DdiActionFeedback}
@@ -212,6 +205,6 @@ public interface DdiRootControllerRestApi {
+ DdiRestConstants.FEEDBACK, method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE) + DdiRestConstants.FEEDBACK, method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
ResponseEntity<Void> postCancelActionFeedback(@Valid @RequestBody final DdiActionFeedback feedback, ResponseEntity<Void> postCancelActionFeedback(@Valid @RequestBody final DdiActionFeedback feedback,
@PathVariable("targetid") @NotEmpty final String targetid, @PathVariable("targetid") @NotEmpty final String targetid,
@PathVariable("actionId") @NotEmpty final Long actionId, final HttpServletRequest request); @PathVariable("actionId") @NotEmpty final Long actionId);
} }

View File

@@ -6,7 +6,7 @@
* which accompanies this distribution, and is available at * which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html * http://www.eclipse.org/legal/epl-v10.html
*/ */
package org.eclipse.hawkbit.ddi.annotation; package org.eclipse.hawkbit.ddi;
import java.lang.annotation.ElementType; import java.lang.annotation.ElementType;
import java.lang.annotation.Retention; import java.lang.annotation.Retention;

View File

@@ -118,12 +118,12 @@ public final class DataConversionHelper {
// change the payload of the // change the payload of the
// response because of eTags. // response because of eTags.
result.add(linkTo(methodOn(DdiRootController.class, tenantAware.getCurrentTenant()) result.add(linkTo(methodOn(DdiRootController.class, tenantAware.getCurrentTenant())
.getControllerBasedeploymentAction(target.getControllerId(), action.getId(), actions.hashCode(), .getControllerBasedeploymentAction(target.getControllerId(), action.getId(),
null)).withRel(DdiRestConstants.DEPLOYMENT_BASE_ACTION)); actions.hashCode())).withRel(DdiRestConstants.DEPLOYMENT_BASE_ACTION));
addedUpdate = true; addedUpdate = true;
} else if (action.isCancelingOrCanceled() && !addedCancel) { } else if (action.isCancelingOrCanceled() && !addedCancel) {
result.add(linkTo(methodOn(DdiRootController.class, tenantAware.getCurrentTenant()) result.add(linkTo(methodOn(DdiRootController.class, tenantAware.getCurrentTenant())
.getControllerCancelAction(target.getControllerId(), action.getId(), null)) .getControllerCancelAction(target.getControllerId(), action.getId()))
.withRel(DdiRestConstants.CANCEL_ACTION)); .withRel(DdiRestConstants.CANCEL_ACTION));
addedCancel = true; addedCancel = true;
} }
@@ -131,7 +131,7 @@ public final class DataConversionHelper {
if (target.getTargetInfo().isRequestControllerAttributes()) { if (target.getTargetInfo().isRequestControllerAttributes()) {
result.add(linkTo(methodOn(DdiRootController.class, tenantAware.getCurrentTenant()).putConfigData(null, result.add(linkTo(methodOn(DdiRootController.class, tenantAware.getCurrentTenant()).putConfigData(null,
target.getControllerId(), null)).withRel(DdiRestConstants.CONFIG_DATA_ACTION)); target.getControllerId())).withRel(DdiRestConstants.CONFIG_DATA_ACTION));
} }
return result; return result;
} }

View File

@@ -35,6 +35,8 @@ import org.springframework.http.ResponseEntity;
import org.springframework.security.web.bind.annotation.AuthenticationPrincipal; import org.springframework.security.web.bind.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
/** /**
* The {@link DdiArtifactStoreController} of the HawkBit server controller API * The {@link DdiArtifactStoreController} of the HawkBit server controller API
@@ -61,10 +63,18 @@ public class DdiArtifactStoreController implements DdiArtifactStoreControllerRes
@Autowired @Autowired
private HawkbitSecurityProperties securityProperties; private HawkbitSecurityProperties securityProperties;
private HttpServletResponse getResponse() {
return ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getResponse();
}
private HttpServletRequest getRequest() {
return ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();
}
@Override @Override
public ResponseEntity<Void> downloadArtifactByFilename(@PathVariable("fileName") final String fileName, public ResponseEntity<Void> downloadArtifactByFilename(@PathVariable("fileName") final String fileName,
final HttpServletResponse response, final HttpServletRequest request,
@AuthenticationPrincipal final String targetid) { @AuthenticationPrincipal final String targetid) {
ResponseEntity<Void> result; ResponseEntity<Void> result;
final List<LocalArtifact> foundArtifacts = artifactManagement.findLocalArtifactByFilename(fileName); final List<LocalArtifact> foundArtifacts = artifactManagement.findLocalArtifactByFilename(fileName);
@@ -79,7 +89,7 @@ public class DdiArtifactStoreController implements DdiArtifactStoreControllerRes
final LocalArtifact artifact = foundArtifacts.get(0); final LocalArtifact artifact = foundArtifacts.get(0);
final String ifMatch = request.getHeader("If-Match"); final String ifMatch = getRequest().getHeader("If-Match");
if (ifMatch != null && !RestResourceConversionHelper.matchesHttpHeader(ifMatch, artifact.getSha1Hash())) { if (ifMatch != null && !RestResourceConversionHelper.matchesHttpHeader(ifMatch, artifact.getSha1Hash())) {
result = new ResponseEntity<>(HttpStatus.PRECONDITION_FAILED); result = new ResponseEntity<>(HttpStatus.PRECONDITION_FAILED);
} else { } else {
@@ -88,11 +98,12 @@ public class DdiArtifactStoreController implements DdiArtifactStoreControllerRes
// we set a download status only if we are aware of the // we set a download status only if we are aware of the
// targetid, i.e. authenticated and not anonymous // targetid, i.e. authenticated and not anonymous
if (targetid != null && !"anonymous".equals(targetid)) { if (targetid != null && !"anonymous".equals(targetid)) {
final Action action = checkAndReportDownloadByTarget(request, targetid, artifact); final Action action = checkAndReportDownloadByTarget(getRequest(), targetid, artifact);
result = RestResourceConversionHelper.writeFileResponse(artifact, response, request, file, result = RestResourceConversionHelper.writeFileResponse(artifact, getResponse(), getRequest(), file,
cacheWriteNotify, action.getId()); cacheWriteNotify, action.getId());
} else { } else {
result = RestResourceConversionHelper.writeFileResponse(artifact, response, request, file); result = RestResourceConversionHelper.writeFileResponse(artifact, getResponse(), getRequest(),
file);
} }
} }
@@ -101,8 +112,7 @@ public class DdiArtifactStoreController implements DdiArtifactStoreControllerRes
} }
@Override @Override
public ResponseEntity<Void> downloadArtifactMD5ByFilename(@PathVariable("fileName") final String fileName, public ResponseEntity<Void> downloadArtifactMD5ByFilename(@PathVariable("fileName") final String fileName) {
final HttpServletResponse response) {
final List<LocalArtifact> foundArtifacts = artifactManagement.findLocalArtifactByFilename(fileName); final List<LocalArtifact> foundArtifacts = artifactManagement.findLocalArtifactByFilename(fileName);
if (foundArtifacts.isEmpty()) { if (foundArtifacts.isEmpty()) {
@@ -113,7 +123,7 @@ public class DdiArtifactStoreController implements DdiArtifactStoreControllerRes
} }
try { try {
DataConversionHelper.writeMD5FileResponse(fileName, response, foundArtifacts.get(0)); DataConversionHelper.writeMD5FileResponse(fileName, getResponse(), foundArtifacts.get(0));
} catch (final IOException e) { } catch (final IOException e) {
LOG.error("Failed to stream MD5 File", e); LOG.error("Failed to stream MD5 File", e);
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);

View File

@@ -54,6 +54,8 @@ import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
/** /**
* The {@link DdiRootController} of the hawkBit server DDI API that is queried * The {@link DdiRootController} of the hawkBit server DDI API that is queried
@@ -89,12 +91,22 @@ public class DdiRootController implements DdiRootControllerRestApi {
@Autowired @Autowired
private ArtifactUrlHandler artifactUrlHandler; private ArtifactUrlHandler artifactUrlHandler;
private HttpServletResponse getResponse() {
return ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getResponse();
}
private HttpServletRequest getRequest() {
return ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();
}
@Override @Override
public ResponseEntity<List<org.eclipse.hawkbit.ddi.json.model.DdiArtifact>> getSoftwareModulesArtifacts( public ResponseEntity<List<org.eclipse.hawkbit.ddi.json.model.DdiArtifact>> getSoftwareModulesArtifacts(
@PathVariable("targetid") final String targetid, @PathVariable("targetid") final String targetid,
@PathVariable("softwareModuleId") final Long softwareModuleId) { @PathVariable("softwareModuleId") final Long softwareModuleId) {
LOG.debug("getSoftwareModulesArtifacts({})", targetid); LOG.debug("getSoftwareModulesArtifacts({})", targetid);
// SecurityContextHolder.getContext().getAuthentication().
final SoftwareModule softwareModule = softwareManagement.findSoftwareModuleById(softwareModuleId); final SoftwareModule softwareModule = softwareManagement.findSoftwareModuleById(softwareModuleId);
if (softwareModule == null) { if (softwareModule == null) {
@@ -108,18 +120,17 @@ public class DdiRootController implements DdiRootControllerRestApi {
} }
@Override @Override
public ResponseEntity<DdiControllerBase> getControllerBase(@PathVariable("targetid") final String targetid, public ResponseEntity<DdiControllerBase> getControllerBase(@PathVariable("targetid") final String targetid) {
final HttpServletRequest request) {
LOG.debug("getControllerBase({})", targetid); LOG.debug("getControllerBase({})", targetid);
final Target target = controllerManagement.findOrRegisterTargetIfItDoesNotexist(targetid, final Target target = controllerManagement.findOrRegisterTargetIfItDoesNotexist(targetid,
IpUtil.getClientIpFromRequest(request, securityProperties.getClients().getRemoteIpHeader())); IpUtil.getClientIpFromRequest(getRequest(), securityProperties.getClients().getRemoteIpHeader()));
if (target.getTargetInfo().getUpdateStatus() == TargetUpdateStatus.UNKNOWN) { if (target.getTargetInfo().getUpdateStatus() == TargetUpdateStatus.UNKNOWN) {
LOG.debug("target with {} extsisted but was in status UNKNOWN -> REGISTERED)", targetid); LOG.debug("target with {} extsisted but was in status UNKNOWN -> REGISTERED)", targetid);
controllerManagement.updateTargetStatus(target.getTargetInfo(), TargetUpdateStatus.REGISTERED, controllerManagement.updateTargetStatus(target.getTargetInfo(), TargetUpdateStatus.REGISTERED,
System.currentTimeMillis(), System.currentTimeMillis(),
IpUtil.getClientIpFromRequest(request, securityProperties.getClients().getRemoteIpHeader())); IpUtil.getClientIpFromRequest(getRequest(), securityProperties.getClients().getRemoteIpHeader()));
} }
return new ResponseEntity<>( return new ResponseEntity<>(
@@ -131,12 +142,11 @@ public class DdiRootController implements DdiRootControllerRestApi {
@Override @Override
public ResponseEntity<Void> downloadArtifact(@PathVariable("targetid") final String targetid, public ResponseEntity<Void> downloadArtifact(@PathVariable("targetid") final String targetid,
@PathVariable("softwareModuleId") final Long softwareModuleId, @PathVariable("softwareModuleId") final Long softwareModuleId,
@PathVariable("fileName") final String fileName, final HttpServletResponse response, @PathVariable("fileName") final String fileName) {
final HttpServletRequest request) {
ResponseEntity<Void> result; ResponseEntity<Void> result;
final Target target = controllerManagement.updateLastTargetQuery(targetid, final Target target = controllerManagement.updateLastTargetQuery(targetid,
IpUtil.getClientIpFromRequest(request, securityProperties.getClients().getRemoteIpHeader())); IpUtil.getClientIpFromRequest(getRequest(), securityProperties.getClients().getRemoteIpHeader()));
final SoftwareModule module = softwareManagement.findSoftwareModuleById(softwareModuleId); final SoftwareModule module = softwareManagement.findSoftwareModuleById(softwareModuleId);
if (checkModule(fileName, module)) { if (checkModule(fileName, module)) {
@@ -146,12 +156,12 @@ public class DdiRootController implements DdiRootControllerRestApi {
final LocalArtifact artifact = module.getLocalArtifactByFilename(fileName).get(); final LocalArtifact artifact = module.getLocalArtifactByFilename(fileName).get();
final DbArtifact file = artifactManagement.loadLocalArtifactBinary(artifact); final DbArtifact file = artifactManagement.loadLocalArtifactBinary(artifact);
final String ifMatch = request.getHeader("If-Match"); final String ifMatch = getRequest().getHeader("If-Match");
if (ifMatch != null && !RestResourceConversionHelper.matchesHttpHeader(ifMatch, artifact.getSha1Hash())) { if (ifMatch != null && !RestResourceConversionHelper.matchesHttpHeader(ifMatch, artifact.getSha1Hash())) {
result = new ResponseEntity<>(HttpStatus.PRECONDITION_FAILED); result = new ResponseEntity<>(HttpStatus.PRECONDITION_FAILED);
} else { } else {
final Action action = checkAndLogDownload(request, target, module); final Action action = checkAndLogDownload(getRequest(), target, module);
result = RestResourceConversionHelper.writeFileResponse(artifact, response, request, file, result = RestResourceConversionHelper.writeFileResponse(artifact, getResponse(), getRequest(), file,
cacheWriteNotify, action.getId()); cacheWriteNotify, action.getId());
} }
} }
@@ -185,10 +195,9 @@ public class DdiRootController implements DdiRootControllerRestApi {
@Override @Override
public ResponseEntity<Void> downloadArtifactMd5(@PathVariable("targetid") final String targetid, public ResponseEntity<Void> downloadArtifactMd5(@PathVariable("targetid") final String targetid,
@PathVariable("softwareModuleId") final Long softwareModuleId, @PathVariable("softwareModuleId") final Long softwareModuleId,
@PathVariable("fileName") final String fileName, final HttpServletResponse response, @PathVariable("fileName") final String fileName) {
final HttpServletRequest request) {
controllerManagement.updateLastTargetQuery(targetid, controllerManagement.updateLastTargetQuery(targetid,
IpUtil.getClientIpFromRequest(request, securityProperties.getClients().getRemoteIpHeader())); IpUtil.getClientIpFromRequest(getRequest(), securityProperties.getClients().getRemoteIpHeader()));
final SoftwareModule module = softwareManagement.findSoftwareModuleById(softwareModuleId); final SoftwareModule module = softwareManagement.findSoftwareModuleById(softwareModuleId);
@@ -198,7 +207,7 @@ public class DdiRootController implements DdiRootControllerRestApi {
} }
try { try {
DataConversionHelper.writeMD5FileResponse(fileName, response, DataConversionHelper.writeMD5FileResponse(fileName, getResponse(),
module.getLocalArtifactByFilename(fileName).get()); module.getLocalArtifactByFilename(fileName).get());
} catch (final IOException e) { } catch (final IOException e) {
LOG.error("Failed to stream MD5 File", e); LOG.error("Failed to stream MD5 File", e);
@@ -211,12 +220,11 @@ public class DdiRootController implements DdiRootControllerRestApi {
@Override @Override
public ResponseEntity<DdiDeploymentBase> getControllerBasedeploymentAction( public ResponseEntity<DdiDeploymentBase> getControllerBasedeploymentAction(
@PathVariable("targetid") final String targetid, @PathVariable("actionId") final Long actionId, @PathVariable("targetid") final String targetid, @PathVariable("actionId") final Long actionId,
@RequestParam(value = "c", required = false, defaultValue = "-1") final int resource, @RequestParam(value = "c", required = false, defaultValue = "-1") final int resource) {
final HttpServletRequest request) {
LOG.debug("getControllerBasedeploymentAction({},{})", targetid, resource); LOG.debug("getControllerBasedeploymentAction({},{})", targetid, resource);
final Target target = controllerManagement.updateLastTargetQuery(targetid, final Target target = controllerManagement.updateLastTargetQuery(targetid,
IpUtil.getClientIpFromRequest(request, securityProperties.getClients().getRemoteIpHeader())); IpUtil.getClientIpFromRequest(getRequest(), securityProperties.getClients().getRemoteIpHeader()));
final Action action = findActionWithExceptionIfNotFound(actionId); final Action action = findActionWithExceptionIfNotFound(actionId);
if (!action.getTarget().getId().equals(target.getId())) { if (!action.getTarget().getId().equals(target.getId())) {
@@ -246,12 +254,11 @@ public class DdiRootController implements DdiRootControllerRestApi {
@Override @Override
public ResponseEntity<Void> postBasedeploymentActionFeedback(@Valid @RequestBody final DdiActionFeedback feedback, public ResponseEntity<Void> postBasedeploymentActionFeedback(@Valid @RequestBody final DdiActionFeedback feedback,
@PathVariable("targetid") final String targetid, @PathVariable("actionId") @NotEmpty final Long actionId, @PathVariable("targetid") final String targetid, @PathVariable("actionId") @NotEmpty final Long actionId) {
final HttpServletRequest request) {
LOG.debug("provideBasedeploymentActionFeedback for target [{},{}]: {}", targetid, actionId, feedback); LOG.debug("provideBasedeploymentActionFeedback for target [{},{}]: {}", targetid, actionId, feedback);
final Target target = controllerManagement.updateLastTargetQuery(targetid, final Target target = controllerManagement.updateLastTargetQuery(targetid,
IpUtil.getClientIpFromRequest(request, securityProperties.getClients().getRemoteIpHeader())); IpUtil.getClientIpFromRequest(getRequest(), securityProperties.getClients().getRemoteIpHeader()));
if (!actionId.equals(feedback.getId())) { if (!actionId.equals(feedback.getId())) {
LOG.warn( LOG.warn(
@@ -343,9 +350,9 @@ public class DdiRootController implements DdiRootControllerRestApi {
@Override @Override
public ResponseEntity<Void> putConfigData(@Valid @RequestBody final DdiConfigData configData, public ResponseEntity<Void> putConfigData(@Valid @RequestBody final DdiConfigData configData,
@PathVariable("targetid") final String targetid, final HttpServletRequest request) { @PathVariable("targetid") final String targetid) {
controllerManagement.updateLastTargetQuery(targetid, controllerManagement.updateLastTargetQuery(targetid,
IpUtil.getClientIpFromRequest(request, securityProperties.getClients().getRemoteIpHeader())); IpUtil.getClientIpFromRequest(getRequest(), securityProperties.getClients().getRemoteIpHeader()));
controllerManagement.updateControllerAttributes(targetid, configData.getData()); controllerManagement.updateControllerAttributes(targetid, configData.getData());
@@ -355,11 +362,11 @@ public class DdiRootController implements DdiRootControllerRestApi {
@Override @Override
public ResponseEntity<DdiCancel> getControllerCancelAction( public ResponseEntity<DdiCancel> getControllerCancelAction(
@PathVariable("targetid") @NotEmpty final String targetid, @PathVariable("targetid") @NotEmpty final String targetid,
@PathVariable("actionId") @NotEmpty final Long actionId, final HttpServletRequest request) { @PathVariable("actionId") @NotEmpty final Long actionId) {
LOG.debug("getControllerCancelAction({})", targetid); LOG.debug("getControllerCancelAction({})", targetid);
final Target target = controllerManagement.updateLastTargetQuery(targetid, final Target target = controllerManagement.updateLastTargetQuery(targetid,
IpUtil.getClientIpFromRequest(request, securityProperties.getClients().getRemoteIpHeader())); IpUtil.getClientIpFromRequest(getRequest(), securityProperties.getClients().getRemoteIpHeader()));
final Action action = findActionWithExceptionIfNotFound(actionId); final Action action = findActionWithExceptionIfNotFound(actionId);
if (!action.getTarget().getId().equals(target.getId())) { if (!action.getTarget().getId().equals(target.getId())) {
@@ -385,11 +392,11 @@ public class DdiRootController implements DdiRootControllerRestApi {
@Override @Override
public ResponseEntity<Void> postCancelActionFeedback(@Valid @RequestBody final DdiActionFeedback feedback, public ResponseEntity<Void> postCancelActionFeedback(@Valid @RequestBody final DdiActionFeedback feedback,
@PathVariable("targetid") @NotEmpty final String targetid, @PathVariable("targetid") @NotEmpty final String targetid,
@PathVariable("actionId") @NotEmpty final Long actionId, final HttpServletRequest request) { @PathVariable("actionId") @NotEmpty final Long actionId) {
LOG.debug("provideCancelActionFeedback for target [{}]: {}", targetid, feedback); LOG.debug("provideCancelActionFeedback for target [{}]: {}", targetid, feedback);
final Target target = controllerManagement.updateLastTargetQuery(targetid, final Target target = controllerManagement.updateLastTargetQuery(targetid,
IpUtil.getClientIpFromRequest(request, securityProperties.getClients().getRemoteIpHeader())); IpUtil.getClientIpFromRequest(getRequest(), securityProperties.getClients().getRemoteIpHeader()));
if (!actionId.equals(feedback.getId())) { if (!actionId.equals(feedback.getId())) {
LOG.warn( LOG.warn(