Search in sources :

Example 1 with Headers

use of okhttp3.Headers in project buck by facebook.

the class ArtifactCaches method createHttpArtifactCache.

private static ArtifactCache createHttpArtifactCache(HttpCacheEntry cacheDescription, final String hostToReportToRemote, final BuckEventBus buckEventBus, ProjectFilesystem projectFilesystem, ListeningExecutorService httpWriteExecutorService, ArtifactCacheBuckConfig config, NetworkCacheFactory factory, boolean distributedBuildModeEnabled) {
    // Setup the default client to use.
    OkHttpClient.Builder storeClientBuilder = new OkHttpClient.Builder();
    storeClientBuilder.networkInterceptors().add(chain -> chain.proceed(chain.request().newBuilder().addHeader("X-BuckCache-User", stripNonAscii(System.getProperty("user.name", "<unknown>"))).addHeader("X-BuckCache-Host", stripNonAscii(hostToReportToRemote)).build()));
    int timeoutSeconds = cacheDescription.getTimeoutSeconds();
    setTimeouts(storeClientBuilder, timeoutSeconds);
    storeClientBuilder.connectionPool(new ConnectionPool(/* maxIdleConnections */
    (int) config.getThreadPoolSize(), /* keepAliveDurationMs */
    config.getThreadPoolKeepAliveDurationMillis(), TimeUnit.MILLISECONDS));
    // The artifact cache effectively only connects to a single host at a time. We should allow as
    // many concurrent connections to that host as we allow threads.
    Dispatcher dispatcher = new Dispatcher();
    dispatcher.setMaxRequestsPerHost((int) config.getThreadPoolSize());
    storeClientBuilder.dispatcher(dispatcher);
    final ImmutableMap<String, String> readHeaders = cacheDescription.getReadHeaders();
    final ImmutableMap<String, String> writeHeaders = cacheDescription.getWriteHeaders();
    // If write headers are specified, add them to every default client request.
    if (!writeHeaders.isEmpty()) {
        storeClientBuilder.networkInterceptors().add(chain -> chain.proceed(addHeadersToBuilder(chain.request().newBuilder(), writeHeaders).build()));
    }
    OkHttpClient storeClient = storeClientBuilder.build();
    // For fetches, use a client with a read timeout.
    OkHttpClient.Builder fetchClientBuilder = storeClient.newBuilder();
    setTimeouts(fetchClientBuilder, timeoutSeconds);
    // If read headers are specified, add them to every read client request.
    if (!readHeaders.isEmpty()) {
        fetchClientBuilder.networkInterceptors().add(chain -> chain.proceed(addHeadersToBuilder(chain.request().newBuilder(), readHeaders).build()));
    }
    fetchClientBuilder.networkInterceptors().add((chain -> {
        Response originalResponse = chain.proceed(chain.request());
        return originalResponse.newBuilder().body(new ProgressResponseBody(originalResponse.body(), buckEventBus)).build();
    }));
    OkHttpClient fetchClient = fetchClientBuilder.build();
    HttpService fetchService;
    HttpService storeService;
    switch(config.getLoadBalancingType()) {
        case CLIENT_SLB:
            HttpLoadBalancer clientSideSlb = config.getSlbConfig().createClientSideSlb(new DefaultClock(), buckEventBus, new CommandThreadFactory("ArtifactCaches.HttpLoadBalancer", SLB_THREAD_PRIORITY));
            fetchService = new RetryingHttpService(buckEventBus, new LoadBalancedService(clientSideSlb, fetchClient, buckEventBus), config.getMaxFetchRetries());
            storeService = new LoadBalancedService(clientSideSlb, storeClient, buckEventBus);
            break;
        case SINGLE_SERVER:
            URI url = cacheDescription.getUrl();
            fetchService = new SingleUriService(url, fetchClient);
            storeService = new SingleUriService(url, storeClient);
            break;
        default:
            throw new IllegalArgumentException("Unknown HttpLoadBalancer type: " + config.getLoadBalancingType());
    }
    String cacheName = cacheDescription.getName().map(input -> "http-" + input).orElse("http");
    boolean doStore = cacheDescription.getCacheReadMode().isDoStore();
    return factory.newInstance(NetworkCacheArgs.builder().setThriftEndpointPath(config.getHybridThriftEndpoint()).setCacheName(cacheName).setRepository(config.getRepository()).setScheduleType(config.getScheduleType()).setFetchClient(fetchService).setStoreClient(storeService).setDoStore(doStore).setProjectFilesystem(projectFilesystem).setBuckEventBus(buckEventBus).setHttpWriteExecutorService(httpWriteExecutorService).setErrorTextTemplate(cacheDescription.getErrorMessageFormat()).setDistributedBuildModeEnabled(distributedBuildModeEnabled).build());
}
Also used : ConnectionPool(okhttp3.ConnectionPool) BuckEventBus(com.facebook.buck.event.BuckEventBus) Okio(okio.Okio) Source(okio.Source) BytesReceivedEvent(com.facebook.buck.event.NetworkEvent.BytesReceivedEvent) RetryingHttpService(com.facebook.buck.slb.RetryingHttpService) Dispatcher(okhttp3.Dispatcher) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) CommonGroups(com.facebook.buck.randomizedtrial.CommonGroups) BuckConfig(com.facebook.buck.cli.BuckConfig) ImmutableList(com.google.common.collect.ImmutableList) DefaultClock(com.facebook.buck.timing.DefaultClock) Map(java.util.Map) ForwardingSource(okio.ForwardingSource) Response(okhttp3.Response) HttpLoadBalancer(com.facebook.buck.slb.HttpLoadBalancer) URI(java.net.URI) LoadBalancedService(com.facebook.buck.slb.LoadBalancedService) Path(java.nio.file.Path) MediaType(okhttp3.MediaType) ResponseBody(okhttp3.ResponseBody) HttpService(com.facebook.buck.slb.HttpService) Logger(com.facebook.buck.log.Logger) Request(okhttp3.Request) Buffer(okio.Buffer) SingleUriService(com.facebook.buck.slb.SingleUriService) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableMap(com.google.common.collect.ImmutableMap) CharMatcher(com.google.common.base.CharMatcher) IOException(java.io.IOException) HumanReadableException(com.facebook.buck.util.HumanReadableException) CommandThreadFactory(com.facebook.buck.log.CommandThreadFactory) TimeUnit(java.util.concurrent.TimeUnit) BufferedSource(okio.BufferedSource) OkHttpClient(okhttp3.OkHttpClient) Optional(java.util.Optional) ConnectionPool(okhttp3.ConnectionPool) AsyncCloseable(com.facebook.buck.util.AsyncCloseable) RandomizedTrial(com.facebook.buck.randomizedtrial.RandomizedTrial) DirCacheExperimentEvent(com.facebook.buck.event.DirCacheExperimentEvent) ListeningExecutorService(com.google.common.util.concurrent.ListeningExecutorService) OkHttpClient(okhttp3.OkHttpClient) CommandThreadFactory(com.facebook.buck.log.CommandThreadFactory) Dispatcher(okhttp3.Dispatcher) RetryingHttpService(com.facebook.buck.slb.RetryingHttpService) URI(java.net.URI) Response(okhttp3.Response) RetryingHttpService(com.facebook.buck.slb.RetryingHttpService) HttpService(com.facebook.buck.slb.HttpService) DefaultClock(com.facebook.buck.timing.DefaultClock) LoadBalancedService(com.facebook.buck.slb.LoadBalancedService) SingleUriService(com.facebook.buck.slb.SingleUriService) HttpLoadBalancer(com.facebook.buck.slb.HttpLoadBalancer)

