Search in sources :

Example 46 with Headers

use of com.amazonaws.services.s3.Headers in project openhab-android by openhab.

the class MyWebImage method getBitmapFromUrl.

private Bitmap getBitmapFromUrl(Context context, final String url) {
    final Map<String, Object> result = new HashMap<String, Object>();
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
    MyAsyncHttpClient client = new MyAsyncHttpClient(context, prefs.getBoolean(Constants.PREFERENCE_SSLHOST, false), prefs.getBoolean(Constants.PREFERENCE_SSLCERT, false));
    client.setTimeout(READ_TIMEOUT);
    client.setBaseUrl(url);
    if (shouldAuth) {
        client.setBasicAuth(authUsername, authPassword);
    }
    client.get(url, new MyHttpClient.ResponseHandler() {

        @Override
        public void onFailure(Call call, int statusCode, Headers headers, byte[] responseBody, Throwable error) {
            Log.e(TAG, "Failed to get " + url + " with code " + statusCode + ":" + error);
            synchronized (result) {
                result.put("error", error);
                result.notify();
            }
        }

        @Override
        public void onSuccess(Call call, int statusCode, Headers headers, byte[] responseBody) {
            InputStream is = new ByteArrayInputStream(responseBody);
            Map headersMap = headers.toMultimap();
            boolean isSVG = headersMap.containsKey("content-type") && headersMap.get("content-type").toString().contains("svg");
            synchronized (result) {
                result.put("bitmap", getBitmapFromInputStream(isSVG, is));
                result.notify();
            }
        }
    });
    synchronized (result) {
        try {
            result.wait(60000);
        } catch (InterruptedException e) {
            Log.e(TAG, "Timeout fetching " + url);
            return null;
        }
        if (result.containsKey("error")) {
            return null;
        }
    }
    Log.i(TAG, "fetched bitmap for " + url);
    return (Bitmap) result.get("bitmap");
}
Also used : Call(okhttp3.Call) HashMap(java.util.HashMap) SharedPreferences(android.content.SharedPreferences) Headers(okhttp3.Headers) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Bitmap(android.graphics.Bitmap) ByteArrayInputStream(java.io.ByteArrayInputStream) HashMap(java.util.HashMap) Map(java.util.Map)

Example 47 with Headers

use of com.amazonaws.services.s3.Headers in project openhab-android by openhab.

the class DefaultConnectionTest method testSyncResolveAbsoluteUrl.

@Test
public void testSyncResolveAbsoluteUrl() {
    MyHttpClient httpClient = testConnection.getSyncHttpClient();
    httpClient.get("http://mylocalmachine.local/rest/test", new MyHttpClient.TextResponseHandler() {

        @Override
        public void onFailure(Call call, int statusCode, Headers headers, String responseBody, Throwable error) {
            assertEquals("http://mylocalmachine.local/rest/test", call.request().url().toString());
        }

        @Override
        public void onSuccess(Call call, int statusCode, Headers headers, String responseBody) {
            fail("The request should never succeed in tests.");
        }
    });
}
Also used : Call(okhttp3.Call) MyHttpClient(org.openhab.habdroid.util.MyHttpClient) Headers(okhttp3.Headers) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.Test)

Example 48 with Headers

use of com.amazonaws.services.s3.Headers in project openhab-android by openhab.

the class MySyncHttpClientTest method testMethodErrorResponse.

/**
 * Unit test against Issue #315 "Crash when connection could not be established"
 */
@Test
public void testMethodErrorResponse() {
    MySyncHttpClient httpClient = new MySyncHttpClient(null, false, true);
    httpClient.setBaseUrl("https://demo.test");
    String host = "just.a.local.url.local";
    Response resp = httpClient.method("https://" + host, "GET", new HashMap<String, String>(), null, "", new MyHttpClient.ResponseHandler() {

        public void onFailure(Call call, int statusCode, Headers headers, byte[] responseBody, Throwable error) {
        }

        public void onSuccess(Call call, int statusCode, Headers headers, byte[] responseBody) {
        }
    });
    assertEquals(500, resp.code());
    assertTrue(resp.message().startsWith(UnknownHostException.class.getName()));
}
Also used : Response(okhttp3.Response) Call(okhttp3.Call) Headers(okhttp3.Headers) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 49 with Headers

use of com.amazonaws.services.s3.Headers in project protools by SeanDragon.

the class ToolSendHttp method send.

