@Override public Response get(Request request) throws IOException { CacheResponse javaResponse = getJavaCachedResponse(request); if (javaResponse == null) { return null; } return JavaApiConverter.createOkResponseForCacheGet(request, javaResponse); }
public OkHttpsURLConnection(URL url, OkHttpClient client, URLFilter filter) { this(new OkHttpURLConnection(url, client, filter)); }
@Override public void setFixedLengthStreamingMode(int contentLength) { setFixedLengthStreamingMode((long) contentLength); }
@Override public OutputStream getOutputStream() throws IOException { OutputStreamRequestBody requestBody = (OutputStreamRequestBody) buildCall().request().body(); if (requestBody == null) { throw new ProtocolException("method does not support a request body: " + method); } // If this request needs to stream bytes to the server, build a physical connection immediately // and start streaming those bytes over that connection. if (requestBody instanceof StreamedRequestBody) { connect(); networkInterceptor.proceed(); } if (requestBody.isClosed()) { throw new ProtocolException("cannot write request body after response has been read"); } return requestBody.outputStream(); }
@Override public Principal getLocalPrincipal() { Handshake handshake = handshake(); return handshake != null ? handshake.localPrincipal() : null; }
@Override public String getHeaderFieldKey(int position) { try { Headers headers = getHeaders(); if (position < 0 || position >= headers.size()) return null; return headers.name(position); } catch (IOException e) { return null; } }
@Override public String getResponseMessage() throws IOException { return getResponse(true).message(); }
BufferedRequestBody(long expectedContentLength) { initOutputStream(buffer, expectedContentLength); }
@Override public void write(int b) throws IOException { write(new byte[] {(byte) b}, 0, 1); }
/** * Creates an {@link java.net.HttpURLConnection} of the correct subclass from the supplied OkHttp * {@link Response}. */ static HttpURLConnection createJavaUrlConnectionForCachePut(Response okResponse) { okResponse = okResponse.newBuilder() .body(null) .headers(withSyntheticHeaders(okResponse)) .build(); Request request = okResponse.request(); // Create an object of the correct class in case the ResponseCache uses instanceof. if (request.isHttps()) { return new CacheHttpsURLConnection(new CacheHttpURLConnection(okResponse)); } else { return new CacheHttpURLConnection(okResponse); } }
@Override public void disconnect() { // Calling disconnect() before a connection exists should have no effect. if (call == null) return; networkInterceptor.proceed(); // Unblock any waiting async thread. call.cancel(); }
@Override public CacheResponse get(URI uri, String requestMethod, Map<String, List<String>> requestHeaders) throws IOException { Request okRequest = JavaApiConverter.createOkRequest(uri, requestMethod, requestHeaders); Response okResponse = delegate.internalCache.get(okRequest); if (okResponse == null) { return null; } return JavaApiConverter.createJavaCacheResponse(okResponse); }
/** * Extracts OkHttp headers from the supplied {@link java.net.HttpURLConnection}. Only real headers * are extracted. See {@link #extractStatusLine(java.net.HttpURLConnection)}. */ private static Headers extractOkResponseHeaders( HttpURLConnection httpUrlConnection, Response.Builder okResponseBuilder) { Map<String, List<String>> javaResponseHeaders = httpUrlConnection.getHeaderFields(); return extractOkHeaders(javaResponseHeaders, okResponseBuilder); }
/** * Extracts the status line from the supplied Java API {@link java.net.CacheResponse}. As per the * spec, the status line is held as the header with the null key. Throws a {@link * ProtocolException} if there is no status line. */ private static String extractStatusLine(CacheResponse javaResponse) throws IOException { Map<String, List<String>> javaResponseHeaders = javaResponse.getHeaders(); return extractStatusLine(javaResponseHeaders); }
public OkHttpsURLConnection(URL url, OkHttpClient client) { this(new OkHttpURLConnection(url, client)); }
@Override public Principal getPeerPrincipal() throws SSLPeerUnverifiedException { Handshake handshake = handshake(); return handshake != null ? handshake.peerPrincipal() : null; }
/** * Returns the value of the field at {@code position}. Returns null if there are fewer than {@code * position} headers. */ @Override public String getHeaderField(int position) { try { Headers headers = getHeaders(); if (position < 0 || position >= headers.size()) return null; return headers.value(position); } catch (IOException e) { return null; } }
@Override public int getResponseCode() throws IOException { return getResponse(true).code(); }
/** * Extracts OkHttp headers from the supplied {@link java.net.CacheResponse}. Only real headers are * extracted. See {@link #extractStatusLine(java.net.CacheResponse)}. */ private static Headers extractOkHeaders( CacheResponse javaResponse, Response.Builder okResponseBuilder) throws IOException { Map<String, List<String>> javaResponseHeaders = javaResponse.getHeaders(); return extractOkHeaders(javaResponseHeaders, okResponseBuilder); }
@Override public Certificate[] getServerCertificates() throws SSLPeerUnverifiedException { Handshake handshake = handshake(); if (handshake == null) return null; List<Certificate> result = handshake.peerCertificates(); return !result.isEmpty() ? result.toArray(new Certificate[result.size()]) : null; }