@Override public Bitmap convertResponse(Response response) throws Throwable { ResponseBody body = response.body(); if (body == null) return null; return parse(body.bytes()); }
private String retrieveAuthCode(OkHttpClient client) throws IOException { Request.Builder builder = new Request.Builder().url(authRetrievalUrl); try (Response authResponse = client.newCall(builder.build()).execute()) { ResponseBody authBody = authResponse.body(); if (authBody == null) { throw new AssertionError("AUTH ERROR: " + authResponse.code() + ":" + "<empty body>"); } String authCode = new String(authBody.bytes()); // System.out.println("AUTH: " + authResponse.code() + ":" + authCode); return authCode; } } }
private static void sendRequest(OkHttpClient client, String url) { System.out.printf("%-40s ", url); System.out.flush(); System.out.println(Platform.get()); Request request = new Request.Builder().url(url).build(); try (Response response = client.newCall(request).execute()) { Handshake handshake = response.handshake(); System.out.println(handshake.tlsVersion() + " " + handshake.cipherSuite() + " " + response.protocol() + " " + response.code + " " + response.body.bytes().length + "b"); } catch (IOException ioe) { System.out.println(ioe.toString()); } } }
private byte[] getResponseBytes(final okhttp3.Response callResponse) throws IOException { final byte[] rawBytes = callResponse.body().bytes(); final String contentEncoding = callResponse.header("Content-Encoding"); if (gzipEncodings.contains(contentEncoding)) { try (final InputStream gzipIn = new GZIPInputStream(new ByteArrayInputStream(rawBytes)); final ByteArrayOutputStream baos = new ByteArrayOutputStream()) { StreamUtils.copy(gzipIn, baos); return baos.toByteArray(); } } else { return rawBytes; } }
/** * Perform a geocoding request. Both forward and revers are possible, just configure the <code>request</code> * accordingly. * * @param request the request to send to the API * @return found results for your request */ public GHGeocodingResponse geocode(GHGeocodingRequest request) { String url = buildUrl(request); try { Request okRequest = new Request.Builder().url(url).build(); ResponseBody rspBody = getClientForRequest(request).newCall(okRequest).execute().body(); return objectMapper.readValue(rspBody.bytes(), GHGeocodingResponse.class); } catch (Exception ex) { throw new RuntimeException("Problem performing geocoding for " + url + ": " + ex.getMessage(), ex); } }
public T convert(ResponseBody value) throws IOException { try { return JSON.parseObject(value.bytes() , fastJsonConfig.getCharset() , type , fastJsonConfig.getParserConfig() , fastJsonConfig.getParseProcess() , JSON.DEFAULT_PARSER_FEATURE , fastJsonConfig.getFeatures() ); } catch (Exception e) { throw new IOException("JSON parse error: " + e.getMessage(), e); } finally { value.close(); } } }
String folderContent = new String(folderBody.bytes());
String contentBody = new String(body.bytes());
/** * Execute the given remote interface function on the master node. * <p> * This is used to forward an API request to the master node. It is useful in situations where an API call can * only be executed on the master node. * <p> * The returned {@link MasterResponse} object is constructed from the remote response's status code and body. */ protected <RemoteInterfaceType, RemoteCallResponseType> MasterResponse<RemoteCallResponseType> requestOnMaster( Function<RemoteInterfaceType, Call<RemoteCallResponseType>> remoteInterfaceFunction, Function<String, Optional<RemoteInterfaceType>> remoteInterfaceProvider ) throws IOException { final Node masterNode = nodeService.allActive().values().stream() .filter(Node::isMaster) .findFirst() .orElseThrow(() -> new IllegalStateException("No active master node found")); final RemoteInterfaceType remoteInterfaceType = remoteInterfaceProvider.apply(masterNode.getNodeId()) .orElseThrow(() -> new IllegalStateException("Master node " + masterNode.getNodeId() + " not found")); final Call<RemoteCallResponseType> call = remoteInterfaceFunction.apply(remoteInterfaceType); final Response<RemoteCallResponseType> response = call.execute(); final byte[] errorBody = response.errorBody() == null ? null : response.errorBody().bytes(); return MasterResponse.create(response.isSuccessful(), response.code(), response.body(), errorBody); }
Map<String, Object> responseData = objectMapper.readValue(body.bytes(), Map.class); String folderId = (String) responseData.get("id"); if (folderId == null) {
@Override public ServerResponse getResponse(final RequestBodyDelegate delegate) throws IOException { if (HttpMethod.permitsRequestBody(mMethod) || HttpMethod.requiresRequestBody(mMethod)) { RequestBody body = new RequestBody() { @Override public long contentLength() throws IOException { return mBodyLength; } @Override public MediaType contentType() { if (mContentType == null) return null; return MediaType.parse(mContentType); } @Override public void writeTo(BufferedSink sink) throws IOException { final OkHttpBodyWriter bodyWriter = new OkHttpBodyWriter(sink); delegate.onBodyReady(bodyWriter); bodyWriter.flush(); } }; mRequestBuilder.method(mMethod, body); } else { mRequestBuilder.method(mMethod, null); } mResponse = mClient.newCall(mRequestBuilder.build()).execute(); return new ServerResponse(mResponse.code(), mResponse.body().bytes(), getServerResponseHeaders(mResponse.headers())); }
@SuppressWarnings("unchecked") private ExportResult<ContactsModelWrapper> doExport(TokensAndUrlAuthData authData, String url) { Request.Builder graphReqBuilder = new Request.Builder().url(url); graphReqBuilder.header("Authorization", "Bearer " + authData.getAccessToken()); try (Response graphResponse = client.newCall(graphReqBuilder.build()).execute()) { ResponseBody body = graphResponse.body(); if (body == null) { return new ExportResult<>(new Exception( "Error retrieving contacts: response body was null")); } String graphBody = new String(body.bytes()); Map graphMap = objectMapper.reader().forType(Map.class).readValue(graphBody); String nextLink = (String) graphMap.get(ODATA_NEXT); ContinuationData continuationData = nextLink == null ? null : new ContinuationData(new GraphPagination(nextLink)); List<Map<String, Object>> rawContacts = (List<Map<String, Object>>) graphMap.get("value"); if (rawContacts == null) { return new ExportResult<>(ExportResult.ResultType.END); } ContactsModelWrapper wrapper = transform(rawContacts); return new ExportResult<>(ExportResult.ResultType.CONTINUE, wrapper, continuationData); } catch (IOException e) { e.printStackTrace(); // FIXME log error return new ExportResult<>(e); } }
input.processRawMessage(new RawMessage(r.body().bytes(), remoteAddress)); } catch (IOException e) { LOG.error("Could not fetch HTTP resource at " + url, e);
public T convert(ResponseBody value) throws IOException { try { return JSON.parseObject(value.bytes() , fastJsonConfig.getCharset() , type , fastJsonConfig.getParserConfig() , fastJsonConfig.getParseProcess() , JSON.DEFAULT_PARSER_FEATURE , fastJsonConfig.getFeatures() ); } catch (Exception e) { throw new IOException("JSON parse error: " + e.getMessage(), e); } finally { value.close(); } } }
Map<String, Object> responseData = objectMapper.readValue(body.bytes(), Map.class); return new BatchResponse( new ImportResult(ImportResult.ResultType.OK),
seimiResponse.setBodyType(BodyType.TEXT); try { byte[] data = okResponseBody.bytes(); String utfContent = new String(data,"UTF-8"); String charsetFinal = renderRealCharset(utfContent); seimiResponse.setBodyType(BodyType.BINARY); try { seimiResponse.setData(okResponseBody.bytes()); } catch (Exception e) { logger.error("no content data");
new Exception("Error retrieving contacts: response body was null")); String graphBody = new String(body.bytes()); Map graphMap = objectMapper.reader().forType(Map.class).readValue(graphBody); new Exception("Error retrieving calendar: response body was null")); String graphBody = new String(body.bytes()); Map graphMap = objectMapper.reader().forType(Map.class).readValue(graphBody);
final byte[] bodyBytes = body.bytes(); JsonNode responseJson = parseJsonResponse(new ByteArrayInputStream(bodyBytes)); String scrollId = responseJson.get("_scroll_id").asText();
bytes = body.bytes();
@Test public void testWithResponseCompression() throws IOException { ByteArrayOutputStream capturedOutput = hookAlmostRealInterpretResponseStream(mMockEventReporter); byte[] uncompressedData = repeat(".", 1024).getBytes(); byte[] compressedData = compress(uncompressedData); MockWebServer server = new MockWebServer(); server.start(); server.enqueue(new MockResponse() .setBody(new Buffer().write(compressedData)) .addHeader("Content-Encoding: gzip")); Request request = new Request.Builder() .url(server.url("/")) .build(); Response response = mClientWithInterceptor.newCall(request).execute(); // Verify that the final output and the caller both saw the uncompressed stream. assertArrayEquals(uncompressedData, response.body().bytes()); assertArrayEquals(uncompressedData, capturedOutput.toByteArray()); // And verify that the StethoInterceptor was able to see both. Mockito.verify(mMockEventReporter) .dataReceived( anyString(), eq(compressedData.length), eq(uncompressedData.length)); server.shutdown(); }