// Path: commons/src/main/java/org/nesty/commons/constant/NestyConstants.java
// public class NestyConstants {
// 
//     public static final String NESTY_SERVER = "Nesty";
// }
// 
// Path: commons/src/main/java/org/nesty/commons/constant/http/HttpConstants.java
// public class HttpConstants {
// 
//     public static final String HEADER_X_FORWARDED_FOR = "X-Forwarded-For";
//     public static final String HEADER_CONTENT_LENGTH = "Content-Length";
//     public static final String HEADER_CONTENT_TYPE = "Content-Type";
//     public static final String HEADER_CONNECTION = "Connection";
// 
//     public static final String HEADER_SERVER = "Server";
//     public static final String HEADER_REQUEST_ID = "Request-Id";
//     public static final String HEADER_CONNECTION_KEEPALIVE = "keep-alive";
//     public static final String HEADER_CONNECTION_CLOSE = "close";
//     public static final String HEADER_CONTENT_TYPE_TEXT = "application/text";
//     public static final String HEADER_CONTENT_TYPE_JSON = "application/json";
// }
// 
// Path: commons/src/main/java/org/nesty/commons/utils/SerializeUtils.java
// public class SerializeUtils {
// 
//     private static final Charset CHARSET = Charsets.UTF_8;
// 
//     private static final Gson GSON = new GsonBuilder().create();
// 
//     public static byte[] encode(Object stuff) {
//         return GSON.toJson(stuff).getBytes(CHARSET);
//     }
// 
//     public static Object decode(String stuff, Class<?> clazz) throws SerializeException {
//         try {
//             return GSON.fromJson(stuff, clazz);
//         } catch (JsonSyntaxException e) {
//             throw new SerializeException(String.format("%s decode exception", stuff));
//         }
//     }
// }
// 
// Path: core/src/main/java/org/nesty/core/server/rest/HttpContext.java
// public class HttpContext extends HttpSession {
//     private static String NestyUID = UUID.randomUUID().toString();
//     private static AtomicLong SequenceId = new AtomicLong(0);
// 
//     // raw body
//     protected String httpBody;
//     // params include query string and body param
//     protected Map<String, String> httpParams;
//     // attribute map values
//     protected Map<String, Object> httpAttributes;
//     // header values
//     protected Map<String, String> httpHeaders;
//     // client request with unique id, fetch it from http header(Request-Id)
//     // if exsit. or generate by UUID
//     private String requestId = String.format("%s-%d", NestyUID, SequenceId.incrementAndGet());
//     // ip address from origin client, fetch it from getRemoteAddr()
//     // or header X-FORWARDED-FOR
//     private String remoteAddress;
//     //  raw uri exclude query string
//     private String uri;
//     // http uri terms split by "/"
//     private String[] terms;
//     // Http request method. NOT null
//     private RequestMethod requestMethod;
//     // Http long connection
//     private boolean isKeepAlive = true;
// 
//     private HttpContext() {
//     }
// 
//     public static HttpContext build(HttpRequestVisitor visitor) {
//         HttpContext context = new HttpContext();
//         context.remoteAddress = visitor.visitRemoteAddress();
//         context.uri = visitor.visitURI();
//         context.terms = visitor.visitTerms();
//         context.requestMethod = visitor.visitHttpMethod();
//         context.httpHeaders = visitor.visitHttpHeaders();
//         context.httpParams = visitor.visitHttpParams();
// 
//         // TODO : if exclude GET or not ?
//         //
//         context.httpBody = visitor.visitHttpBody();
// 
//         if (visitor.visitHttpVersion() == HttpVersion.HTTP_1_1 &&
//                 HttpConstants.HEADER_CONNECTION_CLOSE.equals(context.httpHeaders.get(HttpConstants.HEADER_CONNECTION)))
//             context.isKeepAlive = false;
// 
//         if (visitor.visitHttpVersion() == HttpVersion.HTTP_1_0 &&
//                 !HttpConstants.HEADER_CONNECTION_KEEPALIVE.equalsIgnoreCase(context.httpHeaders.get(HttpConstants.HEADER_CONNECTION)))
//             context.isKeepAlive = false;
// 
//         return context;
//     }
// 
//     public boolean isKeepAlive() {
//         return isKeepAlive;
//     }
// 
//     public String getRemoteAddress() {
//         return remoteAddress;
//     }
// 
//     public String getUri() {
//         return uri;
//     }
// 
//     public String[] getTerms() {
//         return terms;
//     }
// 
//     public RequestMethod getRequestMethod() {
//         return requestMethod;
//     }
// 
//     public String getHttpBody() {
//         return httpBody;
//     }
// 
//     @Override
//     public Map<String, String> getHttpParams() {
//         return httpParams;
//     }
// 
//     public Map<String, String> getHttpHeaders() {
//         return httpHeaders;
//     }
// 
//     @Override
//     public String getRequestId() {
//         return requestId;
//     }
// 
//     @Override
//     public Map<String, Object> getHttpAttributes() {
//         return httpAttributes;
//     }
// }


