Example usage for com.squareup.okhttp Response code

List of usage examples for com.squareup.okhttp Response code

Introduction

In this page you can find the example usage for com.squareup.okhttp Response code.

Prototype

int code

To view the source code for com.squareup.okhttp Response code.

Click Source Link

Usage

From source file:org.sonar.process.systeminfo.SystemInfoHttpServerTest.java

License:Open Source License

@Test
public void start_starts_http_server_and_publishes_URL_in_IPC() throws Exception {
    underTest.start();//from ww  w  .  j a  v a2 s .c om
    Response response = call(underTest.getUrl());
    assertThat(response.code()).isEqualTo(200);
    ProtobufSystemInfo.SystemInfo systemInfo = ProtobufSystemInfo.SystemInfo.parseFrom(response.body().bytes());
    assertThat(systemInfo.getSectionsCount()).isEqualTo(2);
    assertThat(systemInfo.getSections(0).getName()).isEqualTo("state1");
    assertThat(systemInfo.getSections(1).getName()).isEqualTo("state2");
}

From source file:org.sonar.runner.impl.ServerConnection.java

License:Open Source License

/**
 * @throws IOException           if connectivity error/timeout (network)
 * @throws IllegalStateException if HTTP code is different than 2xx
 *//*from   w  ww . j a  v a 2s  .c o  m*/
private ResponseBody callUrl(String url) throws IOException, IllegalStateException {
    Request request = new Request.Builder().url(url).addHeader("User-Agent", userAgent).get().build();
    Response response = httpClient.newCall(request).execute();
    if (!response.isSuccessful()) {
        throw new IllegalStateException(format("Status returned by url [%s] is not valid: [%s]",
                response.request().url(), response.code()));
    }
    return response.body();
}

From source file:org.sufficientlysecure.keychain.keyimport.HkpKeyserver.java

License:Apache License

private String query(String request, @NonNull Proxy proxy) throws QueryFailedException, HttpError {
    try {//from w  w  w. j a v a  2 s.  c o m
        URL url = new URL(getUrlPrefix() + mHost + ":" + mPort + request);
        Log.d(Constants.TAG, "hkp keyserver query: " + url + " Proxy: " + proxy);
        OkHttpClient client = getClient(url, proxy);
        Response response = client.newCall(new Request.Builder().url(url).build()).execute();

        String responseBody = response.body().string(); // contains body both in case of success or failure

        if (response.isSuccessful()) {
            return responseBody;
        } else {
            throw new HttpError(response.code(), responseBody);
        }
    } catch (IOException e) {
        Log.e(Constants.TAG, "IOException at HkpKeyserver", e);
        throw new QueryFailedException(
                "Keyserver '" + mHost + "' is unavailable. Check your Internet connection!"
                        + (proxy == Proxy.NO_PROXY ? "" : " Using proxy " + proxy));
    }
}

From source file:org.sufficientlysecure.keychain.keyimport.HkpKeyserver.java

License:Apache License

