Search in sources :

Example 86 with HttpURLConnection

use of java.net.HttpURLConnection in project che by eclipse.

the class DockerRegistryChecker method checkRegistryIsAvailable.

/**
     * Checks that registry is available and if it is not - logs warning message.
     */
@PostConstruct
private void checkRegistryIsAvailable() throws IOException {
    if (snapshotUseRegistry && !isNullOrEmpty(machineDockerRegistry)) {
        String registryUrl = "http://" + machineDockerRegistry;
        LOG.info("Probing registry '{}'", registryUrl);
        final HttpURLConnection conn = (HttpURLConnection) new URL(registryUrl).openConnection();
        conn.setConnectTimeout(30 * 1000);
        try {
            final int responseCode = conn.getResponseCode();
            LOG.info("Probe of registry '{}' succeed with HTTP response code '{}'", registryUrl, responseCode);
        } catch (IOException ioEx) {
            LOG.warn("Docker registry {} is not available, " + "which means that you won't be able to save snapshots of your workspaces." + "\nHow to configure registry?" + "\n\tLocal registry  -> https://docs.docker.com/registry/" + "\n\tRemote registry -> set up 'che.docker.registry.auth.*' properties", registryUrl);
        } finally {
            conn.disconnect();
        }
    }
}
Also used : HttpURLConnection(java.net.HttpURLConnection) IOException(java.io.IOException) URL(java.net.URL) PostConstruct(javax.annotation.PostConstruct)

Example 87 with HttpURLConnection

use of java.net.HttpURLConnection in project pinpoint by naver.

the class HttpURLConnectionInterceptor method before.

@Override
public void before(Object target, Object[] args) {
    if (isDebug) {
        logger.beforeInterceptor(target, args);
    }
    Trace trace = traceContext.currentRawTraceObject();
    if (trace == null) {
        return;
    }
    final HttpURLConnection request = (HttpURLConnection) target;
    boolean connected = false;
    if (target instanceof ConnectedGetter) {
        connected = ((ConnectedGetter) target)._$PINPOINT$_isConnected();
    }
    boolean connecting = false;
    if (target instanceof ConnectingGetter) {
        connecting = ((ConnectingGetter) target)._$PINPOINT$_isConnecting();
    }
    if (connected || connecting) {
        return;
    }
    final boolean sampling = trace.canSampled();
    if (!sampling) {
        request.setRequestProperty(Header.HTTP_SAMPLED.toString(), SamplingFlagUtils.SAMPLING_RATE_FALSE);
        return;
    }
    scope.getCurrentInvocation().setAttachment(TRACE_BLOCK_BEGIN_MARKER);
    SpanEventRecorder recorder = trace.traceBlockBegin();
    TraceId nextId = trace.getTraceId().getNextTraceId();
    recorder.recordNextSpanId(nextId.getSpanId());
    final URL url = request.getURL();
    final String host = url.getHost();
    final int port = url.getPort();
    // TODO How to represent protocol?
    String endpoint = getEndpoint(host, port);
    request.setRequestProperty(Header.HTTP_TRACE_ID.toString(), nextId.getTransactionId());
    request.setRequestProperty(Header.HTTP_SPAN_ID.toString(), String.valueOf(nextId.getSpanId()));
    request.setRequestProperty(Header.HTTP_PARENT_SPAN_ID.toString(), String.valueOf(nextId.getParentSpanId()));
    request.setRequestProperty(Header.HTTP_FLAGS.toString(), String.valueOf(nextId.getFlags()));
    request.setRequestProperty(Header.HTTP_PARENT_APPLICATION_NAME.toString(), traceContext.getApplicationName());
    request.setRequestProperty(Header.HTTP_PARENT_APPLICATION_TYPE.toString(), Short.toString(traceContext.getServerTypeCode()));
    if (host != null) {
        request.setRequestProperty(Header.HTTP_HOST.toString(), endpoint);
    }
    recorder.recordServiceType(JdkHttpConstants.SERVICE_TYPE);
    // Don't record end point because it's same with destination id.
    recorder.recordDestinationId(endpoint);
    recorder.recordAttribute(AnnotationKey.HTTP_URL, InterceptorUtils.getHttpUrl(url.toString(), param));
}
Also used : Trace(com.navercorp.pinpoint.bootstrap.context.Trace) HttpURLConnection(java.net.HttpURLConnection) ConnectedGetter(com.navercorp.pinpoint.plugin.jdk.http.ConnectedGetter) SpanEventRecorder(com.navercorp.pinpoint.bootstrap.context.SpanEventRecorder) ConnectingGetter(com.navercorp.pinpoint.plugin.jdk.http.ConnectingGetter) TraceId(com.navercorp.pinpoint.bootstrap.context.TraceId) URL(java.net.URL)

Example 88 with HttpURLConnection

use of java.net.HttpURLConnection in project jersey by jersey.

the class HelloWorldTest method testHelloWorld.

