@Override public Object apply(ChainedHttpConfig requestConfig, FromServer fromServer) { try { final BiFunction<ChainedHttpConfig, FromServer, Object> parser = requestConfig.findParser(fromServer.getContentType()); final BiFunction<FromServer, Object, ?> action = requestConfig.getChainedResponse().actualAction(fromServer.getStatusCode()); return action.apply(fromServer, fromServer.getHasBody() ? parser.apply(requestConfig, fromServer) : null); } finally { fromServer.finish(); } } }
public List<FromServer.Header<?>> getHeaders() { return fromServer.getHeaders(); }
/** * Standard parser for responses with content type 'application/x-www-form-urlencoded'. * * @param fromServer Backend indenpendent representation of data returned from http server * @return Form data */ public static Map<String, List<String>> form(final ChainedHttpConfig config, final FromServer fromServer) { return Form.decode(fromServer.getInputStream(), fromServer.getCharset()); }
/** * Method that provides an HTML parser for response configuration (uses JSoup). * * @param config the chained configuration * @param fromServer the server response adapter * @return the parsed HTML content (a {@link Document} object) */ public static Object jsoupParse(final ChainedHttpConfig config, final FromServer fromServer) { try { return Jsoup.parse(fromServer.getInputStream(), fromServer.getCharset().name(), fromServer.getUri().toString()); } catch (IOException e) { throw new RuntimeException(e); } }
public static Object parse(final ChainedHttpConfig config, final FromServer fromServer) { try { final Csv.Context ctx = (Csv.Context) config.actualContext(fromServer.getContentType(), Csv.Context.ID); return ctx.makeReader(fromServer.getReader()).readAll(); } catch(IOException e) { throw new RuntimeException(e); } }
private static OutputStream streamParser(final ChainedHttpConfig config, final FromServer fs) { final OutputStream ostream = (OutputStream) config.actualContext(fs.getContentType(), ID); transfer(fs.getInputStream(), ostream, false); return ostream; } }
@Test @SuppressWarnings("unchecked") public void head_request_with_raw_function() throws Exception { ersatzServer.expectations(ex -> ex.head("/foo").responds().contentType("text/plain")).start(); http = HttpBuilder.configure(config -> { config.getRequest().setUri(ersatzServer.getHttpUrl()); }); BiFunction<FromServer, Object, Object> successFunction = (from, body) -> { assertFalse(from.getHasBody()); return from.getHeaders(); }; List<FromServer.Header> headers = (List<FromServer.Header>) http.head(List.class, config -> { config.getRequest().getUri().setPath("/foo"); config.getResponse().success(successFunction); }); assertEquals(4, headers.size()); assertEquals(headers.get(0).toString(), "Connection: keep-alive"); assertEquals(headers.get(1).toString(), "Content-Type: text/plain"); assertEquals(headers.get(2).toString(), "Content-Length: 0"); assertTrue(headers.get(3).toString().startsWith("Date: ")); }
/** * Retrieves a {@link Reader} for the response body content (if there is any). The content may have already been processed. * * @return a {@link Reader} for the response body content (may be empty) */ default Reader getReader() { return new BufferedReader(new InputStreamReader(getInputStream())); } }
public HttpException(final FromServer fromServer, final Object body) { super(fromServer.getMessage()); this.fromServer = fromServer; this.body = body; }
public int getStatusCode() { return fromServer.getStatusCode(); }
/** * Method that provides an HTML parser for response configuration (uses JSoup). * * @param config the chained configuration * @param fromServer the server response adapter * @return the parsed HTML content (a {@link Document} object) */ public static Object jsoupParse(final ChainedHttpConfig config, final FromServer fromServer) { try { return Jsoup.parse(fromServer.getInputStream(), fromServer.getCharset().name(), fromServer.getUri().toString()); } catch (IOException e) { throw new TransportingException(e); } }
/** * Used to parse the server response content using the Jackson JSON parser. * * @param config the configuration * @param fromServer the server content accessor * @return the parsed object */ @SuppressWarnings("WeakerAccess") public static Object parse(final ChainedHttpConfig config, final FromServer fromServer) { try { final ObjectMapper mapper = (ObjectMapper) config.actualContext(fromServer.getContentType(), OBJECT_MAPPER_ID); return mapper.readValue(fromServer.getReader(), config.getChainedResponse().getType()); } catch (IOException e) { throw new RuntimeException(e); } }
private static OutputStream streamParser(final ChainedHttpConfig config, final FromServer fs) { final OutputStream ostream = (OutputStream) config.actualContext(fs.getContentType(), ID); transfer(fs.getInputStream(), ostream, false); return ostream; } }
config.getRequest().getUri().setPath("/foo"); config.getResponse().success((from, o) -> { assertFalse(from.getHasBody()); return from.getHeaders(); }); });
/** * Retrieves a {@link Reader} for the response body content (if there is any). The content may have already been processed. * * @return a {@link Reader} for the response body content (may be empty) */ default Reader getReader() { return new BufferedReader(new InputStreamReader(getInputStream())); } }
public HttpException(final FromServer fromServer, final Object body) { super(fromServer.getMessage()); this.fromServer = fromServer; this.body = body; }
public int getStatusCode() { return fromServer.getStatusCode(); }
/** * Standard parser for responses with content type 'application/x-www-form-urlencoded'. * * @param fromServer Backend indenpendent representation of data returned from http server * @return Form data */ public static Map<String, List<String>> form(final ChainedHttpConfig config, final FromServer fromServer) { return Form.decode(fromServer.getInputStream(), fromServer.getCharset()); }
@Override public Object apply(ChainedHttpConfig requestConfig, FromServer fromServer) { try { final BiFunction<ChainedHttpConfig, FromServer, Object> parser = requestConfig.findParser(fromServer.getContentType()); final BiFunction<FromServer, Object, ?> action = requestConfig.getChainedResponse().actualAction(fromServer.getStatusCode()); return action.apply(fromServer, fromServer.getHasBody() ? parser.apply(requestConfig, fromServer) : null); } finally { fromServer.finish(); } } }
/** * Method that provides an HTML parser for response configuration (uses JSoup). * * @param config the chained configuration * @param fromServer the server response adapter * @return the parsed HTML content (a {@link Document} object) */ public static Object jsoupParse(final ChainedHttpConfig config, final FromServer fromServer) { try { return Jsoup.parse(fromServer.getInputStream(), fromServer.getCharset().name(), fromServer.getUri().toString()); } catch (IOException e) { throw new TransportingException(e); } }