HttpURLConnection генерирует исключение NullPointerException в getResponseCode - PullRequest
1 голос
/ 07 марта 2019

Мой запрос POST выдает NullPointerException в getResponseCode(), однако до сегодняшнего дня он работал хорошо. Эквивалентный вызов curl работает как надо, поэтому проблема должна быть в Java. Та же самая реализация (без некоторых свойств, конечно) работает хорошо для запросов GET. Вот оно:

private Object executeRequest(URL url, String httpMethod, String contentType, URL jobPropsPath)
            throws NoSuchAlgorithmException, KeyManagementException,
            MalformedURLException, IOException, ParseException {

        if(contentType == null) {
            contentType = "application/json";
        }

        // curl -k
        TrustManager[] trustAllCerts = { new OozieWorkflowTest.CustomX509TrustManager() };
        SSLContext sc = SSLContext.getInstance("SSL");
        sc.init(null, trustAllCerts, new SecureRandom());
        HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());

        HostnameVerifier allHostsValid = (hostname, session) -> true;
        HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);

        // execute a request
        HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
        conn.setRequestProperty("Content-Type", contentType);
        conn.setRequestMethod(httpMethod);
        conn.setConnectTimeout(30000);

        // prepare request body (for XML only)
        if( (jobPropsPath != null) && ("POST".equals(httpMethod)) ) {
            File jobPropsXml = new File(jobPropsPath.getPath());

            if(jobPropsXml.exists()) {
                String jobPropsSerialized = this.serializeXml(jobPropsXml);
                conn.setDoOutput(true);

                // Add serialized String to a request body
                try(OutputStream output = new BufferedOutputStream(conn.getOutputStream())) {
                    output.write(jobPropsSerialized.getBytes());
                    output.flush();
                }

            } else {
                throw new IOException("Requested file does not exist in specified path.");
            }
        }

        // Process response
        int status = conn.getResponseCode();
        StringBuilder sb = new StringBuilder();

        switch (status) {
        ...
        }

        conn.disconnect();
        return new JSONParser().parse(sb.toString());
}

А вот эквивалентный вызов curl (для большей ясности)

curl -i -k --negotiate -u : -X POST -H "Content-Type: application/xml" -d foo/bar/wf.xml "https://host:port/oozie/v1/jobs"

Трассировка стека:

java.lang.RuntimeException: java.lang.NullPointerException

    at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1488)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1474)
    at sun.net.www.protocol.http.HttpURLConnection.getHeaderField(HttpURLConnection.java:3018)
    at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:489)
    at sun.net.www.protocol.https.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:338)
    at foo.bar.proj.OozieWorkflowTest.executeRequest(OozieWorkflowTest.java:193)
    at foo.bar.proj.OozieWorkflowTest.testWorkflowOverRestApi(OozieWorkflowTest.java:120)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at junit.framework.TestCase.runTest(TestCase.java:176)
    at junit.framework.TestCase.runBare(TestCase.java:141)
    at junit.framework.TestResult$1.protect(TestResult.java:122)
    at junit.framework.TestResult.runProtected(TestResult.java:142)
    at junit.framework.TestResult.run(TestResult.java:125)
    at junit.framework.TestCase.run(TestCase.java:129)
    at junit.framework.TestSuite.runTest(TestSuite.java:252)
    at junit.framework.TestSuite.run(TestSuite.java:247)
    at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:86)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
    at org.junit.vintage.engine.execution.RunnerExecutor.execute(RunnerExecutor.java:42)
    at java.util.stream.ForEachOps$ForEachOp$OfRef.accept(ForEachOps.java:184)
    at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193)
    at java.util.Iterator.forEachRemaining(Iterator.java:116)
    at java.util.Spliterators$IteratorSpliterator.forEachRemaining(Spliterators.java:1801)
    at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481)
    at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471)
    at java.util.stream.ForEachOps$ForEachOp.evaluateSequential(ForEachOps.java:151)
    at java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential(ForEachOps.java:174)
    at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
    at java.util.stream.ReferencePipeline.forEach(ReferencePipeline.java:418)
    at org.junit.vintage.engine.VintageTestEngine.executeAllChildren(VintageTestEngine.java:83)
    at org.junit.vintage.engine.VintageTestEngine.execute(VintageTestEngine.java:74)
    at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:170)
    at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:154)
    at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:90)
    at com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:74)
    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: java.lang.NullPointerException
    at java.util.Base64$Encoder.encode(Base64.java:261)
    at java.util.Base64$Encoder.encodeToString(Base64.java:315)
    at sun.net.www.protocol.http.NegotiateAuthentication.setHeaders(NegotiateAuthentication.java:182)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1731)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1474)
    at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:480)
    ... 38 more

1 Ответ

0 голосов
/ 13 марта 2019

Ошибка произошла из-за некоторых синтаксических ошибок в файле XML, который был отправлен с запросом, поэтому весь запрос не мог быть обработан правильно. Однако запрос curl возвратил как минимум 400, тогда как Java вообще не получила никакого ответа. Я создал отчет об ошибке, но больших шансов нет, так как API является закрытым, и это поведение трудно воспроизвести в противном случае.

...