/** * Returns the URL to redirect to if it is determined that a connection needs to be renewed. * By default, the filter will redirect to /connect/{provider ID} with a "reconnect" query parameter. * This filter also handles GET requests to that same path before submitting a POST request to {@link ConnectController} for authorization. * May be overridden by a subclass to handle other flows, such as redirecting to a page that informs the user that a new connection is needed. * @param request The HTTP request that triggered the exception. * @param apiException The {@link ApiException}. * @return the URL to redirect to if a connection needs to be renewed. */ protected String getRefreshUrl(HttpServletRequest request, ApiException apiException) { String scopeNeeded = getRequiredScope(apiException); StringBuilder sb = new StringBuilder(request.getContextPath() + CONNECT_PATH + apiException.getProviderId()) .append(RECONNECT_PARAMETER_EQUALS_TRUE); if (scopeNeeded != null) { sb.append(SCOPE_PARAMETER_EQUALS + scopeNeeded); } return sb.toString(); }
private void handleExceptionFromFilterChain(Exception e, HttpServletRequest httpRequest, HttpServletResponse httpResponse) throws IOException, ServletException { RuntimeException ase = (ApiException) throwableAnalyzer.getFirstThrowableOfType(ApiException.class, throwableAnalyzer.determineCauseChain(e)); if (ase != null && ase instanceof ApiException) { ApiException apiException = (ApiException) ase; if (logger.isDebugEnabled()) { logger.debug("API Exception: " + e.getMessage()); } if (apiException instanceof NotAuthorizedException || apiException instanceof OperationNotPermittedException) { if (logger.isDebugEnabled()) { logger.debug("Redirecting for refresh of " + apiException.getProviderId() + " connection."); } httpResponse.sendRedirect(getRefreshUrl(httpRequest, apiException)); return; } } if (e instanceof ServletException) { throw (ServletException) e; } else if (e instanceof RuntimeException) { throw (RuntimeException) e; } // Wrap other Exceptions in a generic RuntimeException. This should never happen because // we've already covered all the possibilities for doFilter throw new RuntimeException(e); }