Example usage for org.springframework.http HttpStatus SERVICE_UNAVAILABLE

List of usage examples for org.springframework.http HttpStatus SERVICE_UNAVAILABLE

Introduction

In this page you can find the example usage for org.springframework.http HttpStatus SERVICE_UNAVAILABLE.

Prototype

HttpStatus SERVICE_UNAVAILABLE

To view the source code for org.springframework.http HttpStatus SERVICE_UNAVAILABLE.

Click Source Link

Document

503 Service Unavailable .

Usage

From source file:org.emonocot.portal.controller.SearchController.java

@ExceptionHandler(SolrServerException.class)
@ResponseStatus(value = HttpStatus.SERVICE_UNAVAILABLE)
public ModelAndView handleObjectNotFoundException(SolrServerException sse) {
    ModelAndView modelAndView = new ModelAndView("serviceUnavailable");
    modelAndView.addObject("exception", sse);
    return modelAndView;
}

From source file:org.springframework.boot.actuate.endpoint.web.HeapDumpWebEndpoint.java

@ReadOperation
public WebEndpointResponse<Resource> heapDump(Boolean live) {
    try {//  w w w  .j a v a  2 s .  c o  m
        if (this.lock.tryLock(this.timeout, TimeUnit.MILLISECONDS)) {
            try {
                return new WebEndpointResponse<>(dumpHeap(live == null ? true : live));
            } finally {
                this.lock.unlock();
            }
        }
    } catch (InterruptedException ex) {
        Thread.currentThread().interrupt();
    } catch (IOException ex) {
        return new WebEndpointResponse<>(HttpStatus.INTERNAL_SERVER_ERROR.value());
    } catch (HeapDumperUnavailableException ex) {
        return new WebEndpointResponse<>(HttpStatus.SERVICE_UNAVAILABLE.value());
    }
    return new WebEndpointResponse<>(HttpStatus.TOO_MANY_REQUESTS.value());
}

From source file:org.springframework.integration.http.inbound.HttpRequestHandlingControllerTests.java

