Java Code Examples for io.netty.handler.codec.http.HttpResponseStatus#SERVICE_UNAVAILABLE

The following examples show how to use io.netty.handler.codec.http.HttpResponseStatus#SERVICE_UNAVAILABLE . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: StatusPage.java    From arcusplatform with Apache License 2.0 6 votes vote down vote up
@Override
public FullHttpResponse respond(FullHttpRequest req, ChannelHandlerContext ctx) throws Exception {
   HttpResponseStatus status;
   String content;
   if(service != null) {
      ClusterServiceRecord record = service.getServiceRecord().orNull();
      if(record == null) {
         status = HttpResponseStatus.SERVICE_UNAVAILABLE;
         content = clusteredPendingId();
      }
      else {
         status = HttpResponseStatus.OK;
         content = clusteredHealthy(record);
      }
   }
   else {
      status = HttpResponseStatus.OK;
      content = healthy();
   }
   ByteBuf buffer = Unpooled.wrappedBuffer(content.getBytes(Charsets.UTF_8));
   FullHttpResponse res = new DefaultFullHttpResponse(HTTP_1_1, status, buffer);
   res.headers().set(CONTENT_TYPE, "text/html; charset=UTF-8");
   setContentLength(res, buffer.readableBytes());
   return res;
}
 
Example 2
Source File: HealthCheckHandlerTest.java    From ambry with Apache License 2.0 6 votes vote down vote up
/**
 * Does a test to see that a health check request results in expected response from the health check handler
 * @param httpMethod the {@link HttpMethod} for the request.
 * @param keepAlive true if keep alive has to be set in the request, false otherwise
 * @throws IOException
 */
private void testHealthCheckRequest(HttpMethod httpMethod, boolean isServiceUp, boolean keepAlive)
    throws IOException {
  EmbeddedChannel channel = createChannel();
  for (int i = 0; i < 2; i++) {
    if (isServiceUp) {
      restServerState.markServiceUp();
    }
    HttpRequest request = RestTestUtils.createRequest(httpMethod, healthCheckUri, null);
    HttpUtil.setKeepAlive(request, keepAlive);
    FullHttpResponse response = sendRequestAndGetResponse(channel, request);
    HttpResponseStatus httpResponseStatus =
        (isServiceUp) ? HttpResponseStatus.OK : HttpResponseStatus.SERVICE_UNAVAILABLE;
    assertEquals("Unexpected response status", httpResponseStatus, response.status());
    String expectedStr = (isServiceUp) ? goodStr : badStr;
    assertEquals("Unexpected content", expectedStr, RestTestUtils.getContentString(response));
    restServerState.markServiceDown();
    if (keepAlive && isServiceUp) {
      Assert.assertTrue("Channel should not be closed ", channel.isOpen());
    } else {
      Assert.assertFalse("Channel should have been closed by now ", channel.isOpen());
      channel = createChannel();
    }
  }
  channel.close();
}
 
Example 3
Source File: BrowserUpHttpFilterChain.java    From browserup-proxy with Apache License 2.0 5 votes vote down vote up
@Override
public HttpResponse clientToProxyRequest(HttpObject httpObject) {
    if (proxyServer.isStopped()) {
        log.warn("Aborting request to {} because proxy is stopped", originalRequest.getUri());
        HttpResponse abortedResponse = new DefaultFullHttpResponse(originalRequest.getProtocolVersion(), HttpResponseStatus.SERVICE_UNAVAILABLE);
        HttpHeaders.setContentLength(abortedResponse, 0L);
        return abortedResponse;
    }

    for (HttpFilters filter : filters) {
        try {
            HttpResponse filterResponse = filter.clientToProxyRequest(httpObject);
            if (filterResponse != null) {
                // if we are short-circuiting the response to an HttpRequest, update ModifiedRequestAwareFilter instances
                // with this (possibly) modified HttpRequest before returning the short-circuit response
                if (httpObject instanceof HttpRequest) {
                    updateFiltersWithModifiedResponse((HttpRequest) httpObject);
                }

                return filterResponse;
            }
        } catch (RuntimeException e) {
            log.warn("Filter in filter chain threw exception. Filter method may have been aborted.", e);
        }
    }

    // if this httpObject is the HTTP request, set the modified request object on all ModifiedRequestAwareFilter
    // instances, so they have access to all modifications the request filters made while filtering
    if (httpObject instanceof HttpRequest) {
        updateFiltersWithModifiedResponse((HttpRequest) httpObject);
    }

    return null;
}
 