Example 2 with Headers

use of okhttp3.Headers in project okhttp-OkGo by jeasonlzy.

the class DownloadTask method doInBackground.

/** 一旦该方法执行,意味着开始下载了 */
@Override
protected DownloadInfo doInBackground(Void... params) {
    if (isCancelled())
        return mDownloadInfo;
    mPreviousTime = System.currentTimeMillis();
    mDownloadInfo.setNetworkSpeed(0);
    mDownloadInfo.setState(DownloadManager.DOWNLOADING);
    postMessage(null, null);
    long startPos = mDownloadInfo.getDownloadLength();
    Response response;
    try {
        response = mDownloadInfo.getRequest().headers("RANGE", "bytes=" + startPos + "-").execute();
    } catch (IOException e) {
        e.printStackTrace();
        mDownloadInfo.setNetworkSpeed(0);
        mDownloadInfo.setState(DownloadManager.ERROR);
        postMessage("网络异常", e);
        return mDownloadInfo;
    }
    int code = response.code();
    if (code == 404 || code >= 500) {
        mDownloadInfo.setNetworkSpeed(0);
        mDownloadInfo.setState(DownloadManager.ERROR);
        postMessage("服务器数据错误", null);
        return mDownloadInfo;
    }
    //构建下载文件路径,如果有设置,就用设置的,否者就自己创建
    String url = mDownloadInfo.getUrl();
    String fileName = mDownloadInfo.getFileName();
    if (TextUtils.isEmpty(fileName)) {
        fileName = HttpUtils.getNetFileName(response, url);
        mDownloadInfo.setFileName(fileName);
    }
    if (TextUtils.isEmpty(mDownloadInfo.getTargetPath())) {
        File targetFolder = new File(mDownloadInfo.getTargetFolder());
        if (!targetFolder.exists())
            targetFolder.mkdirs();
        File file = new File(targetFolder, fileName);
        mDownloadInfo.setTargetPath(file.getAbsolutePath());
    }
    //检查文件有效性,文件大小大于总文件大小
    if (startPos > mDownloadInfo.getTotalLength()) {
        mDownloadInfo.setNetworkSpeed(0);
        mDownloadInfo.setState(DownloadManager.ERROR);
        postMessage("断点文件异常,需要删除后重新下载", null);
        return mDownloadInfo;
    }
    if (startPos == mDownloadInfo.getTotalLength() && startPos > 0) {
        mDownloadInfo.setProgress(1.0f);
        mDownloadInfo.setNetworkSpeed(0);
        mDownloadInfo.setState(DownloadManager.FINISH);
        postMessage(null, null);
        return mDownloadInfo;
    }
    //设置断点写文件
    File file = new File(mDownloadInfo.getTargetPath());
    ProgressRandomAccessFile randomAccessFile;
    try {
        randomAccessFile = new ProgressRandomAccessFile(file, "rw", startPos);
        randomAccessFile.seek(startPos);
    } catch (Exception e) {
        e.printStackTrace();
        mDownloadInfo.setNetworkSpeed(0);
        mDownloadInfo.setState(DownloadManager.ERROR);
        postMessage("没有找到已存在的断点文件", e);
        return mDownloadInfo;
    }
    //获取流对象,准备进行读写文件
    long totalLength = response.body().contentLength();
    if (mDownloadInfo.getTotalLength() == 0) {
        mDownloadInfo.setTotalLength(totalLength);
    }
    InputStream is = response.body().byteStream();
    //读写文件流
    try {
        download(is, randomAccessFile);
    } catch (IOException e) {
        e.printStackTrace();
        mDownloadInfo.setNetworkSpeed(0);
        mDownloadInfo.setState(DownloadManager.ERROR);
        postMessage("文件读写异常", e);
        return mDownloadInfo;
    }
    //循环结束走到这里,a.下载完成     b.暂停      c.判断是否下载出错
    if (isCancelled()) {
        mDownloadInfo.setNetworkSpeed(0);
        if (//暂停
        isPause)
            //暂停
            mDownloadInfo.setState(DownloadManager.PAUSE);
        else
            //停止
            mDownloadInfo.setState(DownloadManager.NONE);
        postMessage(null, null);
    } else if (file.length() == mDownloadInfo.getTotalLength() && mDownloadInfo.getState() == DownloadManager.DOWNLOADING) {
        mDownloadInfo.setNetworkSpeed(0);
        //下载完成
        mDownloadInfo.setState(DownloadManager.FINISH);
        postMessage(null, null);
    } else if (file.length() != mDownloadInfo.getDownloadLength()) {
        mDownloadInfo.setNetworkSpeed(0);
        //由于不明原因,文件保存有误
        mDownloadInfo.setState(DownloadManager.ERROR);
        postMessage("未知原因", null);
    }
    return mDownloadInfo;
}
Also used : Response(okhttp3.Response) BufferedInputStream(java.io.BufferedInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException)

Example 3 with Headers

use of okhttp3.Headers in project okhttp-OkGo by jeasonlzy.

the class CacheCall method execute.

@Override
public void execute(AbsCallback<T> callback) {
    synchronized (this) {
        if (executed)
            throw new IllegalStateException("Already executed.");
        executed = true;
    }
    mCallback = callback;
    if (mCallback == null)
        mCallback = new AbsCallbackWrapper<>();
    //请求执行前UI线程调用
    mCallback.onBefore(baseRequest);
    //请求之前获取缓存信息,添加缓存头和其他的公共头
    if (baseRequest.getCacheKey() == null)
        baseRequest.setCacheKey(HttpUtils.createUrlFromParams(baseRequest.getBaseUrl(), baseRequest.getParams().urlParamsMap));
    if (baseRequest.getCacheMode() == null)
        baseRequest.setCacheMode(CacheMode.NO_CACHE);
    //无缓存模式,不需要进入缓存逻辑
    final CacheMode cacheMode = baseRequest.getCacheMode();
    if (cacheMode != CacheMode.NO_CACHE) {
        //noinspection unchecked
        cacheEntity = (CacheEntity<T>) CacheManager.INSTANCE.get(baseRequest.getCacheKey());
        //检查缓存的有效时间,判断缓存是否已经过期
        if (cacheEntity != null && cacheEntity.checkExpire(cacheMode, baseRequest.getCacheTime(), System.currentTimeMillis())) {
            cacheEntity.setExpire(true);
        }
        HeaderParser.addCacheHeaders(baseRequest, cacheEntity, cacheMode);
    }
    //构建请求
    RequestBody requestBody = baseRequest.generateRequestBody();
    final Request request = baseRequest.generateRequest(baseRequest.wrapRequestBody(requestBody));
    rawCall = baseRequest.generateCall(request);
    if (cacheMode == CacheMode.IF_NONE_CACHE_REQUEST) {
        //如果没有缓存,或者缓存过期,就请求网络,否者直接使用缓存
        if (cacheEntity != null && !cacheEntity.isExpire()) {
            T data = cacheEntity.getData();
            HttpHeaders headers = cacheEntity.getResponseHeaders();
            if (data == null || headers == null) {
                //由于没有序列化等原因,可能导致数据为空
                sendFailResultCallback(true, rawCall, null, OkGoException.INSTANCE("没有获取到缓存,或者缓存已经过期!"));
            } else {
                sendSuccessResultCallback(true, data, rawCall, null);
                //获取缓存成功,不请求网络
                return;
            }
        } else {
            sendFailResultCallback(true, rawCall, null, OkGoException.INSTANCE("没有获取到缓存,或者缓存已经过期!"));
        }
    } else if (cacheMode == CacheMode.FIRST_CACHE_THEN_REQUEST) {
        //先使用缓存,不管是否存在,仍然请求网络
        if (cacheEntity != null && !cacheEntity.isExpire()) {
            T data = cacheEntity.getData();
            HttpHeaders headers = cacheEntity.getResponseHeaders();
            if (data == null || headers == null) {
                //由于没有序列化等原因,可能导致数据为空
                sendFailResultCallback(true, rawCall, null, OkGoException.INSTANCE("没有获取到缓存,或者缓存已经过期!"));
            } else {
                sendSuccessResultCallback(true, data, rawCall, null);
            }
        } else {
            sendFailResultCallback(true, rawCall, null, OkGoException.INSTANCE("没有获取到缓存,或者缓存已经过期!"));
        }
    }
    if (canceled) {
        rawCall.cancel();
    }
    currentRetryCount = 0;
    rawCall.enqueue(new okhttp3.Callback() {

        @Override
        public void onFailure(okhttp3.Call call, IOException e) {
            if (e instanceof SocketTimeoutException && currentRetryCount < baseRequest.getRetryCount()) {
                //超时重试处理
                currentRetryCount++;
                okhttp3.Call newCall = baseRequest.generateCall(call.request());
                newCall.enqueue(this);
            } else {
                mCallback.parseError(call, e);
                //请求失败,一般为url地址错误,网络错误等,并且过滤用户主动取消的网络请求
                if (!call.isCanceled()) {
                    sendFailResultCallback(false, call, null, e);
                }
            }
        }

        @Override
        public void onResponse(okhttp3.Call call, okhttp3.Response response) throws IOException {
            int responseCode = response.code();
            //304缓存数据
            if (responseCode == 304 && cacheMode == CacheMode.DEFAULT) {
                if (cacheEntity == null) {
                    sendFailResultCallback(true, call, response, OkGoException.INSTANCE("服务器响应码304,但是客户端没有缓存!"));
                } else {
                    T data = cacheEntity.getData();
                    HttpHeaders headers = cacheEntity.getResponseHeaders();
                    if (data == null || headers == null) {
                        //由于没有序列化等原因,可能导致数据为空
                        sendFailResultCallback(true, call, response, OkGoException.INSTANCE("没有获取到缓存,或者缓存已经过期!"));
                    } else {
                        sendSuccessResultCallback(true, data, call, response);
                    }
                }
                return;
            }
            //响应失败,一般为服务器内部错误,或者找不到页面等
            if (responseCode == 404 || responseCode >= 500) {
                sendFailResultCallback(false, call, response, OkGoException.INSTANCE("服务器数据异常!"));
                return;
            }
            try {
                Response<T> parseResponse = parseResponse(response);
                T data = parseResponse.body();
                //网络请求成功,保存缓存数据
                handleCache(response.headers(), data);
                //网络请求成功回调
                sendSuccessResultCallback(false, data, call, response);
            } catch (Exception e) {
                //一般为服务器响应成功,但是数据解析错误
                sendFailResultCallback(false, call, response, e);
            }
        }
    });
}
Also used : HttpHeaders(com.lzy.okgo.model.HttpHeaders) Request(okhttp3.Request) BaseRequest(com.lzy.okgo.request.BaseRequest) CacheMode(com.lzy.okgo.cache.CacheMode) IOException(java.io.IOException) OkGoException(com.lzy.okgo.exception.OkGoException) IOException(java.io.IOException) SocketTimeoutException(java.net.SocketTimeoutException) Response(com.lzy.okgo.model.Response) AbsCallbackWrapper(com.lzy.okgo.callback.AbsCallbackWrapper) SocketTimeoutException(java.net.SocketTimeoutException) RequestBody(okhttp3.RequestBody)

Example 4 with Headers

use of okhttp3.Headers in project okhttp-OkGo by jeasonlzy.

the class BaseDetailActivity method handleError.

protected void handleError(Call call, Response response) {
    StringBuilder sb;
    if (call != null) {
        requestState.setText("请求失败  请求方式:" + call.request().method() + "\n" + "url:" + call.request().url());
        Headers requestHeadersString = call.request().headers();
        Set<String> requestNames = requestHeadersString.names();
        sb = new StringBuilder();
        for (String name : requestNames) {
            sb.append(name).append(" : ").append(requestHeadersString.get(name)).append("\n");
        }
        requestHeaders.setText(sb.toString());
    } else {
        requestState.setText("--");
        requestHeaders.setText("--");
    }
    responseData.setText("--");
    if (response != null) {
        Headers responseHeadersString = response.headers();
        Set<String> names = responseHeadersString.names();
        sb = new StringBuilder();
        sb.append("stateCode : ").append(response.code()).append("\n");
        for (String name : names) {
            sb.append(name).append(" : ").append(responseHeadersString.get(name)).append("\n");
        }
        responseHeader.setText(sb.toString());
    } else {
        responseHeader.setText("--");
    }
}
Also used : Headers(okhttp3.Headers)

Example 5 with Headers

use of okhttp3.Headers in project okhttp-OkGo by jeasonlzy.

