Skip to content

Commit

Permalink
Fixes for error redirection and query string
Browse files Browse the repository at this point in the history
  • Loading branch information
haarli committed Jun 3, 2024
1 parent 8792820 commit 0560bf4
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class RequestHelper {
private final String contextPath;
private final URI prettyRequestURL;
private final URI originalRequestURL;
private final String requestQueryString;
private String requestQueryString;



Expand Down Expand Up @@ -62,6 +62,10 @@ public class RequestHelper {
}

this.requestQueryString = request.getQueryString() != null ? request.getQueryString() : "";
if(!requestQueryString.isEmpty() && !requestQueryString.startsWith("?"))
{
this.requestQueryString= "?" + requestQueryString;
}
addParameters(requestQueryString);
}

Expand Down
16 changes: 8 additions & 8 deletions src/main/java/de/mpg/imeji/presentation/security/LoginBean.java
Original file line number Diff line number Diff line change
Expand Up @@ -135,15 +135,15 @@ private void redirectAfterLogin() {

private void initRequestUrl() {
//LOGGER.info("PrettyContext: " + PrettyContext.getCurrentInstance().getRequestURL().toURL());
LOGGER.info("RequestHelper pretty: " + RequestHelper.getCurrentInstance().getPrettyRequestURL().toString());
LOGGER.info("RequestHelper original: " + RequestHelper.getCurrentInstance().getOriginalRequestURL().toString());
LOGGER.info(
"HttpRequestUri: " + ((HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest()).getRequestURI());
LOGGER.info("HttpRequestAttribute: " + ((HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest())
.getAttribute("javax.servlet.forward.request_uri"));
//LOGGER.info("RequestHelper pretty: " + RequestHelper.getCurrentInstance().getPrettyRequestURL().toString());
//LOGGER.info("RequestHelper original: " + RequestHelper.getCurrentInstance().getOriginalRequestURL().toString());
//LOGGER.info(
// "HttpRequestUri: " + ((HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest()).getRequestURI());
//LOGGER.info("HttpRequestAttribute: " + ((HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest())
// .getAttribute("javax.servlet.forward.request_uri"));
//LOGGER.info("PrettyContext: " + PrettyContext.getCurrentInstance().getRequestQueryString().toQueryString());
LOGGER.info(((HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest()).getQueryString());
LOGGER.info("RequestHelper: " + RequestHelper.getCurrentInstance().getRequestQueryString());
//LOGGER.info(((HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest()).getQueryString());
//LOGGER.info("RequestHelper: " + RequestHelper.getCurrentInstance().getRequestQueryString());

this.requestUrl = getNavigation().getApplicationUri() + RequestHelper.getCurrentInstance().getPrettyRequestURL().toString()
+ RequestHelper.getCurrentInstance().getRequestQueryString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,16 +90,22 @@ public void doFilter(ServletRequest serv, ServletResponse resp, FilterChain chai
}
} catch (NotFoundException e) {
((HttpServletResponse) resp).sendError(Status.NOT_FOUND.getStatusCode(), "RESOURCE_NOT_FOUND");
return;
} catch (AuthenticationError e) {
redirectToLoginPage(serv, resp);
return;
} catch (NotAllowedError e) {
((HttpServletResponse) resp).sendError(Status.FORBIDDEN.getStatusCode(), "FORBIDDEN");
return;
} catch (Exception e) {
LOGGER.error("Error in security Filter", e);
((HttpServletResponse) resp).sendError(Status.INTERNAL_SERVER_ERROR.getStatusCode(), "INTERNAL_SERVER_ERROR");
return;
} finally {
chain.doFilter(serv, resp);

}
chain.doFilter(serv, resp);

}

/**
Expand Down Expand Up @@ -220,8 +226,9 @@ private void redirectToLoginPage(ServletRequest serv, ServletResponse resp) thro
HttpServletRequest request = (HttpServletRequest) serv;
String url = NAVIGATION.getApplicationUri() + RequestHelper.getCurrentInstance(request).getPrettyRequestURL().toString();
Map<String, List<String>> params = RequestHelper.getCurrentInstance(request).getRequestQueryParameters();
((HttpServletResponse) resp).sendRedirect(serv.getServletContext().getContextPath() + "/login?redirect="
+ URLEncoder.encode(url + HistoryUtil.paramsMapToString(params), "UTF-8"));
String urlToRedirect = serv.getServletContext().getContextPath() + "/login?redirect="
+ URLEncoder.encode(url + HistoryUtil.paramsMapToString(params), "UTF-8");
((HttpServletResponse) resp).sendRedirect(urlToRedirect);

}

Expand Down

0 comments on commit 0560bf4

Please sign in to comment.