当前位置: 首页>>代码示例>>Java>>正文


Java HttpServerExchange.setStatusCode方法代码示例

本文整理汇总了Java中io.undertow.server.HttpServerExchange.setStatusCode方法的典型用法代码示例。如果您正苦于以下问题:Java HttpServerExchange.setStatusCode方法的具体用法?Java HttpServerExchange.setStatusCode怎么用?Java HttpServerExchange.setStatusCode使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在io.undertow.server.HttpServerExchange的用法示例。


在下文中一共展示了HttpServerExchange.setStatusCode方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: ban

import io.undertow.server.HttpServerExchange; //导入方法依赖的package包/类
public void ban(HttpServerExchange exchange) {
    User user = parseUserFromForm(exchange);
    User user_perform = exchange.getAttachment(AuthReader.USER);
    if (user != null && user.getRole().lessThan(user_perform.getRole())) {
        String time = "86400";
        FormData data = exchange.getAttachment(FormDataParser.FORM_DATA);
        FormData.FormValue time_form = data.getFirst("time");
        if (time_form != null) {
            time = time_form.getValue();
        }
        if (doLog(exchange)) {
            App.getDatabase().adminLog("ban " + user.getName(), user_perform.getId());
        }
        user.ban(Integer.parseInt(time), getBanReason(exchange), getRollbackTime(exchange));
        exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, "application/text");
        exchange.setStatusCode(200);
    } else {
        exchange.setStatusCode(400);
    }
}
 
开发者ID:xSke,项目名称:Pxls,代码行数:21,代码来源:WebHandler.java

示例2: handleRequest

import io.undertow.server.HttpServerExchange; //导入方法依赖的package包/类
@Override
public void handleRequest(HttpServerExchange exchange) throws Exception {
    String ip = exchange.getAttachment(IPReader.IP);

    RequestBucket bucket = buckets.<String, RequestBucket>compute(ip, (key, old) -> {
        if (old == null) return new RequestBucket(System.currentTimeMillis(), 0);
        if (old.startTime + time * 1000 < System.currentTimeMillis())
            return new RequestBucket(System.currentTimeMillis(), 0);
        return old;
    });
    bucket.count++;
    if (bucket.count > count)
    {
        int timeSeconds = (int) ((bucket.startTime + time * 1000) - System.currentTimeMillis()) / 1000;
        exchange.setStatusCode(StatusCodes.TOO_MANY_REQUESTS);
        exchange.getResponseSender().send("You are doing that too much, try again in " + timeSeconds / 60 + " minutes");
    } else {
        next.handleRequest(exchange);
    }
}
 
开发者ID:xSke,项目名称:Pxls,代码行数:21,代码来源:RateLimitingHandler.java

示例3: handleRequest

import io.undertow.server.HttpServerExchange; //导入方法依赖的package包/类
@Override
public void handleRequest(HttpServerExchange exchange) throws Exception {
    // get the request parameters as a Map<String, Object>
    @SuppressWarnings("unchecked")
    Map<String, Object> requestParameters = (Map<String, Object>)exchange.getAttachment(GraphqlUtil.GRAPHQL_PARAMS);
    if(logger.isDebugEnabled()) logger.debug("requestParameters: " + requestParameters);
    GraphQL graphQL = GraphQL.newGraphQL(schema).build();
    String query = (String)requestParameters.get("query");
    if(query == null) {
        Status status = new Status(STATUS_GRAPHQL_MISSING_QUERY);
        exchange.setStatusCode(status.getStatusCode());
        exchange.getResponseSender().send(status.toString());
        return;
    }
    @SuppressWarnings("unchecked")
    Map<String, Object> variables = (Map<String, Object>)requestParameters.get("variables");
    if(variables == null) {
        variables = new HashMap<>();
    }
    String operationName = (String)requestParameters.get("operationName");
    ExecutionInput executionInput = ExecutionInput.newExecutionInput().query(query).operationName(operationName).context(exchange).root(exchange).variables(variables).build();
    ExecutionResult executionResult = graphQL.execute(executionInput);
    Map<String, Object> result = new HashMap<>();
    if (executionResult.getErrors().size() > 0) {
        result.put("errors", executionResult.getErrors());
        logger.error("Errors: {}", executionResult.getErrors());
    } else {
        result.put("data", executionResult.getData());
    }
    exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, "application/json");
    exchange.getResponseSender().send(Config.getInstance().getMapper().writeValueAsString(result));
}
 