the class BaseDetailActivity method handleResponse.

protected <T> void handleResponse(T data, Call call, Response response) {
    StringBuilder sb;
    if (call != null) {
        requestState.setText("请求成功  请求方式:" + call.request().method() + "\n" + "url:" + call.request().url());
        Headers requestHeadersString = call.request().headers();
        Set<String> requestNames = requestHeadersString.names();
        sb = new StringBuilder();
        for (String name : requestNames) {
            sb.append(name).append(" : ").append(requestHeadersString.get(name)).append("\n");
        }
        requestHeaders.setText(sb.toString());
    } else {
        requestState.setText("--");
        requestHeaders.setText("--");
    }
    if (data == null) {
        responseData.setText("--");
    } else {
        if (data instanceof String) {
            responseData.setText((String) data);
        } else if (data instanceof List) {
            sb = new StringBuilder();
            List list = (List) data;
            for (Object obj : list) {
                sb.append(obj.toString()).append("\n");
            }
            responseData.setText(sb.toString());
        } else if (data instanceof Set) {
            sb = new StringBuilder();
            Set set = (Set) data;
            for (Object obj : set) {
                sb.append(obj.toString()).append("\n");
            }
            responseData.setText(sb.toString());
        } else if (data instanceof Map) {
            sb = new StringBuilder();
            Map map = (Map) data;
            Set keySet = map.keySet();
            for (Object key : keySet) {
                sb.append(key.toString()).append(" : ").append(map.get(key)).append("\n");
            }
            responseData.setText(sb.toString());
        } else if (data instanceof File) {
            File file = (File) data;
            responseData.setText("数据内容即为文件内容\n下载文件路径:" + file.getAbsolutePath());
        } else if (data instanceof Bitmap) {
            responseData.setText("图片的内容即为数据");
        } else {
            responseData.setText(data.toString());
        }
    }
    if (response != null) {
        Headers responseHeadersString = response.headers();
        Set<String> names = responseHeadersString.names();
        sb = new StringBuilder();
        sb.append("url : ").append(response.request().url()).append("\n\n");
        sb.append("stateCode : ").append(response.code()).append("\n");
        for (String name : names) {
            sb.append(name).append(" : ").append(responseHeadersString.get(name)).append("\n");
        }
        responseHeader.setText(sb.toString());
    } else {
        responseHeader.setText("--");
    }
}
Also used : Bitmap(android.graphics.Bitmap) Set(java.util.Set) Headers(okhttp3.Headers) List(java.util.List) Map(java.util.Map) File(java.io.File)

Aggregations

Request (okhttp3.Request)166 Headers (okhttp3.Headers)154 Response (okhttp3.Response)135 Test (org.junit.Test)128 IOException (java.io.IOException)92 MockResponse (okhttp3.mockwebserver.MockResponse)68 RequestBody (okhttp3.RequestBody)58 Call (okhttp3.Call)54 ResponseBody (okhttp3.ResponseBody)48 HashMap (java.util.HashMap)41 MediaType (okhttp3.MediaType)39 OkHttpClient (okhttp3.OkHttpClient)38 HttpHeaders (okhttp3.internal.http.HttpHeaders)37 Map (java.util.Map)32 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)29 List (java.util.List)23 Buffer (okio.Buffer)23 MockWebServer (okhttp3.mockwebserver.MockWebServer)22 JSONObject (org.json.JSONObject)22 HttpUrl (okhttp3.HttpUrl)20