Revize c72a4d3d
Přidáno uživatelem Jiri Trefil před téměř 2 roky(ů)
src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/controller/ConfigurationController.java | ||
---|---|---|
1 | 1 |
package cz.zcu.fav.kiv.antipatterndetectionapp.v2.controller; |
2 | 2 |
|
3 | 3 |
import com.google.gson.Gson; |
4 |
import cz.zcu.fav.kiv.antipatterndetectionapp.v2.dials.ConfigurationControllerStatusCodes; |
|
4 | 5 |
import cz.zcu.fav.kiv.antipatterndetectionapp.v2.model.*; |
5 | 6 |
import cz.zcu.fav.kiv.antipatterndetectionapp.v2.service.ConfigService; |
6 | 7 |
import cz.zcu.fav.kiv.antipatterndetectionapp.v2.utils.JSONBuilder; |
... | ... | |
24 | 25 |
@Autowired |
25 | 26 |
private ConfigService configurationService; |
26 | 27 |
|
28 |
/** |
|
29 |
* Endpoint for user to save his custom configuration |
|
30 |
* @param userConfiguration Wrapper around http request - contains User and Configuration objects |
|
31 |
* which point to the user uploading the configuration and the definition of his configuration |
|
32 |
* @return ResponseEntity with json body appropriate status code indicating result of the operation |
|
33 |
*/ |
|
27 | 34 |
@PostMapping(value="/upload_configuration") |
28 |
public ResponseEntity<String> test(@RequestBody UserConfiguration userConfiguration) { |
|
29 |
ResponseEntity<String> response = configurationService.addConfiguration(userConfiguration); |
|
30 |
return response; |
|
35 |
public ResponseEntity<String> uploadConfiguration(@RequestBody UserConfiguration userConfiguration) { |
|
36 |
ConfigurationControllerStatusCodes returnCode = configurationService.addConfiguration(userConfiguration); |
|
37 |
String message = returnCode.getLabel(); |
|
38 |
Map<String,Object> json = new HashMap<>(); |
|
39 |
json.put("message",message); |
|
40 |
//ResponseEntity<String> response = configurationService.addConfiguration(userConfiguration); |
|
41 |
return new ResponseEntity<>(JSONBuilder.buildJSON(json),HttpStatus.valueOf(returnCode.getStatusCode())); |
|
31 | 42 |
} |
32 | 43 |
|
44 |
/** |
|
45 |
* Endpoint returns the definitions of configurations (ie the json objects) and their names |
|
46 |
* @param user User querying the configurations |
|
47 |
* @return ResponseEntity with json body appropriate status code indicating result of the operation |
|
48 |
* 200 with requested body if everything is okay |
|
49 |
* 500 if db server died or no default configurations are present in the database |
|
50 |
*/ |
|
33 | 51 |
@PostMapping(value="/configuration") |
34 | 52 |
public ResponseEntity<String> getUserConfigurations(@RequestBody User user) { |
35 |
ResponseEntity<String> response = this.configurationService.getUserConfigurations(user); |
|
36 |
return response; |
|
53 |
Map<String, Object> json = new HashMap<>(); |
|
54 |
List<Configuration> configuration = this.configurationService.getUserConfigurations(user); |
|
55 |
//this can only happen if db server is offline |
|
56 |
//or no default configuration exists in database |
|
57 |
if(configuration == null) { |
|
58 |
json.put("message", "internal sever error"); |
|
59 |
return new ResponseEntity<>(JSONBuilder.buildJSON(json), HttpStatus.INTERNAL_SERVER_ERROR); |
|
60 |
} |
|
61 |
//list of configuration definitions |
|
62 |
List<String> configurationDefinition = new ArrayList<>(); |
|
63 |
//and their names |
|
64 |
List<String> configurationNames = new ArrayList<>(); |
|
65 |
//hipster syntax for Petr |
|
66 |
configuration.forEach(cfg -> { |
|
67 |
configurationDefinition.add(cfg.getConfig()); |
|
68 |
configurationNames.add(cfg.getConfigurationName()); |
|
69 |
}); |
|
70 |
json.put("configuration",configurationDefinition); |
|
71 |
json.put("configuration_names",configurationNames); |
|
72 |
|
|
73 |
return new ResponseEntity<>(new Gson().toJson(json),HttpStatus.OK); |
|
37 | 74 |
} |
38 | 75 |
|
76 |
/** |
|
77 |
* Endpoint which retrieves configuration names and their ids |
|
78 |
* @param user wrapper around username send in body of the request |
|
79 |
* @return ResponseEntity with json body appropriate status code indicating result of the operation |
|
80 |
*/ |
|
39 | 81 |
@PostMapping(value="/configuration_name") |
40 | 82 |
public ResponseEntity<String> getConfigurationNames(@RequestBody User user) { |
41 | 83 |
Map<String, Object> json = new HashMap<>(); |
42 |
List<Configuration> configuration = this.configurationService.getConfigurationNamesAndIds(user); |
|
84 |
List<Configuration> configuration = this.configurationService.getUserConfigurations(user); |
|
85 |
//this can only happen if db server is offline |
|
86 |
//or no default configuration exists in database |
|
43 | 87 |
if(configuration == null) { |
44 | 88 |
json.put("message", "internal sever error"); |
45 | 89 |
return new ResponseEntity<>(JSONBuilder.buildJSON(json), HttpStatus.INTERNAL_SERVER_ERROR); |
... | ... | |
51 | 95 |
configurationIds.add(o.getId()); |
52 | 96 |
} |
53 | 97 |
|
54 |
HashMap<String, Object> toJsonString = new HashMap<>();
|
|
98 |
Map<String, Object> toJsonString = new HashMap<>(); |
|
55 | 99 |
toJsonString.put("configuration_names", configurationNames); |
56 | 100 |
toJsonString.put("configuration_ids", configurationIds); |
57 | 101 |
|
... | ... | |
60 | 104 |
|
61 | 105 |
|
62 | 106 |
|
63 |
|
|
64 |
|
|
65 |
|
|
66 |
|
|
67 |
|
|
68 | 107 |
} |
src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/dials/ConfigurationControllerStatusCodes.java | ||
---|---|---|
1 |
package cz.zcu.fav.kiv.antipatterndetectionapp.v2.dials; |
|
2 |
|
|
3 |
/** |
|
4 |
* enum codes for configuration service |
|
5 |
* this enum is used by configuration controller for the purpose of building response entities |
|
6 |
*/ |
|
7 |
public enum ConfigurationControllerStatusCodes { |
|
8 |
|
|
9 |
EMPTY_CONFIGURATION_DEFINITION("No configuration definition provided",400), |
|
10 |
|
|
11 |
INSERT_FAILED("Failed to save configuration",500), |
|
12 |
|
|
13 |
INSERT_SUCCESSFUL("Configuration saved",200), |
|
14 |
|
|
15 |
CONFIGURATION_PAIRING_EXISTS("Configuration already exists in your collection",400), |
|
16 |
CONFIGURATION_PAIRING_CREATED("configuration added to collection",200); |
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
|
21 |
private final String label; |
|
22 |
private final int statusCode; |
|
23 |
|
|
24 |
ConfigurationControllerStatusCodes(String s, int i) { |
|
25 |
this.label = s; |
|
26 |
this.statusCode = i; |
|
27 |
} |
|
28 |
|
|
29 |
public int getStatusCode(){ |
|
30 |
return this.statusCode; |
|
31 |
} |
|
32 |
public String getLabel(){ |
|
33 |
return this.label; |
|
34 |
} |
|
35 |
|
|
36 |
} |
src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/repository/ConfigRepository.java | ||
---|---|---|
12 | 12 |
import java.util.List; |
13 | 13 |
@Repository |
14 | 14 |
public interface ConfigRepository extends JpaRepository<Configuration,Integer> { |
15 |
@Value("${default_user_id}") |
|
16 |
int defaultUserId = 0; |
|
15 |
|
|
17 | 16 |
Configuration findConfigurationById(int id); |
18 | 17 |
Configuration findConfigurationByConfigHash(String hash); |
19 | 18 |
//query to get all public configurations |
... | ... | |
24 | 23 |
@Query("select config, userconfig from Configuration config left join UserConfigurationJoin userconfig on config.id = userconfig.id.configId where userconfig.id.userId = ?1 or config.isDefault = 'Y'") |
25 | 24 |
List<Object[]> getAllUserConfigurations(int userId); |
26 | 25 |
|
27 |
//todo default user id parametrem |
|
28 | 26 |
@Query("select userconfig.configurationName,userconfig.id.configId from UserConfigurationJoin userconfig where userconfig.id.userId = ?1") |
29 | 27 |
List<Object[]> getAllUserConfigurationNames(int userId); |
30 | 28 |
|
src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/service/ConfigService.java | ||
---|---|---|
1 | 1 |
package cz.zcu.fav.kiv.antipatterndetectionapp.v2.service; |
2 |
import cz.zcu.fav.kiv.antipatterndetectionapp.v2.dials.ConfigurationControllerStatusCodes; |
|
2 | 3 |
import cz.zcu.fav.kiv.antipatterndetectionapp.v2.model.Configuration; |
3 | 4 |
import cz.zcu.fav.kiv.antipatterndetectionapp.v2.model.User; |
4 | 5 |
import cz.zcu.fav.kiv.antipatterndetectionapp.v2.model.UserConfiguration; |
5 |
import org.springframework.http.ResponseEntity; |
|
6 |
|
|
6 | 7 |
import java.util.List; |
7 | 8 |
public interface ConfigService { |
8 | 9 |
//upload configuration |
9 |
ResponseEntity<String> addConfiguration(Configuration cfg); |
|
10 |
ResponseEntity<String> addConfiguration(UserConfiguration cfg); |
|
10 |
ConfigurationControllerStatusCodes addConfiguration(Configuration cfg); |
|
11 |
ConfigurationControllerStatusCodes addConfiguration(UserConfiguration cfg); |
|
12 |
|
|
13 |
ConfigurationControllerStatusCodes pairConfigurationWithUser(User user, Configuration configuration); |
|
11 | 14 |
|
12 |
ResponseEntity<String> pairConfigurationWithUser(User user, Configuration configuration); |
|
13 |
//get all configurations available to user |
|
14 |
ResponseEntity<String> getUserConfigurations(User user); |
|
15 | 15 |
|
16 |
List<Configuration> getConfigurationNamesAndIds(User user);
|
|
16 |
List<Configuration> getUserConfigurations(User user);
|
|
17 | 17 |
|
18 | 18 |
Configuration getConfigurationById(int id); |
19 | 19 |
String getConfigurationName(int userId, int configurationId); |
src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/service/ConfigurationServiceImplementation.java | ||
---|---|---|
1 | 1 |
package cz.zcu.fav.kiv.antipatterndetectionapp.v2.service; |
2 | 2 |
|
3 |
import com.google.gson.Gson;
|
|
3 |
import cz.zcu.fav.kiv.antipatterndetectionapp.v2.dials.ConfigurationControllerStatusCodes;
|
|
4 | 4 |
import cz.zcu.fav.kiv.antipatterndetectionapp.v2.model.*; |
5 | 5 |
import cz.zcu.fav.kiv.antipatterndetectionapp.v2.repository.ConfigRepository; |
6 | 6 |
import cz.zcu.fav.kiv.antipatterndetectionapp.v2.repository.UserConfigurationJoinRepository; |
7 | 7 |
import cz.zcu.fav.kiv.antipatterndetectionapp.v2.utils.Crypto; |
8 |
import cz.zcu.fav.kiv.antipatterndetectionapp.v2.utils.JSONBuilder; |
|
9 | 8 |
import org.springframework.beans.factory.annotation.Autowired; |
10 |
import org.springframework.http.HttpStatus; |
|
11 |
import org.springframework.http.ResponseEntity; |
|
12 | 9 |
import org.springframework.stereotype.Service; |
13 | 10 |
|
14 | 11 |
import java.util.ArrayList; |
15 |
import java.util.HashMap; |
|
16 | 12 |
import java.util.List; |
17 |
import java.util.Map; |
|
18 | 13 |
|
19 | 14 |
@Service |
20 | 15 |
public class ConfigurationServiceImplementation implements ConfigService { |
... | ... | |
34 | 29 |
* @return ResponseEntity with status code and message with more information about operation |
35 | 30 |
*/ |
36 | 31 |
@Override |
37 |
public ResponseEntity<String> addConfiguration(Configuration cfg) {
|
|
32 |
public ConfigurationControllerStatusCodes addConfiguration(Configuration cfg) {
|
|
38 | 33 |
return null; |
39 | 34 |
} |
40 | 35 |
/** |
... | ... | |
44 | 39 |
* @return ResponseEntity with status code and message with more information about operation |
45 | 40 |
*/ |
46 | 41 |
@Override |
47 |
public ResponseEntity<String> addConfiguration(UserConfiguration cfg) {
|
|
42 |
public ConfigurationControllerStatusCodes addConfiguration(UserConfiguration cfg) {
|
|
48 | 43 |
User user = cfg.getUser(); |
49 | 44 |
Configuration configuration = cfg.getConfiguration(); |
50 | 45 |
|
51 |
Map<String,Object> json = new HashMap<>(); |
|
52 | 46 |
String userName = user.getName(); |
53 | 47 |
//fetch the user from db because user in UserConfiguration does not contain id |
54 | 48 |
user = this.userService.getUserByName(userName); |
55 | 49 |
String configurationDefinition = configuration.getConfig(); |
56 | 50 |
//if the request is missing the configuration definition then we kill it |
57 |
if(configurationDefinition == null){ |
|
58 |
json.put("message","no configuration definition provided."); |
|
59 |
String jsonString = JSONBuilder.buildJSON(json); |
|
60 |
return new ResponseEntity<>(jsonString,HttpStatus.BAD_REQUEST); |
|
51 |
if(configurationDefinition == null) { |
|
52 |
return ConfigurationControllerStatusCodes.EMPTY_CONFIGURATION_DEFINITION; |
|
61 | 53 |
} |
62 | 54 |
//create the hash of the configuration (w/o salting) |
63 | 55 |
String configHash = Crypto.hashString(configurationDefinition); |
... | ... | |
69 | 61 |
Configuration tmp = this.configurationRepository.save(configuration); |
70 | 62 |
//can only happen if db server fails or a constraint is breached |
71 | 63 |
if(tmp == null){ |
72 |
json.put("message","fatal server failure"); |
|
73 |
return new ResponseEntity<>(JSONBuilder.buildJSON(json),HttpStatus.INTERNAL_SERVER_ERROR); |
|
64 |
return ConfigurationControllerStatusCodes.INSERT_FAILED; |
|
74 | 65 |
} |
75 | 66 |
} |
76 | 67 |
//pair the configuration to the user |
77 | 68 |
pairConfigurationWithUser(user,configuration); |
78 |
json.put("message","configuration uploaded successfully"); |
|
79 |
return new ResponseEntity<>(JSONBuilder.buildJSON(json),HttpStatus.OK); |
|
69 |
return ConfigurationControllerStatusCodes.INSERT_SUCCESSFUL; |
|
80 | 70 |
|
81 | 71 |
|
82 | 72 |
} |
... | ... | |
90 | 80 |
* @return ResponseEntity<String> - Http response with status code and message about the operation |
91 | 81 |
*/ |
92 | 82 |
@Override |
93 |
public ResponseEntity<String> pairConfigurationWithUser(User user, Configuration configuration) { |
|
94 |
Map<String,Object> json = new HashMap<>(); |
|
83 |
public ConfigurationControllerStatusCodes pairConfigurationWithUser(User user, Configuration configuration) { |
|
95 | 84 |
final UserConfigKey key = new UserConfigKey(user.getId(),configuration.getId()); |
96 | 85 |
boolean exists = this.userConfigurationJoinRepository.existsById(key); |
97 | 86 |
//the configuration pairing already exists, we do not have to do anything |
98 | 87 |
//request like this should not happen from client, something fishy might be going on |
99 | 88 |
//or the request is a duplicate |
100 |
if(exists){ |
|
101 |
json.put("message","configuration already exists in your collection!"); |
|
102 |
return new ResponseEntity<>(JSONBuilder.buildJSON(json),HttpStatus.BAD_REQUEST); |
|
89 |
if(exists) { |
|
90 |
return ConfigurationControllerStatusCodes.CONFIGURATION_PAIRING_EXISTS; |
|
103 | 91 |
} |
104 | 92 |
//save the relation between user and configuration |
105 | 93 |
this.userConfigurationJoinRepository.save(new UserConfigurationJoin(key,configuration.getConfigurationName())); |
106 |
json.put("message","configuration added to collection."); |
|
107 |
return new ResponseEntity<>(JSONBuilder.buildJSON(json),HttpStatus.OK); |
|
94 |
return ConfigurationControllerStatusCodes.CONFIGURATION_PAIRING_CREATED; |
|
108 | 95 |
} |
109 | 96 |
|
110 |
@Override |
|
111 |
public ResponseEntity<String> getUserConfigurations(User user) { |
|
112 |
final String userName = user.getName(); |
|
113 |
//client can only send his name - he obviously does not know his id in db, we have to query that |
|
114 |
User userInfo = this.userService.getUserByName(userName); |
|
115 |
//fetch all configurations this particular user can see |
|
116 |
//ie all public configs + configurations uploaded by this particular user |
|
117 |
List<Object[]> configurations = this.configurationRepository.getAllUserConfigurations(userInfo.getId()); |
|
118 |
// Map<String,Object> json = new HashMap<>(); |
|
119 |
// json.put("message","configuration retrived"); |
|
120 |
// json.put("configurations",configurations); |
|
121 |
// String jsonString = JSONBuilder.buildJSON(json); |
|
122 |
return new ResponseEntity<>(new Gson().toJson(configurations),HttpStatus.OK); |
|
123 |
} |
|
124 | 97 |
|
98 |
/** |
|
99 |
* Method queries db to retrieve all configurations available to @param user |
|
100 |
* if he/she does not have any of his/her (inclusive for netflix) only default configurations are returned |
|
101 |
* @param user user querying configuration - ONLY username is valid in this object |
|
102 |
* @return List<Configuration> list of available configurations |
|
103 |
*/ |
|
125 | 104 |
@Override |
126 |
public List<Configuration> getConfigurationNamesAndIds(User user) {
|
|
105 |
public List<Configuration> getUserConfigurations(User user) {
|
|
127 | 106 |
final String userName = user.getName(); |
128 | 107 |
if(userName == null){ |
129 | 108 |
return null; |
... | ... | |
144 | 123 |
} |
145 | 124 |
return configurationList; |
146 | 125 |
} |
147 |
|
|
126 |
//Wrappers around db connection to configuration table |
|
148 | 127 |
@Override |
149 | 128 |
public Configuration getConfigurationById(int id) { |
150 | 129 |
return this.configurationRepository.findConfigurationById(id); |
src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/service/UserServiceImpl.java | ||
---|---|---|
6 | 6 |
import cz.zcu.fav.kiv.antipatterndetectionapp.v2.utils.Crypto; |
7 | 7 |
import org.springframework.beans.factory.annotation.Autowired; |
8 | 8 |
import org.springframework.stereotype.Service; |
9 |
|
|
10 |
import javax.xml.bind.annotation.adapters.HexBinaryAdapter; |
|
11 |
import java.nio.charset.StandardCharsets; |
|
12 |
import java.security.MessageDigest; |
|
13 |
import java.security.NoSuchAlgorithmException; |
|
14 | 9 |
import java.util.regex.Pattern; |
15 | 10 |
|
16 | 11 |
/** |
src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/utils/JSONBuilder.java | ||
---|---|---|
1 | 1 |
package cz.zcu.fav.kiv.antipatterndetectionapp.v2.utils; |
2 |
import com.fasterxml.jackson.databind.JsonNode; |
|
3 | 2 |
import com.google.gson.Gson; |
4 |
import cz.zcu.fav.kiv.antipatterndetectionapp.utils.JsonParser; |
|
5 | 3 |
import cz.zcu.fav.kiv.antipatterndetectionapp.v2.model.AntiPatternDto; |
6 | 4 |
import cz.zcu.fav.kiv.antipatterndetectionapp.v2.model.ConfigurationDto; |
7 | 5 |
import cz.zcu.fav.kiv.antipatterndetectionapp.v2.model.ThresholdDto; |
... | ... | |
10 | 8 |
import org.json.simple.parser.JSONParser; |
11 | 9 |
import org.json.simple.parser.ParseException; |
12 | 10 |
|
13 |
import java.io.IOException; |
|
14 | 11 |
import java.util.ArrayList; |
15 | 12 |
import java.util.HashMap; |
16 | 13 |
import java.util.Map; |
src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/utils/RequestBuilder.java | ||
---|---|---|
1 | 1 |
package cz.zcu.fav.kiv.antipatterndetectionapp.v2.utils; |
2 | 2 |
|
3 | 3 |
import cz.zcu.fav.kiv.antipatterndetectionapp.v2.httpErrorHandler.CustomErrorHandler; |
4 |
import org.json.simple.JSONObject; |
|
5 | 4 |
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; |
6 | 5 |
import org.springframework.stereotype.Component; |
7 |
import org.springframework.web.client.HttpClientErrorException; |
|
8 |
import org.springframework.web.client.HttpServerErrorException; |
|
9 |
import org.springframework.web.client.HttpStatusCodeException; |
|
10 | 6 |
import org.springframework.web.client.RestTemplate; |
11 |
|
|
12 |
import java.util.HashMap; |
|
13 | 7 |
import java.util.Map; |
14 |
import java.util.Optional;
|
|
8 |
import java.util.logging.Level;
|
|
15 | 9 |
import java.util.logging.Logger; |
16 |
|
|
17 | 10 |
import org.springframework.http.*; |
18 | 11 |
|
19 |
import javax.servlet.http.HttpServletRequest; |
|
20 |
|
|
21 | 12 |
@Component |
22 | 13 |
public class RequestBuilder { |
23 | 14 |
private static Logger logger = Logger.getLogger(RequestBuilder.class.getName()); |
... | ... | |
31 | 22 |
HttpHeaders headers = new HttpHeaders(); |
32 | 23 |
headers.setContentType(MediaType.APPLICATION_JSON); |
33 | 24 |
//headers.set("X-spade-request",spadeSignature); |
25 |
logger.log(Level.FINE,"Sending http request with body: \n"+json+"\n to: "+url); |
|
26 |
|
|
34 | 27 |
HttpEntity<String> entity = new HttpEntity<>(json, headers); |
35 | 28 |
return restTemplate.postForEntity(url, entity, String.class); |
36 | 29 |
} |
... | ... | |
45 | 38 |
headers.setContentType(MediaType.APPLICATION_JSON); |
46 | 39 |
//headers.set("X-spade-request",spadeSignature); |
47 | 40 |
headers.set("Authorization", "Bearer " + token); |
48 |
|
|
41 |
logger.log(Level.FINE,"Sending http request with no body to: "+url); |
|
49 | 42 |
HttpEntity<String> entity = new HttpEntity<>(null, headers); |
50 | 43 |
ResponseEntity<String> response = restTemplate.postForEntity(url, entity, String.class); |
51 | 44 |
return response; |
Také k dispozici: Unified diff
#10558 refactor service a controller vrstvy u configuration endpointu.