Commit 39dc692a authored by 0Tyler's avatar 0Tyler

gateway api

parent 7d0a9238
This diff is collapsed.
......@@ -8,13 +8,10 @@ import edu.prlab.tyler.iotgateway.cloud.services.PrivacyPolicyReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
@RestController
@RequestMapping("device")
@RequestMapping
public class DeviceController {
private DeviceService deviceService;
private PrivacyPolicyReportService privacyPolicyReportService;
......@@ -26,21 +23,21 @@ public class DeviceController {
this.privacyPolicyReportService = privacyPolicyReportService;
}
@PostMapping
@PostMapping("/device")
public ResponseEntity<Device> addDevice(@RequestBody Device device) {
return deviceService.add(device)
.map(ResponseEntity::ok)
.orElseGet(()->ResponseEntity.noContent().build());
}
@GetMapping("/{id}")
public ResponseEntity<Device> readDevice(@PathVariable(value = "id",required = false) long id) {
return deviceService.read(id)
@GetMapping("/device/{udn}")
public ResponseEntity<Device> readDevice(@PathVariable(value = "udn",required = false) String udn) {
return deviceService.readByUDN(udn)
.map(ResponseEntity::ok)
.orElseGet(()->ResponseEntity.noContent().build());
}
@GetMapping
@GetMapping("/device")
public ResponseEntity<Iterable<Device>> readDevices() {
return Optional.of(deviceService.readll())
.map(ResponseEntity::ok)
......@@ -48,10 +45,7 @@ public class DeviceController {
}
// @GetMapping
// public ResponseEntity<Iterable<Device>> readDevices(SensitiveUser user) {
// return Optional.of(deviceService.readll())
// .map(ResponseEntity::ok)
// .orElseGet(()->ResponseEntity.noContent().build());
// public ResponseEntity<Iterable<Device>> readDevicesByUser(SensitiveUser user) {
// }
@PostMapping("/privacy")
......@@ -61,7 +55,6 @@ public class DeviceController {
.orElseGet(()->ResponseEntity.noContent().build());
}
@GetMapping("/privacy/{UDN}")
public ResponseEntity<PrivacyPolicyReport> readPrivacyPolicyReportByDevice(@PathVariable(value = "UDN") String UDN) {
return privacyPolicyReportService.readByDevice(UDN)
......
......@@ -6,10 +6,13 @@ import edu.prlab.tyler.iotgateway.cloud.services.PrivacyChoiceService;
import edu.prlab.tyler.iotgateway.cloud.services.SensitiveUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import java.util.Optional;
@RestController
public class GatewayController {
private PrivacyChoiceService privacyChoiceService;
......@@ -22,8 +25,6 @@ public class GatewayController {
this.sensitiveUserService = sensitiveUserService;
}
@PostMapping("/choice")
public ResponseEntity<PrivacyChoice> setPrivacyChoice(@RequestBody PrivacyChoice privacyChoice) {
return privacyChoiceService.add(privacyChoice)
......@@ -31,10 +32,17 @@ public class GatewayController {
.orElseGet(() -> ResponseEntity.noContent().build());
}
@PostMapping("user")
public ResponseEntity<SensitiveUser> setUser(@RequestBody SensitiveUser sensitiveUser) {
return sensitiveUserService.add(sensitiveUser)
.map(ResponseEntity::ok)
.orElseGet(()->ResponseEntity.noContent().build());
@GetMapping("/choice")
public ResponseEntity<Iterable<PrivacyChoice>> readPrivacyChoice() {
return Optional.of(privacyChoiceService.readll())
.map(ResponseEntity::ok)
.orElseGet(() -> ResponseEntity.noContent().build());
}
// @PostMapping("/user")
// public ResponseEntity<SensitiveUser> setUser(@RequestBody SensitiveUser sensitiveUser) {
// return sensitiveUserService.add(sensitiveUser)
// .map(ResponseEntity::ok)
// .orElseGet(()->ResponseEntity.noContent().build());
// }
}
......@@ -17,7 +17,6 @@ public class SensitiveUserService {
}
public Optional<SensitiveUser> add(SensitiveUser pojo) {
System.out.println(pojo.toString());
return Optional.of(repository.saveAndFlush(pojo));
}
}
......@@ -26,7 +26,6 @@ import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
import java.util.ArrayList;
import java.util.Arrays;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
......@@ -89,13 +88,13 @@ public class CloudHttpApiTest {
.andDo(print())
.andExpect(status().isOk())
.andReturn();
ArrayList<Device> devices = mapper.readValue(result.getResponse().getContentAsString(),
new TypeReference<ArrayList<Device>>() {
Iterable<Device> devices = mapper.readValue(result.getResponse().getContentAsString(),
new TypeReference<Iterable<Device>>() {
});
Assert.assertNotNull(devices);
result = mvc.perform(MockMvcRequestBuilders
.get("/device/" + device.getId())
.get("/device/" + device.getUdn())
.accept(MediaType.APPLICATION_JSON_UTF8))
.andDo(print())
.andExpect(status().isOk())
......@@ -157,7 +156,7 @@ public class CloudHttpApiTest {
.build();
result = mvc.perform(MockMvcRequestBuilders
.post("/device/privacy")
.post("/privacy")
.contentType(MediaType.APPLICATION_JSON_UTF8)
.accept(MediaType.APPLICATION_JSON_UTF8)
.content(mapper.writeValueAsString(privacyPolicyReport)))
......@@ -168,9 +167,8 @@ public class CloudHttpApiTest {
PrivacyPolicyReport.class);
Assert.assertNotNull(returnPolicy);
result = mvc.perform(MockMvcRequestBuilders
.get("/device/privacy/" + device.getUdn())
.get("/privacy/" + device.getUdn())
.accept(MediaType.APPLICATION_JSON_UTF8))
.andDo(print())
.andExpect(status().isOk())
......@@ -200,5 +198,16 @@ public class CloudHttpApiTest {
.andReturn();
Assert.assertNotNull(result);
result = mvc.perform(MockMvcRequestBuilders
.get("/choice")
.accept(MediaType.APPLICATION_JSON_UTF8))
.andDo(print())
.andExpect(status().isOk())
.andReturn();
Iterable<PrivacyChoice> privacyChoices = mapper.readValue(result.getResponse().getContentAsString(),
new TypeReference<Iterable<PrivacyChoice>>(){});
Assert.assertNotNull(privacyChoices);
}
}
package edu.prlab.tyler.iotgateway.gateway.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
@Configuration
public class RemoteConfig {
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
......@@ -46,6 +46,7 @@ public class GatewayController {
.orElseGet(() -> ResponseEntity.noContent().build());
}
//
@GetMapping("/device/{UDN}")
public ResponseEntity<PrivacyPolicyReport> readPrivacyPolicyReportByDevice(@PathVariable(value = "UDN") String UDN) {
return privacyService.getRelatedPrivacyPolicies(UDN)
......@@ -69,5 +70,4 @@ public class GatewayController {
.map(ResponseEntity::ok)
.orElseGet(() -> ResponseEntity.noContent().build());
}
}
package edu.prlab.tyler.iotgateway.gateway.pojo;
import lombok.Builder;
import lombok.Data;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Data
@Builder
@Entity
public class DeviceIndex {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private String udn;
}
package edu.prlab.tyler.iotgateway.gateway.pojo;
import edu.prlab.tyler.iotgateway.cloud.pojo.privacy.PrivacyChoice;
import lombok.Builder;
import lombok.Data;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import java.time.LocalDateTime;
@Data
@Builder
@Entity
public class PrivacyChoiceIndex {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
private LocalDateTime localDateTime;
private PrivacyChoice privacyChoice;
}
package edu.prlab.tyler.iotgateway.gateway.pojo.auth;
public class User {
private String username;
private String password;
// private BigInteger publicKey;
// private BigInteger privateKey;
// private String address;
}
package edu.prlab.tyler.iotgateway.gateway.repositories;
import edu.prlab.tyler.iotgateway.gateway.pojo.DeviceIndex;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface DeviceIndexRepository extends CrudRepository<DeviceIndex, String> {
}
package edu.prlab.tyler.iotgateway.gateway.repositories;
import edu.prlab.tyler.iotgateway.gateway.pojo.PrivacyChoiceIndex;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface PrivacyChoiceIndexRepository extends CrudRepository<PrivacyChoiceIndex, String> {
}
package edu.prlab.tyler.iotgateway.gateway.services;
import edu.prlab.tyler.iotgateway.cloud.pojo.device.Device;
import edu.prlab.tyler.iotgateway.gateway.pojo.DeviceIndex;
import edu.prlab.tyler.iotgateway.gateway.repositories.DeviceIndexRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import java.util.Optional;
@Service
public class DeviceService {
}
private RestTemplate template;
DeviceIndexRepository deviceIndexRepository;
private String cloudAddress;
@Autowired
public DeviceService(DeviceIndexRepository deviceIndexRepository,
RestTemplate template) {
this.deviceIndexRepository = deviceIndexRepository;
this.template = template;
}
@Value("${iotgateway.cloud.address}")
private void setCloudAddress(String cloudAddress) {
this.cloudAddress = cloudAddress;
}
//TODO bind and read from blockchain
public Optional<Device> bindDeviceAndGateway(String udn) {
return Optional.ofNullable(template.getForObject(cloudAddress + "/device/" + udn, Device.class));
// .map(device -> deviceIndexRepository.save(DeviceIndex.builder().udn(udn).build()));
}
public Optional<Iterable<Device>> readDevices() {
return Optional.ofNullable(template.exchange(cloudAddress + "/device", HttpMethod.GET,null,
new ParameterizedTypeReference<Iterable<Device>>() { }).getBody());
}
}
\ No newline at end of file
package edu.prlab.tyler.iotgateway.gateway.services;
import edu.prlab.tyler.iotgateway.cloud.pojo.privacy.PrivacyChoice;
import edu.prlab.tyler.iotgateway.cloud.pojo.privacy.PrivacyContent;
import edu.prlab.tyler.iotgateway.cloud.pojo.privacy.PrivacyPolicyReport;
import edu.prlab.tyler.iotgateway.gateway.pojo.PrivacyChoiceIndex;
import edu.prlab.tyler.iotgateway.gateway.repositories.PrivacyChoiceIndexRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import java.time.LocalDateTime;
import java.util.Optional;
@Service
public class PrivacyService {
private RestTemplate template;
private PrivacyChoiceIndexRepository privacyChoiceIndexRepository;
private String cloudAddress;
public PrivacyService() {
this.template = new RestTemplate();
@Autowired
public PrivacyService(PrivacyChoiceIndexRepository privacyChoiceIndexRepository,
RestTemplate template) {
this.template = template;
this.privacyChoiceIndexRepository = privacyChoiceIndexRepository;
}
@Value("${iotgateway.cloud.address}")
......@@ -25,15 +33,25 @@ public class PrivacyService {
this.cloudAddress = cloudAddress;
}
public Optional<PrivacyPolicyReport> getRelatedPrivacyPolicies(String Udn) {
return Optional.ofNullable(template.getForObject(cloudAddress + "/device/privacy/" + Udn, PrivacyPolicyReport.class));
public Optional<PrivacyPolicyReport> getRelatedPrivacyPolicies(String udn) {
return Optional.ofNullable(template.getForObject(cloudAddress + "/privacy/" + udn, PrivacyPolicyReport.class));
}
public Optional<PrivacyChoice> setPrivacyPolicyChoice(PrivacyChoice privacyChoice) {
return Optional.of(privacyChoice)
.map((choice) -> template.postForObject(cloudAddress + "/choice", choice, PrivacyChoice.class));
.map(choice -> privacyChoiceIndexRepository.save(PrivacyChoiceIndex.builder()
.localDateTime(LocalDateTime.now())
.privacyChoice(choice)
.build()))
.map((choice) -> template.postForObject(cloudAddress + "/choice", choice.getPrivacyChoice(), PrivacyChoice.class));
}
public void getPrivacyPolicyChoice(PrivacyContent content) {
// public Optional<Iterable<PrivacyChoice>> getPrivacyPolicyChoices() {
// return Optional.ofNullable(template.exchange(cloudAddress + "/choice", HttpMethod.GET, null,
// new ParameterizedTypeReference<Iterable<PrivacyChoice>>() { }).getBody());
// }
public Iterable<PrivacyChoiceIndex> getPrivacyPolicyChoices() {
return privacyChoiceIndexRepository.findAll();
}
}
......@@ -46,9 +46,11 @@ public class GatewayHttpApiTest {
//取得裝置清單
//拿取隱私政策
MvcResult result = mvc.perform(MockMvcRequestBuilders
.get("/privacy/" + "a1252c49-4188-4e6d-a32e-66604c664fb8")
.get("/device/" + "a1252c49-4188-4e6d-a32e-66604c664fb8")
.accept(MediaType.APPLICATION_JSON_UTF8))
.andDo(print())
.andExpect(status().isOk())
......@@ -79,5 +81,14 @@ public class GatewayHttpApiTest {
.andExpect(status().isOk())
.andReturn();
Assert.assertNotNull(result);
//取得所有隱私偏好
// result = mvc.perform(MockMvcRequestBuilders
// .get("/choice")
// .accept(MediaType.APPLICATION_JSON_UTF8))
// .andDo(print())
// .andExpect(status().isOk())
// .andReturn();
// Assert.assertNotNull(result);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment