本文整理汇总了Java中io.reactivex.netty.protocol.http.server.HttpServerResponse.setStatus方法的典型用法代码示例。如果您正苦于以下问题:Java HttpServerResponse.setStatus方法的具体用法?Java HttpServerResponse.setStatus怎么用?Java HttpServerResponse.setStatus使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类io.reactivex.netty.protocol.http.server.HttpServerResponse
的用法示例。
在下文中一共展示了HttpServerResponse.setStatus方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: handle
import io.reactivex.netty.protocol.http.server.HttpServerResponse; //导入方法依赖的package包/类
@Override
public Observable<Void> handle(HttpServerRequest<ByteBuf> request, HttpServerResponse<ByteBuf> response) {
if (alarmService == null) {
response.setStatus(HttpResponseStatus.SERVICE_UNAVAILABLE);
return response.close();
}
if (HttpMethod.GET.equals(request.getHttpMethod())) {
handleGet(response, request.getUri());
} else if (HttpMethod.PUT.equals(request.getHttpMethod())) {
handlePut(response, request.getContent());
} else if (HttpMethod.DELETE.equals(request.getHttpMethod())) {
handleDelete(response, request.getUri());
} else {
response.setStatus(HttpResponseStatus.NOT_IMPLEMENTED);
}
return response.close();
}
示例2: handleGet
import io.reactivex.netty.protocol.http.server.HttpServerResponse; //导入方法依赖的package包/类
private void handleGet(HttpServerResponse<ByteBuf> response, String uri) {
String[] parts = uri.substring(1).split("/");
ObjectMapper mapper = new ObjectMapper();
try {
if (parts.length == 1) {
response.writeBytes(mapper.writeValueAsBytes(alarmService.getAlarms()));
} else if (parts.length == 2) {
String alarmName = URLDecoder.decode(parts[1], "UTF-8");
WeatherAlarm alarm = alarmService.getAlarm(alarmName);
if (alarm != null) {
response.writeBytes(mapper.writeValueAsBytes(alarm));
} else {
logger.debug("No alarm found with name " + alarmName);
response.setStatus(HttpResponseStatus.NOT_FOUND);
}
} else {
logger.error("Unsupported resource request " + uri);
response.setStatus(HttpResponseStatus.NOT_FOUND);
}
} catch (IOException e) {
logger.error("Failed to write JSON to response", e);
response.setStatus(HttpResponseStatus.INTERNAL_SERVER_ERROR);
}
}
示例3: handleDelete
import io.reactivex.netty.protocol.http.server.HttpServerResponse; //导入方法依赖的package包/类
private void handleDelete(HttpServerResponse<ByteBuf> response, String uri) {
String[] parts = uri.substring(1).split("/");
try {
if (parts.length == 1) {
//Not allowed to delete all alarms
response.setStatus(HttpResponseStatus.UNAUTHORIZED);
} else if (parts.length == 2) {
String alarmName = URLDecoder.decode(parts[1], "UTF-8");
boolean removed = alarmService.removeAlarm(alarmName);
if (!removed) {
logger.debug("No alarm found with name " + alarmName);
response.setStatus(HttpResponseStatus.NOT_FOUND);
}
} else {
logger.error("Unsupported resource request " + uri);
response.setStatus(HttpResponseStatus.NOT_FOUND);
}
} catch (IOException e) {
logger.error("Failed to write JSON to response", e);
response.setStatus(HttpResponseStatus.INTERNAL_SERVER_ERROR);
}
}
示例4: handleRecommendationsByUserId
import io.reactivex.netty.protocol.http.server.HttpServerResponse; //导入方法依赖的package包/类
private Observable<Void> handleRecommendationsByUserId(HttpServerRequest<ByteBuf> request, HttpServerResponse<ByteBuf> response) {
System.out.println("HTTP request -> recommendations by user id request: " + request.getPath());
final String userId = userIdFromPath(request.getPath());
if (userId == null) {
response.setStatus(HttpResponseStatus.BAD_REQUEST);
return response.close();
}
if (!userRecommendations.containsKey(userId)) {
response.setStatus(HttpResponseStatus.NOT_FOUND);
return response.close();
}
StringBuilder builder = new StringBuilder();
for (String movieId : userRecommendations.get(userId)) {
System.out.println(" returning: " + movies.get(movieId));
builder.append(movies.get(movieId)).append('\n');
}
ByteBuf byteBuf = UnpooledByteBufAllocator.DEFAULT.buffer();
byteBuf.writeBytes(builder.toString().getBytes(Charset.defaultCharset()));
response.write(byteBuf);
return response.close();
}
示例5: simulateTimeout
import io.reactivex.netty.protocol.http.server.HttpServerResponse; //导入方法依赖的package包/类
public Observable<Void> simulateTimeout(HttpServerRequest<ByteBuf> httpRequest, HttpServerResponse<ByteBuf> response) {
String uri = httpRequest.getUri();
QueryStringDecoder decoder = new QueryStringDecoder(uri);
List<String> timeout = decoder.parameters().get("timeout");
byte[] contentBytes;
HttpResponseStatus status = HttpResponseStatus.NO_CONTENT;
if (null != timeout && !timeout.isEmpty()) {
try {
Thread.sleep(Integer.parseInt(timeout.get(0)));
contentBytes = "".getBytes();
} catch (Exception e) {
contentBytes = e.getMessage().getBytes();
status = HttpResponseStatus.INTERNAL_SERVER_ERROR;
}
} else {
status = HttpResponseStatus.BAD_REQUEST;
contentBytes = "Please provide a timeout parameter.".getBytes();
}
response.setStatus(status);
return response.writeBytesAndFlush(contentBytes);
}
示例6: handle
import io.reactivex.netty.protocol.http.server.HttpServerResponse; //导入方法依赖的package包/类
@Override
public Observable<Void> handle(HttpServerRequest<ByteBuf> request, HttpServerResponse<ByteBuf> response) {
String uri = request.getUri();
if (uri.startsWith("test/singleEntity")) {
return handleSingleEntity(response);
} else if (uri.startsWith("test/stream")) {
return handleStream(response);
} else if (uri.startsWith("test/nochunk_stream")) {
return handleStreamWithoutChunking(response);
} else if (uri.startsWith("test/largeStream")) {
return handleLargeStream(response);
} else if (uri.startsWith("test/timeout")) {
return simulateTimeout(request, response);
} else if (uri.startsWith("test/post")) {
return handlePost(request, response);
} else {
response.setStatus(HttpResponseStatus.NOT_FOUND);
return response.flush();
}
}
示例7: bridgeResponse
import io.reactivex.netty.protocol.http.server.HttpServerResponse; //导入方法依赖的package包/类
ContainerResponseWriter bridgeResponse(final HttpServerResponse<ByteBuf> serverResponse) {
return new ContainerResponseWriter() {
private final ByteBuf contentBuffer = serverResponse.getChannel().alloc().buffer();
@Override
public OutputStream writeStatusAndHeaders(long contentLength, ContainerResponse response) {
int responseStatus = response.getStatus();
serverResponse.setStatus(HttpResponseStatus.valueOf(responseStatus));
HttpResponseHeaders responseHeaders = serverResponse.getHeaders();
for(Map.Entry<String, List<Object>> header : response.getHttpHeaders().entrySet()){
responseHeaders.setHeader(header.getKey(), header.getValue());
}
return new ByteBufOutputStream(contentBuffer);
}
@Override
public void finish() {
serverResponse.writeAndFlush(contentBuffer);
}
};
}
示例8: handle
import io.reactivex.netty.protocol.http.server.HttpServerResponse; //导入方法依赖的package包/类
@Override
public Observable<Void> handle(HttpServerRequest<ByteBuf> request, HttpServerResponse<ByteBuf> response) {
if (request.getUri().startsWith(healthCheckUri)) {
return healthCheckEndpoint.handle(request, response);
} else if (request.getUri().startsWith("/hello/to/")) {
int prefixLength = "/hello/to".length();
String userName = request.getPath().substring(prefixLength);
if (userName.isEmpty() || userName.length() == 1 /*The uri is /hello/to/ but no name */) {
response.setStatus(HttpResponseStatus.BAD_REQUEST);
return response.writeStringAndFlush(
"{\"Error\":\"Please provide a username to say hello. The URI should be /hello/to/{username}\"}");
} else {
String msg = "Hello " + userName.substring(1) /*Remove the / prefix*/ + " from Netflix OSS";
return response.writeStringAndFlush("{\"Message\":\"" + msg + "\"}");
}
} else if (request.getUri().startsWith("/hello")) {
return response.writeStringAndFlush("{\"Message\":\"Hello newbee from Netflix OSS\"}");
} else {
response.setStatus(HttpResponseStatus.NOT_FOUND);
return response.close();
}
}
示例9: sayHelloToUser
import io.reactivex.netty.protocol.http.server.HttpServerResponse; //导入方法依赖的package包/类
public Observable<Void> sayHelloToUser(HttpServerRequest<ByteBuf> request, HttpServerResponse<ByteBuf> response) {
JSONObject content = new JSONObject();
int prefixLength = "/hello/to".length();
String userName = request.getPath().substring(prefixLength);
try {
if (userName.isEmpty() || userName.length() == 1 /*The uri is /hello/to/ but no name */) {
response.setStatus(HttpResponseStatus.BAD_REQUEST);
content.put("Error", "Please provide a username to say hello. The URI should be /hello/to/{username}");
} else {
content.put("Message", "Hello " + userName.substring(1) /*Remove the / prefix*/ + " from Netflix OSS");
}
} catch (JSONException e) {
logger.error("Error creating json response.", e);
return Observable.error(e);
}
response.write(content.toString(), StringTransformer.DEFAULT_INSTANCE);
return response.close();
}
示例10: handle
import io.reactivex.netty.protocol.http.server.HttpServerResponse; //导入方法依赖的package包/类
@Override
public Observable<Void> handle(HttpServerRequest<ByteBuf> request, HttpServerResponse<ByteBuf> response) {
RequestHandler<ByteBuf, ByteBuf> handler = findRequestHandler(request.getUri());
if (handler != null) {
return handler.handle(request, response);
} else {
response.setStatus(HttpResponseStatus.NOT_FOUND);
return response.close();
}
}
示例11: createMockHttpResponse
import io.reactivex.netty.protocol.http.server.HttpServerResponse; //导入方法依赖的package包/类
@SuppressWarnings("unchecked")
public static HttpServerResponse<ByteBuf> createMockHttpResponse(Capture<HttpResponseStatus> captureResponseStatus,
Capture<byte[]> captureWrittenBytes) {
HttpServerResponse<ByteBuf> mockResponse = EasyMock.createMock(HttpServerResponse.class);
mockResponse.writeBytes(EasyMock.capture(captureWrittenBytes));
mockResponse.setStatus(EasyMock.capture(captureResponseStatus));
EasyMock.expect(mockResponse.close()).andReturn(Observable.empty()).anyTimes();
EasyMock.replay(mockResponse);
return mockResponse;
}
示例12: handleTest
import io.reactivex.netty.protocol.http.server.HttpServerResponse; //导入方法依赖的package包/类
private Observable<Void> handleTest(HttpServerRequest<ByteBuf> request, final HttpServerResponse<ByteBuf> response) {
response.setStatus(HttpResponseStatus.OK);
ByteBuf byteBuf = UnpooledByteBufAllocator.DEFAULT.buffer();
byteBuf.writeBytes("test".getBytes(Charset.defaultCharset()));
response.write(byteBuf);
return response.close();
}
示例13: handleRequest
import io.reactivex.netty.protocol.http.server.HttpServerResponse; //导入方法依赖的package包/类
@Override
protected Observable<Void> handleRequest(HttpServerRequest<?> request, HttpServerResponse<ServerSentEvent> response) {
List<String> _id = request.getQueryParameters().get("id");
if (_id == null || _id.size() != 1) {
return writeError(request, response, "Please provide a numerical 'id' value. It can be a random number (uuid). Received => " + _id);
}
long id = Long.parseLong(String.valueOf(_id.get(0)));
int delay = getParameter(request, "delay", 50); // default to 50ms server-side delay
int itemSize = getParameter(request, "itemSize", 128); // default to 128 bytes item size (assuming ascii text)
int numItems = getParameter(request, "numItems", 10); // default to 10 items in a list
// no more than 100 items
if (numItems < 1 || numItems > 100) {
return writeError(request, response, "Please choose a 'numItems' value from 1 to 100.");
}
// no larger than 50KB per item
if (itemSize < 1 || itemSize > 1024 * 50) {
return writeError(request, response, "Please choose an 'itemSize' value from 1 to 1024*50 (50KB).");
}
// no larger than 60 second delay
if (delay < 0 || delay > 60000) {
return writeError(request, response, "Please choose a 'delay' value from 0 to 60000 (60 seconds).");
}
response.setStatus(HttpResponseStatus.OK);
return MockResponse.generateJson(id, delay, itemSize, numItems)
.flatMap(json -> response.writeStringAndFlush("data:" + json + "\n"))
.doOnCompleted(response::close);
}
示例14: handleUpdateRecommendationsForUser
import io.reactivex.netty.protocol.http.server.HttpServerResponse; //导入方法依赖的package包/类
private Observable<Void> handleUpdateRecommendationsForUser(HttpServerRequest<ByteBuf> request, final HttpServerResponse<ByteBuf> response) {
System.out.println("HTTP request -> update recommendations for user: " + request.getPath());
final String userId = userIdFromPath(request.getPath());
if (userId == null) {
response.setStatus(HttpResponseStatus.BAD_REQUEST);
return response.close();
}
return request.getContent().flatMap(new Func1<ByteBuf, Observable<Void>>() {
@Override
public Observable<Void> call(ByteBuf byteBuf) {
String movieId = byteBuf.toString(Charset.defaultCharset());
System.out.println(format(" updating: {user=%s, movie=%s}", userId, movieId));
synchronized (this) {
Set<String> recommendations;
if (userRecommendations.containsKey(userId)) {
recommendations = userRecommendations.get(userId);
} else {
recommendations = new ConcurrentSet<String>();
userRecommendations.put(userId, recommendations);
}
recommendations.add(movieId);
}
response.setStatus(HttpResponseStatus.OK);
return response.close();
}
});
}
示例15: handle
import io.reactivex.netty.protocol.http.server.HttpServerResponse; //导入方法依赖的package包/类
@Override
public Observable<Void> handle(HttpServerRequest<ByteBuf> request, HttpServerResponse<ByteBuf> response) {
if (request.getPath().contains("/sendOK")) {
response.setStatus(HttpResponseStatus.OK);
} else if (request.getPath().contains("/sendNotFound")) {
response.setStatus(HttpResponseStatus.NOT_FOUND);
} else {
response.setStatus(HttpResponseStatus.INTERNAL_SERVER_ERROR);
}
return Observable.empty();
}