Example 4
Source File: BrowserMobHttpFilterChain.java    From CapturePacket with MIT License 5 votes vote down vote up
@Override
public HttpResponse clientToProxyRequest(HttpObject httpObject) {
    if (proxyServer.isStopped()) {
        log.warn("Aborting request to {} because proxy is stopped", originalRequest.getUri());
        HttpResponse abortedResponse = new DefaultFullHttpResponse(originalRequest.getProtocolVersion(), HttpResponseStatus.SERVICE_UNAVAILABLE);
        HttpHeaders.setContentLength(abortedResponse, 0L);
        return abortedResponse;
    }

    for (HttpFilters filter : filters) {
        try {
            HttpResponse filterResponse = filter.clientToProxyRequest(httpObject);
            if (filterResponse != null) {
                // if we are short-circuiting the response to an HttpRequest, update ModifiedRequestAwareFilter instances
                // with this (possibly) modified HttpRequest before returning the short-circuit response
                if (httpObject instanceof HttpRequest) {
                    updateFiltersWithModifiedResponse((HttpRequest) httpObject);
                }

                return filterResponse;
            }
        } catch (RuntimeException e) {
            log.warn("Filter in filter chain threw exception. Filter method may have been aborted.", e);
        }
    }

    // if this httpObject is the HTTP request, set the modified request object on all ModifiedRequestAwareFilter
    // instances, so they have access to all modifications the request filters made while filtering
    if (httpObject instanceof HttpRequest) {
        updateFiltersWithModifiedResponse((HttpRequest) httpObject);
    }

    return null;
}
 
Example 5
Source File: BrowserMobHttpFilterChain.java    From Dream-Catcher with MIT License 5 votes vote down vote up
@Override
public HttpResponse clientToProxyRequest(HttpObject httpObject) {
    if (proxyServer.isStopped()) {
        log.warn("Aborting request to {} because proxy is stopped", originalRequest.getUri());
        HttpResponse abortedResponse = new DefaultFullHttpResponse(originalRequest.getProtocolVersion(), HttpResponseStatus.SERVICE_UNAVAILABLE);
        HttpHeaders.setContentLength(abortedResponse, 0L);
        return abortedResponse;
    }

    for (HttpFilters filter : filters) {
        try {
            HttpResponse filterResponse = filter.clientToProxyRequest(httpObject);
            if (filterResponse != null) {
                // if we are short-circuiting the response to an HttpRequest, update ModifiedRequestAwareFilter instances
                // with this (possibly) modified HttpRequest before returning the short-circuit response
                if (httpObject instanceof HttpRequest) {
                    updateFiltersWithModifiedResponse((HttpRequest) httpObject);
                }

                return filterResponse;
            }
        } catch (RuntimeException e) {
            log.warn("Filter in filter chain threw exception. Filter method may have been aborted.", e);
        }
    }

    // if this httpObject is the HTTP request, set the modified request object on all ModifiedRequestAwareFilter
    // instances, so they have access to all modifications the request filters made while filtering
    if (httpObject instanceof HttpRequest) {
        updateFiltersWithModifiedResponse((HttpRequest) httpObject);
    }

    return null;
}
 