public static HttpReceive send(HttpSend httpSend, OkHttpClient okHttpClient) {
    final HttpReceive httpReceive = new HttpReceive();
    httpReceive.setHaveError(true);
    try {
        Request request = convertRequest(httpSend);
        Call call = okHttpClient.newCall(request);
        Response response = call.execute();
        ResponseBody body = response.body();
        if (body == null) {
            throw new HttpException("发送http失败,响应体为空");
        }
        final Map<String, String> responseHeaders = Maps.newHashMap();
        if (httpSend.getNeedReceiveHeaders()) {
            final Headers headers = response.headers();
            final Set<String> headerNameSet = headers.names();
            headerNameSet.forEach(oneHeaderName -> {
                final String oneHeaderValue = headers.get(oneHeaderName);
                responseHeaders.put(oneHeaderName, oneHeaderValue);
            });
        }
        int responseStatusCode = response.code();
        if (responseStatusCode != 200) {
            throw new HttpException("本次请求响应码不是200,是" + responseStatusCode);
        }
        String responseBody = body.string();
        if (log.isDebugEnabled()) {
            log.debug(responseBody);
        }
        httpReceive.setResponseBody(responseBody).setHaveError(false).setStatusCode(responseStatusCode).setStatusText(responseStatusCode + "").setResponseHeader(responseHeaders);
        response.close();
        okHttpClient.dispatcher().executorService().shutdown();
    } catch (IOException e) {
        httpReceive.setErrMsg("获取返回内容失败!").setThrowable(e);
    } catch (HttpException e) {
        httpReceive.setErrMsg(e.getMessage()).setThrowable(e);
    }
    if (httpReceive.getHaveError()) {
        if (log.isWarnEnabled()) {
            Throwable throwable = httpReceive.getThrowable();
            log.warn(ToolFormat.toException(throwable), throwable);
        }
    }
    httpReceive.setIsDone(true);
    return httpReceive;
}
Also used : HttpReceive(pro.tools.http.pojo.HttpReceive) Call(okhttp3.Call) Headers(okhttp3.Headers) Request(okhttp3.Request) IOException(java.io.IOException) ResponseBody(okhttp3.ResponseBody) Response(okhttp3.Response) HttpException(pro.tools.http.pojo.HttpException)

Example 50 with Headers

use of com.amazonaws.services.s3.Headers in project nifi-minifi by apache.

the class RestChangeIngestorCommonTest method testFileUploadSameConfig.

@Test
public void testFileUploadSameConfig() throws Exception {
    when(mockDifferentiator.isNew(Mockito.any(InputStream.class))).thenReturn(false);
    Request request = new Request.Builder().url(url).post(RequestBody.create(MEDIA_TYPE_MARKDOWN, testString)).addHeader("charset", "UTF-8").build();
    Response response = client.newCall(request).execute();
    if (response.isSuccessful())
        throw new IOException("Unexpected code " + response);
    Headers responseHeaders = response.headers();
    for (int i = 0; i < responseHeaders.size(); i++) {
        System.out.println(responseHeaders.name(i) + ": " + responseHeaders.value(i));
    }
    assertEquals("Request received but instance is already running this config.", response.body().string());
    verify(testNotifier, Mockito.never()).notifyListeners(Mockito.any());
}
Also used : Response(okhttp3.Response) InputStream(java.io.InputStream) Headers(okhttp3.Headers) Request(okhttp3.Request) IOException(java.io.IOException) Test(org.junit.Test)

Aggregations

Headers (okhttp3.Headers)128 Request (okhttp3.Request)61 Response (okhttp3.Response)54 Test (org.junit.Test)40 IOException (java.io.IOException)32 Call (okhttp3.Call)30 RequestBody (okhttp3.RequestBody)25 CancelledException (com.hippo.ehviewer.client.exception.CancelledException)20 EhException (com.hippo.ehviewer.client.exception.EhException)20 NoHAtHClientException (com.hippo.ehviewer.client.exception.NoHAtHClientException)20 ParseException (com.hippo.ehviewer.client.exception.ParseException)20 StatusCodeException (com.hippo.network.StatusCodeException)20 ResponseBody (okhttp3.ResponseBody)18 HttpHeaders (okhttp3.internal.http.HttpHeaders)18 MediaType (okhttp3.MediaType)15 ServiceResponseWithHeaders (com.microsoft.rest.ServiceResponseWithHeaders)14 HeaderResponseBoolHeaders (fixtures.header.models.HeaderResponseBoolHeaders)14 HeaderResponseByteHeaders (fixtures.header.models.HeaderResponseByteHeaders)14 HeaderResponseDateHeaders (fixtures.header.models.HeaderResponseDateHeaders)14 HeaderResponseDatetimeHeaders (fixtures.header.models.HeaderResponseDatetimeHeaders)14