Summary Table
Categories |
Total Count |
PII |
0 |
URL |
0 |
DNS |
0 |
EKL |
0 |
IP |
0 |
PORT |
0 |
VsID |
0 |
CF |
0 |
AI |
0 |
VPD |
0 |
PL |
0 |
Other |
0 |
File Content
/*
* UserController.java
* Copyright (c) 2017 Veterans Affairs.
*/
package gov.va.oneconsult.seoc.api.controller;
import java.util.Set;
import javax.validation.Valid;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.WebRequest;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.Version;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import gov.va.oneconsult.seoc.api.json.CreateUserRequest;
import gov.va.oneconsult.seoc.api.json.SeocGenericResponse;
import gov.va.oneconsult.seoc.api.model.User;
import gov.va.oneconsult.seoc.api.serializer.StringSerializer;
import gov.va.oneconsult.seoc.api.service.GenericService;
import gov.va.oneconsult.seoc.api.service.UserService;
import gov.va.oneconsult.seoc.api.util.Constants;
import gov.va.oneconsult.seoc.api.util.EncodeLoggerFactory;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
/**
* RestController for User related endpoints
* <p>To Retrieve User Details</p>
*
* @author AbleVets
*/
@RestController
@RequestMapping(value = "v1/user")
@Api(value = "UserController", description = "Allows to retrieve user data")
public class UserController {
@Autowired
private UserService userService;
@Autowired
private GenericService genericService;
public static final Logger logger = EncodeLoggerFactory.getLogger(UserController.class);
private static ObjectMapper mapper = new ObjectMapper();
private static SimpleModule module = new SimpleModule("Serializer",
new Version(1, 0, 0, null, null, null));
static {
module.addSerializer((Class<String>) String.class, new StringSerializer());
mapper.registerModule(module);
}
@InitBinder
public void initBinder(WebDataBinder binder, WebRequest request)
{
binder.setDisallowedFields();
}
/**
* Description: Retrieve role information for the user requested
* @param domain - non null domain
* @param userId - non null vaUserId
* @return ResponseEntity<String>
*/
@CrossOrigin
@ApiOperation(value = "UserRole", notes = "Get User Role", nickname = "role")
@ApiResponses(value = { @ApiResponse(code = 400, message = "Bad Request Found"),
@ApiResponse(code = 200, message = "Completed"),
@ApiResponse(code = 204, message = "No Content")})
@RequestMapping(value = "/role/{domain}/{userId}", method = RequestMethod.GET)
@ResponseBody
public ResponseEntity<String> getUserRole(@PathVariable("domain") String domain,
@PathVariable("userId") String userId)
{
logger.info("Get user roles for Domain : " + domain + " userId " + userId);
String result = null;
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
try
{
if (domain.trim().isEmpty() || userId.trim().isEmpty())
{
logger.info("Invalid domain name/ vaUserId.");
SeocGenericResponse response = new SeocGenericResponse(Constants.FAILURE,
"Invalid domain name/ vaUserId.");
result = mapper.writeValueAsString(response);
return new ResponseEntity<String>(result, headers, HttpStatus.BAD_REQUEST);
}
String vaUserId = domain.trim() + Constants.DOMAIN_SEPARATOR + userId.trim();
String userRole = userService.getRoleByVaUserId(vaUserId);
if (userRole == null || userRole.isEmpty())
{
logger.info("No role available for this user");
SeocGenericResponse response = new SeocGenericResponse(Constants.FAILURE,
"No role available for this user");
response.setVaUserId(vaUserId);
result = mapper.writeValueAsString(response);
return new ResponseEntity<String>(result, headers, HttpStatus.NO_CONTENT);
}
logger.info("Retrieved user role successfully.");
SeocGenericResponse response = new SeocGenericResponse(Constants.SUCCESS, null);
response.setUserRole(userRole);
result = mapper.writeValueAsString(response);
return new ResponseEntity<String>(result, headers, HttpStatus.OK);
}catch (JsonProcessingException e)
{
logger.error("Json Exception occured in response of getUserRole." + e.getMessage());
return new ResponseEntity<String>("Exception occured generating JSON",
HttpStatus.NO_CONTENT);
}
}
/**
* Description: Retrieve active users
* @return ResponseEntity<String>
*/
@CrossOrigin
@ApiOperation(value = "Users", notes = "Get All Users", nickname = "users")
@ApiResponses(value = { @ApiResponse(code = 400, message = "Bad Request Found"),
@ApiResponse(code = 200, message = "Request Completed"),
@ApiResponse(code = 204, message = "No Content")})
@RequestMapping(value = "/all", method = RequestMethod.GET)
@ResponseBody
public ResponseEntity<String> getAllUsers()
{
logger.info("Get All Users");
String result = null;
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
try
{
Set<User> users = userService.getActiveUsers();
if (users==null)
{
logger.info("User data not available");
return new ResponseEntity<String>("User data not available", headers, HttpStatus.NO_CONTENT);
}
logger.info("Retrieved user data successfully.");
result = mapper.writeValueAsString(users);;
return new ResponseEntity<String>(result, headers, HttpStatus.OK);
}catch (JsonProcessingException e)
{
logger.error("Json Exception occured in response of getAllUsers." + e.getMessage());
return new ResponseEntity<String>("Exception occured generating JSON",
HttpStatus.NO_CONTENT);
}
}
/**
* Description: Save or Update User
* @param userRequest
* @param headers
* @return ResponseEntity<String>
*/
@CrossOrigin
@ApiOperation(value = "SaveUser", notes = "Save User Data", nickname = "saveuser")
@ApiResponses(value = { @ApiResponse(code = 400, message = "Bad Request Found"),
@ApiResponse(code = 200, message = "Request Completed"),
@ApiResponse(code = 201, message = "Created")})
@RequestMapping(value = "/save", method = RequestMethod.PUT)
@ResponseBody
public ResponseEntity<String> saveUser(
@RequestBody @Valid CreateUserRequest userRequest, @RequestHeader HttpHeaders headers)
{
if (userRequest == null)
{
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}
logger.info("*** User Request ***" + userRequest);
SeocGenericResponse seocResponse = userService.saveUser(userRequest);
try
{
String result = mapper.writeValueAsString(seocResponse);
HttpHeaders responseHeaders = new HttpHeaders();
responseHeaders.setContentType(MediaType.APPLICATION_JSON_UTF8);
if ((Constants.CREATED).equals(seocResponse.getStatus()))
{
logger.info("**CREATED User**");
return new ResponseEntity<String>(result, responseHeaders,
HttpStatus.CREATED);
}
if((Constants.UPDATED).equals(seocResponse.getStatus()))
{
logger.info("**UPDATED User**");
return new ResponseEntity<String>(result, responseHeaders,
HttpStatus.OK);
}else {
logger.info("**Error processing the request**");
return new ResponseEntity<String>(result, responseHeaders,
HttpStatus.BAD_REQUEST);
}
} catch (JsonProcessingException e)
{
logger.error("Json Exception occured in response of saveUser." + e.getMessage());
return new ResponseEntity<String>("Exception occured generating JSON",
HttpStatus.BAD_REQUEST);
}
}
/**
* Description: Delete User with the networkId
* @param networkId
* @return ResponseEntity<String>
*/
@CrossOrigin
@ApiOperation(value = "DeleteUser", notes = "Delete User", nickname = "deleteUser")
@ApiResponses(value = { @ApiResponse(code = 400, message = "Bad Request Found"),
@ApiResponse(code = 200, message = "Request Completed"),
@ApiResponse(code = 404, message = "Not Found")})
@RequestMapping(value = "/delete/{networkId}", method = RequestMethod.DELETE)
@ResponseBody
public ResponseEntity<String> deleteUser(@PathVariable("networkId") String networkId)
{
if (networkId == null || networkId.trim().isEmpty())
{
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}
SeocGenericResponse seocResponse = userService.deleteUser(networkId);
try
{
String result = mapper.writeValueAsString(seocResponse);
HttpHeaders responseHeaders = new HttpHeaders();
responseHeaders.setContentType(MediaType.APPLICATION_JSON_UTF8);
if ((Constants.SUCCESS).equals(seocResponse.getStatus()))
{
logger.info("**DELETED User**");
return new ResponseEntity<String>(result, responseHeaders,
HttpStatus.OK);
} else {
if((Constants.NOTFOUND).equals(seocResponse.getAction()))
{
logger.info("** User Not Found**");
return new ResponseEntity<String>(result, responseHeaders,
HttpStatus.NOT_FOUND);
}else {
logger.info("**Error processing the request**");
return new ResponseEntity<String>(result, responseHeaders,
HttpStatus.BAD_REQUEST);
}
}
} catch (JsonProcessingException e)
{
logger.error("Json Exception occured in response of deleteUser." + e.getMessage());
return new ResponseEntity<String>("Exception occured generating JSON",
HttpStatus.BAD_REQUEST);
}
}
/**
* Description: Get maintenance mode
*
* @return ResponseEntity<String>
*/
@CrossOrigin
@ApiOperation(value = "maitenanceBy", notes = "Get inMaintenanceBy data", nickname = "MaintenanceModeStatus")
@ApiResponses(value = { @ApiResponse(code = 400, message = "Bad Request Found"),
@ApiResponse(code = 200, message = "Request Completed") })
@RequestMapping(value = "/maint", method = RequestMethod.GET)
@ResponseBody
public ResponseEntity<String> maintenanceStatus()
{
String vaUser = genericService.getMaintenanceBy();
SeocGenericResponse seocResponse = new SeocGenericResponse();
seocResponse.setInMaintenanceBy(vaUser);
try
{
String result = mapper.writeValueAsString(seocResponse);
HttpHeaders responseHeaders = new HttpHeaders();
responseHeaders.setContentType(MediaType.APPLICATION_JSON_UTF8);
logger.info("**Retrieving current status of maintenance **");
return new ResponseEntity<String>(result, responseHeaders, HttpStatus.OK);
} catch (JsonProcessingException e)
{
logger.error("Json Exception occured in response of maintenanceStatus." + e.getMessage());
return new ResponseEntity<String>("Exception occured generating JSON",
HttpStatus.BAD_REQUEST);
}
}
/**
* Description: Update MaintenanceBy field
*
* @param switchMaitenance
* @param headers
* @return ResponseEntity<String>
*/
@CrossOrigin
@ApiOperation(value = "UpdateMaintenanceBy", notes = "Save MaintenanceBy", nickname = "updateMaintenanceBy")
@ApiResponses(value = { @ApiResponse(code = 400, message = "Bad Request Found"),
@ApiResponse(code = 200, message = "Request Completed") })
@RequestMapping(value = "/maint", method = RequestMethod.PUT)
@ResponseBody
public ResponseEntity<String> updateMaintenance(@RequestBody @Valid String switchMaitenance,
@RequestHeader HttpHeaders headers)
{
String userIdFromRequest = headers.getFirst(Constants.USERID);
String user = "";
if (userIdFromRequest == null || userIdFromRequest.isEmpty())
{
logger.error("UserId is missing in the request.");
return new ResponseEntity<String>("UserId is missing in the request.",
HttpStatus.BAD_REQUEST);
}
logger.info("*** User currently updating maintenance ***" + userIdFromRequest);
if (switchMaitenance != null && switchMaitenance.equalsIgnoreCase(Constants.TRUE))
{
user = userIdFromRequest;
} else if (switchMaitenance != null && switchMaitenance.equalsIgnoreCase(Constants.FALSE))
{
user = "";
} else
{
logger.error("Invalid switchMaitenance value in the request.");
return new ResponseEntity<String>("Invalid setMaintenance value in the request.",
HttpStatus.BAD_REQUEST);
}
String inMaintenanceBy = genericService.updateMaintenanceBy(user);
SeocGenericResponse seocResponse = new SeocGenericResponse();
seocResponse.setInMaintenanceBy(inMaintenanceBy);
try
{
String result = mapper.writeValueAsString(seocResponse);
HttpHeaders responseHeaders = new HttpHeaders();
responseHeaders.setContentType(MediaType.APPLICATION_JSON_UTF8);
logger.info("**Returning from updating inMaintenanceBy data ** ");
return new ResponseEntity<String>(result, responseHeaders, HttpStatus.OK);
} catch (JsonProcessingException e)
{
logger.error("Json Exception occured in response of updateMaintenance." + e.getMessage());
return new ResponseEntity<String>("Exception occured generating JSON",
HttpStatus.BAD_REQUEST);
}
}
}