Example 6
Source File: BrowserMobHttpFilterChain.java    From AndroidHttpCapture with MIT License 5 votes vote down vote up
@Override
public HttpResponse clientToProxyRequest(HttpObject httpObject) {
    if (proxyServer.isStopped()) {
        log.warn("Aborting request to {} because proxy is stopped", originalRequest.getUri());
        HttpResponse abortedResponse = new DefaultFullHttpResponse(originalRequest.getProtocolVersion(), HttpResponseStatus.SERVICE_UNAVAILABLE);
        HttpHeaders.setContentLength(abortedResponse, 0L);
        return abortedResponse;
    }

    for (HttpFilters filter : filters) {
        try {
            HttpResponse filterResponse = filter.clientToProxyRequest(httpObject);
            if (filterResponse != null) {
                // if we are short-circuiting the response to an HttpRequest, update ModifiedRequestAwareFilter instances
                // with this (possibly) modified HttpRequest before returning the short-circuit response
                if (httpObject instanceof HttpRequest) {
                    updateFiltersWithModifiedResponse((HttpRequest) httpObject);
                }

                return filterResponse;
            }
        } catch (RuntimeException e) {
            log.warn("Filter in filter chain threw exception. Filter method may have been aborted.", e);
        }
    }

    // if this httpObject is the HTTP request, set the modified request object on all ModifiedRequestAwareFilter
    // instances, so they have access to all modifications the request filters made while filtering
    if (httpObject instanceof HttpRequest) {
        updateFiltersWithModifiedResponse((HttpRequest) httpObject);
    }

    return null;
}
 
Example 7
Source File: NettyResponseChannel.java    From ambry with Apache License 2.0 4 votes vote down vote up
/**
 * Converts a {@link ResponseStatus} into a {@link HttpResponseStatus}.
 * @param responseStatus {@link ResponseStatus} that needs to be mapped to a {@link HttpResponseStatus}.
 * @return the {@link HttpResponseStatus} that maps to the {@link ResponseStatus}.
 */
private HttpResponseStatus getHttpResponseStatus(ResponseStatus responseStatus) {
  HttpResponseStatus status;
  switch (responseStatus) {
    case Ok:
      nettyMetrics.okCount.inc();
      status = HttpResponseStatus.OK;
      break;
    case Created:
      nettyMetrics.createdCount.inc();
      status = HttpResponseStatus.CREATED;
      break;
    case Accepted:
      nettyMetrics.acceptedCount.inc();
      status = HttpResponseStatus.ACCEPTED;
      break;
    case PartialContent:
      nettyMetrics.partialContentCount.inc();
      status = HttpResponseStatus.PARTIAL_CONTENT;
      break;
    case NotModified:
      nettyMetrics.notModifiedCount.inc();
      status = HttpResponseStatus.NOT_MODIFIED;
      break;
    case BadRequest:
      nettyMetrics.badRequestCount.inc();
      status = HttpResponseStatus.BAD_REQUEST;
      break;
    case Unauthorized:
      nettyMetrics.unauthorizedCount.inc();
      status = HttpResponseStatus.UNAUTHORIZED;
      break;
    case NotFound:
      nettyMetrics.notFoundCount.inc();
      status = HttpResponseStatus.NOT_FOUND;
      break;
    case Conflict:
      nettyMetrics.conflictCount.inc();
      status = HttpResponseStatus.CONFLICT;
      break;
    case Gone:
      nettyMetrics.goneCount.inc();
      status = HttpResponseStatus.GONE;
      break;
    case Forbidden:
      nettyMetrics.forbiddenCount.inc();
      status = HttpResponseStatus.FORBIDDEN;
      break;
    case ProxyAuthenticationRequired:
      nettyMetrics.proxyAuthRequiredCount.inc();
      status = HttpResponseStatus.PROXY_AUTHENTICATION_REQUIRED;
      break;
    case RangeNotSatisfiable:
      nettyMetrics.rangeNotSatisfiableCount.inc();
      status = HttpResponseStatus.REQUESTED_RANGE_NOT_SATISFIABLE;
      break;
    case TooManyRequests:
      nettyMetrics.tooManyRequests.inc();
      status = HttpResponseStatus.TOO_MANY_REQUESTS;
      break;
    case RequestTooLarge:
      nettyMetrics.requestTooLargeCount.inc();
      status = HttpResponseStatus.REQUEST_ENTITY_TOO_LARGE;
      break;
    case InternalServerError:
      nettyMetrics.internalServerErrorCount.inc();
      status = HttpResponseStatus.INTERNAL_SERVER_ERROR;
      break;
    case ServiceUnavailable:
      nettyMetrics.serviceUnavailableErrorCount.inc();
      status = HttpResponseStatus.SERVICE_UNAVAILABLE;
      break;
    case InsufficientCapacity:
      nettyMetrics.insufficientCapacityErrorCount.inc();
      status = HttpResponseStatus.INSUFFICIENT_STORAGE;
      break;
    case PreconditionFailed:
      nettyMetrics.preconditionFailedErrorCount.inc();
      status = HttpResponseStatus.PRECONDITION_FAILED;
      break;
    case MethodNotAllowed:
      nettyMetrics.methodNotAllowedErrorCount.inc();
      status = HttpResponseStatus.METHOD_NOT_ALLOWED;
      break;
    default:
      nettyMetrics.unknownResponseStatusCount.inc();
      status = HttpResponseStatus.INTERNAL_SERVER_ERROR;
      break;
  }
  return status;
}
 
