final class ApiThrowables { static final int BAD_REQUEST = 400; static final int NOT_FOUND = 404; public static ApiException newCourseIdNotFound(String courseId) { return new ApiException(2, NOT_FOUND,"Course with ID [" + courseId + "] does not exist."); } public static ApiException newBadCourseId(String courseId) { return new ApiException(2, BAD_REQUEST,"Course ID [" + courseId + "] is not valid."); } }
/** * 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(); }
@Test(expected = ApiException.class) public void nonJSONErrorResponse() { try { mockServer.expect(requestTo("https://api.twitter.com/1.1/statuses/home_timeline.json?count=20&include_entities=true")) .andExpect(method(GET)) .andRespond(withBadRequest().body("<h1>HTML response</h1>").contentType(APPLICATION_JSON)); twitter.timelineOperations().getHomeTimeline(); } catch (ApiException e) { assertEquals("Error consuming Twitter REST API", e.getMessage()); throw e; } }
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); }
if (statusCode != HttpStatus.SC_OK) { status = "ApiError"; throw new ApiException();
private <T> T getProfileByPublicUrl(String url, String fields, Class<T> type) { try { URI uri = URIBuilder.fromUri(BASE_URL + "url=" + URLEncoder.encode(url, "UTF-8") + fields).build(); return restOperations.exchange(uri, HttpMethod.GET, new HttpEntity<String>(""), type).getBody(); } catch (UnsupportedEncodingException unlikely) { unlikely.printStackTrace(); throw new ApiException("linkedin", "Unlikely unsupported encoding error", unlikely); } }
private <T> T getProfileByPublicUrl(String url, String fields, Class<T> type) { try { URI uri = URIBuilder.fromUri(BASE_URL + "url=" + URLEncoder.encode(url, "UTF-8") + fields).build(); return restOperations.exchange(uri, HttpMethod.GET, new HttpEntity<String>(""), type).getBody(); } catch (UnsupportedEncodingException unlikely) { unlikely.printStackTrace(); throw new ApiException("linkedin", "Unlikely unsupported encoding error", unlikely); } }