开发者ID:networknt,项目名称:light-graphql-4j,代码行数:33,代码来源:GraphqlPostHandler.java

示例4: handleRequest

import io.undertow.server.HttpServerExchange; //导入方法依赖的package包/类
@Override
public void handleRequest(HttpServerExchange exchange) throws Exception {
    exchange.getResponseHeaders()
            .add(Headers.LOCATION,
                    "https://" + exchange.getHostName() + ":" + (exchange.getHostPort() + 363) + exchange.getRelativePath());
    exchange.setStatusCode(StatusCodes.TEMPORARY_REDIRECT);
}
 
开发者ID:icha024,项目名称:spur,代码行数:8,代码来源:RedirectHttpsHandler.java

示例5: invokePathTemplateHandler

import io.undertow.server.HttpServerExchange; //导入方法依赖的package包/类
private void invokePathTemplateHandler(SpurOptions options, Map<HttpString, Endpoint> methodEndpointsMap, HttpServerExchange exchange) {

        HttpString requestMethod = exchange.getRequestMethod();
        String requestAccessControlRequestMethod = getRequestHeader(exchange, ACCESS_CONTROL_REQUEST_METHOD);
        String requestOrigin = getRequestHeader(exchange, Headers.ORIGIN);
        if (requestMethod.equals(HEAD)) {
            requestMethod = GET;
        } else if (requestMethod.equals(OPTIONS) && requestAccessControlRequestMethod != null && methodEndpointsMap.containsKey(
                new HttpString(requestAccessControlRequestMethod)) && isValidCorsOrigin(options, requestOrigin)) {
            setCorsMethodHeader(options, methodEndpointsMap, exchange);
            exchange.endExchange();
            return;
        }

        Endpoint endpoint = methodEndpointsMap.get(requestMethod);
        if (endpoint == null) {
            exchange.setStatusCode(StatusCodes.METHOD_NOT_ALLOWED);
            exchange.getResponseHeaders()
                    .put(Headers.ALLOW, getAllowedMethods(methodEndpointsMap, options));
            exchange.endExchange();
            return;
        }

        Req req = new Req(exchange, endpoint.getBodyClassType());
        req.parseBody((newExchange, body) -> endpoint.getReqResBiConsumer()
                .accept(req, new Res(newExchange)));
    }
 
开发者ID:icha024,项目名称:spur,代码行数:28,代码来源:SpurServer.java

示例6: deleteUser

import io.undertow.server.HttpServerExchange; //导入方法依赖的package包/类
public static void deleteUser(HttpServerExchange exchange) {
    String email = userRequests.email(exchange);

    // If you care about it you can handle it.
    if (false == userDao.delete(email)) {
        ApiHandlers.notFound(exchange, String.format("User {} not found.", email));
        return;
    }
    exchange.setStatusCode(StatusCodes.NO_CONTENT);
    exchange.endExchange();
}
 
开发者ID:StubbornJava,项目名称:StubbornJava,代码行数:12,代码来源:UserRoutes.java

示例7: getSitemap

import io.undertow.server.HttpServerExchange; //导入方法依赖的package包/类
public void getSitemap(HttpServerExchange exchange) {
    String sitemapName = Exchange.pathParams().pathParam(exchange, "sitemap").orElse(null);
    String content = sitemap.getIndex(sitemapName);
    if (null == content) {
        exchange.setStatusCode(404);
        Exchange.body().sendText(exchange, String.format("Sitemap %s doesn't exist", sitemapName));
        return;
    }
    Exchange.body().sendXml(exchange, content);
}
 
开发者ID:StubbornJava,项目名称:StubbornJava,代码行数:11,代码来源:SitemapRoutes.java

示例8: report