@Test
public void shutDown() throws Exception {
    DirectChannel requestChannel = new DirectChannel();
    final CountDownLatch latch1 = new CountDownLatch(1);
    final CountDownLatch latch2 = new CountDownLatch(1);
    AbstractReplyProducingMessageHandler handler = new AbstractReplyProducingMessageHandler() {
        @Override//  ww w  .ja  v a  2  s .com
        protected Object handleRequestMessage(Message<?> requestMessage) {
            try {
                latch2.countDown();
                // hold up an active thread so we can verify the count and that it completes ok
                latch1.await(10, TimeUnit.SECONDS);
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
            return requestMessage.getPayload().toString().toUpperCase();
        }
    };
    requestChannel.subscribe(handler);
    final HttpRequestHandlingController controller = new HttpRequestHandlingController(true);
    controller.setBeanFactory(mock(BeanFactory.class));
    controller.setRequestChannel(requestChannel);
    controller.setViewName("foo");
    controller.afterPropertiesSet();
    controller.start();

    final MockHttpServletRequest request = new MockHttpServletRequest();
    request.setMethod("POST");
    request.setContent("hello".getBytes());

    //request.setContentType("text/plain"); //Works in Spring 3.1.2.RELEASE but NOT in 3.0.7.RELEASE
    //Instead do:
    request.addHeader("Content-Type", "text/plain");

    MockHttpServletResponse response = new MockHttpServletResponse();
    final AtomicInteger active = new AtomicInteger();
    final AtomicBoolean expected503 = new AtomicBoolean();
    Executors.newSingleThreadExecutor().execute(() -> {
        try {
            // wait for the active thread
            latch2.await(10, TimeUnit.SECONDS);
        } catch (InterruptedException e1) {
            Thread.currentThread().interrupt();
        }
        // start the shutdown
        active.set(controller.beforeShutdown());
        try {
            MockHttpServletResponse response1 = new MockHttpServletResponse();
            controller.handleRequest(request, response1);
            expected503.set(response1.getStatus() == HttpStatus.SERVICE_UNAVAILABLE.value());
            latch1.countDown();
        } catch (Exception e) {
            LogFactory.getLog(getClass()).error("Async handleRequest failed", e);
        }
    });
    ModelAndView modelAndView = controller.handleRequest(request, response);
    // verify we get a 503 after shutdown starts
    assertEquals(1, active.get());
    assertTrue(expected503.get());
    // verify the active request still processed ok
    assertEquals("foo", modelAndView.getViewName());
    assertEquals(1, modelAndView.getModel().size());
    Object reply = modelAndView.getModel().get("reply");
    assertNotNull(reply);
    assertEquals("HELLO", reply);
}

From source file:org.springframework.social.cloudplaylists.api.impl.CloudPlaylistsErrorHandler.java

void handleCloudPlaylistsError(HttpStatus statusCode, Map errorDetails) {

    String message = (String) errorDetails.get("error_description");
    HttpStatus httpStatus = statusCode;//from  w w w  . j av a2  s.  c om

    if (httpStatus == HttpStatus.OK) {
        // Should never happen
    } else if (httpStatus == HttpStatus.BAD_REQUEST) {

        String error = (String) errorDetails.get("error");
        String error_description = (String) errorDetails.get("error_description");

        if (error != null && PlaylistUpdateException.class.getName().equals(error)) {
            throw new PlaylistUpdateException(error_description);
        }
        if (error != null && PlaylistCreationException.class.getName().equals(error)) {
            throw new PlaylistCreationException(error_description);
        }

        throw new ResourceNotFoundException("cloudplaylists", message);

    } else if (httpStatus == HttpStatus.NOT_FOUND) {
        throw new ResourceNotFoundException("cloudplaylists", message);

    } else if (httpStatus == HttpStatus.UNAUTHORIZED) {

        throw new NotAuthorizedException("cloudplaylists", message);
    } else if (httpStatus == HttpStatus.FORBIDDEN) {
        String provider = (String) errorDetails.get("provider");
        String error = (String) errorDetails.get("error");
        if (error != null && provider != null && NotConnectedException.class.getName().equals(error)) {
            throw new NotConnectedException(provider);
        }
        if (error != null && provider != null && ExpiredAuthorizationException.class.getName().equals(error)) {
            throw new ExpiredAuthorizationException(provider);
        }
        throw new OperationNotPermittedException("cloudplaylists", message);
    } else if (httpStatus == HttpStatus.INTERNAL_SERVER_ERROR) {
        throw new InternalServerErrorException("cloudplaylists", message);
    } else if (httpStatus == HttpStatus.SERVICE_UNAVAILABLE) {
        throw new ServerDownException("cloudplaylists", message);
    }
}

From source file:org.springframework.social.exfm.api.impl.ExFmErrorHandler.java

void handleExFmError(HttpStatus statusCode, Status status) {

    String message = status.getStatus_text();

    HttpStatus httpStatus = statusCode != HttpStatus.OK ? statusCode
            : HttpStatus.valueOf(Integer.parseInt(status.getStatus_code()));

    if (httpStatus == HttpStatus.OK) {
        // Should never happen
    } else if (httpStatus == HttpStatus.BAD_REQUEST) {
        throw new ResourceNotFoundException("exfm", message);

    } else if (httpStatus == HttpStatus.NOT_FOUND) {
        throw new ResourceNotFoundException("exfm", message);

    } else if (httpStatus == HttpStatus.UNAUTHORIZED) {

        throw new NotAuthorizedException("exfm", message);
    } else if (httpStatus == HttpStatus.FORBIDDEN) {

        throw new OperationNotPermittedException("exfm", message);
    } else if (httpStatus == HttpStatus.INTERNAL_SERVER_ERROR) {
        throw new InternalServerErrorException("exfm", message);
    } else if (httpStatus == HttpStatus.SERVICE_UNAVAILABLE) {
        throw new ServerDownException("exfm", message);
    }//from w  ww  .j  a v  a  2s . co m
}

From source file:org.springframework.social.lastfm.api.impl.LastFmErrorHandler.java

void handleLastFmError(HttpStatus statusCode, Map<Integer, String> errorDetails) {

    String message = errorDetails.values().iterator().next();
    if (statusCode == HttpStatus.OK) {
        // TODO I've just put a single error code in here for now - need to
        // complete with other error codes
        if (errorDetails.containsKey(3)) {
            throw new ResourceNotFoundException("lastfm", message);
        }// w w  w . j  a v a2  s. c om
        if (errorDetails.containsKey(6)) {
            throw new ResourceNotFoundException("lastfm", message);
        }
        if (errorDetails.containsKey(10)) {
            throw new NotAuthorizedException("lastfm", message);
        }
        if (errorDetails.containsKey(8)) {
            throw new ResourceNotFoundException("lastfm", message);
        }
        if (errorDetails.containsKey(13)) {
            throw new NotAuthorizedException("lastfm", message);
        }

    } else if (statusCode == HttpStatus.BAD_REQUEST) {
        throw new ResourceNotFoundException("lastfm", message);

    } else if (statusCode == HttpStatus.UNAUTHORIZED) {

        throw new NotAuthorizedException("lastfm", message);
    } else if (statusCode == HttpStatus.FORBIDDEN) {

        throw new OperationNotPermittedException("lastfm", message);
    } else if (statusCode == HttpStatus.INTERNAL_SERVER_ERROR) {
        throw new InternalServerErrorException("lastfm", message);
    } else if (statusCode == HttpStatus.SERVICE_UNAVAILABLE) {
        throw new ServerDownException("lastfm", message);
    }
}

From source file:org.springframework.social.mixcloud.api.impl.MixcloudErrorHandler.java

/**
 * Examines the error data returned from Mixcloud and throws the most
 * applicable exception.//from www  .j  a va  2 s  .  c  o  m
 * 
 * @param errorDetails
 *            a Map containing an "error"
 */
void handleMixcloudError(HttpStatus statusCode, SocialException errorDetails) {
    if (statusCode == HttpStatus.OK) {

    } else if (statusCode == HttpStatus.BAD_REQUEST) {
        if (errorDetails instanceof UncategorizedApiException) {
            String message = errorDetails.getMessage();
            if (AUTHORIZATION_FAILURE_MESSAGES.contains(message)) {
                throw new NotAuthorizedException("mixcloud", message);
            } else {
                throw errorDetails;

            }
        } else {
            throw errorDetails;
        }

    } else if (statusCode == HttpStatus.UNAUTHORIZED) {
        throw new NotAuthorizedException("mixcloud", errorDetails.getMessage());
    } else if (statusCode == HttpStatus.FORBIDDEN) {

        throw new OperationNotPermittedException("mixcloud", errorDetails.getMessage());
    } else if (statusCode == HttpStatus.INTERNAL_SERVER_ERROR) {
        throw new InternalServerErrorException("mixcloud", errorDetails.getMessage());
    } else if (statusCode == HttpStatus.SERVICE_UNAVAILABLE) {
        throw new ServerDownException("mixcloud", errorDetails.getMessage());
    }
}

From source file:org.springframework.social.soundcloud.api.impl.SoundCloudErrorHandler.java

/**
 * Examines the error data returned from SoundCloud and throws the most applicable exception.
 * @param errorDetails a Map containing an "error_message"
 *//*from w  ww.j ava2  s.co m*/
void handleSoundCloudError(HttpStatus statusCode, List<Map<String, String>> errorDetailsList) {
    // Can't trust the type to be useful. It's often OAuthException, even for things not OAuth-related. 
    // Can rely only on the message (which itself isn't very consistent).
    List<String> messages = new ArrayList<String>();
    for (Map<String, String> errorDetails : errorDetailsList) {
        String message = errorDetails.get("error_message");
        messages.add(message);
    }
    String message = constructMessage(messages);

    if (statusCode == HttpStatus.OK) {

    } else if (statusCode == HttpStatus.BAD_REQUEST) {
        throw new ResourceNotFoundException("soundcloud", message);

    } else if (statusCode == HttpStatus.NOT_FOUND) {
        throw new ResourceNotFoundException("soundcloud", message);

    } else if (statusCode == HttpStatus.UNAUTHORIZED) {
        if (isMessageStartsWithText(messages, "invalid_token")) {
            handleInvalidAccessToken(message);
        }
        throw new NotAuthorizedException("soundcloud", message);
    } else if (statusCode == HttpStatus.FORBIDDEN) {

        throw new OperationNotPermittedException("soundcloud", message);
    } else if (statusCode == HttpStatus.INTERNAL_SERVER_ERROR) {
        throw new InternalServerErrorException("soundcloud", message);
    } else if (statusCode == HttpStatus.SERVICE_UNAVAILABLE) {
        throw new ServerDownException("soundcloud", message);
    }
}

From source file:org.springframework.social.twitter.api.impl.TwitterErrorHandler.java

private void handleServerErrors(HttpStatus statusCode) throws IOException {
    if (statusCode == HttpStatus.INTERNAL_SERVER_ERROR) {
        throw new InternalServerErrorException("twitter",
                "Something is broken at Twitter. Please see http://dev.twitter.com/pages/support to report the issue.");
    } else if (statusCode == HttpStatus.BAD_GATEWAY) {
        throw new ServerDownException("twitter", "Twitter is down or is being upgraded.");
    } else if (statusCode == HttpStatus.SERVICE_UNAVAILABLE) {
        throw new ServerOverloadedException("twitter", "Twitter is overloaded with requests. Try again later.");
    }/*from   w  w  w  .j a  v a 2s  . c om*/
}

From source file:org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler.java

/**
 * Provides handling for standard Spring MVC exceptions.
 * @param ex the target exception//from w w w  . j  a  v a  2  s  .c o  m
 * @param request the current request
 */
@ExceptionHandler({ HttpRequestMethodNotSupportedException.class, HttpMediaTypeNotSupportedException.class,
        HttpMediaTypeNotAcceptableException.class, MissingPathVariableException.class,
        MissingServletRequestParameterException.class, ServletRequestBindingException.class,
        ConversionNotSupportedException.class, TypeMismatchException.class,
        HttpMessageNotReadableException.class, HttpMessageNotWritableException.class,
        MethodArgumentNotValidException.class, MissingServletRequestPartException.class, BindException.class,
        NoHandlerFoundException.class, AsyncRequestTimeoutException.class })
@Nullable
public final ResponseEntity<Object> handleException(Exception ex, WebRequest request) {
    HttpHeaders headers = new HttpHeaders();
    if (ex instanceof HttpRequestMethodNotSupportedException) {
        HttpStatus status = HttpStatus.METHOD_NOT_ALLOWED;
        return handleHttpRequestMethodNotSupported((HttpRequestMethodNotSupportedException) ex, headers, status,
                request);
    } else if (ex instanceof HttpMediaTypeNotSupportedException) {
        HttpStatus status = HttpStatus.UNSUPPORTED_MEDIA_TYPE;
        return handleHttpMediaTypeNotSupported((HttpMediaTypeNotSupportedException) ex, headers, status,
                request);
    } else if (ex instanceof HttpMediaTypeNotAcceptableException) {
        HttpStatus status = HttpStatus.NOT_ACCEPTABLE;
        return handleHttpMediaTypeNotAcceptable((HttpMediaTypeNotAcceptableException) ex, headers, status,
                request);
    } else if (ex instanceof MissingPathVariableException) {
        HttpStatus status = HttpStatus.INTERNAL_SERVER_ERROR;
        return handleMissingPathVariable((MissingPathVariableException) ex, headers, status, request);
    } else if (ex instanceof MissingServletRequestParameterException) {
        HttpStatus status = HttpStatus.BAD_REQUEST;
        return handleMissingServletRequestParameter((MissingServletRequestParameterException) ex, headers,
                status, request);
    } else if (ex instanceof ServletRequestBindingException) {
        HttpStatus status = HttpStatus.BAD_REQUEST;
        return handleServletRequestBindingException((ServletRequestBindingException) ex, headers, status,
                request);
    } else if (ex instanceof ConversionNotSupportedException) {
        HttpStatus status = HttpStatus.INTERNAL_SERVER_ERROR;
        return handleConversionNotSupported((ConversionNotSupportedException) ex, headers, status, request);
    } else if (ex instanceof TypeMismatchException) {
        HttpStatus status = HttpStatus.BAD_REQUEST;
        return handleTypeMismatch((TypeMismatchException) ex, headers, status, request);
    } else if (ex instanceof HttpMessageNotReadableException) {
        HttpStatus status = HttpStatus.BAD_REQUEST;
        return handleHttpMessageNotReadable((HttpMessageNotReadableException) ex, headers, status, request);
    } else if (ex instanceof HttpMessageNotWritableException) {
        HttpStatus status = HttpStatus.INTERNAL_SERVER_ERROR;
        return handleHttpMessageNotWritable((HttpMessageNotWritableException) ex, headers, status, request);
    } else if (ex instanceof MethodArgumentNotValidException) {
        HttpStatus status = HttpStatus.BAD_REQUEST;
        return handleMethodArgumentNotValid((MethodArgumentNotValidException) ex, headers, status, request);
    } else if (ex instanceof MissingServletRequestPartException) {
        HttpStatus status = HttpStatus.BAD_REQUEST;
        return handleMissingServletRequestPart((MissingServletRequestPartException) ex, headers, status,
                request);
    } else if (ex instanceof BindException) {
        HttpStatus status = HttpStatus.BAD_REQUEST;
        return handleBindException((BindException) ex, headers, status, request);
    } else if (ex instanceof NoHandlerFoundException) {
        HttpStatus status = HttpStatus.NOT_FOUND;
        return handleNoHandlerFoundException((NoHandlerFoundException) ex, headers, status, request);
    } else if (ex instanceof AsyncRequestTimeoutException) {
        HttpStatus status = HttpStatus.SERVICE_UNAVAILABLE;
        return handleAsyncRequestTimeoutException((AsyncRequestTimeoutException) ex, headers, status, request);
    } else {
        if (logger.isWarnEnabled()) {
            logger.warn("Unknown exception type: " + ex.getClass().getName());
        }
        HttpStatus status = HttpStatus.INTERNAL_SERVER_ERROR;
        return handleExceptionInternal(ex, null, headers, status, request);
    }
}