本文整理汇总了Java中io.vertx.core.http.HttpServerResponse类的典型用法代码示例。如果您正苦于以下问题:Java HttpServerResponse类的具体用法?Java HttpServerResponse怎么用?Java HttpServerResponse使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
HttpServerResponse类属于io.vertx.core.http包,在下文中一共展示了HttpServerResponse类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: sendFile
import io.vertx.core.http.HttpServerResponse; //导入依赖的package包/类
private void sendFile(RoutingContext routingContext, String fileName, File saneFile) {
HttpServerResponse response = routingContext.response();
response.putHeader("Content-Description", "File Transfer");
response.putHeader("Content-Type", "application/octet-stream");
response.putHeader("Content-Disposition", "attachment; filename=" + fileName); // @TODO: don't trust this name?
response.putHeader("Content-Transfer-Encoding", "binary");
response.putHeader("Expires", "0");
response.putHeader("Pragma", "Public");
response.putHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
response.putHeader("Content-Length", "" + saneFile.length());
response.sendFile(saneFile.getAbsolutePath());
}
示例2: addResponseHeaders
import io.vertx.core.http.HttpServerResponse; //导入依赖的package包/类
default void addResponseHeaders(RouteDefinition definition, HttpServerResponse response) {
if (!response.ended() &&
!response.headers().contains(HttpHeaders.CONTENT_TYPE)) {
if (definition != null &&
definition.getProduces() != null) {
for (MediaType produces : definition.getProduces()) {
response.putHeader(HttpHeaders.CONTENT_TYPE, MediaTypeHelper.toString(produces));
}
}
else {
response.putHeader(HttpHeaders.CONTENT_TYPE, MediaType.WILDCARD);
}
}
}
示例3: getFormattedElement
import io.vertx.core.http.HttpServerResponse; //导入依赖的package包/类
@Test
public void getFormattedElement() {
AccessLogParam param = new AccessLogParam();
RoutingContext mockContext = Mockito.mock(RoutingContext.class);
HttpServerResponse mockResponse = Mockito.mock(HttpServerResponse.class);
VertxHttpHeaders headers = new VertxHttpHeaders();
String headerValue = "headerValue";
param.setRoutingContext(mockContext);
headers.add(IDENTIFIER, headerValue);
Mockito.when(mockContext.response()).thenReturn(mockResponse);
Mockito.when(mockResponse.headers()).thenReturn(headers);
String result = ELEMENT.getFormattedElement(param);
assertEquals(headerValue, result);
assertEquals(ELEMENT.getIdentifier(), IDENTIFIER);
}
示例4: getFormattedElementOnNotFound
import io.vertx.core.http.HttpServerResponse; //导入依赖的package包/类
@Test
public void getFormattedElementOnNotFound() {
AccessLogParam param = new AccessLogParam();
RoutingContext mockContext = Mockito.mock(RoutingContext.class);
HttpServerResponse mockResponse = Mockito.mock(HttpServerResponse.class);
VertxHttpHeaders headers = new VertxHttpHeaders();
String headerValue = "headerValue";
param.setRoutingContext(mockContext);
headers.add("anotherHeader", headerValue);
Mockito.when(mockContext.response()).thenReturn(mockResponse);
Mockito.when(mockResponse.headers()).thenReturn(headers);
String result = ELEMENT.getFormattedElement(param);
assertEquals("-", result);
}
示例5: handle
import io.vertx.core.http.HttpServerResponse; //导入依赖的package包/类
public void handle(RoutingContext ctx) {
HttpServerResponse response = ctx.response();
if (userManager.isLogined(ctx)) {
try {
String uid = userManager.getUid(userManager.getIdFromSession(ctx));
JobResult result = requestManager.getApiKeys(uid);
if (result.isSuccess()) {
SafeResultSet rs = (SafeResultSet) result.getArgs()[0];
while (rs.next()) {
//
//
//
}
}
} catch (SQLException e) {
e.printStackTrace();
response.setStatusCode(500);
}
} else {
response.setStatusCode(400);
}
response.end();
response.close();
}
示例6: writeDatasetContents
import io.vertx.core.http.HttpServerResponse; //导入依赖的package包/类
private void writeDatasetContents(
RoutingContext routingContext, Observable<DataEnvelope> datasetContents) {
HttpServerResponse httpServerResponse =
jsonContentType(routingContext.response()).setChunked(true);
final AtomicBoolean isFirst = new AtomicBoolean(true);
datasetContents.subscribe(
(DataEnvelope dataEnvelope) -> {
if (!isFirst.get()) {
httpServerResponse.write(",");
} else {
isFirst.set(false);
httpServerResponse.write("[");
}
httpServerResponse.write(new JsonObject(dataEnvelope.getPayload()).encodePrettily());
},
throwable -> GlobalExceptionHandler.error(routingContext, throwable),
() -> httpServerResponse.end("]"));
}
示例7: writeDatasets
import io.vertx.core.http.HttpServerResponse; //导入依赖的package包/类
private void writeDatasets(RoutingContext routingContext, Observable<Dataset> datasets) {
HttpServerResponse httpServerResponse =
jsonContentType(routingContext.response()).setChunked(true);
final AtomicBoolean isFirst = new AtomicBoolean(true);
datasets.subscribe(
dataset -> {
if (!isFirst.get()) {
httpServerResponse.write(",");
} else {
isFirst.set(false);
httpServerResponse.write("[");
}
httpServerResponse.write(viewTransformer.transform(dataset));
},
throwable -> GlobalExceptionHandler.error(routingContext, throwable),
() -> httpServerResponse.end("]"));
}
示例8: applyForOauth
import io.vertx.core.http.HttpServerResponse; //导入依赖的package包/类
/**
* 申请微信授权
* /awp/wxOauth/apply/{body}
* web服务需要授权时,向用户发送重定向,重定向到当前接口
* 参数只有一个,内容为JSON,请用http://localhost:8083/awp/base64.html进行加密
* {
* "eid":web项目使用的公众号在本项目中的用户ID
* "type":0=静默授权,只能获取OpenID,1=正常授权,会弹出授权确认页面,可以获取到用户信息
* "callback":授权成功后调用的web项目回调接口地址,请使用完整地址,
* 回调时会使用GET方法,加上rs参数,
* 如果静默授权,rs参数内容就是openid
* 如果正常授权,rs参数内容是turingBase64加密的授权结果(JSON)
* }
*
* @param rc Vertx的RoutingContext对象
* @author Leibniz.Hu
*/
private void applyForOauth(RoutingContext rc) {
HttpServerResponse resp = rc.response();
String decodedBody = TuringBase64Util.decode(rc.request().getParam("body"));
JsonObject reqJson = new JsonObject(decodedBody);
Integer eid = reqJson.getInteger("eid");
int type = reqJson.getInteger("type");
String callback = TuringBase64Util.encode(reqJson.getString("callback"));//授权后回调方法
vertx.eventBus().<JsonObject>send(ADDR_ACCOUNT_DB.get(), makeMessage(COMMAND_GET_ACCOUNT_BY_ID, eid), ar -> {
if (ar.succeeded()) {
JsonObject account = ar.result().body();
String redirectAfterUrl = PROJ_URL + "oauth/wx/" + (type == 0 ? "baseCb" : "infoCb") + "?eid=" + eid + "&visitUrl=" + callback;
String returnUrl = null;
try {
returnUrl = String.format((type == 0 ? OAUTH_BASE_API : OAUTH_INFO_API)
, account.getString(WXAPPID), URLEncoder.encode(redirectAfterUrl, "UTF-8"));
} catch (UnsupportedEncodingException ignored) { //不可能出现的
}
resp.setStatusCode(302).putHeader("Location", returnUrl).end();
} else {
log.error("EventBus消息响应错误", ar.cause());
resp.setStatusCode(500).end("EventBus error!");
}
});
}
示例9: applyForOauth
import io.vertx.core.http.HttpServerResponse; //导入依赖的package包/类
/**
* 申请支付宝授权
* /awp/wxOauth/apply/{body}
* web服务需要授权时,向用户发送重定向,重定向到当前接口
* 参数只有一个,内容为JSON,请用http://localhost:8083/awp/base64.html进行加密
* {
* "eid":web项目使用的公众号在本项目中的用户ID
* "type":0=静默授权,只能获取OpenID,1=正常授权,会弹出授权确认页面,可以获取到用户信息
* "callback":授权成功后调用的web项目回调接口地址,请使用完整地址,
* 回调时会使用GET方法,加上rs参数,
* 如果静默授权,rs参数内容就是openid
* 如果正常授权,rs参数内容是turingBase64加密的授权结果(JSON)
* }
*
* @param rc Vertx的RoutingContext对象
* @author Leibniz.Hu
*/
private void applyForOauth(RoutingContext rc) {
HttpServerResponse resp = rc.response();
String decodedBody = TuringBase64Util.decode(rc.request().getParam("body"));
JsonObject reqJson = new JsonObject(decodedBody);
Integer eid = reqJson.getInteger("eid");
int type = reqJson.getInteger("type");
String callback = TuringBase64Util.encode(reqJson.getString("callback"));//授权后回调方法
vertx.eventBus().<JsonObject>send(ADDR_ACCOUNT_DB.get(), makeMessage(COMMAND_GET_ACCOUNT_BY_ID, eid), ar -> {
if (ar.succeeded()) {
JsonObject acc = ar.result().body();
String redirectAfterUrl = PROJ_URL + "oauth/zfb/" + (type == 0 ? "baseCb" : "infoCb") + "?eid=" + eid + "&visitUrl=" + callback;
AliAccountInfo aliAcc = new AliAccountInfo(acc.getString(ZFBAPPID), acc.getString(ZFBPRIVKEY), acc.getString(ZFBPUBKEY), null, null, redirectAfterUrl);
AliPayApi.auth(aliAcc, resp, type == 1);
} else {
log.error("EventBus消息响应错误", ar.cause());
resp.setStatusCode(500).end("EventBus error!");
}
});
}
示例10: updateAlipayPaySetting
import io.vertx.core.http.HttpServerResponse; //导入依赖的package包/类
private void updateAlipayPaySetting(RoutingContext rc) {
if (forbidAccess(rc, "uid", true)) {
return;
}
HttpServerRequest req = rc.request();
HttpServerResponse resp = rc.response().putHeader("content-type", "application/json; charset=utf-8");
//解析参数
Long uid = Long.parseLong(req.getParam("uid"));
Integer paySwitch = Integer.parseInt(req.getParam("paySwitch"));
String appId = req.getParam("appId");
String appPrivKey = req.getParam("appPrivKey");
String zfbPubKey = req.getParam("zfbPubKey");
//参数检查
if (paySwitch == 1 && !CommonUtils.notEmptyString(appId, appPrivKey, zfbPubKey)) {
resp.end(new JsonObject().put("status", "invalid").toString());
return;
}
//保存支付参数
JsonObject acc = new JsonObject().put(ID, uid).put(ZFBAPPID, appId).put(ZFBPRIVKEY, appPrivKey).put(ZFBPUBKEY, zfbPubKey).put(ZFBPAYON, paySwitch);
updatePaySetting(resp, acc, COMMAND_UPDATE_ALIPAY);
}
示例11: refuseNonLanAccess
import io.vertx.core.http.HttpServerResponse; //导入依赖的package包/类
/**
* 判断当前请求是否允许,如果不允许,则将状态码设为403并结束响应
*
* @return true:禁止访问 false=允许访问
* @author Leibniz.Hu
*/
protected boolean refuseNonLanAccess(RoutingContext rc) {
HttpServerRequest req = rc.request();
HttpServerResponse resp = rc.response();
String realIp = req.getHeader("X-Real-IP");
String xforward = req.getHeader("X-Forwarded-For");
//禁止外网访问
if (realIp != null && !isLanIP(realIp)) {
log.warn("检测到非法访问,来自X-Real-IP={}", realIp);
resp.setStatusCode(403).end();
return true;
}
if (xforward != null && !isLanIP(xforward)) {
log.warn("检测到非法访问,来自X-Forwarded-For={}", xforward);
resp.setStatusCode(403).end();
return true;
}
return false;
}
示例12: wapPay
import io.vertx.core.http.HttpServerResponse; //导入依赖的package包/类
/**
* 该方法实现了支付宝手机网页的支付功能;
* 方法将调用支付宝支付页面需要用到的数据拼接和设置好,然后,写到响应中,前台会自动弹出支付宝支付页面;
* 其中,支付金额要求大于0.01,并且小于100000000,需要调用者自己检查该值再传进来,否则页面会报获取不到订单信息错误;
*
* @param aliAccountInfo 保存了支付宝账户的信息,包括appId、用户私钥、支付宝公钥和回调地址等,具体参考AliAccountInfo类的属性说明
* @param payBizContent 保存了订单信息,包括本地订单号、金额等,具体参考BizContent类的属性说明
* Create by quandong
*/
public static void wapPay(AliAccountInfo aliAccountInfo, PayBizContent payBizContent, HttpServerResponse httpResponse) throws IOException {
AlipayClient alipayClient = AliPayCliFactory.getAlipayClient(aliAccountInfo); // 从客户端工厂中获取AlipayClient
AlipayTradeWapPayRequest alipayRequest = new AlipayTradeWapPayRequest(); // 创建API对应的request
String form = "";
alipayRequest.setReturnUrl(aliAccountInfo.getReturnUrl()); // 设置回跳地址
alipayRequest.setNotifyUrl(aliAccountInfo.getNotifyUrl()); // 设置通知地址
alipayRequest.setBizContent(payBizContent.toString()); // 填充业务参数
try {
form = alipayClient.pageExecute(alipayRequest).getBody(); // 调用SDK生成表单
} catch (AlipayApiException e) {
e.printStackTrace();
}
httpResponse.putHeader("Content-Type", "text/html;charset=" + AlipayConstants.CHARSET_UTF8); // 设置文本类型及编码
httpResponse.end(form); // 直接将完整的表单html输出到页面
}
示例13: wechatPreHandle
import io.vertx.core.http.HttpServerResponse; //导入依赖的package包/类
/**
* 微信支付的预处理,js的wx.config需要用
*
* 异步返回 wx.config需要用的数据
*
* @author Leibniz
*/
private void wechatPreHandle(RoutingContext rc) {
HttpServerRequest req = rc.request();
HttpServerResponse response = rc.response();
int eid = Integer.parseInt(req.getParam("eid"));
vertx.eventBus().<JsonObject>send(ADDR_ACCOUNT_DB.get(), makeMessage(COMMAND_GET_ACCOUNT_BY_ID, eid), ar -> {
if (ar.succeeded()) {
JsonObject acc = ar.result().body();
String curUrl = null;
try {
curUrl = URLDecoder.decode(req.getParam("url"), "UTF-8");
} catch (UnsupportedEncodingException ignore) {
}
//调用微信jdk类
Map<String, String> jdkMap = new WechatJdk(req, acc, curUrl).getMap();
jdkMap.put("appId", acc.getString(WXAPPID));
String jsonStr = JsonObject.mapFrom(jdkMap).toString();
log.debug("接收到(ID={})微信JSSDK初始化请求,返回Json:{}", eid, jsonStr);
response.putHeader("content-type", "application/json;charset=UTF-8").end(jsonStr);
} else {
log.error("EventBus消息响应错误", ar.cause());
response.setStatusCode(500).end("EventBus error!");
}
});
}
示例14: handle
import io.vertx.core.http.HttpServerResponse; //导入依赖的package包/类
@Override
public void handle(RoutingContext routingContext) {
String id = routingContext.request().getParam(MESSAGE_ID);
redisClient.hget(MESSAGES, id, result -> {
HttpServerResponse response = routingContext.response();
if (result == null) {
response.setStatusCode(404);
response.end();
} else {
response.setStatusCode(200);
response.putHeader("content-type", "application/json; charset=utf-8");
response.end(result.result());
}
});
}
示例15: reply
import io.vertx.core.http.HttpServerResponse; //导入依赖的package包/类
public static void reply(
final RoutingContext context,
final Envelop envelop,
final Event event
) {
// 1. Get response reference
final HttpServerResponse response
= context.response();
// 2. Set response status
final HttpStatusCode code = envelop.status();
response.setStatusCode(code.code());
response.setStatusMessage(code.message());
// 3. Media processing
Normalizer.out(response, envelop, event);
// 4. Store Session
storeSession(context, envelop.data(), event.getAction());
// 5. Response process
if (!response.ended()) {
response.end(envelop.response());
}
response.close();
}