Example 8
Source File: NettyResponseChannelTest.java    From ambry with Apache License 2.0 4 votes vote down vote up
/**
 * @param code the {@link RestServiceErrorCode} whose {@link HttpResponseStatus} equivalent is required.
 * @return the {@link HttpResponseStatus} equivalent of {@code code}.
 */
private HttpResponseStatus getExpectedHttpResponseStatus(RestServiceErrorCode code) {
  switch (code) {
    case RequestTooLarge:
      return HttpResponseStatus.REQUEST_ENTITY_TOO_LARGE;
    case Conflict:
      return HttpResponseStatus.CONFLICT;
    case Deleted:
      return HttpResponseStatus.GONE;
    case NotFound:
      return HttpResponseStatus.NOT_FOUND;
    case BadRequest:
    case InvalidArgs:
    case InvalidAccount:
    case InvalidContainer:
    case InvalidRequestState:
    case MalformedRequest:
    case MissingArgs:
    case UnsupportedHttpMethod:
      return HttpResponseStatus.BAD_REQUEST;
    case ResourceDirty:
    case AccessDenied:
      return HttpResponseStatus.FORBIDDEN;
    case Unauthorized:
      return HttpResponseStatus.UNAUTHORIZED;
    case ResourceScanInProgress:
      return HttpResponseStatus.PROXY_AUTHENTICATION_REQUIRED;
    case RangeNotSatisfiable:
      return HttpResponseStatus.REQUESTED_RANGE_NOT_SATISFIABLE;
    case ServiceUnavailable:
      return HttpResponseStatus.SERVICE_UNAVAILABLE;
    case TooManyRequests:
      return HttpResponseStatus.TOO_MANY_REQUESTS;
    case InsufficientCapacity:
      return HttpResponseStatus.INSUFFICIENT_STORAGE;
    case IdConverterServiceError:
    case InternalServerError:
    case RequestChannelClosed:
    case RequestResponseQueuingFailure:
    case UnsupportedRestMethod:
      return HttpResponseStatus.INTERNAL_SERVER_ERROR;
    case PreconditionFailed:
      return HttpResponseStatus.PRECONDITION_FAILED;
    case NotAllowed:
      return HttpResponseStatus.METHOD_NOT_ALLOWED;
    default:
      throw new IllegalArgumentException("Unrecognized RestServiceErrorCode - " + code);
  }
}