public static String extractCookie(HttpResponse response) {
    if (response.containsHeader(HttpHeaders.Names.SET_COOKIE)) {
      return response.getHeader(HttpHeaders.Names.SET_COOKIE);
    }

    return null;
  }
Example #2
0
  @Override
  public Object encode(ChannelHandlerContext ctx, Object msg) throws Exception {

    List<Object> out = new ArrayList<Object>();
    if (msg instanceof HttpRequest) {

      HttpRequest httpRequest = (HttpRequest) msg;
      SpdySynStreamFrame spdySynStreamFrame = createSynStreamFrame(httpRequest);
      int streamID = spdySynStreamFrame.getStreamID();
      out.add(spdySynStreamFrame);
      addContent(out, streamID, httpRequest);

    } else if (msg instanceof HttpResponse) {

      HttpResponse httpResponse = (HttpResponse) msg;
      if (httpResponse.containsHeader(SpdyHttpHeaders.Names.ASSOCIATED_TO_STREAM_ID)) {
        SpdySynStreamFrame spdySynStreamFrame = createSynStreamFrame(httpResponse);
        int streamID = spdySynStreamFrame.getStreamID();
        out.add(spdySynStreamFrame);
        addContent(out, streamID, httpResponse);
      } else {
        SpdySynReplyFrame spdySynReplyFrame = createSynReplyFrame(httpResponse);
        int streamID = spdySynReplyFrame.getStreamID();
        out.add(spdySynReplyFrame);
        addContent(out, streamID, httpResponse);
      }

    } else if (msg instanceof HttpChunk) {

      HttpChunk chunk = (HttpChunk) msg;
      SpdyDataFrame spdyDataFrame = new DefaultSpdyDataFrame(currentStreamID);
      spdyDataFrame.setData(chunk.getContent());
      spdyDataFrame.setLast(chunk.isLast());

      if (chunk instanceof HttpChunkTrailer) {
        HttpChunkTrailer trailer = (HttpChunkTrailer) chunk;
        List<Map.Entry<String, String>> trailers = trailer.getHeaders();
        if (trailers.isEmpty()) {
          out.add(spdyDataFrame);
        } else {
          // Create SPDY HEADERS frame out of trailers
          SpdyHeadersFrame spdyHeadersFrame = new DefaultSpdyHeadersFrame(currentStreamID);
          for (Map.Entry<String, String> entry : trailers) {
            spdyHeadersFrame.addHeader(entry.getKey(), entry.getValue());
          }

          // Write HEADERS frame and append Data Frame
          out.add(spdyHeadersFrame);
          out.add(spdyDataFrame);
        }
      } else {
        out.add(spdyDataFrame);
      }
    } else {
      throw new UnsupportedMessageTypeException(msg);
    }

    return out.toArray();
  }