import io.undertow.server.HttpServerExchange; //导入方法依赖的package包/类
public void report(HttpServerExchange exchange) {
    User user = exchange.getAttachment(AuthReader.USER);

    if (user == null) {
        exchange.setStatusCode(StatusCodes.BAD_REQUEST);
        exchange.endExchange();
        return;
    }

    FormData data = exchange.getAttachment(FormDataParser.FORM_DATA);

    FormData.FormValue xq = data.getFirst("x");
    FormData.FormValue yq = data.getFirst("y");
    FormData.FormValue idq = data.getFirst("id");
    FormData.FormValue msgq = data.getFirst("message");

    if (xq == null || yq == null || idq == null || msgq == null) {
        exchange.setStatusCode(StatusCodes.BAD_REQUEST);
        exchange.endExchange();
        return;
    }
    int x = Integer.parseInt(xq.getValue());
    int y = Integer.parseInt(yq.getValue());
    int id = Integer.parseInt(idq.getValue());
    if (x < 0 || x >= App.getWidth() || y < 0 || y >= App.getHeight()) {
        exchange.setStatusCode(StatusCodes.BAD_REQUEST);
        exchange.endExchange();
        return;
    }
    DBPixelPlacement pxl = App.getDatabase().getPixelByID(id);
    if (pxl.x != x || pxl.y != y) {
        exchange.setStatusCode(StatusCodes.BAD_REQUEST);
        exchange.endExchange();
        return;
    }
    App.getDatabase().addReport(user.getId(), id, x, y, msgq.getValue());
    exchange.setStatusCode(200);
}
 
开发者ID:xSke,项目名称:Pxls,代码行数:39,代码来源:WebHandler.java

示例9: notFound

import io.undertow.server.HttpServerExchange; //导入方法依赖的package包/类
public static void notFound(HttpServerExchange exchange, String message) {
    ApiError error = new ApiError(404, message);
    exchange.setStatusCode(error.getStatusCode());
    Exchange.body().sendJson(exchange, error);
}
 
开发者ID:StubbornJava,项目名称:StubbornJava,代码行数:6,代码来源:ApiHandlers.java

示例10: convertBodyStringToObj

import io.undertow.server.HttpServerExchange; //导入方法依赖的package包/类
private void convertBodyStringToObj(PostParseConsumer objectConsumer, HttpServerExchange exchange, String str) {
    if (bodyClassType.equals(String.class)) {
        this.body = (T) str;
        objectConsumer.postParse(exchange, str);
        return;
    }

    ObjectMapper objectMapper = LOCAL_MAPPER.get();
    if (objectMapper == null) {
        objectMapper = JsonFactory.createUseJSONDates();
        LOCAL_MAPPER.set(objectMapper);
    }

    T parsedType;
    try {
        parsedType = objectMapper.readValue(str, bodyClassType);
    } catch (Exception e) {
        exchange.setStatusCode(StatusCodes.BAD_REQUEST);
        exchange.endExchange();
        return;
    }

    if (parsedType == null) {
        exchange.setStatusCode(StatusCodes.BAD_REQUEST);
        exchange.endExchange();
        return;
    }

    Set<ConstraintViolation<T>> constraintViolations = validator.validate(parsedType);
    if (constraintViolations.isEmpty()) {
        this.body = parsedType;
        objectConsumer.postParse(exchange, parsedType);
    } else {
        exchange.setStatusCode(StatusCodes.BAD_REQUEST);
        exchange.getResponseSender()
                .send(objectMapper.toJson(new InvalidValues(constraintViolations.stream()
                        .map(violation -> violation.getPropertyPath()
                                .toString())
                        .collect(Collectors.toList()))));
        exchange.endExchange();
    }
}
 
开发者ID:icha024,项目名称:spur,代码行数:43,代码来源:Req.java

示例11: getConnection

