本文整理匯總了Java中io.netty.handler.codec.http.HttpHeaderNames類的典型用法代碼示例。如果您正苦於以下問題:Java HttpHeaderNames類的具體用法?Java HttpHeaderNames怎麽用?Java HttpHeaderNames使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
HttpHeaderNames類屬於io.netty.handler.codec.http包,在下文中一共展示了HttpHeaderNames類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: setCorsResponseHeaders
import io.netty.handler.codec.http.HttpHeaderNames; //導入依賴的package包/類
public static void setCorsResponseHeaders(HttpRequest request, HttpResponse resp, Netty4CorsConfig config) {
if (!config.isCorsSupportEnabled()) {
return;
}
String originHeader = request.headers().get(HttpHeaderNames.ORIGIN);
if (!Strings.isNullOrEmpty(originHeader)) {
final String originHeaderVal;
if (config.isAnyOriginSupported()) {
originHeaderVal = ANY_ORIGIN;
} else if (config.isOriginAllowed(originHeader) || isSameOrigin(originHeader, request.headers().get(HttpHeaderNames.HOST))) {
originHeaderVal = originHeader;
} else {
originHeaderVal = null;
}
if (originHeaderVal != null) {
resp.headers().add(HttpHeaderNames.ACCESS_CONTROL_ALLOW_ORIGIN, originHeaderVal);
}
}
if (config.isCredentialsAllowed()) {
resp.headers().add(HttpHeaderNames.ACCESS_CONTROL_ALLOW_CREDENTIALS, "true");
}
}
示例2: setOrigin
import io.netty.handler.codec.http.HttpHeaderNames; //導入依賴的package包/類
private boolean setOrigin(final HttpResponse response) {
final String origin = request.headers().get(HttpHeaderNames.ORIGIN);
if (!Strings.isNullOrEmpty(origin)) {
if ("null".equals(origin) && config.isNullOriginAllowed()) {
setAnyOrigin(response);
return true;
}
if (config.isAnyOriginSupported()) {
if (config.isCredentialsAllowed()) {
echoRequestOrigin(response);
setVaryHeader(response);
} else {
setAnyOrigin(response);
}
return true;
}
if (config.isOriginAllowed(origin)) {
setOrigin(response, origin);
setVaryHeader(response);
return true;
}
}
return false;
}
示例3: validateOrigin
import io.netty.handler.codec.http.HttpHeaderNames; //導入依賴的package包/類
private boolean validateOrigin() {
if (config.isAnyOriginSupported()) {
return true;
}
final String origin = request.headers().get(HttpHeaderNames.ORIGIN);
if (Strings.isNullOrEmpty(origin)) {
// Not a CORS request so we cannot validate it. It may be a non CORS request.
return true;
}
if ("null".equals(origin) && config.isNullOriginAllowed()) {
return true;
}
// if the origin is the same as the host of the request, then allow
if (isSameOrigin(origin, request.headers().get(HttpHeaderNames.HOST))) {
return true;
}
return config.isOriginAllowed(origin);
}
示例4: executeRequest
import io.netty.handler.codec.http.HttpHeaderNames; //導入依賴的package包/類
private FullHttpResponse executeRequest(final Settings settings, final String originValue, final String host) {
// construct request and send it over the transport layer
try (Netty4HttpServerTransport httpServerTransport =
new Netty4HttpServerTransport(settings, networkService, bigArrays, threadPool, xContentRegistry(),
new NullDispatcher())) {
httpServerTransport.start();
final FullHttpRequest httpRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/");
if (originValue != null) {
httpRequest.headers().add(HttpHeaderNames.ORIGIN, originValue);
}
httpRequest.headers().add(HttpHeaderNames.HOST, host);
final WriteCapturingChannel writeCapturingChannel = new WriteCapturingChannel();
final Netty4HttpRequest request = new Netty4HttpRequest(xContentRegistry(), httpRequest, writeCapturingChannel);
Netty4HttpChannel channel =
new Netty4HttpChannel(httpServerTransport, request, null, randomBoolean(), threadPool.getThreadContext());
channel.sendResponse(new TestResponse());
// get the response
List<Object> writtenObjects = writeCapturingChannel.getWrittenObjects();
assertThat(writtenObjects.size(), is(1));
return (FullHttpResponse) writtenObjects.get(0);
}
}
示例5: setDateAndCacheHeaders
import io.netty.handler.codec.http.HttpHeaderNames; //導入依賴的package包/類
/**
* Sets the Date and Cache headers for the HTTP Response
*
* @param response HTTP response
* @param fileToCache file to extract content type
*/
private static void setDateAndCacheHeaders(HttpResponse response, File fileToCache) {
SimpleDateFormat dateFormatter = new SimpleDateFormat(HTTP_DATE_FORMAT, Locale.US);
dateFormatter.setTimeZone(TimeZone.getTimeZone(HTTP_DATE_GMT_TIMEZONE));
// Date header
Calendar time = new GregorianCalendar();
response.headers().set(HttpHeaderNames.DATE, dateFormatter.format(time.getTime()));
// Add cache headers
time.add(Calendar.SECOND, HTTP_CACHE_SECONDS);
response.headers().set(HttpHeaderNames.EXPIRES, dateFormatter.format(time.getTime()));
response.headers().set(HttpHeaderNames.CACHE_CONTROL, "private, max-age=" + HTTP_CACHE_SECONDS);
response.headers().set(
HttpHeaderNames.LAST_MODIFIED, dateFormatter.format(new Date(fileToCache.lastModified())));
}
示例6: sendTextResource
import io.netty.handler.codec.http.HttpHeaderNames; //導入依賴的package包/類
private void sendTextResource(String prepend, String name, String mimeType, FullHttpRequest req, ChannelHandlerContext ctx) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader((this.getResourceAsStream(name))));
// TODO: read only once and buffer
String line;
StringBuffer buffer = new StringBuffer();
if (prepend != null) buffer.append(prepend);
while ((line = reader.readLine()) != null) {
buffer.append(line);
buffer.append('\n');
}
ByteBuf content = Unpooled.copiedBuffer(buffer, java.nio.charset.Charset.forName("UTF-8"));
FullHttpResponse res = new DefaultFullHttpResponse(HTTP_1_1, OK, content);
res.headers().set(HttpHeaderNames.CONTENT_TYPE, mimeType);
HttpUtil.setContentLength(res, content.readableBytes());
sendHttpResponse(ctx, req, res);
}
示例7: startDownTask
import io.netty.handler.codec.http.HttpHeaderNames; //導入依賴的package包/類
public static void startDownTask(TaskInfo taskInfo, HttpRequest httpRequest,
HttpResponse httpResponse, Channel clientChannel) {
HttpHeaders httpHeaders = httpResponse.headers();
HttpDownInfo httpDownInfo = new HttpDownInfo(taskInfo, httpRequest);
HttpDownServer.DOWN_CONTENT.put(taskInfo.getId(), httpDownInfo);
httpHeaders.clear();
httpResponse.setStatus(HttpResponseStatus.OK);
httpHeaders.set(HttpHeaderNames.CONTENT_TYPE, "text/html");
String host = HttpDownServer.isDev() ? "localhost"
: ((InetSocketAddress) clientChannel.localAddress()).getHostString();
String js =
"<script>window.top.location.href='http://" + host + ":" + HttpDownServer.VIEW_SERVER_PORT
+ "/#/tasks/new/" + httpDownInfo
.getTaskInfo().getId()
+ "';</script>";
HttpContent content = new DefaultLastHttpContent();
content.content().writeBytes(js.getBytes());
httpHeaders.set(HttpHeaderNames.CONTENT_LENGTH, js.getBytes().length);
clientChannel.writeAndFlush(httpResponse);
clientChannel.writeAndFlush(content);
clientChannel.close();
}
示例8: getDownFileSize
import io.netty.handler.codec.http.HttpHeaderNames; //導入依賴的package包/類
/**
* 取下載文件的總大小
*/
public static long getDownFileSize(HttpHeaders resHeaders) {
String contentRange = resHeaders.get(HttpHeaderNames.CONTENT_RANGE);
if (contentRange != null) {
Pattern pattern = Pattern.compile("^[^\\d]*(\\d+)-(\\d+)/.*$");
Matcher matcher = pattern.matcher(contentRange);
if (matcher.find()) {
long startSize = Long.parseLong(matcher.group(1));
long endSize = Long.parseLong(matcher.group(2));
return endSize - startSize + 1;
}
} else {
String contentLength = resHeaders.get(HttpHeaderNames.CONTENT_LENGTH);
if (contentLength != null) {
return Long.valueOf(resHeaders.get(HttpHeaderNames.CONTENT_LENGTH));
}
}
return 0;
}
示例9: setDateAndCacheHeaders
import io.netty.handler.codec.http.HttpHeaderNames; //導入依賴的package包/類
/**
* Sets the Date and Cache headers for the HTTP Response
*
* @param response HTTP response
* @param fileToCache file to extract content type
*/
private void setDateAndCacheHeaders(HttpResponse response, File fileToCache) {
SimpleDateFormat dateFormatter = new SimpleDateFormat(HTTP_DATE_FORMAT, Locale.US);
dateFormatter.setTimeZone(TimeZone.getTimeZone(HTTP_DATE_GMT_TIMEZONE));
// Date header
Calendar time = new GregorianCalendar();
response.headers().set(HttpHeaderNames.DATE, dateFormatter.format(time.getTime()));
// Add cache headers
time.add(Calendar.SECOND, HTTP_CACHE_SECONDS);
response.headers().set(HttpHeaderNames.EXPIRES, dateFormatter.format(time.getTime()));
response.headers().set(HttpHeaderNames.CACHE_CONTROL, "private, max-age=" + HTTP_CACHE_SECONDS);
response.headers().set(
HttpHeaderNames.LAST_MODIFIED, dateFormatter.format(new Date(fileToCache.lastModified())));
}
示例10: testShouldFetchDataWithHeaders
import io.netty.handler.codec.http.HttpHeaderNames; //導入依賴的package包/類
@Test
public void testShouldFetchDataWithHeaders(TestContext testContext) throws Exception {
final String fakeEurekaServerUrl = "http://localhost:" + FAKE_EUREKA_SERVER_PORT + "/eureka/v2/apps";
final String dashboardProxyUrl = DASHBOARD_EUREKA_PROXY_URL + fakeEurekaServerUrl;
final Async fetchData = testContext.async();
httpClient.getNow(dashboardProxyUrl, resp -> resp.bodyHandler(buffer -> {
final String responseData = buffer.toString(StandardCharsets.UTF_8);
if (resp.statusCode() != 200) {
testContext.fail("Response Status => " + resp.statusCode() + "\nResponse: " + responseData);
} else {
testContext.assertTrue("application/xml".equals(resp.getHeader(HttpHeaderNames.CONTENT_TYPE)));
testContext.assertTrue(responseData.contains("<apps__hashcode>UP_2_</apps__hashcode>"));
testContext.assertTrue(responseData.contains("<registrationTimestamp>1472352522224</registrationTimestamp>"));
fetchData.complete();
}
}));
fetchData.awaitSuccess(5000L);
}
開發者ID:kennedyoliveira,項目名稱:standalone-hystrix-dashboard,代碼行數:25,代碼來源:HystrixDashboardProxyEurekaTest.java
示例11: generate
import io.netty.handler.codec.http.HttpHeaderNames; //導入依賴的package包/類
@Override
public HttpResponse generate(JsonObject value) {
ImmutableMap.Builder<String, Object> builder = ImmutableMap.builder();
fields.forEach(field -> {
builder.put(field, value.getValue(field));
});
return new HttpResponse(
HttpResponseStatus.CREATED.code(),
ImmutableMap.of(
HttpHeaderNames.CONTENT_TYPE.toString(), ServerUtils.APPLICATION_JSON_UTF_8
),
new JsonObject(builder.build()).encode()
);
}
示例12: buildHeaders
import io.netty.handler.codec.http.HttpHeaderNames; //導入依賴的package包/類
private MultiMap buildHeaders(String hostHeader, MultiMap headers) {
MultiMap result = filteredHeaders(headers);
if (customRequestHeader.containsKey("name") && customRequestHeader.containsKey("value")) {
result.set(
customRequestHeader.getString("name"),
customRequestHeader.getString("value")
);
}
//Overide host header if provided in client destination
if (StringUtils.isNotBlank(hostHeader)) {
result.set(HttpHeaderNames.HOST.toString(), hostHeader);
}
return result;
}
示例13: writeResourceReport
import io.netty.handler.codec.http.HttpHeaderNames; //導入依賴的package包/類
private void writeResourceReport(Channel channel) {
ByteBuf content = Unpooled.buffer();
Writer writer = new OutputStreamWriter(new ByteBufOutputStream(content), CharsetUtil.UTF_8);
try {
reportAdapter.toJson(resourceReport.get(), writer);
writer.close();
} catch (IOException e) {
LOG.error("error writing resource report", e);
writeAndClose(channel, new DefaultFullHttpResponse(
HttpVersion.HTTP_1_1, HttpResponseStatus.INTERNAL_SERVER_ERROR,
Unpooled.copiedBuffer(e.getMessage(), StandardCharsets.UTF_8)));
return;
}
FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, content);
HttpUtil.setContentLength(response, content.readableBytes());
response.headers().set(HttpHeaderNames.CONTENT_TYPE, "application/json; charset=UTF-8");
channel.writeAndFlush(response);
}
示例14: setDateAndCacheHeaders
import io.netty.handler.codec.http.HttpHeaderNames; //導入依賴的package包/類
/**
* Sets the Date and Cache headers for the HTTP Response
*
* @param response
* HTTP response
* @param fileToCache
* file to extract content type
*/
private static void setDateAndCacheHeaders(HttpResponse response, File fileToCache) {
SimpleDateFormat dateFormatter = new SimpleDateFormat(HTTP_DATE_FORMAT, Locale.US);
dateFormatter.setTimeZone(TimeZone.getTimeZone(HTTP_DATE_GMT_TIMEZONE));
// Date header
Calendar time = new GregorianCalendar();
response.headers().set(HttpHeaderNames.DATE, dateFormatter.format(time.getTime()));
// Add cache headers
time.add(Calendar.SECOND, HTTP_CACHE_SECONDS);
response.headers().set(HttpHeaderNames.EXPIRES, dateFormatter.format(time.getTime()));
response.headers().set(HttpHeaderNames.CACHE_CONTROL, "private, max-age=" + HTTP_CACHE_SECONDS);
response.headers().set(
HttpHeaderNames.LAST_MODIFIED, dateFormatter.format(new Date(fileToCache.lastModified())));
}
示例15: writeResponseHead
import io.netty.handler.codec.http.HttpHeaderNames; //導入依賴的package包/類
@Override
public void writeResponseHead(Response restletResponse) throws IOException {
setNettyResponse(new DefaultHttpResponse(HTTP_1_1, new HttpResponseStatus(getStatusCode(), getReasonPhrase())));
HttpHeaders headers = getNettyResponse().headers();
// this.response.clear();
for (Header header : getResponseHeaders()) {
headers.add(header.getName(), header.getValue());
}
// Decide whether to close the connection or not.
if (isKeepAlive()) {
headers.set(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE);
getNettyChannel().write(getNettyResponse());
} else {
getNettyChannel().writeAndFlush(getNettyResponse()).addListener(ChannelFutureListener.CLOSE);
}
}