use of teammates.common.util.LogMessageGenerator in project teammates by TEAMMATES.
the class ControllerServlet method doPost.
@Override
// used as fallback
@SuppressWarnings("PMD.AvoidCatchingThrowable")
public final void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
UserType userType = new GateKeeper().getCurrentUser();
String url = HttpRequestHelper.getRequestedUrl(req);
Map<String, String[]> params = HttpRequestHelper.getParameterMap(req);
try {
/* We are using the Template Method Design Pattern here.
* This method contains the high level logic of the request processing.
* Concrete details of the processing steps are to be implemented by child
* classes, based on request-specific needs.
*/
long startTime = System.currentTimeMillis();
log.info("Request received : [" + req.getMethod() + "] " + req.getRequestURL().toString() + ":" + HttpRequestHelper.printRequestParameters(req));
log.info("User agent : " + req.getHeader("User-Agent"));
Action c = new ActionFactory().getAction(req);
if (c.isValidUser()) {
ActionResult actionResult = c.executeAndPostProcess();
actionResult.writeSessionTokenToCookieIfRequired(req, resp);
actionResult.send(req, resp);
} else {
resp.sendRedirect(c.getAuthenticationRedirectUrl());
}
long timeTaken = System.currentTimeMillis() - startTime;
// This is the log message that is used to generate the 'activity log' for the admin.
log.info(c.getLogMessage() + "|||" + timeTaken);
} catch (PageNotFoundException e) {
log.warning(new LogMessageGenerator().generateActionFailureLogMessage(url, params, e, userType));
cleanUpStatusMessageInSession(req);
resp.sendRedirect(appendParamsToErrorPageUrl(Const.ViewURIs.ACTION_NOT_FOUND_PAGE, params, url));
} catch (EntityNotFoundException e) {
log.warning(new LogMessageGenerator().generateActionFailureLogMessage(url, params, e, userType));
cleanUpStatusMessageInSession(req);
resp.sendRedirect(appendParamsToErrorPageUrl(Const.ViewURIs.ENTITY_NOT_FOUND_PAGE, params, url));
} catch (FeedbackSessionNotVisibleException e) {
log.warning(new LogMessageGenerator().generateActionFailureLogMessage(url, params, e, userType));
cleanUpStatusMessageInSession(req);
req.getSession().setAttribute(Const.ParamsNames.FEEDBACK_SESSION_NOT_VISIBLE, e.getStartTimeString());
resp.sendRedirect(appendParamsToErrorPageUrl(Const.ViewURIs.FEEDBACK_SESSION_NOT_VISIBLE, params, url));
} catch (InvalidOriginException e) {
log.warning(new LogMessageGenerator().generateActionFailureLogMessage(url, params, e, userType));
cleanUpStatusMessageInSession(req);
resp.sendRedirect(appendParamsToErrorPageUrl(Const.ViewURIs.INVALID_ORIGIN, params, url));
} catch (UnauthorizedAccessException e) {
log.warning(new LogMessageGenerator().generateActionFailureLogMessage(url, params, e, userType));
cleanUpStatusMessageInSession(req);
resp.sendRedirect(appendParamsToErrorPageUrl(Const.ViewURIs.UNAUTHORIZED, params, url));
} catch (DeadlineExceededException | DatastoreTimeoutException e) {
/*This exception may not be caught because GAE kills
the request soon after throwing it. In that case, the error
message in the log will be emailed to the admin by a separate
cron job.*/
cleanUpStatusMessageInSession(req);
log.severe("Deadline exceeded exception caught by ControllerServlet : " + TeammatesException.toStringWithStackTrace(e));
resp.sendRedirect(appendParamsToErrorPageUrl(Const.ViewURIs.DEADLINE_EXCEEDED_ERROR_PAGE, params, url));
} catch (InvalidPostParametersException e) {
String requestUrl = req.getRequestURL().toString();
log.info(e.getMessage());
cleanUpStatusMessageInSession(req);
List<StatusMessage> statusMessagesToUser = new ArrayList<>();
statusMessagesToUser.add(new StatusMessage(Const.StatusMessages.NULL_POST_PARAMETER_MESSAGE, StatusMessageColor.WARNING));
req.getSession().setAttribute(Const.ParamsNames.STATUS_MESSAGES_LIST, statusMessagesToUser);
if (requestUrl.contains("/instructor")) {
resp.sendRedirect(Const.ActionURIs.INSTRUCTOR_HOME_PAGE);
} else if (requestUrl.contains("/student")) {
resp.sendRedirect(Const.ActionURIs.STUDENT_HOME_PAGE);
} else if (requestUrl.contains("/admin")) {
resp.sendRedirect(Const.ActionURIs.ADMIN_HOME_PAGE);
} else {
cleanUpStatusMessageInSession(req);
resp.sendRedirect(appendParamsToErrorPageUrl(Const.ViewURIs.ERROR_PAGE, params, url));
}
} catch (Throwable t) {
/* Log only stack trace to prevent delay in termination of request
* which can result in GAE shutting down the instance.
* Note that severe logs are sent by email automatically in the cron job auto/compileLogs.
*/
log.severe("Unexpected exception caught by ControllerServlet : " + TeammatesException.toStringWithStackTrace(t));
cleanUpStatusMessageInSession(req);
resp.sendRedirect(appendParamsToErrorPageUrl(Const.ViewURIs.ERROR_PAGE, params, url));
}
}
use of teammates.common.util.LogMessageGenerator in project teammates by TEAMMATES.
the class AutomatedServlet method doPost.
@Override
// used as fallback
@SuppressWarnings("PMD.AvoidCatchingThrowable")
public void doPost(HttpServletRequest req, HttpServletResponse resp) {
try {
if (req.getParameterNames().hasMoreElements()) {
log.info(HttpRequestHelper.printRequestParameters(req));
}
AutomatedAction action = new AutomatedActionFactory().getAction(req, resp);
String url = HttpRequestHelper.getRequestedUrl(req);
// Do not log task queue worker actions to prevent excessive logging
if (!url.startsWith("/worker/")) {
Map<String, String[]> params = HttpRequestHelper.getParameterMap(req);
// no logged-in user for automated servlet
LogMessageGenerator logGenerator = new LogMessageGenerator();
log.info(logGenerator.generateBasicActivityLogMessage(url, params, action.getActionMessage(), null));
}
action.execute();
} catch (Throwable t) {
String requestUrl = req.getRequestURL().toString();
log.severe("Exception occured while performing " + requestUrl + "|||" + TeammatesException.toStringWithStackTrace(t));
// so task will be recognised as failed and GAE retry mechanism can kick in
resp.setStatus(500);
}
}
use of teammates.common.util.LogMessageGenerator in project teammates by TEAMMATES.
the class FeedbackResponseAdjustmentWorkerAction method execute.
@Override
public void execute() {
String courseId = getRequestParamValue(ParamsNames.COURSE_ID);
Assumption.assertPostParamNotNull(ParamsNames.COURSE_ID, courseId);
String sessionName = getRequestParamValue(ParamsNames.FEEDBACK_SESSION_NAME);
Assumption.assertPostParamNotNull(ParamsNames.FEEDBACK_SESSION_NAME, sessionName);
String enrollmentDetails = getRequestParamValue(ParamsNames.ENROLLMENT_DETAILS);
Assumption.assertPostParamNotNull(ParamsNames.ENROLLMENT_DETAILS, enrollmentDetails);
log.info("Adjusting submissions for feedback session :" + sessionName + "in course : " + courseId);
FeedbackSessionAttributes feedbackSession = logic.getFeedbackSession(sessionName, courseId);
String errorString = "Error encountered while adjusting feedback session responses of %s in course %s: %s%n%s";
if (feedbackSession == null) {
log.severe(String.format(errorString, sessionName, courseId, "feedback session is null", ""));
setForRetry();
return;
}
List<FeedbackResponseAttributes> allResponses = logic.getFeedbackResponsesForSession(feedbackSession.getFeedbackSessionName(), feedbackSession.getCourseId());
List<StudentEnrollDetails> enrollmentList = JsonUtils.fromJson(enrollmentDetails, new TypeToken<List<StudentEnrollDetails>>() {
}.getType());
for (FeedbackResponseAttributes response : allResponses) {
try {
logic.adjustFeedbackResponseForEnrollments(enrollmentList, response);
} catch (Exception e) {
String url = HttpRequestHelper.getRequestedUrl(request);
Map<String, String[]> params = HttpRequestHelper.getParameterMap(request);
// no logged-in user for worker
String logMessage = new LogMessageGenerator().generateActionFailureLogMessage(url, params, e, null);
log.severe(String.format(errorString, sessionName, courseId, e.getMessage(), logMessage));
setForRetry();
return;
}
}
}
use of teammates.common.util.LogMessageGenerator in project teammates by TEAMMATES.
the class PublicImageServlet method doPost.
@SuppressWarnings("unchecked")
@Override
public void doPost(HttpServletRequest req, HttpServletResponse resp) {
String url = HttpRequestHelper.getRequestedUrl(req);
UserType userType = new GateKeeper().getCurrentUser();
Map<String, String[]> requestParameters = req.getParameterMap();
String blobKey = HttpRequestHelper.getValueFromParamMap(requestParameters, Const.ParamsNames.BLOB_KEY);
Assumption.assertPostParamNotNull(Const.ParamsNames.BLOB_KEY, blobKey);
try {
if (blobKey.isEmpty()) {
String message = "Failed to serve image with URL : blobKey is missing";
Map<String, String[]> params = HttpRequestHelper.getParameterMap(req);
log.info(new LogMessageGenerator().generateBasicActivityLogMessage(url, params, message, userType));
resp.sendError(1, "No image found");
} else {
resp.setContentType("image/png");
BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();
blobstoreService.serve(new BlobKey(blobKey), resp);
// TODO : restrict image request to those "public" files only
String message = "Public image request with URL: <br>" + "<a href=\"" + url + "\" target=\"_blank\" rel=\"noopener noreferrer\" >" + url + "</a>";
Map<String, String[]> params = HttpRequestHelper.getParameterMap(req);
log.info(new LogMessageGenerator().generateBasicActivityLogMessage(url, params, message, userType));
}
} catch (IOException ioe) {
Map<String, String[]> params = HttpRequestHelper.getParameterMap(req);
log.warning(new LogMessageGenerator().generateActionFailureLogMessage(url, params, ioe, userType));
} catch (Exception e) {
log.severe("Exception occured while performing " + Const.PublicActionNames.PUBLIC_IMAGE_SERVE_ACTION + ": " + TeammatesException.toStringWithStackTrace(e));
}
}
Aggregations