/** * Adds a collection of HTTP headers to the current list of headers. * * @param headers * the collection of headers * @return this RestData */ public RestData addHeaders(Map<String, String> headers) { for (Map.Entry<String, String> e : headers.entrySet()) { addHeader(e.getKey(), e.getValue()); } return this; }
/** * @return the body of this http request/response */ public String getBody() { if(raw == null) { return null; } try { return new String(raw, getCharset()); } catch (UnsupportedEncodingException e) { throw new IllegalStateException("Unsupported encoding: " + getCharset()); } }
public String getContentLength() { return getHeaderValue("Content-Length"); }
/** * A visually easy to read representation of this {@code RestData}. * * It tryes to match the typical Http Request/Response */ @Override public String toString() { StringBuilder builder = new StringBuilder(); for (Header h : getHeaders()) { builder.append(h).append(LINE_SEPARATOR); } if (raw != null) { builder.append(LINE_SEPARATOR); builder.append(this.getBody()); } else { builder.append("[empty/null body]"); } return builder.toString(); }
/** * @return string representation of this response */ public String toString() { StringBuilder builder = new StringBuilder(); if (getStatusCode() != null) builder.append(String.format("[%s] %s", this.getStatusCode(), this.getStatusText())); builder.append(LINE_SEPARATOR); builder.append(super.toString()); return builder.toString(); }
public RestData setBody(String body) { if(body == null) { setRawBody(null); } else { setRawBody(body.getBytes()); } return this; }
public String getHeaderValue(String name) { List<Header> headers = getHeader(name); if(headers.size() > 0) { return headers.get(0).getValue(); } return null; }
/** * String representation of this request. * * @return the string representation */ public String toString() { StringBuilder builder = new StringBuilder(); if (getMethod() != null) builder.append(getMethod().toString()).append(" "); if (getResource() != null) builder.append(this.getResource()); if (getQuery() != null) builder.append("?").append(this.getQuery()); builder.append(LINE_SEPARATOR); builder.append(super.toString()); return builder.toString(); } }
public String getContentType() { return getHeaderValue("Content-Type"); }
/** * Adds a collection of HTTP headers to the current list of headers. * * @param headers * the list of headers * @return this RestData */ public RestData addHeaders(List<Header> headers) { for (Header e : headers) { addHeader(e.getName(), e.getValue()); } return this; }
public String getCharset() { String v = getHeaderValue("Content-Type"); if(v == null || !v.contains("charset")) { return DEFAULT_ENCODING; } int pos = v.indexOf("charset"); pos = v.indexOf("=", pos); try { String substring = v.substring(pos + 1); return substring.trim(); } catch(RuntimeException e) { return DEFAULT_ENCODING; } }