You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by us...@apache.org on 2014/06/11 19:50:45 UTC
svn commit: r1601970 - in /lucene/dev/trunk/solr: CHANGES.txt
core/src/java/org/apache/solr/util/SimplePostTool.java
Author: uschindler
Date: Wed Jun 11 17:50:45 2014
New Revision: 1601970
URL: http://svn.apache.org/r1601970
Log:
SOLR-5940: post.jar reports back detailed error in case of error responses
Modified:
lucene/dev/trunk/solr/CHANGES.txt
lucene/dev/trunk/solr/core/src/java/org/apache/solr/util/SimplePostTool.java
Modified: lucene/dev/trunk/solr/CHANGES.txt
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/solr/CHANGES.txt?rev=1601970&r1=1601969&r2=1601970&view=diff
==============================================================================
--- lucene/dev/trunk/solr/CHANGES.txt (original)
+++ lucene/dev/trunk/solr/CHANGES.txt Wed Jun 11 17:50:45 2014
@@ -215,6 +215,9 @@ Other Changes
* SOLR-6067: Refactor duplicate Collector code in SolrIndexSearcher
(Christine Poerschke via hossman)
+* SOLR-5940: post.jar reports back detailed error in case of error responses.
+ (Sameer Maggon, shalin, Uwe Schindler)
+
Optimizations
----------------------
Modified: lucene/dev/trunk/solr/core/src/java/org/apache/solr/util/SimplePostTool.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/solr/core/src/java/org/apache/solr/util/SimplePostTool.java?rev=1601970&r1=1601969&r2=1601970&view=diff
==============================================================================
--- lucene/dev/trunk/solr/core/src/java/org/apache/solr/util/SimplePostTool.java (original)
+++ lucene/dev/trunk/solr/core/src/java/org/apache/solr/util/SimplePostTool.java Wed Jun 11 17:50:45 2014
@@ -32,6 +32,7 @@ import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.net.URLEncoder;
+import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
@@ -817,10 +818,8 @@ public class SimplePostTool {
String encoding = DatatypeConverter.printBase64Binary(url.getUserInfo().getBytes(StandardCharsets.US_ASCII));
urlc.setRequestProperty("Authorization", "Basic " + encoding);
}
- if (HttpURLConnection.HTTP_OK != urlc.getResponseCode()) {
- warn("Solr returned an error #" + urlc.getResponseCode() +
- " " + urlc.getResponseMessage() + " for url "+url);
- }
+ urlc.connect();
+ checkResponseCode(urlc);
} catch (IOException e) {
warn("An error occurred posting data to "+url+". Please check that Solr is running.");
}
@@ -855,45 +854,63 @@ public class SimplePostTool {
urlc.setRequestProperty("Authorization", "Basic " + encoding);
}
if (null != length) urlc.setFixedLengthStreamingMode(length);
-
+ urlc.connect();
} catch (IOException e) {
fatal("Connection error (is Solr running at " + solrUrl + " ?): " + e);
success = false;
}
- OutputStream out = null;
- try {
- out = urlc.getOutputStream();
+ try (final OutputStream out = urlc.getOutputStream()) {
pipe(data, out);
} catch (IOException e) {
fatal("IOException while posting data: " + e);
success = false;
- } finally {
- try { if(out!=null) out.close(); } catch (IOException x) { /*NOOP*/ }
}
- InputStream in = null;
try {
- if (HttpURLConnection.HTTP_OK != urlc.getResponseCode()) {
- warn("Solr returned an error #" + urlc.getResponseCode() +
- " " + urlc.getResponseMessage());
- success = false;
+ success &= checkResponseCode(urlc);
+ try (final InputStream in = urlc.getInputStream()) {
+ pipe(in, output);
}
-
- in = urlc.getInputStream();
- pipe(in, output);
} catch (IOException e) {
warn("IOException while reading response: " + e);
success = false;
- } finally {
- try { if(in!=null) in.close(); } catch (IOException x) { /*NOOP*/ }
}
-
} finally {
- if(urlc!=null) urlc.disconnect();
+ if (urlc!=null) urlc.disconnect();
}
return success;
}
+
+ private static boolean checkResponseCode(HttpURLConnection urlc) throws IOException {
+ if (urlc.getResponseCode() >= 400) {
+ warn("Solr returned an error #" + urlc.getResponseCode() +
+ " (" + urlc.getResponseMessage() + ") for url: " + urlc.getURL());
+ Charset charset = StandardCharsets.ISO_8859_1;
+ final String contentType = urlc.getContentType();
+ // code cloned from ContentStreamBase, but post.jar should be standalone!
+ if (contentType != null) {
+ int idx = contentType.toLowerCase(Locale.ROOT).indexOf("charset=");
+ if (idx > 0) {
+ charset = Charset.forName(contentType.substring(idx + "charset=".length()).trim());
+ }
+ }
+ // Print the response returned by Solr
+ try (InputStream errStream = urlc.getErrorStream()) {
+ if (errStream != null) {
+ BufferedReader br = new BufferedReader(new InputStreamReader(errStream, charset));
+ final StringBuilder response = new StringBuilder("Response: ");
+ int ch;
+ while ((ch = br.read()) != -1) {
+ response.append((char) ch);
+ }
+ warn(response.toString().trim());
+ }
+ }
+ return false;
+ }
+ return true;
+ }
/**
* Converts a string to an input stream