From 6bc1e9248478e509decee09a4de23bbcc89e93b7 Mon Sep 17 00:00:00 2001 From: Spencer Gibb Date: Mon, 16 Jan 2017 20:56:03 -0700 Subject: [PATCH] Adds AddRequestParameter --- .../support/ServerWebExchangeUtils.java | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/main/java/org/springframework/cloud/gateway/support/ServerWebExchangeUtils.java b/src/main/java/org/springframework/cloud/gateway/support/ServerWebExchangeUtils.java index baa2ccd8..fc862e37 100644 --- a/src/main/java/org/springframework/cloud/gateway/support/ServerWebExchangeUtils.java +++ b/src/main/java/org/springframework/cloud/gateway/support/ServerWebExchangeUtils.java @@ -1,11 +1,15 @@ package org.springframework.cloud.gateway.support; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.springframework.http.HttpStatus; import org.springframework.web.server.ServerWebExchange; /** * @author Spencer Gibb */ public class ServerWebExchangeUtils { + private static final Log logger = LogFactory.getLog(ServerWebExchangeUtils.class); public static final String CLIENT_RESPONSE_ATTR = "webHandlerClientResponse"; public static final String GATEWAY_ROUTE_ATTR = "gatewayRoute"; @@ -28,4 +32,25 @@ public class ServerWebExchangeUtils { Boolean responseCommitted = getAttribute(exchange, RESPONSE_COMMITTED_ATTR, Boolean.class); return responseCommitted != null && responseCommitted; } + + public static boolean setResponseStatus(ServerWebExchange exchange, HttpStatus httpStatus) { + boolean response = exchange.getResponse().setStatusCode(httpStatus); + if (!response && logger.isWarnEnabled()) { + logger.warn("Unable to set status code to "+ httpStatus + ". Response already committed."); + } + return response; + } + + public static HttpStatus parse(String statusString) { + HttpStatus httpStatus; + + try { + int status = Integer.parseInt(statusString); + httpStatus = HttpStatus.valueOf(status); + } catch (NumberFormatException e) { + // try the enum string + httpStatus = HttpStatus.valueOf(statusString.toUpperCase()); + } + return httpStatus; + } }