roy_sanu
asked on
Code Review Please...
Dear Experts,
let us know if logger is good for production or we have remove it out ? is that slows down at the applicationl level ?
if we use only after catch is that ok..
Thanks
Roy...
let us know if logger is good for production or we have remove it out ? is that slows down at the applicationl level ?
if we use only after catch is that ok..
logger.warn("proceeding with incomplete request from mobile: {}. cause: {}, message: {}",
requestBody.length(), ex.getCause(), ex.getMessage());
let us know in our application we are using hashmap is that good or map would be good ? @CrossOrigin
@RequestMapping(value = beneficiaryURL, method = RequestMethod.POST, produces = {"application/json" })
public HashMap<String, Object> screeningRecord(@RequestBody String requestBody,
HttpServletRequest request) throws Exception {
String remoteAddr = request.getRemoteAddr();
logger.info("got request from: {}", remoteAddr);
logger.debug("{}: received message: {}", remoteAddr, requestBody);
HashMap<String, Object> screeningRespone = new HashMap<>();
// Transaction id is generated
String transactionId = String.format("%s-%s", UUID.randomUUID().toString(),
dateTimeFormatter.format(new Date()));
logger.info("{}: Generated id: {}, url: {}. Saving...", remoteAddr,
transactionId, beneficiaryURL);
SubmissionInfo submissionInfo = new SubmissionInfo(transactionId, requestBody,
SubmissionInfo.PENDING, beneficiaryURL);
Integer partition = null;
try {
Map<String, Object> jsonRequest = load(requestBody);
Long microPlanId = Long.valueOf(jsonRequest.get("microPlanId").toString());
partition = (int) Math.abs(microPlanId % mobileUploadPartitionSize);
int beneficiaryCount = ((List) jsonRequest.getOrDefault("beneficiaryData",
Arrays.asList())).size();
submissionInfo.setBeneficiaryCount(beneficiaryCount);
submissionInfo.setTeamname(jsonRequest.getOrDefault("teamName", UNDECLARED).toString());
submissionInfo.setInstitutionCode(jsonRequest.getOrDefault("institutionCode",
UNDECLARED).toString());
submissionInfo.setDaySignOffID(jsonRequest.getOrDefault("daySignOffID",
UNDECLARED).toString());
String screeningDateUnix = jsonRequest.get("screeningDate").toString();
if(screeningDateUnix != null)
submissionInfo.setScreeningDate(new Date(Long.valueOf(screeningDateUnix)));
}
catch(Exception ex) {
logger.warn("proceeding with incomplete request from mobile: {}. cause: {}, message: {}",
requestBody.length(), ex.getCause(), ex.getMessage());
//ex.printStackTrace();
}
Thanks
Roy...
ASKER
can we provide me a sample of your understanding on the code so that i could take it forward from there..
I would normally use a factory method or a builder in this case
Thanks
roy
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks Stefan
The only things about your actual code:
You're catching a general exception (which is bad).
I would normally use a factory method or a builder in this case. Cause you should for sure refactor your code and extract this part. Then you can control there the amount of warnings logged. Especially as this would be the place for sanitation and validation of the user input. Which currently seems to miss entirely.
Your SubmissionInfo class seems to be a bit of a mess..
Why does it store the actual plain request and decoupled from it its properties? This allows the request and the properties to contain different, contradicting values.
Why do you need to create the transaction ID manually? This should be retrieved from a generator.