Commit ff83120d authored by Spiros Koulouzis's avatar Spiros Koulouzis

Added specific excptions

parent 5e0247c7
...@@ -22,7 +22,7 @@ import org.springframework.web.bind.annotation.ResponseStatus; ...@@ -22,7 +22,7 @@ import org.springframework.web.bind.annotation.ResponseStatus;
* *
* @author S. Koulouzis * @author S. Koulouzis
*/ */
@ResponseStatus(value = HttpStatus.BAD_REQUEST) @ResponseStatus(value = HttpStatus.BAD_REQUEST, reason = "MMMMMMMMMMMMMMMMMM")
public class BadRequestException extends RuntimeException { public class BadRequestException extends RuntimeException {
public BadRequestException(String massage) { public BadRequestException(String massage) {
......
/*
* Copyright 2017 S. Koulouzis, Wang Junchao, Huan Zhou, Yang Hu
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package nl.uva.sne.drip.api.exception;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;
/**
*
* @author S. Koulouzis
*/
@ResponseStatus(value = HttpStatus.BAD_REQUEST, reason = "User already exists")
public class PasswordNullException extends RuntimeException {
public PasswordNullException() {
super();
}
public PasswordNullException(String string) {
super(string);
}
}
/*
* Copyright 2017 S. Koulouzis, Wang Junchao, Huan Zhou, Yang Hu
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package nl.uva.sne.drip.api.exception;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;
/**
*
* @author S. Koulouzis
*/
@ResponseStatus(value = HttpStatus.BAD_REQUEST, reason = "User already exists")
public class UserExistsException extends RuntimeException {
public UserExistsException() {
super();
}
public UserExistsException(String string) {
super(string);
}
}
/*
* Copyright 2017 S. Koulouzis, Wang Junchao, Huan Zhou, Yang Hu
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package nl.uva.sne.drip.api.exception;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;
/**
*
* @author S. Koulouzis
*/
@ResponseStatus(value = HttpStatus.NOT_FOUND, reason = "User not found")
public class UserNotFoundException extends RuntimeException {
public UserNotFoundException(String string) {
super(string);
}
public UserNotFoundException() {
super();
}
}
/*
* Copyright 2017 S. Koulouzis, Wang Junchao, Huan Zhou, Yang Hu
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package nl.uva.sne.drip.api.exception;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;
/**
*
* @author S. Koulouzis
*/
@ResponseStatus(value = HttpStatus.BAD_REQUEST, reason = "User name can't be null")
public class UserNullException extends RuntimeException {
public UserNullException(String massage) {
super(massage);
}
public UserNullException() {
super();
}
}
...@@ -22,6 +22,10 @@ import java.util.logging.Logger; ...@@ -22,6 +22,10 @@ import java.util.logging.Logger;
import javax.annotation.security.RolesAllowed; import javax.annotation.security.RolesAllowed;
import nl.uva.sne.drip.api.exception.BadRequestException; import nl.uva.sne.drip.api.exception.BadRequestException;
import nl.uva.sne.drip.api.exception.NotFoundException; import nl.uva.sne.drip.api.exception.NotFoundException;
import nl.uva.sne.drip.api.exception.PasswordNullException;
import nl.uva.sne.drip.api.exception.UserExistsException;
import nl.uva.sne.drip.api.exception.UserNotFoundException;
import nl.uva.sne.drip.api.exception.UserNullException;
import nl.uva.sne.drip.commons.types.User; import nl.uva.sne.drip.commons.types.User;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
...@@ -62,14 +66,14 @@ public class UserController { ...@@ -62,14 +66,14 @@ public class UserController {
public @ResponseBody public @ResponseBody
String register(@RequestBody User user) { String register(@RequestBody User user) {
if (user.getUsername() == null) { if (user.getUsername() == null) {
throw new BadRequestException("Username can't be null"); throw new UserNullException();
} }
if (user.getPassword() == null) { if (user.getPassword() == null) {
throw new BadRequestException("Password can't be null"); throw new PasswordNullException();
} }
UserDetails registeredUser = service.loadUserByUsername(user.getUsername()); UserDetails registeredUser = service.loadUserByUsername(user.getUsername());
if (registeredUser != null) { if (registeredUser != null) {
throw new BadRequestException("Username " + user.getUsername() + " is used"); throw new UserExistsException("Username " + user.getUsername() + " is used");
} }
user.setPassword(new BCryptPasswordEncoder().encode(user.getPassword())); user.setPassword(new BCryptPasswordEncoder().encode(user.getPassword()));
service.getDao().save(user); service.getDao().save(user);
...@@ -82,7 +86,7 @@ public class UserController { ...@@ -82,7 +86,7 @@ public class UserController {
String modify(@RequestBody User user) { String modify(@RequestBody User user) {
UserDetails registeredUser = service.loadUserByUsername(user.getUsername()); UserDetails registeredUser = service.loadUserByUsername(user.getUsername());
if (registeredUser == null) { if (registeredUser == null) {
throw new NotFoundException("User " + user.getUsername() + " not found"); throw new UserNotFoundException("User " + user.getUsername() + " not found");
} }
return this.register(user); return this.register(user);
} }
...@@ -94,7 +98,7 @@ public class UserController { ...@@ -94,7 +98,7 @@ public class UserController {
try { try {
User user = service.getDao().findOne(id); User user = service.getDao().findOne(id);
if (user == null) { if (user == null) {
throw new NotFoundException(); throw new UserNotFoundException("User " + id + " not found");
} }
return user; return user;
} catch (Exception ex) { } catch (Exception ex) {
...@@ -110,7 +114,7 @@ public class UserController { ...@@ -110,7 +114,7 @@ public class UserController {
try { try {
User user = service.getDao().findOne(id); User user = service.getDao().findOne(id);
if (user == null) { if (user == null) {
throw new NotFoundException(); throw new UserNotFoundException("User " + id + " not found");
} }
service.getDao().delete(user); service.getDao().delete(user);
return "Deleted used :" + id; return "Deleted used :" + id;
......
...@@ -31,7 +31,6 @@ import nl.uva.sne.drip.api.rpc.PlannerCaller; ...@@ -31,7 +31,6 @@ import nl.uva.sne.drip.api.rpc.PlannerCaller;
import nl.uva.sne.drip.commons.types.Message; import nl.uva.sne.drip.commons.types.Message;
import nl.uva.sne.drip.commons.types.MessageParameter; import nl.uva.sne.drip.commons.types.MessageParameter;
import nl.uva.sne.drip.commons.types.Plan; import nl.uva.sne.drip.commons.types.Plan;
import nl.uva.sne.drip.commons.types.SimplePlanContainer;
import nl.uva.sne.drip.commons.types.ToscaRepresentation; import nl.uva.sne.drip.commons.types.ToscaRepresentation;
import nl.uva.sne.drip.commons.utils.Converter; import nl.uva.sne.drip.commons.utils.Converter;
import org.json.JSONException; import org.json.JSONException;
...@@ -64,23 +63,23 @@ public class PlannerService { ...@@ -64,23 +63,23 @@ public class PlannerService {
ObjectMapper mapper = new ObjectMapper(); ObjectMapper mapper = new ObjectMapper();
mapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true); mapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
String jsonString = mapper.writeValueAsString(plannerReturnedMessage); String jsonString = mapper.writeValueAsString(plannerReturnedMessage);
SimplePlanContainer simplePlan = Converter.plannerOutput2SimplePlanContainer(jsonString); // SimplePlanContainer simplePlan = Converter.plannerOutput2SimplePlanContainer(jsonString);
Plan topLevel = new Plan(); Plan topLevel = new Plan();
topLevel.setLevel(0); topLevel.setLevel(0);
topLevel.setToscaID(toscaId); topLevel.setToscaID(toscaId);
topLevel.setName("planner_output_all.yml"); topLevel.setName("planner_output_all.yml");
topLevel.setKvMap(Converter.ymlString2Map(simplePlan.getTopLevel())); // topLevel.setKvMap(Converter.ymlString2Map(simplePlan.getTopLevel()));
Map<String, String> map = simplePlan.getLowerLevels(); // Map<String, String> map = simplePlan.getLowerLevels();
Set<String> loweLevelPlansIDs = new HashSet<>(); Set<String> loweLevelPlansIDs = new HashSet<>();
for (String lowLevelNames : map.keySet()) { // for (String lowLevelNames : map.keySet()) {
Plan lowLevelPlan = new Plan(); // Plan lowLevelPlan = new Plan();
lowLevelPlan.setLevel(1); // lowLevelPlan.setLevel(1);
lowLevelPlan.setToscaID(toscaId); // lowLevelPlan.setToscaID(toscaId);
lowLevelPlan.setName(lowLevelNames); // lowLevelPlan.setName(lowLevelNames);
lowLevelPlan.setKvMap(Converter.ymlString2Map(map.get(lowLevelNames))); // lowLevelPlan.setKvMap(Converter.ymlString2Map(map.get(lowLevelNames)));
planDao.save(lowLevelPlan); // planDao.save(lowLevelPlan);
loweLevelPlansIDs.add(lowLevelPlan.getId()); // loweLevelPlansIDs.add(lowLevelPlan.getId());
} // }
topLevel.setLoweLevelPlansIDs(loweLevelPlansIDs); topLevel.setLoweLevelPlansIDs(loweLevelPlansIDs);
planDao.save(topLevel); planDao.save(topLevel);
......
...@@ -71,11 +71,10 @@ public class Converter { ...@@ -71,11 +71,10 @@ public class Converter {
return jsonObject2Map(jsonObject); return jsonObject2Map(jsonObject);
} }
public static SimplePlanContainer plannerOutput2SimplePlanContainer(String jsonString) throws JSONException { // public static SimplePlanContainer plannerOutput2SimplePlanContainer(String jsonString) throws JSONException {
//
return null; // return null;
} // }
public static Map<String, Object> jsonObject2Map(JSONObject object) throws JSONException { public static Map<String, Object> jsonObject2Map(JSONObject object) throws JSONException {
Map<String, Object> map = new HashMap(); Map<String, Object> map = new HashMap();
......
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