@Test
@Ignore("not compatible with test framework (doesn't use client())")
public void testHelloWorld() throws Exception {
    URL getUrl = UriBuilder.fromUri(getBaseUri()).path(App.ROOT_PATH).build().toURL();
    HttpURLConnection connection = (HttpURLConnection) getUrl.openConnection();
    try {
        connection.setDoOutput(true);
        connection.setInstanceFollowRedirects(false);
        connection.setRequestMethod("GET");
        connection.setRequestProperty("Content-Type", "text/plain");
        assertEquals(HttpURLConnection.HTTP_OK, connection.getResponseCode());
    } finally {
        connection.disconnect();
    }
}
Also used : HttpURLConnection(java.net.HttpURLConnection) URL(java.net.URL) Ignore(org.junit.Ignore) JerseyTest(org.glassfish.jersey.test.JerseyTest) Test(org.junit.Test)

Example 89 with HttpURLConnection

use of java.net.HttpURLConnection in project jersey by jersey.

the class FormDataMultiPartReaderWriterTest method mimeTempFileRemovedAfterAbortedUpload.

/**
     * Mocked JERSEY-2794 reproducer. Real test is under integration tests.
     */
@Test
public void mimeTempFileRemovedAfterAbortedUpload(@Mocked final MIMEMessage message) throws Exception {
    new Expectations() {

        {
            message.getAttachments();
            result = new MIMEParsingException();
        }
    };
    final URL url = new URL(getBaseUri().toString() + "MediaTypeWithBoundaryResource");
    final HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setRequestMethod("PUT");
    connection.setRequestProperty("Accept", "text/plain");
    connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=XXXX_YYYY");
    connection.setDoOutput(true);
    connection.connect();
    final OutputStream outputStream = connection.getOutputStream();
    outputStream.write("--XXXX_YYYY".getBytes());
    outputStream.write('\n');
    outputStream.write("Content-Type: text/plain".getBytes());
    outputStream.write('\n');
    outputStream.write("Content-Disposition: form-data; name=\"big-part\"".getBytes());
    outputStream.write('\n');
    outputStream.write('\n');
    // Send big chunk of data.
    for (int i = 0; i < 16 * 4096; i++) {
        outputStream.write('E');
        if (i % 1024 == 0) {
            outputStream.flush();
        }
    }
    // Do NOT send end of the MultiPart message to simulate the issue.
    // Get Response ...
    final int response = connection.getResponseCode();
    // ... Disconnect.
    connection.disconnect();
    assertThat("Bad Request expected", response, is(400));
    // Make sure that the Mimepull message and it's parts have been closed and temporary files deleted.
    new Verifications() {

        {
            message.close();
            times = 1;
        }
    };
}
Also used : Expectations(mockit.Expectations) HttpURLConnection(java.net.HttpURLConnection) MIMEParsingException(org.jvnet.mimepull.MIMEParsingException) OutputStream(java.io.OutputStream) Verifications(mockit.Verifications) URL(java.net.URL) Test(org.junit.Test)

Example 90 with HttpURLConnection

use of java.net.HttpURLConnection in project jersey by jersey.

the class DuplicateHeaderITCase method testDuplicateHeaderImpl.

private void testDuplicateHeaderImpl(final int headerCount, int expectedResponseCode, final String path) throws IOException {
    final String headerName = HttpHeaders.CONTENT_TYPE;
    URL getUrl = UriBuilder.fromUri(getBaseUri()).path(path).build().toURL();
    HttpURLConnection connection = (HttpURLConnection) getUrl.openConnection();
    try {
        connection.setRequestMethod("GET");
        for (int i = 0; i < headerCount; i++) {
            connection.addRequestProperty(headerName, "N/A");
        }
        connection.connect();
        assertEquals(path + " [" + headerName + ":" + headerCount + "x]", expectedResponseCode, connection.getResponseCode());
    } finally {
        connection.disconnect();
    }
}
Also used : HttpURLConnection(java.net.HttpURLConnection) URL(java.net.URL)

Aggregations

HttpURLConnection (java.net.HttpURLConnection)3831 URL (java.net.URL)2447 IOException (java.io.IOException)1634 InputStream (java.io.InputStream)1082 InputStreamReader (java.io.InputStreamReader)692 Test (org.junit.Test)650 BufferedReader (java.io.BufferedReader)573 OutputStream (java.io.OutputStream)466 MalformedURLException (java.net.MalformedURLException)372 URLConnection (java.net.URLConnection)248 HashMap (java.util.HashMap)216 OutputStreamWriter (java.io.OutputStreamWriter)208 Map (java.util.Map)199 Gson (com.google.gson.Gson)190 ByteArrayOutputStream (java.io.ByteArrayOutputStream)186 ArrayList (java.util.ArrayList)168 ExecutionException (java.util.concurrent.ExecutionException)161 File (java.io.File)159 AsyncTask (android.os.AsyncTask)158 HttpsURLConnection (javax.net.ssl.HttpsURLConnection)157