change logconfig and global exception handler
parent
c96e926f75
commit
ae6c497f36
|
|
@ -1,16 +1,12 @@
|
|||
package com.nttdata.calender.api;
|
||||
|
||||
import java.lang.annotation.Repeatable;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpSession;
|
||||
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
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.PostMapping;
|
||||
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.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.ApprovalUpdateRequest;
|
||||
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.supportgroup.SupportGroup;
|
||||
|
||||
import net.bytebuddy.asm.Advice.Origin;
|
||||
|
||||
/**
|
||||
* REST Controller for Remedy Data
|
||||
*/
|
||||
|
|
@ -54,6 +46,8 @@ public class KalenderRestController {
|
|||
private final PackageType packageType;
|
||||
private final Approval approval;
|
||||
|
||||
private static final Logger applicationLogger = LogManager.getLogger("application");
|
||||
|
||||
@Autowired
|
||||
public KalenderRestController(RemedyJavaAPI javaAPI, Change change, Implementer implementer,
|
||||
PackageType packageType, Approval approval) {
|
||||
|
|
@ -217,6 +211,7 @@ public class KalenderRestController {
|
|||
@PostMapping("/api/updateState")
|
||||
public ResponseEntity<String> updateState(@RequestBody StateChangeRequest request,
|
||||
@Autowired StateChange stateChange) throws ARException {
|
||||
logRequest("/updateState", request.toString());
|
||||
var response = stateChange.createStateChange(request);
|
||||
return ResponseEntity.ok(response);
|
||||
}
|
||||
|
|
@ -259,17 +254,8 @@ public class KalenderRestController {
|
|||
return implementer.get(request);
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles the exception raised by ARException.
|
||||
*
|
||||
* @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);
|
||||
private void logRequest(String endpoint, String request) {
|
||||
applicationLogger.info("Received request on endpoint %s with body:", endpoint);
|
||||
applicationLogger.info(request);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -32,14 +32,14 @@ public class RemedyJavaAPI {
|
|||
|
||||
private ARServerUser server;
|
||||
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
|
||||
private static final String QUALSTR = "\'Request ID\' != \"\"";
|
||||
|
||||
/**
|
||||
* Sets up the Remedy API with the server and admin user/password.
|
||||
*/
|
||||
public RemedyJavaAPI() {
|
||||
public RemedyJavaAPI() throws ARException {
|
||||
server = new ARServerUser();
|
||||
server.setServer("itsm-app-dev.asfinag.at");
|
||||
server.setUser("changecalender_integration");
|
||||
|
|
@ -60,20 +60,10 @@ public class RemedyJavaAPI {
|
|||
/**
|
||||
* Connects the current user to the server.
|
||||
*/
|
||||
void connect() {
|
||||
logger.info("Connecting to AR Server");
|
||||
try {
|
||||
void connect() throws ARException {
|
||||
applicationLogger.info("Connecting to AR Server");
|
||||
server.verifyUser();
|
||||
} 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 " +
|
||||
applicationLogger.info("Connected to AR Server " +
|
||||
server.getServer());
|
||||
}
|
||||
|
||||
|
|
@ -134,12 +124,12 @@ public class RemedyJavaAPI {
|
|||
});
|
||||
|
||||
var entryIdOut = server.createEntry(formName, entry);
|
||||
logger.info("Entry created. The id # is " +
|
||||
applicationLogger.info("Entry created. The id # is " +
|
||||
entryIdOut);
|
||||
|
||||
var lastStatus = server.getLastStatus();
|
||||
if (!server.getLastStatus().isEmpty()) {
|
||||
logger.info("Warning: " + lastStatus);
|
||||
applicationLogger.info("Warning: " + lastStatus);
|
||||
return lastStatus.toString();
|
||||
}
|
||||
|
||||
|
|
@ -194,7 +184,7 @@ public class RemedyJavaAPI {
|
|||
if (qualStr.isEmpty())
|
||||
qualStr = QUALSTR;
|
||||
|
||||
logger.info("Retrieving entries from Form :" + formName + " with qualification " +
|
||||
applicationLogger.info("Retrieving entries from Form :" + formName + " with qualification " +
|
||||
qualStr);
|
||||
// Retrieve the detail info of all fields from the form.
|
||||
List<Field> fields = server.getListFieldObjects(formName);
|
||||
|
|
@ -248,9 +238,6 @@ public class RemedyJavaAPI {
|
|||
var total = sliceEnd - sliceStart;
|
||||
var remainder = 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++) {
|
||||
fetched = server.getListEntryObjects(
|
||||
|
|
@ -275,19 +262,6 @@ public class RemedyJavaAPI {
|
|||
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.
|
||||
*
|
||||
|
|
@ -333,17 +307,15 @@ public class RemedyJavaAPI {
|
|||
* @param date the new date to be modified
|
||||
* @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);
|
||||
try {
|
||||
|
||||
Entry entry = server.getEntry(formName, entryId, null);
|
||||
entry.put(fieldId, new Value(ts));
|
||||
if (entry.get(fieldId) != null) {
|
||||
server.setEntry(formName, entryId, entry, null, 0);
|
||||
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.Date;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.TimeZone;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
|
@ -110,7 +107,6 @@ public class Change {
|
|||
request.getSort().getSortInfo(queryChange), request.getSliceStart(),
|
||||
request.getSliceEnd());
|
||||
var entriesSize = api.getFormSize(qualifier, this.queryChange.getFormName());
|
||||
System.out.println(entriesSize);
|
||||
var changes = new ArrayList<ChangeItem>();
|
||||
entries.forEach(entry -> {
|
||||
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
|
||||
|
|
@ -7,7 +7,7 @@ package com.nttdata;
|
|||
*/
|
||||
public class ErrorResponse {
|
||||
private String message;
|
||||
private String details;
|
||||
private String error;
|
||||
|
||||
/**
|
||||
* Constructs an ErrorResponse instance with the provided message and details.
|
||||
|
|
@ -17,7 +17,7 @@ public class ErrorResponse {
|
|||
*/
|
||||
public ErrorResponse(String message, String details) {
|
||||
this.message = message;
|
||||
this.details = details;
|
||||
this.error = details;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -34,8 +34,8 @@ public class ErrorResponse {
|
|||
*
|
||||
* @return String providing additional details about the error
|
||||
*/
|
||||
public String getDetails() {
|
||||
return details;
|
||||
public String getError() {
|
||||
return error;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -52,7 +52,7 @@ public class ErrorResponse {
|
|||
*
|
||||
* @param details String providing additional details about the error
|
||||
*/
|
||||
public void setDetails(String details) {
|
||||
this.details = details;
|
||||
public void setError(String 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">
|
||||
<Appenders>
|
||||
<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>
|
||||
<Pattern>%d{yyyy-MM-dd-HH:mm:ss} %-5p %m%n%ex{full}</Pattern>
|
||||
</PatternLayout>
|
||||
<Policies>
|
||||
<SizeBasedTriggeringPolicy size="20KB" />
|
||||
<SizeBasedTriggeringPolicy size="50KB" />
|
||||
</Policies>
|
||||
</RollingFile>
|
||||
<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>
|
||||
<Pattern>%d{yyyy-MM-dd-HH:mm:ss} %-5p %m%n%ex{full}</Pattern>
|
||||
</PatternLayout>
|
||||
<Policies>
|
||||
<SizeBasedTriggeringPolicy size="5KB" />
|
||||
<SizeBasedTriggeringPolicy size="10KB" />
|
||||
</Policies>
|
||||
</RollingFile>
|
||||
<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>
|
||||
<Pattern>%d{yyyy-MM-dd-HH:mm:ss} %-5p %m%n</Pattern>
|
||||
</PatternLayout>
|
||||
<Policies>
|
||||
<SizeBasedTriggeringPolicy size="5KB" />
|
||||
<SizeBasedTriggeringPolicy size="10KB" />
|
||||
</Policies>
|
||||
</RollingFile>
|
||||
<Console name="Console" target="SYSTEM_OUT">
|
||||
|
|
@ -33,13 +33,21 @@
|
|||
</Console>
|
||||
</Appenders>
|
||||
<Loggers>
|
||||
<Logger name="logger" level="warn" additivity="false">
|
||||
<AppenderRef ref="warn" level="warn"/>
|
||||
<AppenderRef ref="error" level="error"/>
|
||||
<!-- Change level of application to debug or info for logs in a file -->
|
||||
<Logger name="application" level="info" additivity="false">
|
||||
<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>
|
||||
<Root level="info">
|
||||
<AppenderRef ref="application"/>
|
||||
<AppenderRef ref="Console"/>
|
||||
<AppenderRef ref="application"/>
|
||||
<AppenderRef ref="warn"/>
|
||||
<AppenderRef ref="error"/>
|
||||
</Root>
|
||||
</Loggers>
|
||||
</Configuration>
|
||||
Loading…
Reference in New Issue