/** * Build a {@link Response} instance * * @return a {@link Response} instance */ public Response build() { return status == null ? null : new NettyResponse(status, headers, bodyParts); }
@Override public String toString() { StringBuilder sb = new StringBuilder(); sb.append(getClass().getSimpleName()).append(" {\n") .append("\tstatusCode=").append(getStatusCode()).append("\n") .append("\theaders=\n"); for (Map.Entry<String, String> header : getHeaders()) { sb.append("\t\t").append(header.getKey()).append(": ").append(header.getValue()).append("\n"); } return sb.append("\tbody=\n").append(getResponseBody()).append("\n") .append("}").toString(); } }
@Override public byte[] getResponseBodyAsBytes() { return getResponseBodyAsByteBuffer().array(); }
@Override public String getResponseBody() { return getResponseBody(withDefault(extractContentTypeCharsetAttribute(getContentType()), UTF_8)); }
/** * {@inheritDoc} */ @Override public final T onCompleted() throws Exception { if (status != null) { Document document = null; if (status.getStatusCode() == 207) { document = readXMLResponse(new NettyResponse(status, headers, bodyParts).getResponseBodyAsStream()); } // recompute response as readXMLResponse->parse might have updated it return onCompleted(new WebDavResponse(new NettyResponse(status, headers, bodyParts), document)); } else { throw new IllegalStateException("Status is null"); } }
@Test public void testCookieParseMaxAge() { final String cookieDef = "efmembercheck=true; max-age=60; path=/; domain=.eclipse.org"; HttpHeaders responseHeaders = new DefaultHttpHeaders().add(SET_COOKIE, cookieDef); NettyResponse response = new NettyResponse(new NettyResponseStatus(null, null, null), responseHeaders, null); List<Cookie> cookies = response.getCookies(); assertEquals(cookies.size(), 1); Cookie cookie = cookies.get(0); assertEquals(cookie.maxAge(), 60); }
@Override public String getResponseBody() throws IOException { return getResponseBody(null); }
@Override public String getResponseBody(Charset charset) throws IOException { return new String(getResponseBodyAsBytes(), calculateCharset(charset)); }
public String getResponseBody(Charset charset) throws IOException { return getResponseBodyAsChannelBuffer().toString(calculateCharset(charset)); }
@Override public InputStream getResponseBodyAsStream() { return new ByteArrayInputStream(getResponseBodyAsBytes()); }
@Override public InputStream getResponseBodyAsStream() throws IOException { return new ChannelBufferInputStream(getResponseBodyAsChannelBuffer()); }
@Test public void testCookieParseExpires() { // e.g. "Tue, 27 Oct 2015 12:54:24 GMT"; SimpleDateFormat sdf = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.US); sdf.setTimeZone(TimeZone.getTimeZone("GMT")); Date date = new Date(System.currentTimeMillis() + 60000); final String cookieDef = String.format("efmembercheck=true; expires=%s; path=/; domain=.eclipse.org", sdf.format(date)); HttpHeaders responseHeaders = new DefaultHttpHeaders().add(SET_COOKIE, cookieDef); NettyResponse response = new NettyResponse(new NettyResponseStatus(null, null, null), responseHeaders, null); List<Cookie> cookies = response.getCookies(); assertEquals(cookies.size(), 1); Cookie cookie = cookies.get(0); assertTrue(cookie.maxAge() >= 58 && cookie.maxAge() <= 60); }
@Override public String getResponseBody() throws IOException { return getResponseBody(null); }
@Override public String getResponseBody(Charset charset) throws IOException { return new String(getResponseBodyAsBytes(), calculateCharset(charset)); }
public String getResponseBody(Charset charset) throws IOException { return getResponseBodyAsChannelBuffer().toString(calculateCharset(charset)); }
@Override public String getResponseBody(Charset charset) { return new String(getResponseBodyAsBytes(), charset); }
@Override public byte[] getResponseBodyAsBytes() throws IOException { return channelBuffer2bytes(getResponseBodyAsChannelBuffer()); }
@Override public Response prepareResponse(HttpResponseHeaders headers, List<HttpResponseBodyPart> bodyParts) { return new NettyResponse(this, headers, bodyParts); }
@Test public void testCookieParseWeirdExpiresValue() { final String cookieDef = "efmembercheck=true; expires=60; path=/; domain=.eclipse.org"; HttpHeaders responseHeaders = new DefaultHttpHeaders().add(SET_COOKIE, cookieDef); NettyResponse response = new NettyResponse(new NettyResponseStatus(null, null, null), responseHeaders, null); List<Cookie> cookies = response.getCookies(); assertEquals(cookies.size(), 1); Cookie cookie = cookies.get(0); assertEquals(cookie.maxAge(), Long.MIN_VALUE); } }
@Override public String getResponseBody() throws IOException { return getResponseBody(null); }