import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.handler.codec.http.DefaultFullHttpResponse;
import io.netty.handler.codec.http.HttpResponseStatus;
import io.netty.handler.codec.http.HttpVersion;
import org.nesty.commons.constant.NestyConstants;
import org.nesty.commons.constant.http.HttpConstants;
import org.nesty.commons.utils.SerializeUtils;
import org.nesty.core.server.rest.HttpContext;
package org.nesty.core.server.rest.response;


/**
 * Build an DefaultFullHttpResponse {@link DefaultFullHttpResponse}
 *
 * [Author] Michael
 * [Date] March 04, 2016
 */
public class HttpResponseBuilder {

    // if no content in DefaultFullHttpResponse we will fill empty body
    private static final byte[] uselessBuffer = SerializeUtils.encode(new EMPTY());

// Path: commons/src/main/java/org/nesty/commons/constant/NestyConstants.java
// public class NestyConstants {
// 
//     public static final String NESTY_SERVER = "Nesty";
// }
// 
// Path: commons/src/main/java/org/nesty/commons/constant/http/HttpConstants.java
// public class HttpConstants {
// 
//     public static final String HEADER_X_FORWARDED_FOR = "X-Forwarded-For";
//     public static final String HEADER_CONTENT_LENGTH = "Content-Length";
//     public static final String HEADER_CONTENT_TYPE = "Content-Type";
//     public static final String HEADER_CONNECTION = "Connection";
// 
//     public static final String HEADER_SERVER = "Server";
//     public static final String HEADER_REQUEST_ID = "Request-Id";
//     public static final String HEADER_CONNECTION_KEEPALIVE = "keep-alive";
//     public static final String HEADER_CONNECTION_CLOSE = "close";
//     public static final String HEADER_CONTENT_TYPE_TEXT = "application/text";
//     public static final String HEADER_CONTENT_TYPE_JSON = "application/json";
// }
// 
// Path: commons/src/main/java/org/nesty/commons/utils/SerializeUtils.java
// public class SerializeUtils {
// 
//     private static final Charset CHARSET = Charsets.UTF_8;
// 
//     private static final Gson GSON = new GsonBuilder().create();
// 
//     public static byte[] encode(Object stuff) {
//         return GSON.toJson(stuff).getBytes(CHARSET);
//     }
// 
//     public static Object decode(String stuff, Class<?> clazz) throws SerializeException {
//         try {
//             return GSON.fromJson(stuff, clazz);
//         } catch (JsonSyntaxException e) {
//             throw new SerializeException(String.format("%s decode exception", stuff));
//         }
//     }
// }
// 
// Path: core/src/main/java/org/nesty/core/server/rest/HttpContext.java
// public class HttpContext extends HttpSession {
//     private static String NestyUID = UUID.randomUUID().toString();
//     private static AtomicLong SequenceId = new AtomicLong(0);
// 
//     // raw body
//     protected String httpBody;
//     // params include query string and body param
//     protected Map<String, String> httpParams;
//     // attribute map values
//     protected Map<String, Object> httpAttributes;
//     // header values
//     protected Map<String, String> httpHeaders;
//     // client request with unique id, fetch it from http header(Request-Id)
//     // if exsit. or generate by UUID
//     private String requestId = String.format("%s-%d", NestyUID, SequenceId.incrementAndGet());
//     // ip address from origin client, fetch it from getRemoteAddr()
//     // or header X-FORWARDED-FOR
//     private String remoteAddress;
//     //  raw uri exclude query string
//     private String uri;
//     // http uri terms split by "/"
//     private String[] terms;
//     // Http request method. NOT null
//     private RequestMethod requestMethod;
//     // Http long connection
//     private boolean isKeepAlive = true;
// 
//     private HttpContext() {
//     }
// 
//     public static HttpContext build(HttpRequestVisitor visitor) {
//         HttpContext context = new HttpContext();
//         context.remoteAddress = visitor.visitRemoteAddress();
//         context.uri = visitor.visitURI();
//         context.terms = visitor.visitTerms();
//         context.requestMethod = visitor.visitHttpMethod();
//         context.httpHeaders = visitor.visitHttpHeaders();
//         context.httpParams = visitor.visitHttpParams();
// 
//         // TODO : if exclude GET or not ?
//         //
//         context.httpBody = visitor.visitHttpBody();
// 
//         if (visitor.visitHttpVersion() == HttpVersion.HTTP_1_1 &&
//                 HttpConstants.HEADER_CONNECTION_CLOSE.equals(context.httpHeaders.get(HttpConstants.HEADER_CONNECTION)))
//             context.isKeepAlive = false;
// 
//         if (visitor.visitHttpVersion() == HttpVersion.HTTP_1_0 &&
//                 !HttpConstants.HEADER_CONNECTION_KEEPALIVE.equalsIgnoreCase(context.httpHeaders.get(HttpConstants.HEADER_CONNECTION)))
//             context.isKeepAlive = false;
// 
//         return context;
//     }
// 
//     public boolean isKeepAlive() {
//         return isKeepAlive;
//     }
// 
//     public String getRemoteAddress() {
//         return remoteAddress;
//     }
// 
//     public String getUri() {
//         return uri;
//     }
// 
//     public String[] getTerms() {
//         return terms;
//     }
// 
//     public RequestMethod getRequestMethod() {
//         return requestMethod;
//     }
// 
//     public String getHttpBody() {
//         return httpBody;
//     }
// 
//     @Override
//     public Map<String, String> getHttpParams() {
//         return httpParams;
//     }
// 
//     public Map<String, String> getHttpHeaders() {
//         return httpHeaders;
//     }
// 
//     @Override
//     public String getRequestId() {
//         return requestId;
//     }
// 
//     @Override
//     public Map<String, Object> getHttpAttributes() {
//         return httpAttributes;
//     }
// }


// Path: core/src/main/java/org/nesty/core/server/rest/response/HttpResponseBuilder.java
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.handler.codec.http.DefaultFullHttpResponse;
import io.netty.handler.codec.http.HttpResponseStatus;
import io.netty.handler.codec.http.HttpVersion;
import org.nesty.commons.constant.NestyConstants;
import org.nesty.commons.constant.http.HttpConstants;
import org.nesty.commons.utils.SerializeUtils;
import org.nesty.core.server.rest.HttpContext;

package org.nesty.core.server.rest.response;


/**
 * Build an DefaultFullHttpResponse {@link DefaultFullHttpResponse}
 *
 * [Author] Michael
 * [Date] March 04, 2016
 */
public class HttpResponseBuilder {

    // if no content in DefaultFullHttpResponse we will fill empty body
    private static final byte[] uselessBuffer = SerializeUtils.encode(new EMPTY());

    public static DefaultFullHttpResponse create(HttpContext httpContext, HttpResponseStatus status) {