2626import org .jboss .netty .handler .codec .http .*;
2727import org .jboss .netty .util .CharsetUtil ;
2828
29+ import java .nio .charset .Charset ;
2930import java .text .DateFormat ;
3031import java .text .SimpleDateFormat ;
3132import java .util .Calendar ;
@@ -55,9 +56,7 @@ protected DateFormat initialValue() {
5556 private static String SERVER_HEADER_VALUE ;
5657
5758 public static void addAllowAnyOrigin (HttpResponse response ) {
58- response .setHeader ("Access-Control-Allow-Origin" , "*" );
59- response .setHeader ("Access-Control-Allow-Methods" , "GET, OPTIONS" );
60- response .setHeader ("Access-Control-Allow-Headers" , "*" );
59+ response .setHeader (HttpHeaders .Names .ACCESS_CONTROL_ALLOW_ORIGIN , "*" );
6160 }
6261
6362 public static void addDate (HttpResponse response ) {
@@ -88,10 +87,25 @@ public static void send(String contentType, CharSequence content, HttpRequest re
8887
8988 public static void send (HttpResponse response , HttpRequest request , ChannelHandlerContext context ) {
9089 ChannelBuffer content = response .getContent ();
91- if (content != ChannelBuffers .EMPTY_BUFFER ) {
92- setContentLength (response , content .readableBytes ());
90+ setContentLength (response , content == ChannelBuffers .EMPTY_BUFFER ? 0 : content .readableBytes ());
91+
92+ boolean keepAlive = addKeepAliveIfNeed (response , request );
93+ addCommonHeaders (response );
94+ send (response , context , !keepAlive );
95+ }
96+
97+ public static boolean addKeepAliveIfNeed (HttpResponse response , HttpRequest request ) {
98+ if (isKeepAlive (request )) {
99+ response .setHeader (CONNECTION , HttpHeaders .Values .KEEP_ALIVE );
100+ return true ;
93101 }
94- send (response , context , !isKeepAlive (request ));
102+ return false ;
103+ }
104+
105+ public static void addCommonHeaders (HttpResponse response ) {
106+ addServer (response );
107+ addDate (response );
108+ addAllowAnyOrigin (response );
95109 }
96110
97111 public static HttpResponse create (String contentType ) {
@@ -101,21 +115,17 @@ public static HttpResponse create(String contentType) {
101115 }
102116
103117 public static void send (CharSequence content , HttpRequest request , ChannelHandlerContext context ) {
104- send (new DefaultHttpResponse ( HTTP_1_1 , OK ), ChannelBuffers . copiedBuffer ( content , CharsetUtil .US_ASCII ) , request , context );
118+ send (content , CharsetUtil .US_ASCII , request , context );
105119 }
106120
107- public static void send (HttpResponse response , byte [] bytes , HttpRequest request , ChannelHandlerContext context ) {
108- send (response , ChannelBuffers .wrappedBuffer (bytes ), request , context );
121+ public static void send (CharSequence content , Charset charset , HttpRequest request , ChannelHandlerContext context ) {
122+ DefaultHttpResponse response = new DefaultHttpResponse (HTTP_1_1 , OK );
123+ response .setContent (ChannelBuffers .copiedBuffer (content , charset ));
124+ send (response , request , context );
109125 }
110126
111- public static void send (HttpResponse response , ChannelBuffer content , HttpRequest request , ChannelHandlerContext context ) {
112- if (isKeepAlive (request )) {
113- response .setHeader (CONNECTION , HttpHeaders .Values .KEEP_ALIVE );
114- }
115- response .setContent (content );
116- addServer (response );
117- addDate (response );
118- addAllowAnyOrigin (response );
127+ public static void send (byte [] bytes , HttpResponse response , HttpRequest request , ChannelHandlerContext context ) {
128+ response .setContent (ChannelBuffers .wrappedBuffer (bytes ));
119129 send (response , request , context );
120130 }
121131
@@ -134,21 +144,26 @@ public static void send(HttpResponse response, ChannelHandlerContext context, bo
134144 }
135145 }
136146
137- public static void sendError (HttpRequest request , ChannelHandlerContext context , HttpResponseStatus responseStatus ) {
138- sendError ( request , context , new DefaultHttpResponse (HTTP_1_1 , responseStatus ));
147+ public static void sendStatus (HttpRequest request , ChannelHandlerContext context , HttpResponseStatus responseStatus ) {
148+ sendStatus ( new DefaultHttpResponse (HTTP_1_1 , responseStatus ), request , context );
139149 }
140150
141- public static void sendError ( HttpRequest request , ChannelHandlerContext context , HttpResponse response ) {
151+ public static void sendStatus ( HttpResponse response , HttpRequest request , ChannelHandlerContext context ) {
142152 response .setHeader (CONTENT_TYPE , "text/html" );
143- addServer (response );
144- addDate (response );
145-
146- String message = response .getStatus ().toString ();
147153 if (request .getMethod () != HttpMethod .HEAD ) {
154+ String message = response .getStatus ().toString ();
148155 response .setContent (ChannelBuffers .copiedBuffer ("<!doctype html><title>" + message + "</title>" +
149156 "<h1 style=\" text-align: center\" >" + message + "</h1><hr/><p style=\" text-align: center\" >" + SERVER_HEADER_VALUE + "</p>" ,
150157 CharsetUtil .US_ASCII ));
151158 }
152159 send (response , request , context );
153160 }
161+
162+ public static void sendOptionsResponse (String allowHeaders , HttpRequest request , ChannelHandlerContext context ) {
163+ HttpResponse response = new DefaultHttpResponse (HTTP_1_1 , OK );
164+ response .setHeader (HttpHeaders .Names .ACCESS_CONTROL_ALLOW_ORIGIN , "*" );
165+ response .setHeader (HttpHeaders .Names .ACCESS_CONTROL_ALLOW_METHODS , allowHeaders );
166+ response .setHeader (HttpHeaders .Names .ALLOW , allowHeaders );
167+ send (response , request , context );
168+ }
154169}
0 commit comments