change logconfig and global exception handler
parent
c96e926f75
commit
ae6c497f36
|
|
@ -1,16 +1,12 @@
|
||||||
package com.nttdata.calender.api;
|
package com.nttdata.calender.api;
|
||||||
|
|
||||||
import java.lang.annotation.Repeatable;
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
|
||||||
import javax.servlet.http.HttpServletRequest;
|
import org.apache.logging.log4j.LogManager;
|
||||||
import javax.servlet.http.HttpSession;
|
import org.apache.logging.log4j.Logger;
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.http.HttpStatus;
|
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.web.bind.annotation.CrossOrigin;
|
import org.springframework.web.bind.annotation.CrossOrigin;
|
||||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
import org.springframework.web.bind.annotation.PostMapping;
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestBody;
|
import org.springframework.web.bind.annotation.RequestBody;
|
||||||
|
|
@ -19,8 +15,6 @@ import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
import com.bmc.arsys.api.ARException;
|
import com.bmc.arsys.api.ARException;
|
||||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||||
import com.nttdata.ErrorResponse;
|
|
||||||
import com.nttdata.calender.api.rsso.Rsso;
|
|
||||||
import com.nttdata.calender.approval.Approval;
|
import com.nttdata.calender.approval.Approval;
|
||||||
import com.nttdata.calender.approval.ApprovalUpdateRequest;
|
import com.nttdata.calender.approval.ApprovalUpdateRequest;
|
||||||
import com.nttdata.calender.changes.Change;
|
import com.nttdata.calender.changes.Change;
|
||||||
|
|
@ -41,8 +35,6 @@ import com.nttdata.calender.states.StateChangeRequest;
|
||||||
import com.nttdata.calender.states.StateResponse;
|
import com.nttdata.calender.states.StateResponse;
|
||||||
import com.nttdata.calender.supportgroup.SupportGroup;
|
import com.nttdata.calender.supportgroup.SupportGroup;
|
||||||
|
|
||||||
import net.bytebuddy.asm.Advice.Origin;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* REST Controller for Remedy Data
|
* REST Controller for Remedy Data
|
||||||
*/
|
*/
|
||||||
|
|
@ -54,6 +46,8 @@ public class KalenderRestController {
|
||||||
private final PackageType packageType;
|
private final PackageType packageType;
|
||||||
private final Approval approval;
|
private final Approval approval;
|
||||||
|
|
||||||
|
private static final Logger applicationLogger = LogManager.getLogger("application");
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
public KalenderRestController(RemedyJavaAPI javaAPI, Change change, Implementer implementer,
|
public KalenderRestController(RemedyJavaAPI javaAPI, Change change, Implementer implementer,
|
||||||
PackageType packageType, Approval approval) {
|
PackageType packageType, Approval approval) {
|
||||||
|
|
@ -217,6 +211,7 @@ public class KalenderRestController {
|
||||||
@PostMapping("/api/updateState")
|
@PostMapping("/api/updateState")
|
||||||
public ResponseEntity<String> updateState(@RequestBody StateChangeRequest request,
|
public ResponseEntity<String> updateState(@RequestBody StateChangeRequest request,
|
||||||
@Autowired StateChange stateChange) throws ARException {
|
@Autowired StateChange stateChange) throws ARException {
|
||||||
|
logRequest("/updateState", request.toString());
|
||||||
var response = stateChange.createStateChange(request);
|
var response = stateChange.createStateChange(request);
|
||||||
return ResponseEntity.ok(response);
|
return ResponseEntity.ok(response);
|
||||||
}
|
}
|
||||||
|
|
@ -259,17 +254,8 @@ public class KalenderRestController {
|
||||||
return implementer.get(request);
|
return implementer.get(request);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
private void logRequest(String endpoint, String request) {
|
||||||
* Handles the exception raised by ARException.
|
applicationLogger.info("Received request on endpoint %s with body:", endpoint);
|
||||||
*
|
applicationLogger.info(request);
|
||||||
* @param e the ARException raised
|
|
||||||
* @return an {@link ErrorResponse} with the error message
|
|
||||||
*/
|
|
||||||
@CrossOrigin("*")
|
|
||||||
@ExceptionHandler(ARException.class)
|
|
||||||
public ResponseEntity<ErrorResponse> handleARException(ARException e) {
|
|
||||||
var errorResponse = new ErrorResponse("Error occured", e.getMessage());
|
|
||||||
javaAPI.handleException(e, e.getMessage());
|
|
||||||
return new ResponseEntity<ErrorResponse>(errorResponse, HttpStatus.INTERNAL_SERVER_ERROR);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -32,14 +32,14 @@ public class RemedyJavaAPI {
|
||||||
|
|
||||||
private ARServerUser server;
|
private ARServerUser server;
|
||||||
private String formName = "ASF:WI_TAS_Paket";
|
private String formName = "ASF:WI_TAS_Paket";
|
||||||
private static Logger logger = LogManager.getLogger("logger");
|
private static Logger applicationLogger = LogManager.getLogger("application");
|
||||||
// TODO: work with form CTM:Support Group
|
// TODO: work with form CTM:Support Group
|
||||||
private static final String QUALSTR = "\'Request ID\' != \"\"";
|
private static final String QUALSTR = "\'Request ID\' != \"\"";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets up the Remedy API with the server and admin user/password.
|
* Sets up the Remedy API with the server and admin user/password.
|
||||||
*/
|
*/
|
||||||
public RemedyJavaAPI() {
|
public RemedyJavaAPI() throws ARException {
|
||||||
server = new ARServerUser();
|
server = new ARServerUser();
|
||||||
server.setServer("itsm-app-dev.asfinag.at");
|
server.setServer("itsm-app-dev.asfinag.at");
|
||||||
server.setUser("changecalender_integration");
|
server.setUser("changecalender_integration");
|
||||||
|
|
@ -60,20 +60,10 @@ public class RemedyJavaAPI {
|
||||||
/**
|
/**
|
||||||
* Connects the current user to the server.
|
* Connects the current user to the server.
|
||||||
*/
|
*/
|
||||||
void connect() {
|
void connect() throws ARException {
|
||||||
logger.info("Connecting to AR Server");
|
applicationLogger.info("Connecting to AR Server");
|
||||||
try {
|
server.verifyUser();
|
||||||
server.verifyUser();
|
applicationLogger.info("Connected to AR Server " +
|
||||||
} catch (ARException e) {
|
|
||||||
// This exception is triggered by a bad server, password or,
|
|
||||||
// if guest access is turned off, by an unknown username.
|
|
||||||
logger.error(e + "| Cannot verify user " +
|
|
||||||
server.getUser() + ".");
|
|
||||||
handleException(e, "Cannot verify user " +
|
|
||||||
server.getUser() + ".");
|
|
||||||
System.exit(1);
|
|
||||||
}
|
|
||||||
logger.info("Connected to AR Server " +
|
|
||||||
server.getServer());
|
server.getServer());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -134,12 +124,12 @@ public class RemedyJavaAPI {
|
||||||
});
|
});
|
||||||
|
|
||||||
var entryIdOut = server.createEntry(formName, entry);
|
var entryIdOut = server.createEntry(formName, entry);
|
||||||
logger.info("Entry created. The id # is " +
|
applicationLogger.info("Entry created. The id # is " +
|
||||||
entryIdOut);
|
entryIdOut);
|
||||||
|
|
||||||
var lastStatus = server.getLastStatus();
|
var lastStatus = server.getLastStatus();
|
||||||
if (!server.getLastStatus().isEmpty()) {
|
if (!server.getLastStatus().isEmpty()) {
|
||||||
logger.info("Warning: " + lastStatus);
|
applicationLogger.info("Warning: " + lastStatus);
|
||||||
return lastStatus.toString();
|
return lastStatus.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -194,7 +184,7 @@ public class RemedyJavaAPI {
|
||||||
if (qualStr.isEmpty())
|
if (qualStr.isEmpty())
|
||||||
qualStr = QUALSTR;
|
qualStr = QUALSTR;
|
||||||
|
|
||||||
logger.info("Retrieving entries from Form :" + formName + " with qualification " +
|
applicationLogger.info("Retrieving entries from Form :" + formName + " with qualification " +
|
||||||
qualStr);
|
qualStr);
|
||||||
// Retrieve the detail info of all fields from the form.
|
// Retrieve the detail info of all fields from the form.
|
||||||
List<Field> fields = server.getListFieldObjects(formName);
|
List<Field> fields = server.getListFieldObjects(formName);
|
||||||
|
|
@ -248,9 +238,6 @@ public class RemedyJavaAPI {
|
||||||
var total = sliceEnd - sliceStart;
|
var total = sliceEnd - sliceStart;
|
||||||
var remainder = total % maxEntriesNumber;
|
var remainder = total % maxEntriesNumber;
|
||||||
var loop = total / maxEntriesNumber;
|
var loop = total / maxEntriesNumber;
|
||||||
System.out.println(loop);
|
|
||||||
System.out.println(remainder);
|
|
||||||
System.out.println(sliceStart);
|
|
||||||
|
|
||||||
for (int i = 0; i < loop; i++) {
|
for (int i = 0; i < loop; i++) {
|
||||||
fetched = server.getListEntryObjects(
|
fetched = server.getListEntryObjects(
|
||||||
|
|
@ -275,19 +262,6 @@ public class RemedyJavaAPI {
|
||||||
return statistics.get(0).getResult().getIntValue();
|
return statistics.get(0).getResult().getIntValue();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Exception handling method that logs errors, prints a stack trace, and outputs
|
|
||||||
* the last server status.
|
|
||||||
*
|
|
||||||
* @param e the ARException
|
|
||||||
* @param errMessage the error message
|
|
||||||
*/
|
|
||||||
public void handleException(ARException e, String errMessage) {
|
|
||||||
logger.error(errMessage, e);
|
|
||||||
logger.error(server.getLastStatus());
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Prints a specific status type depending on the status list.
|
* Prints a specific status type depending on the status list.
|
||||||
*
|
*
|
||||||
|
|
@ -333,17 +307,15 @@ public class RemedyJavaAPI {
|
||||||
* @param date the new date to be modified
|
* @param date the new date to be modified
|
||||||
* @param fieldId the ID of the field
|
* @param fieldId the ID of the field
|
||||||
*/
|
*/
|
||||||
void modifyEntryForReset(String entryId, Date date, int fieldId) {
|
void modifyEntryForReset(String entryId, Date date, int fieldId) throws ARException {
|
||||||
Timestamp ts = new Timestamp(date);
|
Timestamp ts = new Timestamp(date);
|
||||||
try {
|
|
||||||
Entry entry = server.getEntry(formName, entryId, null);
|
Entry entry = server.getEntry(formName, entryId, null);
|
||||||
entry.put(fieldId, new Value(ts));
|
entry.put(fieldId, new Value(ts));
|
||||||
if (entry.get(fieldId) != null) {
|
if (entry.get(fieldId) != null) {
|
||||||
server.setEntry(formName, entryId, entry, null, 0);
|
server.setEntry(formName, entryId, entry, null, 0);
|
||||||
System.out.println("Entry #" + entryId + " modified successfully.");
|
System.out.println("Entry #" + entryId + " modified successfully.");
|
||||||
}
|
|
||||||
} catch (ARException e) {
|
|
||||||
handleException(e, "Cannot modify the entry. ");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -5,10 +5,7 @@ import java.text.SimpleDateFormat;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
import java.util.Set;
|
|
||||||
import java.util.TimeZone;
|
import java.util.TimeZone;
|
||||||
import java.util.stream.Collectors;
|
|
||||||
|
|
||||||
import org.apache.logging.log4j.LogManager;
|
import org.apache.logging.log4j.LogManager;
|
||||||
import org.apache.logging.log4j.Logger;
|
import org.apache.logging.log4j.Logger;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
|
@ -110,7 +107,6 @@ public class Change {
|
||||||
request.getSort().getSortInfo(queryChange), request.getSliceStart(),
|
request.getSort().getSortInfo(queryChange), request.getSliceStart(),
|
||||||
request.getSliceEnd());
|
request.getSliceEnd());
|
||||||
var entriesSize = api.getFormSize(qualifier, this.queryChange.getFormName());
|
var entriesSize = api.getFormSize(qualifier, this.queryChange.getFormName());
|
||||||
System.out.println(entriesSize);
|
|
||||||
var changes = new ArrayList<ChangeItem>();
|
var changes = new ArrayList<ChangeItem>();
|
||||||
entries.forEach(entry -> {
|
entries.forEach(entry -> {
|
||||||
var change = new ChangeItem(entry.getEntryId());
|
var change = new ChangeItem(entry.getEntryId());
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
package com.nttdata;
|
package com.nttdata.calender.errorhandling;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The ErrorResponse class represents an error response that can be returned by
|
* The ErrorResponse class represents an error response that can be returned by
|
||||||
|
|
@ -7,7 +7,7 @@ package com.nttdata;
|
||||||
*/
|
*/
|
||||||
public class ErrorResponse {
|
public class ErrorResponse {
|
||||||
private String message;
|
private String message;
|
||||||
private String details;
|
private String error;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructs an ErrorResponse instance with the provided message and details.
|
* Constructs an ErrorResponse instance with the provided message and details.
|
||||||
|
|
@ -17,7 +17,7 @@ public class ErrorResponse {
|
||||||
*/
|
*/
|
||||||
public ErrorResponse(String message, String details) {
|
public ErrorResponse(String message, String details) {
|
||||||
this.message = message;
|
this.message = message;
|
||||||
this.details = details;
|
this.error = details;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -34,8 +34,8 @@ public class ErrorResponse {
|
||||||
*
|
*
|
||||||
* @return String providing additional details about the error
|
* @return String providing additional details about the error
|
||||||
*/
|
*/
|
||||||
public String getDetails() {
|
public String getError() {
|
||||||
return details;
|
return error;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -52,7 +52,7 @@ public class ErrorResponse {
|
||||||
*
|
*
|
||||||
* @param details String providing additional details about the error
|
* @param details String providing additional details about the error
|
||||||
*/
|
*/
|
||||||
public void setDetails(String details) {
|
public void setError(String details) {
|
||||||
this.details = details;
|
this.error = details;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -0,0 +1,38 @@
|
||||||
|
package com.nttdata.calender.errorhandling;
|
||||||
|
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
|
||||||
|
import org.apache.logging.log4j.LogManager;
|
||||||
|
import org.apache.logging.log4j.Logger;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.web.bind.annotation.ControllerAdvice;
|
||||||
|
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||||
|
|
||||||
|
import com.bmc.arsys.api.ARException;
|
||||||
|
|
||||||
|
@ControllerAdvice
|
||||||
|
public class GlobalExceptionHandler {
|
||||||
|
private static final Logger errorLogger = LogManager.getLogger("error");
|
||||||
|
|
||||||
|
@ExceptionHandler(ARException.class)
|
||||||
|
public ResponseEntity<ErrorResponse> handleARException(ARException e, HttpServletRequest request) {
|
||||||
|
var errorResponse = new ErrorResponse(e.getMessage(), e.getClass().getSimpleName());
|
||||||
|
errorResponse.setMessage(e.getMessage());
|
||||||
|
errorResponse.setError(e.getClass().getSimpleName());
|
||||||
|
|
||||||
|
errorLogger.error(e.getMessage(), e);
|
||||||
|
return new ResponseEntity<>(errorResponse, HttpStatus.INTERNAL_SERVER_ERROR);
|
||||||
|
}
|
||||||
|
|
||||||
|
@ExceptionHandler(Exception.class)
|
||||||
|
public ResponseEntity<ErrorResponse> handleGenericException(Exception e, HttpServletRequest request) {
|
||||||
|
var errorResponse = new ErrorResponse(e.getMessage(), e.getClass().getSimpleName());
|
||||||
|
errorLogger.error(e.getMessage(), e);
|
||||||
|
var response = new ResponseEntity<>(errorResponse, HttpStatus.INTERNAL_SERVER_ERROR);
|
||||||
|
System.out.println(response.toString());
|
||||||
|
System.out.println(HttpStatus.INTERNAL_SERVER_ERROR);
|
||||||
|
System.out.println(errorResponse.toString());
|
||||||
|
return response;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -2,30 +2,30 @@
|
||||||
<Configuration status="info">
|
<Configuration status="info">
|
||||||
<Appenders>
|
<Appenders>
|
||||||
<RollingFile name="application"
|
<RollingFile name="application"
|
||||||
fileName="./backend/log/application.log" filePattern="backend/log/application-%d{yyyy-MM-dd-HH-mm}-%i.log">
|
fileName="./backend/log/application.log" filePattern="./backend/log/application-%d{yyyy-MM-dd-HH-mm}-%i.log">
|
||||||
<PatternLayout>
|
<PatternLayout>
|
||||||
<Pattern>%d{yyyy-MM-dd-HH:mm:ss} %-5p %m%n%ex{full}</Pattern>
|
<Pattern>%d{yyyy-MM-dd-HH:mm:ss} %-5p %m%n%ex{full}</Pattern>
|
||||||
</PatternLayout>
|
</PatternLayout>
|
||||||
<Policies>
|
<Policies>
|
||||||
<SizeBasedTriggeringPolicy size="20KB" />
|
<SizeBasedTriggeringPolicy size="50KB" />
|
||||||
</Policies>
|
</Policies>
|
||||||
</RollingFile>
|
</RollingFile>
|
||||||
<RollingFile name="error"
|
<RollingFile name="error"
|
||||||
fileName="./backend/log/error.log" filePattern="backend/log/error-%d{yyyy-MM-dd-HH-mm}-%i.log">
|
fileName="./backend/log/error.log" filePattern="./backend/log/error-%d{yyyy-MM-dd-HH-mm}-%i.log">
|
||||||
<PatternLayout>
|
<PatternLayout>
|
||||||
<Pattern>%d{yyyy-MM-dd-HH:mm:ss} %-5p %m%n%ex{full}</Pattern>
|
<Pattern>%d{yyyy-MM-dd-HH:mm:ss} %-5p %m%n%ex{full}</Pattern>
|
||||||
</PatternLayout>
|
</PatternLayout>
|
||||||
<Policies>
|
<Policies>
|
||||||
<SizeBasedTriggeringPolicy size="5KB" />
|
<SizeBasedTriggeringPolicy size="10KB" />
|
||||||
</Policies>
|
</Policies>
|
||||||
</RollingFile>
|
</RollingFile>
|
||||||
<RollingFile name="warn"
|
<RollingFile name="warn"
|
||||||
fileName="./backend/log/warn.log" filePattern="backend/log/warn-%d{yyyy-MM-dd-HH-mm}-%i.log">
|
fileName="./backend/log/warn.log" filePattern="./backend/log/warn-%d{yyyy-MM-dd-HH-mm}-%i.log">
|
||||||
<PatternLayout>
|
<PatternLayout>
|
||||||
<Pattern>%d{yyyy-MM-dd-HH:mm:ss} %-5p %m%n</Pattern>
|
<Pattern>%d{yyyy-MM-dd-HH:mm:ss} %-5p %m%n</Pattern>
|
||||||
</PatternLayout>
|
</PatternLayout>
|
||||||
<Policies>
|
<Policies>
|
||||||
<SizeBasedTriggeringPolicy size="5KB" />
|
<SizeBasedTriggeringPolicy size="10KB" />
|
||||||
</Policies>
|
</Policies>
|
||||||
</RollingFile>
|
</RollingFile>
|
||||||
<Console name="Console" target="SYSTEM_OUT">
|
<Console name="Console" target="SYSTEM_OUT">
|
||||||
|
|
@ -33,13 +33,21 @@
|
||||||
</Console>
|
</Console>
|
||||||
</Appenders>
|
</Appenders>
|
||||||
<Loggers>
|
<Loggers>
|
||||||
<Logger name="logger" level="warn" additivity="false">
|
<!-- Change level of application to debug or info for logs in a file -->
|
||||||
<AppenderRef ref="warn" level="warn"/>
|
<Logger name="application" level="info" additivity="false">
|
||||||
<AppenderRef ref="error" level="error"/>
|
<AppenderRef ref="application"/>
|
||||||
|
</Logger>
|
||||||
|
<Logger name="error" level="error" additivity="false">
|
||||||
|
<AppenderRef ref="error"/>
|
||||||
|
</Logger>
|
||||||
|
<Logger name="warn" level="warn" additivity="false">
|
||||||
|
<AppenderRef ref="warn"/>
|
||||||
</Logger>
|
</Logger>
|
||||||
<Root level="info">
|
<Root level="info">
|
||||||
<AppenderRef ref="application"/>
|
|
||||||
<AppenderRef ref="Console"/>
|
<AppenderRef ref="Console"/>
|
||||||
|
<AppenderRef ref="application"/>
|
||||||
|
<AppenderRef ref="warn"/>
|
||||||
|
<AppenderRef ref="error"/>
|
||||||
</Root>
|
</Root>
|
||||||
</Loggers>
|
</Loggers>
|
||||||
</Configuration>
|
</Configuration>
|
||||||
Loading…
Reference in New Issue