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

The following examples show how to use io.netty.handler.codec.http.HttpResponseStatus#METHOD_NOT_ALLOWED . 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: NettyHttpResponse.java    From netty-http-server with Apache License 2.0 5 votes vote down vote up
public static FullHttpResponse make(HttpResponseStatus status) {
    if (HttpResponseStatus.UNAUTHORIZED == status) {
        return NettyHttpResponse.make(HttpResponseStatus.UNAUTHORIZED, CONTENT_ERROR_401);
    }
    if (HttpResponseStatus.NOT_FOUND == status) {
        return NettyHttpResponse.make(HttpResponseStatus.NOT_FOUND, CONTENT_ERROR_404);
    }
    if (HttpResponseStatus.METHOD_NOT_ALLOWED == status) {
        return NettyHttpResponse.make(HttpResponseStatus.METHOD_NOT_ALLOWED, CONTENT_ERROR_405);
    }
    return NettyHttpResponse.make(HttpResponseStatus.OK,CONTENT_NORMAL_200);
}
 
Example 2
Source File: HttpThriftBufDecoder.java    From nettythrift with Apache License 2.0 5 votes vote down vote up
private boolean directHandleMethod(ChannelHandlerContext ctx, FullHttpRequest request, HttpMethod method) {
	if (method.equals(HttpMethod.GET) || method.equals(HttpMethod.POST)) {
		return false;
	}
	// 处理 OPTIONS 请求
	HttpResponseStatus status = HttpResponseStatus.OK;
	boolean invalid = false;
	if (!method.equals(HttpMethod.OPTIONS)) {
		invalid = true;
		status = HttpResponseStatus.METHOD_NOT_ALLOWED;
	}
	DefaultFullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, status, Unpooled.EMPTY_BUFFER);
	HttpHeaders headers = response.headers();
	// headers.set(HttpHeaderNames.ACCESS_CONTROL_ALLOW_HEADERS,
	// "X-Requested-With, accept, origin, content-type");
	headers.set(HttpHeaderNames.ACCESS_CONTROL_ALLOW_HEADERS, "X-Requested-With, content-type");
	headers.set(HttpHeaderNames.ACCESS_CONTROL_ALLOW_METHODS, "GET,POST,OPTIONS");
	headers.set(HttpHeaderNames.SERVER, "Netty5");
	if (invalid) {
		headers.set("Client-Warning", "Invalid Method");
	}
	boolean keepAlive = HttpHeaderUtil.isKeepAlive(request);
	if (keepAlive) {
		response.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE);
	}
	ctx.write(response);
	ChannelFuture future = ctx.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT);
	if (!keepAlive) {
		future.addListener(ChannelFutureListener.CLOSE);
	}
	return true;
}
 
Example 3
Source File: HttpDecoder.java    From ffwd with Apache License 2.0 5 votes vote down vote up
@Override
protected void decode(ChannelHandlerContext ctx, FullHttpRequest in, List<Object> out)
    throws Exception {
    switch (in.uri()) {
        case "/ping":
            if (in.method() == GET) {
                getPing(ctx, in, out);
                return;
            }

            throw new HttpException(HttpResponseStatus.METHOD_NOT_ALLOWED);
        case "/v1/batch":
            if (in.method() == POST) {
                if (matchContentType(in, "application/json")) {
                    postBatch(ctx, in, out);
                    return;
                }
                log.error("Unsupported Media Type");
                throw new HttpException(HttpResponseStatus.UNSUPPORTED_MEDIA_TYPE);
            }
            log.error("HTTP Method Not Allowed");
            throw new HttpException(HttpResponseStatus.METHOD_NOT_ALLOWED);
        default:
            /* do nothing */
            break;
    }

    throw new HttpException(HttpResponseStatus.NOT_FOUND);
}
 
Example 4
Source File: NotImplementedException.java    From appium-uiautomator2-server with Apache License 2.0 4 votes vote down vote up
public HttpResponseStatus getHttpStatus() {
    return HttpResponseStatus.METHOD_NOT_ALLOWED;
}
 
Example 5
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 6
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);
  }
}