import io.undertow.server.HttpServerExchange; //导入方法依赖的package包/类
@Override
public void getConnection(ProxyTarget target, HttpServerExchange exchange, ProxyCallback<ProxyConnection> callback, long timeout, TimeUnit timeUnit) {
    Realm realm = realmCache.matches(exchange.getRequestPath());
    if (realm != null) {
        parseToken(exchange).ifPresent(token -> {
            Optional<UserPermissionPrincipal> principal = authenticate(realm.getId(), token);

            principal.ifPresent(p -> {
                Map<String, Object> body = new HashMap<>();
                body.put("sub", p.getName());
                body.put("permissions", p.getPermissions());
                p.getProfiles().forEach((k, v) -> {
                    body.put(k, v);
                });
                exchange.getRequestHeaders().put(HttpString.tryFromString(config.getBackendHeaderName()),
                        jwt.sign(body, jwtHeader, null));
            });
        });
    } else {
        exchange.setStatusCode(404);
        exchange.endExchange();
        return;
    }

    Application application = realmCache.getApplication(realm);
    ClientConnection existing = exchange.getConnection().getAttachment(clientAttachmentKey);
    if (existing != null) {
        if (existing.isOpen()) {
            //this connection already has a client, re-use it
            String path = exchange.getRequestURI();
            if (path.startsWith(application.getVirtualPath())) {
                String passTo = calculatePathTo(path, application);
                exchange.setRequestPath(passTo);
                exchange.setRequestURI(passTo);
            }
            callback.completed(exchange, new ProxyConnection(existing, "/"));
            return;
        } else {
            exchange.getConnection().removeAttachment(clientAttachmentKey);
        }
    }

    try {
        URI uri = application.getUriToPass();
        client.connect(new ConnectNotifier(callback, exchange),
                new URI(uri.getScheme(), /*userInfo*/null, uri.getHost(), uri.getPort(),
                        /*path*/null, /*query*/null, /*fragment*/null),
                exchange.getIoThread(),
                exchange.getConnection().getByteBufferPool(), OptionMap.EMPTY);
    } catch (URISyntaxException e) {
        throw new MisconfigurationException("", application.getUriToPass(), e);
    }
}
 
开发者ID:kawasima,项目名称:bouncr,代码行数:54,代码来源:MultiAppProxyClient.java

示例12: referer

import io.undertow.server.HttpServerExchange; //导入方法依赖的package包/类
default void referer(HttpServerExchange exchange) {
    exchange.setStatusCode(StatusCodes.FOUND);
    exchange.getResponseHeaders().put(Headers.LOCATION, exchange.getRequestHeaders().get(Headers.REFERER, 0));
    exchange.endExchange();
}
 
开发者ID:StubbornJava,项目名称:StubbornJava,代码行数:6,代码来源:RedirectSenders.java

示例13: handleAllExceptions

import io.undertow.server.HttpServerExchange; //导入方法依赖的package包/类
public static void handleAllExceptions(HttpServerExchange exchange) {
    exchange.setStatusCode(500);
    Exchange.body().sendText(exchange, "Internal Server Error!");
}
 
开发者ID:StubbornJava,项目名称:StubbornJava,代码行数:5,代码来源:ExceptionHandlers.java

示例14: handleApiException

import io.undertow.server.HttpServerExchange; //导入方法依赖的package包/类
public static void handleApiException(HttpServerExchange exchange) {
    ApiException ex = (ApiException) exchange.getAttachment(ExceptionHandler.THROWABLE);
    exchange.setStatusCode(ex.getStatusCode());
    Exchange.body().sendJson(exchange, new ApiError(ex.getStatusCode(), ex.getMessage()));
}
 
开发者ID:StubbornJava,项目名称:StubbornJava,代码行数:6,代码来源:ApiHandlers.java

示例15: badRequest

import io.undertow.server.HttpServerExchange; //导入方法依赖的package包/类
public static void badRequest(HttpServerExchange exchange, String message) {
    ApiError error = new ApiError(400, message);
    exchange.setStatusCode(error.getStatusCode());
    Exchange.body().sendJson(exchange, error);
}
 
开发者ID:StubbornJava,项目名称:StubbornJava,代码行数:6,代码来源:ApiHandlers.java


注:本文中的io.undertow.server.HttpServerExchange.setStatusCode方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。