com.raskasa.oksolr.SolrClient.java Source code

Java tutorial

Introduction

Here is the source code for com.raskasa.oksolr.SolrClient.java

Source

/*
 * Copyright (C) 2015 Ras Kasa Williams
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.raskasa.oksolr;

import com.squareup.okhttp.Cache;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Response;
import java.io.File;
import java.io.IOException;

import static java.util.concurrent.TimeUnit.SECONDS;

public final class SolrClient {
    private final OkHttpClient client;

    public SolrClient() {
        this.client = createOkHttpClient();
    }

    public SolrClient(OkHttpClient client) {
        this.client = client;
    }

    public SolrResponse execute(SolrRequest request) throws IOException {
        Response okResponse = client.newCall(request.getOkRequest()).execute();
        return new SolrResponse(okResponse);
    }

    public void installCache() {
        File cacheDir = new File(System.getProperty("user.home"), "oksolr/http");
        Cache cache = new Cache(cacheDir, 50 * 1024 * 1024);
        client.setCache(cache);
    }

    public Cache getCache() {
        return client.getCache();
    }

    public int getConnectTimeout() {
        return client.getConnectTimeout();
    }

    public int getReadTimeout() {
        return client.getReadTimeout();
    }

    public int getWriteTimeout() {
        return client.getWriteTimeout();
    }

    private OkHttpClient createOkHttpClient() {
        OkHttpClient client = new OkHttpClient();
        client.setConnectTimeout(10, SECONDS);
        client.setReadTimeout(10, SECONDS);
        client.setWriteTimeout(10, SECONDS);
        return client;
    }
}