List of usage examples for io.netty.handler.codec.http HttpHeaderValues ZERO
AsciiString ZERO
To view the source code for io.netty.handler.codec.http HttpHeaderValues ZERO.
Click Source Link
From source file:org.iotivity.cloud.base.protocols.http.HCProxyProcessor.java
License:Open Source License
/** * This function returns a created HTTP error response * created by an error information./*w ww .j a v a2 s . c o m*/ * * @param errorStatusCode HTTP status code and reason phrase * @return httpResponse */ public HttpResponse getErrorResponse(String errorStatusCode) { String[] responseStatus = errorStatusCode.split(Pattern.quote(" "), 2); int httpCode = Integer.valueOf(responseStatus[0]); String reasonPhrase = responseStatus[1]; HttpVersion httpVersion = HttpVersion.HTTP_1_1; HttpResponseStatus httpStatus = new HttpResponseStatus(httpCode, reasonPhrase); HttpResponse httpResponse = new DefaultHttpResponse(httpVersion, httpStatus); // Default: Non-persistent connection httpResponse.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.CLOSE); // Some clients requires content-length=0 // to decide a proper size of one response message. httpResponse.headers().set(HttpHeaderNames.CONTENT_TYPE, HttpHeaderValues.NONE); httpResponse.headers().set(HttpHeaderNames.CONTENT_LENGTH, HttpHeaderValues.ZERO); return httpResponse; }
From source file:org.iotivity.cloud.base.protocols.http.HCProxyProcessor.java
License:Open Source License
/** * This function returns a created HTTP response * translated from the CoAP response./*from w w w. j ava 2 s .co m*/ * * @param response * @return httpResponse */ public HttpResponse getHttpResponse(IResponse response) { HttpResponse httpResponse = null; ResponseStatus coapResponseStatus = response.getStatus(); ContentFormat coapContentFormat = response.getContentFormat(); byte[] coapPayload = response.getPayload(); boolean isPayload = false; HttpVersion httpVersion = HttpVersion.HTTP_1_1; if (coapPayload != null && coapPayload.length > 0) { isPayload = true; HttpResponseStatus httpStatus = getHttpResponseStatus(coapResponseStatus, isPayload); String httpContentType = null; ByteBuf httpContent = Unpooled.EMPTY_BUFFER; if (coapContentFormat == ContentFormat.APPLICATION_CBOR) { // Content-Type: application/json httpContentType = APPLICATION_JSON; String payloadString = null; try { // Cbor: Map to JSON string if (getTargetCoapPath().contains(DISCOVER_URI)) { Log.v("Cbor decoding using ArrayList.class"); ArrayList<Object> cborMap = mStackCborArray.parsePayloadFromCbor(coapPayload, ArrayList.class); ObjectMapper cborMapper = new ObjectMapper(); payloadString = cborMapper.writerWithDefaultPrettyPrinter().writeValueAsString(cborMap); } else { Log.v("Cbor decoding using HashMap.class"); HashMap<String, Object> cborMap = mStackCborMap.parsePayloadFromCbor(coapPayload, HashMap.class); ObjectMapper cborMapper = new ObjectMapper(); payloadString = cborMapper.writerWithDefaultPrettyPrinter().writeValueAsString(cborMap); } } catch (JsonProcessingException e) { e.printStackTrace(); } if (payloadString == null || payloadString.trim().equalsIgnoreCase("null")) { payloadString = ""; } byte[] payloadBytes = payloadString.getBytes(); httpContent = Unpooled.wrappedBuffer(payloadBytes); } else { if (response instanceof CoapResponse) { // Content-Type: // application/coap-payload;cf=<Content-Format value> httpContentType = COAP_FORMAT_TYPE + ((CoapResponse) response).getContentFormatValue(); } else { httpContentType = coapContentFormat.toString(); } httpContent = Unpooled.wrappedBuffer(coapPayload); } httpResponse = new DefaultFullHttpResponse(httpVersion, httpStatus, httpContent); // Default: Non-persistent connection httpResponse.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.CLOSE); httpResponse.headers().set(HttpHeaderNames.CONTENT_TYPE, httpContentType); httpResponse.headers().set(HttpHeaderNames.CONTENT_LENGTH, httpContent.readableBytes()); byte[] bytes = new byte[httpContent.readableBytes()]; httpContent.getBytes(httpContent.readerIndex(), bytes); StringBuilder contentStrBuilder = new StringBuilder(new String(bytes)); mCtx.channel().attr(HCProxyProcessor.ctxStrContent).set(contentStrBuilder); } else { isPayload = false; HttpResponseStatus httpStatus = getHttpResponseStatus(coapResponseStatus, isPayload); httpResponse = new DefaultFullHttpResponse(httpVersion, httpStatus); // Default: Non-persistent connection httpResponse.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.CLOSE); // Some clients requires content-length=0 // to decide a proper size of one response message. httpResponse.headers().set(HttpHeaderNames.CONTENT_TYPE, HttpHeaderValues.NONE); httpResponse.headers().set(HttpHeaderNames.CONTENT_LENGTH, HttpHeaderValues.ZERO); } /* * If HTTP request was sign-in and HTTP response is 200 OK, * then set HTTP Cookie in the response for the session. */ if (mTargetCoapPath != null && mTargetCoapPath.contains(SIGNIN_URI) && httpResponse.status() == HttpResponseStatus.OK) { long timeoutSeconds = SESSION_TIMEOUT; String createdSid = setSessionId(httpResponse, timeoutSeconds); // Set SessionExpiredTime in HttpDevice // and put it into httpDeviceMap using the session id as a key Device device = mCtx.channel().attr(ServerSystem.keyDevice).get(); if (device != null) { ((HttpDevice) device).setSessionExpiredTime(timeoutSeconds); HttpServer.httpDeviceMap.put(createdSid, (HttpDevice) device); mCtx.channel().attr(ServerSystem.keyDevice).set(device); } } // Put HttpDevice into httpDeviceMap using the session id as a key HttpDevice httpDevice = (HttpDevice) mCtx.channel().attr(ServerSystem.keyDevice).get(); if (httpDevice != null && mSessionId != null) { HttpServer.httpDeviceMap.put(mSessionId, httpDevice); } return httpResponse; }