protected void encode(ChannelHandlerContext ctx, RestResponse response, List<Object> out) throws Exception { final ByteString entity = response.getEntity(); ByteBuf content = Unpooled.wrappedBuffer(entity.asByteBuffer()); HttpResponse nettyResponse = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.valueOf(response.getStatus()), content); for (Map.Entry<String, String> e : response.getHeaders().entrySet()) { nettyResponse.headers().set(e.getKey(), e.getValue()); } nettyResponse.headers().set(HttpConstants.RESPONSE_COOKIE_HEADER_NAME, response.getCookies()); nettyResponse.headers().set(HttpHeaderNames.CONTENT_LENGTH, entity.length()); out.add(nettyResponse); } }
for (String cookie : restResponse.getCookies())
public Response<T> decodeResponse(RestResponse restResponse) throws RestLiDecodingException { return createResponse(restResponse.getHeaders(), restResponse.getStatus(), restResponse.getEntity(), restResponse.getCookies()); }
private static StreamResponse createStreamResponseWithAttachment(RestResponse structuredFirstPart, RestLiResponseAttachments attachments) { //Construct the StreamResponse and invoke the callback. The RestResponse entity should be the first part. //There may potentially be attachments included in the response. Note that unlike the client side request builders, //here it is possible to have a non-null attachment list with 0 attachments due to the way the builder in //RestLiResponseAttachments works. Therefore we have to make sure its a non zero size as well. final ByteStringWriter firstPartWriter = new ByteStringWriter(structuredFirstPart.getEntity()); final MultiPartMIMEWriter multiPartMIMEWriter = AttachmentUtils.createMultiPartMIMEWriter(firstPartWriter, structuredFirstPart.getHeader(RestConstants.HEADER_CONTENT_TYPE), attachments.getMultiPartMimeWriterBuilder()); //Ensure that any headers or cookies from the RestResponse make into the outgoing StreamResponse. The exception //of course being the Content-Type header which will be overridden by MultiPartMIMEStreamResponseFactory. return MultiPartMIMEStreamResponseFactory.generateMultiPartMIMEStreamResponse(AttachmentUtils.RESTLI_MULTIPART_SUBTYPE, multiPartMIMEWriter, Collections.emptyMap(), structuredFirstPart.getHeaders(), structuredFirstPart.getStatus(), structuredFirstPart.getCookies()); }
@SuppressWarnings("unchecked") private Response<T> createResponseFromError(RestLiResponseException restLiResponseException) throws RemoteInvocationException { Response<T> response = null; // If the exception contains a decoded response, we use it. Otherwise we do manual response // creation which will not have an entity. if (restLiResponseException.hasDecodedResponse()) { response = new ResponseImpl<T>( (Response<T>) restLiResponseException.getDecodedResponse(), restLiResponseException); } else { response = new ResponseImpl<T>( restLiResponseException.getStatus(), restLiResponseException.getResponse().getHeaders(), CookieUtil.decodeSetCookies(restLiResponseException.getResponse().getCookies()), restLiResponseException); } return response; }
@Override protected Promise<? extends IndividualResponseWithCookies> run(Context context) throws Throwable { if (_restResponse.isFailed()) { return Promises.value(toErrorIndividualResponse(_restResponse.getError(), _errorResponseBuilder)); } try { RestResponse restResponse = _restResponse.get(); IndividualResponse response = toIndividualResponse(_restResponseId, restResponse); return Promises.value(new IndividualResponseWithCookies(response, restResponse.getCookies())); } catch (MimeTypeParseException e) { return Promises.value(createInternalServerErrorResponse("Invalid content type for individual response: " + _restResponseId, _errorResponseBuilder)); } catch (IOException e) { return Promises.value(createInternalServerErrorResponse("Unable to set body for individual response: " + _restResponseId, _errorResponseBuilder)); } catch(Exception e) { return Promises.value(toErrorIndividualResponse(e, _errorResponseBuilder)); } }