List of usage examples for org.springframework.web.client HttpStatusCodeException getStatusCode
public HttpStatus getStatusCode()
From source file:eionet.webq.web.interceptor.CdrAuthorizationInterceptor.java
@Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { String authorization = request.getHeader(AUTHORIZATION_HEADER); // if (true) return PROCEED; if (StringUtils.isNotEmpty(authorization) || request.getParameter("auth") != null) { // if Basic auth is present in the request, then try to log in to CDR to test if it is valid token for given domain. // "auth" parameter is just meant for testing the CDR API in development environment - WebQ asks to authenticate. HttpHeaders headers = new HttpHeaders(); headers.add(AUTHORIZATION_HEADER, authorization); // return PROCEED; try {/*w w w.j a v a 2 s . c om*/ ResponseEntity<String> loginResponse = restOperations.postForEntity( extractCdrUrl(request) + "/" + cdrLoginMethod, new HttpEntity<Object>(headers), String.class); LOGGER.info("Response code received from CDR basic authorization request " + loginResponse.getStatusCode()); return PROCEED; } catch (HttpStatusCodeException e) { if (e.getStatusCode() != HttpStatus.UNAUTHORIZED) { LOGGER.warn("Authorization against CDR failed with unexpected HTTP status code", e); } } } else { // if Basic auth is not present, then test if user is already authorised in this domain // by using provided cookies to fetch CDR envelope properties page. Cookie[] cookies = request.getCookies(); if (cookies != null) { HttpHeaders headers = new HttpHeaders(); for (Cookie cookie : cookies) { // put ZopeId parameter to request header. It works only when the value is surrounded with quotes. headers.add("Cookie", cookiesConverter.convertCookieToString(cookie)); } String urlToFetch = extractCdrEnvelopeUrl(request) + "/" + cdrEnvelopePropertiesMethod; //ResponseEntity<String> loginResponse = restOperations.exchange(urlToFetch, HttpMethod.GET, // new HttpEntity<Object>(headers), String.class); HttpResponse responseFromCdr = fetchUrlWithoutRedirection(urlToFetch, headers); try { int statusCode = responseFromCdr.getStatusLine().getStatusCode(); LOGGER.info("Response code received from CDR envelope request using cookies " + statusCode); if (statusCode == HttpStatus.OK.value()) { request.setAttribute(PARSED_COOKIES_ATTRIBUTE, cookiesConverter.convertCookiesToString(cookies)); return PROCEED; } else if ((statusCode == HttpStatus.MOVED_PERMANENTLY.value() || statusCode == HttpStatus.MOVED_TEMPORARILY.value()) && responseFromCdr.getFirstHeader("Location") != null) { // redirect to CDR login page String redirectUrl = extractCdrUrl(request) + responseFromCdr.getFirstHeader("Location").getValue(); LOGGER.info("Redirect to " + redirectUrl); response.sendRedirect(redirectUrl); } } catch (HttpStatusCodeException e) { if (e.getStatusCode() != HttpStatus.UNAUTHORIZED) { LOGGER.warn("Fetching CDR envelope page failed with unexpected HTTP status code", e); } } } } if (isFailureCountsEqualsToAllowedFailuresCount()) { request.setAttribute(AUTHORIZATION_FAILED_ATTRIBUTE, AUTHORIZATION_FAILED_ATTRIBUTE); session.removeAttribute(AUTHORIZATION_TRY_COUNT); return PROCEED; } increaseFailedAuthorizationsCount(); response.addHeader("WWW-Authenticate", "Basic realm=\"Please login to use webforms.\""); response.sendError(HttpServletResponse.SC_UNAUTHORIZED); return STOP_REQUEST_PROPAGATION; }
From source file:com.redblackit.web.test.RestTemplateTestHelper.java
/** * Test GET for object which should fail * /* w w w. ja v a 2 s. co m*/ * @param url * @param urlArgs * @param assertMsg * @param expectedStatusCode */ public void doGetForHttpStatusCodeException(String url, Object[] urlArgs, String assertMsg, HttpStatus expectedStatusCode) { StringBuilder builder = buildDebugMsg(url, urlArgs, assertMsg); builder.append(":getting object:expecting HttpStatusCodeException"); try { Object obj = restTemplate.getForObject(url, Object.class, (urlArgs == null ? new Object[0] : urlArgs)); Assert.fail(builder.append(":no exception:object returned=").append(obj).toString()); } catch (HttpStatusCodeException hsce) { Assert.assertEquals(builder.append(":statusCode:hsce=").append(hsce.getMessage()).toString(), expectedStatusCode, hsce.getStatusCode()); logger.debug(builder.append(":OK"), hsce); } }
From source file:com.redblackit.web.test.RestTemplateTestHelper.java
/** * Test PUT which should fail//from w ww . ja v a 2 s.c o m * * @param url * @param objToPut * @param urlArgs * @param assertMsg * @param expectedStatusCode */ public void doPutForHttpStatusCodeException(String url, Object objToPut, Object[] urlArgs, String assertMsg, HttpStatus expectedStatusCode) { StringBuilder builder = buildDebugMsg(url, urlArgs, assertMsg); builder.append(":putting object=").append(objToPut).append(":expecting HttpStatusCodeException"); try { restTemplate.put(url, objToPut, (urlArgs == null ? new Object[0] : urlArgs)); Assert.fail(builder.append(":no exception").toString()); } catch (HttpStatusCodeException hsce) { Assert.assertEquals(builder.append(":statusCode:hsce=").append(hsce.getMessage()).toString(), expectedStatusCode, hsce.getStatusCode()); logger.debug(builder.append(":OK"), hsce); } }
From source file:com.redblackit.web.test.RestTemplateTestHelper.java
/** * Test POST for location which should fail * //from w w w.j a va 2 s. c o m * @param url * @param objToPost * @param urlArgs * @param assertMsg * @param expectedStatusCode */ public void doPostForHttpStatusCodeException(String url, Object objToPost, Object[] urlArgs, String assertMsg, HttpStatus expectedStatusCode) { StringBuilder builder = buildDebugMsg(url, urlArgs, assertMsg); builder.append(":posting object=").append(objToPost).append(":expecting HttpStatusCodeException"); try { URI location = restTemplate.postForLocation(url, objToPost, (urlArgs == null ? new Object[0] : urlArgs)); Assert.fail(builder.append(":no exception:location returned=").append(location).toString()); } catch (HttpStatusCodeException hsce) { Assert.assertEquals(builder.append(":statusCode:hsce=").append(hsce.getMessage()).toString(), expectedStatusCode, hsce.getStatusCode()); logger.debug(builder.append(":OK"), hsce); } }
From source file:com.redblackit.web.test.RestTemplateTestHelper.java
/** * Test DELETE which should fail// w w w.jav a 2s.c om * * @param url * @param urlArgs * @param assertMsg * @param expectedStatusCode */ public void doDeleteForHttpStatusCodeException(String url, Object[] urlArgs, String assertMsg, HttpStatus expectedStatusCode) { StringBuilder builder = buildDebugMsg(url, urlArgs, assertMsg); builder.append(":deleting object:expecting HttpStatusCodeException"); try { restTemplate.delete(url, (urlArgs == null ? new Object[0] : urlArgs)); Assert.fail(builder.append(":no exception").toString()); } catch (HttpStatusCodeException hsce) { Assert.assertEquals(builder.append(":statusCode:hsce=").append(hsce.getMessage()).toString(), expectedStatusCode, hsce.getStatusCode()); logger.debug(builder.append(":OK"), hsce); } }
From source file:com.redblackit.web.test.RestTemplateTestHelper.java
/** * Test HEAD which should fail.// w ww .j av a 2s. c o m * Note that the old HttpClient wrongly causes an IOException instead of a HttpStatusCodeException. * * @param url * @param urlArgs * @param assertMsg * @param expectedStatusCode */ public void doHeadForHttpStatusCodeException(String url, Object[] urlArgs, String assertMsg, HttpStatus expectedStatusCode) { StringBuilder builder = buildDebugMsg(url, urlArgs, assertMsg); builder.append(":getting headers:expecting HttpStatusCodeException"); try { restTemplate.headForHeaders(url, (urlArgs == null ? new Object[0] : urlArgs)); Assert.fail(builder.append(":no exception").toString()); } catch (HttpStatusCodeException hsce) { Assert.assertEquals(builder.append(":statusCode:hsce=").append(hsce.getMessage()).toString(), expectedStatusCode, hsce.getStatusCode()); logger.debug(builder.append(":OK"), hsce); } }
From source file:com.redblackit.web.test.RestTemplateTestHelper.java
/** * Test OPTIONS which should fail//ww w .j av a2 s. c o m * * @param url * @param urlArgs * @param assertMsg * @param expectedStatusCode */ public void doOptionsForHttpStatusCodeException(String url, Object[] urlArgs, String assertMsg, HttpStatus expectedStatusCode) { StringBuilder builder = buildDebugMsg(url, urlArgs, assertMsg); builder.append(":getting options:expecting HttpStatusCodeException"); try { restTemplate.optionsForAllow(url, (urlArgs == null ? new Object[0] : urlArgs)); Assert.fail(builder.append(":no exception").toString()); } catch (HttpStatusCodeException hsce) { Assert.assertEquals(builder.append(":statusCode:hsce=").append(hsce.getMessage()).toString(), expectedStatusCode, hsce.getStatusCode()); logger.debug(builder.append(":OK"), hsce); } }
From source file:com.vmware.appfactory.datastore.DatastoreClientService.java
/** * Get a datastore./*ww w .j a va2s. co m*/ * If 'useCache' is true, then a local cache is checked first, and returned * from there if found. If not found, or 'useCache' is false, the datastore * service is queried. * * @param id * @param useCache * @return The named datastore, or null if no such datastore exists. * @throws DsException */ public DsDatastore findDatastore(Long id, boolean useCache) throws DsException { if (useCache && _dsCache.containsKey(id)) { return _dsCache.get(id); } try { Datastore dto = _rest.getForObject(baseUrl() + "/storage/{id}", Datastore.class, id); /* Convert DTO into a real datastore and cache it. */ DsDatastore ds = DsUtil.fromDTO(dto); if (ds != null) { _dsCache.put(id, ds); } return ds; } catch (HttpStatusCodeException ex) { if (ex.getStatusCode() == HttpStatus.NOT_FOUND) { return null; } throw new DsException(ex); } catch (RestClientException ex) { throw new DsException(ex); } }
From source file:org.trustedanalytics.servicecatalog.service.RestErrorHandler.java
@ExceptionHandler(HttpStatusCodeException.class) public void handleHttpStatusCodeException(HttpStatusCodeException e, HttpServletResponse response) throws IOException { String message = extractErrorFromJSON(e.getResponseBodyAsString()); message = StringUtils.isNotBlank(message) ? message : e.getMessage(); ErrorLogger.logAndSendErrorResponse(LOGGER, response, e.getStatusCode(), message, e); }
From source file:com.auditbucket.client.AbRestClient.java
public String getErrorMessage(HttpStatusCodeException e) { if (e.getStatusCode() == HttpStatus.INTERNAL_SERVER_ERROR) { logger.error(e.getResponseBodyAsString()); return e.getResponseBodyAsString(); }// www. jav a2s.com JsonNode n = null; try { n = mapper.readTree(e.getResponseBodyAsByteArray()); } catch (IOException e1) { logger.error(String.valueOf(e1)); } String message; if (n != null) message = String.valueOf(n.get("message")); else message = e.getMessage(); return message; }