@Override
public void add(String armoredKey) throws AddKeyException {
    try {//from   w  ww .jav  a  2  s.  c  om
        String path = "/pks/add";
        String params;
        try {
            params = "keytext=" + URLEncoder.encode(armoredKey, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            throw new AddKeyException();
        }
        URL url = new URL(getUrlPrefix() + mHost + ":" + mPort + path);

        Log.d(Constants.TAG, "hkp keyserver add: " + url);
        Log.d(Constants.TAG, "params: " + params);

        RequestBody body = RequestBody.create(MediaType.parse("application/x-www-form-urlencoded"), params);

        Request request = new Request.Builder().url(url)
                .addHeader("Content-Type", "application/x-www-form-urlencoded")
                .addHeader("Content-Length", Integer.toString(params.getBytes().length)).post(body).build();

        Response response = getClient(url, mProxy).newCall(request).execute();

        Log.d(Constants.TAG, "response code: " + response.code());
        Log.d(Constants.TAG, "answer: " + response.body().string());

        if (response.code() != 200) {
            throw new AddKeyException();
        }

    } catch (IOException e) {
        Log.e(Constants.TAG, "IOException", e);
        throw new AddKeyException();
    }
}

From source file:org.whispersystems.signalservice.internal.push.PushServiceSocket.java

private String makeRequest(String urlFragment, String method, String body)
        throws NonSuccessfulResponseCodeException, PushNetworkException {
    Response response = getConnection(urlFragment, method, body);

    int responseCode;
    String responseMessage;/*from   w w w .j  av a  2  s  .  com*/
    String responseBody;

    try {
        responseCode = response.code();
        responseMessage = response.message();
        responseBody = response.body().string();
    } catch (IOException ioe) {
        throw new PushNetworkException(ioe);
    }

    switch (responseCode) {
    case 413:
        throw new RateLimitException("Rate limit exceeded: " + responseCode);
    case 401:
    case 403:
        throw new AuthorizationFailedException("Authorization failed!");
    case 404:
        throw new NotFoundException("Not found");
    case 409:
        MismatchedDevices mismatchedDevices;

        try {
            mismatchedDevices = JsonUtil.fromJson(responseBody, MismatchedDevices.class);
        } catch (JsonProcessingException e) {
            Log.w(TAG, e);
            throw new NonSuccessfulResponseCodeException(
                    "Bad response: " + responseCode + " " + responseMessage);
        } catch (IOException e) {
            throw new PushNetworkException(e);
        }

        throw new MismatchedDevicesException(mismatchedDevices);
    case 410:
        StaleDevices staleDevices;

        try {
            staleDevices = JsonUtil.fromJson(responseBody, StaleDevices.class);
        } catch (JsonProcessingException e) {
            throw new NonSuccessfulResponseCodeException(
                    "Bad response: " + responseCode + " " + responseMessage);
        } catch (IOException e) {
            throw new PushNetworkException(e);
        }

        throw new StaleDevicesException(staleDevices);
    case 411:
        DeviceLimit deviceLimit;

        try {
            deviceLimit = JsonUtil.fromJson(responseBody, DeviceLimit.class);
        } catch (JsonProcessingException e) {
            throw new NonSuccessfulResponseCodeException(
                    "Bad response: " + responseCode + " " + responseMessage);
        } catch (IOException e) {
            throw new PushNetworkException(e);
        }

        throw new DeviceLimitExceededException(deviceLimit);
    case 417:
        throw new ExpectationFailedException();
    }

    if (responseCode != 200 && responseCode != 204) {
        throw new NonSuccessfulResponseCodeException("Bad response: " + responseCode + " " + responseMessage);
    }

    return responseBody;
}

From source file:org.wildfly.kubernetes.configmap.ConfigMapOperations.java

License:Open Source License

public void createConfigMap(OkHttpClient client, String namespace, String name, Map<String, String> labels,
        Collection<Path> files) throws IOException {
    Request request = new Request.Builder().url(buildUrl(namespace, null))
            .post(createJsonPayload(namespace, name, labels, files)).build();
    Response response = client.newCall(request).execute();
    if (HttpURLConnection.HTTP_CREATED != response.code()) {
        readStatusResponse(response);/*w  w w . j a  va2  s.  c o m*/
    }
}

From source file:org.wildfly.kubernetes.configmap.ConfigMapOperations.java

License:Open Source License

public void deleteConfigMap(OkHttpClient client, String namespace, String name) throws IOException {
    Request request = new Request.Builder().url(buildUrl(namespace, name)).delete().build();
    Response response = client.newCall(request).execute();
    if (HttpURLConnection.HTTP_OK != response.code()) {
        readStatusResponse(response);/*from ww  w.  j  a  v  a2 s . c om*/
    }
}

From source file:org.wildfly.kubernetes.configmap.ConfigMapOperations.java

License:Open Source License

public void updateConfigMap(OkHttpClient client, String namespace, String name, Map<String, String> labels,
        Collection<Path> files) throws IOException {
    Request request = new Request.Builder().url(buildUrl(namespace, name))
            .put(createJsonPayload(namespace, name, labels, files)).build();
    Response response = client.newCall(request).execute();
    if (HttpURLConnection.HTTP_OK != response.code()) {
        readStatusResponse(response);//  w w  w  .j  a  v  a  2 s.c o m
    }
}

From source file:org.xbmc.kore.jsonrpc.HostConnection.java

License:Open Source License

/**
 * Reads the response from the server/*  w w  w.  jav  a 2s .  c  o  m*/
 * @param response Response from OkHttp
 * @return Response body string
 * @throws ApiException
 */
private String handleOkHttpResponse(Response response) throws ApiException {
    try {
        //         LogUtils.LOGD(TAG, "Reading HTTP response.");
        int responseCode = response.code();

        switch (responseCode) {
        case 200:
            // All ok, read response
            String res = response.body().string();
            response.body().close();
            LogUtils.LOGD(TAG, "OkHTTP response: " + res);
            return res;
        case 401:
            LogUtils.LOGD(TAG, "OkHTTP response read error. Got a 401: " + response);
            throw new ApiException(ApiException.HTTP_RESPONSE_CODE_UNAUTHORIZED,
                    "Server returned response code: " + response);
        case 404:
            LogUtils.LOGD(TAG, "OkHTTP response read error. Got a 404: " + response);
            throw new ApiException(ApiException.HTTP_RESPONSE_CODE_NOT_FOUND,
                    "Server returned response code: " + response);
        default:
            LogUtils.LOGD(TAG, "OkHTTP response read error. Got: " + response);
            throw new ApiException(ApiException.HTTP_RESPONSE_CODE_UNKNOWN,
                    "Server returned response code: " + response);
        }
    } catch (IOException e) {
        LogUtils.LOGW(TAG, "Failed to read OkHTTP response.", e);
        throw new ApiException(ApiException.IO_EXCEPTION_WHILE_READING_RESPONSE, e);
    }
}