本文整理汇总了Java中io.undertow.util.StatusCodes类的典型用法代码示例。如果您正苦于以下问题:Java StatusCodes类的具体用法?Java StatusCodes怎么用?Java StatusCodes使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
StatusCodes类属于io.undertow.util包,在下文中一共展示了StatusCodes类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: handleRequest
import io.undertow.util.StatusCodes; //导入依赖的package包/类
/**
* Only allow the request through if successfully authenticated or if authentication is not required.
*
* @see io.undertow.server.HttpHandler#handleRequest(io.undertow.server.HttpServerExchange)
*/
@Override
public void handleRequest(final HttpServerExchange exchange) throws Exception {
if(exchange.isInIoThread()) {
exchange.dispatch(this);
return;
}
SecurityContext context = exchange.getSecurityContext();
if (context.authenticate()) {
if(!exchange.isComplete()) {
next.handleRequest(exchange);
}
} else {
if(exchange.getResponseCode() >= StatusCodes.BAD_REQUEST && !exchange.isComplete()) {
ServletRequestContext src = exchange.getAttachment(ServletRequestContext.ATTACHMENT_KEY);
src.getOriginalResponse().sendError(exchange.getResponseCode());
} else {
exchange.endExchange();
}
}
}
示例2: handleRequest
import io.undertow.util.StatusCodes; //导入依赖的package包/类
@Override
public void handleRequest(final HttpServerExchange exchange) throws Exception {
final ServletRequestContext servletRequestContext = exchange.getAttachment(ServletRequestContext.ATTACHMENT_KEY);
ServletRequest request = servletRequestContext.getServletRequest();
if (request.getDispatcherType() == DispatcherType.REQUEST) {
List<SingleConstraintMatch> constraints = servletRequestContext.getRequiredConstrains();
SecurityContext sc = exchange.getSecurityContext();
if (!authorizationManager.canAccessResource(constraints, sc.getAuthenticatedAccount(), servletRequestContext.getCurrentServlet().getManagedServlet().getServletInfo(), servletRequestContext.getOriginalRequest(), servletRequestContext.getDeployment())) {
HttpServletResponse response = (HttpServletResponse) servletRequestContext.getServletResponse();
response.sendError(StatusCodes.FORBIDDEN);
return;
}
}
next.handleRequest(exchange);
}
示例3: BasicAuthHandler
import io.undertow.util.StatusCodes; //导入依赖的package包/类
public BasicAuthHandler(HttpHandler next, String expectedUsername, String expectedPassword) {
this.expectedCredential = "Basic " + Base64.getEncoder()
.encodeToString((expectedUsername + ":" + expectedPassword).getBytes());
this.authChecker = Handlers.predicate(exchange -> {
String userCredential = getRequestHeader(exchange, new HttpString("Authorization"));
if (userCredential == null) {
return false;
} else if (expectedCredential.equals(userCredential)) {
return true;
}
return false;
}, next, exchange -> {
exchange.getResponseHeaders()
.put(new HttpString("WWW-Authenticate"), "Basic realm=\"realm\"");
exchange.setStatusCode(StatusCodes.UNAUTHORIZED)
.endExchange();
});
}
示例4: handleRequest
import io.undertow.util.StatusCodes; //导入依赖的package包/类
@Override
public void handleRequest(HttpServerExchange exchange) throws Exception {
if (isConfidential(exchange) || !confidentialityRequired(exchange)) {
next.handleRequest(exchange);
} else {
try {
URI redirectUri = getRedirectURI(exchange);
exchange.setResponseCode(StatusCodes.FOUND);
exchange.getResponseHeaders().put(Headers.LOCATION, redirectUri.toString());
} catch (Exception e) {
UndertowLogger.REQUEST_LOGGER.exceptionProcessingRequest(e);
exchange.setResponseCode(StatusCodes.INTERNAL_SERVER_ERROR);
}
exchange.endExchange();
}
}
示例5: sendRedirect
import io.undertow.util.StatusCodes; //导入依赖的package包/类
@Override
public void sendRedirect(final String location) throws IOException {
if (responseStarted()) {
throw UndertowServletMessages.MESSAGES.responseAlreadyCommited();
}
resetBuffer();
setStatus(StatusCodes.FOUND);
String realPath;
if (location.contains("://")) {//absolute url
exchange.getResponseHeaders().put(Headers.LOCATION, location);
} else {
if (location.startsWith("/")) {
realPath = location;
} else {
String current = exchange.getRelativePath();
int lastSlash = current.lastIndexOf("/");
if (lastSlash != -1) {
current = current.substring(0, lastSlash + 1);
}
realPath = CanonicalPathUtils.canonicalize(servletContext.getContextPath() + current + location);
}
String loc = exchange.getRequestScheme() + "://" + exchange.getHostAndPort() + realPath;
exchange.getResponseHeaders().put(Headers.LOCATION, loc);
}
responseDone();
}
示例6: getErrorLocation
import io.undertow.util.StatusCodes; //导入依赖的package包/类
public String getErrorLocation(final Throwable exception) {
if (exception == null) {
return null;
}
//todo: this is kinda slow, but there is probably not a great deal that can be done about it
String location = null;
for (Class c = exception.getClass(); c != null && location == null; c = c.getSuperclass()) {
location = exceptionMappings.get(c);
}
if (location == null && exception instanceof ServletException) {
Throwable rootCause = ((ServletException) exception).getRootCause();
if (rootCause != null) {
for (Class c = rootCause.getClass(); c != null && location == null; c = c.getSuperclass()) {
location = exceptionMappings.get(c);
}
}
}
if (location == null) {
location = getErrorLocation(StatusCodes.INTERNAL_SERVER_ERROR);
}
return location;
}
示例7: sendContinueResponseBlocking
import io.undertow.util.StatusCodes; //导入依赖的package包/类
/**
* Sends a continue response using blocking IO
*
* @param exchange The exchange
*/
public static void sendContinueResponseBlocking(final HttpServerExchange exchange) throws IOException {
if (!exchange.isResponseChannelAvailable()) {
throw UndertowMessages.MESSAGES.cannotSendContinueResponse();
}
if(exchange.getAttachment(ALREADY_SENT) != null) {
return;
}
HttpServerExchange newExchange = exchange.getConnection().sendOutOfBandResponse(exchange);
exchange.putAttachment(ALREADY_SENT, true);
newExchange.setResponseCode(StatusCodes.CONTINUE);
newExchange.getResponseHeaders().put(Headers.CONTENT_LENGTH, 0);
newExchange.startBlocking();
newExchange.getOutputStream().close();
newExchange.getInputStream().close();
}
示例8: transferTo
import io.undertow.util.StatusCodes; //导入依赖的package包/类
@Override
public long transferTo(final long position, final long count, final FileChannel target) throws IOException {
if (exchange.getResponseCode() == StatusCodes.EXPECTATION_FAILED) {
//rejected
return -1;
}
if (!sent) {
sent = true;
response = HttpContinue.createResponseSender(exchange);
}
if (response != null) {
if (!response.send()) {
return 0;
}
response = null;
}
return super.transferTo(position, count, target);
}
示例9: read
import io.undertow.util.StatusCodes; //导入依赖的package包/类
@Override
public int read(final ByteBuffer dst) throws IOException {
if (exchange.getResponseCode() == StatusCodes.EXPECTATION_FAILED) {
//rejected
return -1;
}
if (!sent) {
sent = true;
response = HttpContinue.createResponseSender(exchange);
}
if (response != null) {
if (!response.send()) {
return 0;
}
response = null;
}
return super.read(dst);
}
示例10: awaitReadable
import io.undertow.util.StatusCodes; //导入依赖的package包/类
@Override
public void awaitReadable() throws IOException {
if (exchange.getResponseCode() == StatusCodes.EXPECTATION_FAILED) {
//rejected
return;
}
if (!sent) {
sent = true;
response = HttpContinue.createResponseSender(exchange);
}
if (response != null) {
while (!response.send()) {
response.awaitWritable();
}
response = null;
}
super.awaitReadable();
}
示例11: build
import io.undertow.util.StatusCodes; //导入依赖的package包/类
@Override
public HandlerWrapper build(Map<String, Object> config) {
String[] acl = (String[]) config.get("acl");
Boolean defaultAllow = (Boolean) config.get("default-allow");
Integer failureStatus = (Integer) config.get("failure-status");
List<Holder> peerMatches = new ArrayList<>();
for(String rule :acl) {
String[] parts = rule.split(" ");
if(parts.length != 2) {
throw UndertowMessages.MESSAGES.invalidAclRule(rule);
}
if(parts[1].trim().equals("allow")) {
peerMatches.add(new Holder(parts[0].trim(), false));
} else if(parts[1].trim().equals("deny")) {
peerMatches.add(new Holder(parts[0].trim(), true));
} else {
throw UndertowMessages.MESSAGES.invalidAclRule(rule);
}
}
return new Wrapper(peerMatches, defaultAllow == null ? false : defaultAllow, failureStatus == null ? StatusCodes.FORBIDDEN : failureStatus);
}
示例12: cancel
import io.undertow.util.StatusCodes; //导入依赖的package包/类
void cancel(final HttpServerExchange exchange) {
final ProxyConnection connectionAttachment = exchange.getAttachment(CONNECTION);
if (connectionAttachment != null) {
ClientConnection clientConnection = connectionAttachment.getConnection();
UndertowLogger.REQUEST_LOGGER.timingOutRequest(clientConnection.getPeerAddress() + "" + exchange.getRequestURI());
IoUtils.safeClose(clientConnection);
} else {
UndertowLogger.REQUEST_LOGGER.timingOutRequest(exchange.getRequestURI());
}
if (exchange.isResponseStarted()) {
IoUtils.safeClose(exchange.getConnection());
} else {
exchange.setResponseCode(StatusCodes.SERVICE_UNAVAILABLE);
exchange.endExchange();
}
}
示例13: handleException
import io.undertow.util.StatusCodes; //导入依赖的package包/类
@Override
public void handleException(Channel channel, IOException exception) {
IoUtils.safeClose(channel);
if (exchange.isResponseStarted()) {
IoUtils.safeClose(clientConnection);
UndertowLogger.REQUEST_IO_LOGGER.debug("Exception reading from target server", exception);
if (!exchange.isResponseStarted()) {
exchange.setResponseCode(StatusCodes.INTERNAL_SERVER_ERROR);
exchange.endExchange();
} else {
IoUtils.safeClose(exchange.getConnection());
}
} else {
exchange.setResponseCode(StatusCodes.INTERNAL_SERVER_ERROR);
exchange.endExchange();
}
}
示例14: wrap
import io.undertow.util.StatusCodes; //导入依赖的package包/类
@Override
public StreamSinkConduit wrap(final ConduitFactory<StreamSinkConduit> factory, final HttpServerExchange exchange) {
if (exchange.getResponseHeaders().contains(Headers.CONTENT_ENCODING)) {
//already encoded
return factory.create();
}
//if this is a zero length response we don't want to encode
if (exchange.getResponseContentLength() != 0
&& exchange.getResponseCode() != StatusCodes.NO_CONTENT
&& exchange.getResponseCode() != StatusCodes.NOT_MODIFIED) {
EncodingMapping encoding = getEncoding();
if (encoding != null) {
exchange.getResponseHeaders().put(Headers.CONTENT_ENCODING, encoding.getName());
if (exchange.getRequestMethod().equals(Methods.HEAD)) {
//we don't create an actual encoder for HEAD requests, but we set the header
return factory.create();
} else {
return encoding.getEncoding().getResponseWrapper().wrap(factory, exchange);
}
}
}
return factory.create();
}
示例15: signIn
import io.undertow.util.StatusCodes; //导入依赖的package包/类
public void signIn(HttpServerExchange exchange) {
String id = exchange.getRelativePath().substring(1);
boolean redirect = exchange.getQueryParameters().get("redirect") != null;
AuthService service = services.get(id);
if (service != null) {
String state = service.generateState();
if (redirect) {
exchange.setResponseCookie(new CookieImpl("pxls-auth-redirect", "1").setPath("/"));
redirect(exchange, service.getRedirectUrl(state+"|redirect"));
} else {
respond(exchange, StatusCodes.OK, new SignInResponse(service.getRedirectUrl(state+"|json")));
}
} else {
respond(exchange, StatusCodes.BAD_REQUEST, new Error("bad_service", "No auth method named " + id));
}
}